Skip to content

Commit

Permalink
Feature polling countdown (#29)
Browse files Browse the repository at this point in the history
* Added polling count down
  • Loading branch information
Luka Grabarevic authored Jul 11, 2018
1 parent 45fca7d commit 9aed531
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 20 deletions.
18 changes: 16 additions & 2 deletions BuildsAppReborn.Access/BuildMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ public BuildMonitor(LazyContainer<IBuildProvider, IBuildProviderMetadata> buildP
this.generalSettings = generalSettings;
this.notificationProvider = notificationProviders.GetSupportedNotificationProvider();
this.timer.Elapsed += (sender, args) => BeginPollingBuildsAsync().GetAwaiter().GetResult();
this.timer.ProgressElapsed += (sender, args) =>
{
OnProgressUpdated(MaximumProgress - this.timer.CurrentInterval);
};
}

public Boolean IsConfigured => this.providerSettingsGroup.Any() && (this.generalSettings.ViewStyle == BuildViewStyle.GroupByPullRequest || this.providerSettingsGroup.SelectMany(a => a.Value).SelectMany(a => a.SelectedBuildDefinitions).Any());
public Boolean IsConfigured => this.providerSettingsGroup.Any() && (this.generalSettings.ViewStyle == BuildViewStyle.GroupByPullRequest ||
this.providerSettingsGroup.SelectMany(a => a.Value).SelectMany(a => a.SelectedBuildDefinitions).Any());

public Double MaximumProgress => this.generalSettings.PollingInterval.TotalMilliseconds;

public async Task BeginPollingBuildsAsync()
{
Expand Down Expand Up @@ -79,6 +86,8 @@ public void Stop()

public event EventHandler MonitorStopped;

public event PollingProgressUpdated ProgressUpdated;

private void Initialize(IEnumerable<BuildMonitorSettings> settings)
{
var groupedByProviderId = settings.GroupBy(a => a.BuildProviderId);
Expand Down Expand Up @@ -107,6 +116,11 @@ private void OnMonitorStopped()
MonitorStopped?.Invoke(this, EventArgs.Empty);
}

private void OnProgressUpdated(Double progress)
{
ProgressUpdated?.Invoke(progress);
}

private async Task<IEnumerable<IBuild>> PollBuildsAsync(IBuildProvider provider, BuildMonitorSettings settings)
{
try
Expand Down Expand Up @@ -161,6 +175,6 @@ private async Task<IEnumerable<IBuild>> PollBuildsAsync(IBuildProvider provider,

private readonly Dictionary<IBuildProvider, ICollection<BuildMonitorSettings>> providerSettingsGroup = new Dictionary<IBuildProvider, ICollection<BuildMonitorSettings>>();

private readonly Timer timer = new Timer();
private readonly ProgressTimer timer = new ProgressTimer();
}
}
4 changes: 4 additions & 0 deletions BuildsAppReborn.Client/Cache/BuildCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal BuildCache(IBuildMonitorBasic buildMonitor,
this.buildDefinitionEqualityComparer = buildDefinitionEqualityComparer;
this.pullRequstEqualityComparer = pullRequstEqualityComparer;
this.generalSettings = generalSettings;
this.buildMonitor = buildMonitor;
buildMonitor.BuildsUpdated += OnBuildsUpdated;
buildMonitor.MonitorStopped += (sender, args) => CacheStatus = BuildCacheStatus.NotConfigured;
buildMonitor.MonitorStarted += (sender, args) => CacheStatus = BuildCacheStatus.Loading;
Expand Down Expand Up @@ -68,6 +69,8 @@ public String CurrentIcon
}
}

public IProgressComponent ProgressComponent => this.buildMonitor;

public event EventHandler CacheUpdated;

protected virtual void OnCacheUpdated()
Expand Down Expand Up @@ -205,6 +208,7 @@ private void UpdateCurrentIcon()
}

private readonly IEqualityComparer<IBuildDefinition> buildDefinitionEqualityComparer;
private readonly IBuildMonitorBasic buildMonitor;
private BuildCacheStatus cacheStatus;
private String currentIcon;
private readonly GeneralSettings generalSettings;
Expand Down
38 changes: 38 additions & 0 deletions BuildsAppReborn.Client/ViewModels/BuildsStatusViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public BuildsStatusViewModel(BuildCache buildCache)
this.timer = new Timer {Interval = 10000, AutoReset = true}; // update every 10 seconds
this.timer.Elapsed += (sender, args) => { OnBuildCacheUpdated(null, null); };
BuildCache.CacheUpdated += OnBuildCacheUpdated;

ProgressMinimum = 0;
ProgressMaximum = BuildCache.ProgressComponent.MaximumProgress;
BuildCache.ProgressComponent.ProgressUpdated += progress => Progress = progress;

HistoryClickCommand = new DelegateCommand<BuildItem>(a => Task.Run(() => StartProcess(a?.Build?.WebUrl)));
OpenArtifactCommand = new DelegateCommand<IArtifact>(a => Task.Run(() => OnOpenArtifactCommand(a)));
TestRunClickCommand = new DelegateCommand<ITestRun>(a => Task.Run(() => StartProcess(a?.WebUrl)));
Expand All @@ -37,6 +42,36 @@ public BuildsStatusViewModel(BuildCache buildCache)

public DelegateCommand<IArtifact> OpenArtifactCommand { get; set; }

public Double Progress
{
get { return this.progress; }
set
{
this.progress = value;
OnPropertyChanged();
}
}

public Double ProgressMaximum
{
get { return this.progressMaximum; }
set
{
this.progressMaximum = value;
OnPropertyChanged();
}
}

public Double ProgressMinimum
{
get { return this.progressMinimum; }
set
{
this.progressMinimum = value;
OnPropertyChanged();
}
}

public DelegateCommand<ITestRun> TestRunClickCommand { get; set; }

public void OnClose()
Expand Down Expand Up @@ -109,6 +144,9 @@ private Boolean StartProcess(String url)
}

private readonly ILog logger = LogManager.GetLogger(typeof(BuildsStatusViewModel));
private Double progress;
private Double progressMaximum;
private Double progressMinimum;

private readonly Timer timer;
}
Expand Down
41 changes: 24 additions & 17 deletions BuildsAppReborn.Client/Views/BuildsStatusView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@
</Window.Icon>
<materialDesign:DialogHost CloseOnClickAway="True" >
<Grid>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ProgressBar Grid.Row="0" Value="{Binding Progress}" Maximum="{Binding ProgressMaximum}" Minimum="{Binding ProgressMinimum}" />
<Grid Grid.Column="0" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0"
<ItemsControl Grid.Row="0"
HorizontalContentAlignment="Stretch"
Background="Transparent"
BorderBrush="Transparent"
Expand Down Expand Up @@ -375,28 +380,30 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Grid>

<!-- in case there are no builds (loaded yet): -->
<ProgressBar Grid.Column="0"
<!-- in case there are no builds (loaded yet): -->
<ProgressBar Grid.Column="0"
Grid.Row="1"
Width="128"
Height="128"
Margin="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsIndeterminate="True">
<ProgressBar.Style>
<Style BasedOn="{StaticResource MaterialDesignCircularProgressBar}" TargetType="ProgressBar">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding BuildCache.CacheStatus}" Value="{x:Static client:BuildCacheStatus.Loading}">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</ProgressBar.Style>
</ProgressBar>
<TextBlock Grid.Column="0"
<ProgressBar.Style>
<Style BasedOn="{StaticResource MaterialDesignCircularProgressBar}" TargetType="ProgressBar">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding BuildCache.CacheStatus}" Value="{x:Static client:BuildCacheStatus.Loading}">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</ProgressBar.Style>
</ProgressBar>
<TextBlock Grid.Column="0"
Grid.Row="1"
Margin="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Expand Down
1 change: 1 addition & 0 deletions BuildsAppReborn.Contracts/BuildsAppReborn.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="IBuildMonitorAdvanced.cs" />
<Compile Include="IBuildMonitorBasic.cs" />
<Compile Include="IBuildProvider.cs" />
<Compile Include="IProgressComponent.cs" />
<Compile Include="Models\Base\IWebItem.cs" />
<Compile Include="Models\BuildMonitorSettings.cs" />
<Compile Include="Models\BuildStatus.cs" />
Expand Down
2 changes: 1 addition & 1 deletion BuildsAppReborn.Contracts/IBuildMonitorBasic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace BuildsAppReborn.Contracts
{
public interface IBuildMonitorBasic
public interface IBuildMonitorBasic : IProgressComponent
{
Boolean IsConfigured { get; }

Expand Down
13 changes: 13 additions & 0 deletions BuildsAppReborn.Contracts/IProgressComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace BuildsAppReborn.Contracts
{
public interface IProgressComponent
{
Double MaximumProgress { get; }

event PollingProgressUpdated ProgressUpdated;
}

public delegate void PollingProgressUpdated(Double progress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
</Compile>
<Compile Include="Consts.cs" />
<Compile Include="Helper\CloneHelper.cs" />
<Compile Include="ProgressTimer.cs" />
<Compile Include="TolerantEnumConverter.cs" />
<Compile Include="Collections\RangeObservableCollection.cs" />
<Compile Include="Helper\EnumHelper.cs" />
Expand Down
56 changes: 56 additions & 0 deletions BuildsAppReborn.Infrastructure/ProgressTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Diagnostics;
using System.Timers;

namespace BuildsAppReborn.Infrastructure
{
public class ProgressTimer
{
public ProgressTimer()
{
this.mainTimer = new Timer();
this.progressTimer = new Timer();
this.stopWatch = new Stopwatch();

this.mainTimer.Elapsed += (sender, args) => this.stopWatch.Restart();
}

public Double CurrentInterval => this.stopWatch.ElapsedMilliseconds;

public Double Interval
{
get { return this.mainTimer.Interval; }
set { this.mainTimer.Interval = value; }
}

public void Start()
{
this.mainTimer.Start();
this.progressTimer.Start();
this.stopWatch.Start();
}

public void Stop()
{
this.mainTimer.Stop();
this.progressTimer.Stop();
this.stopWatch.Stop();
}

public event ElapsedEventHandler Elapsed
{
add { this.mainTimer.Elapsed += value; }
remove { this.mainTimer.Elapsed -= value; }
}

public event ElapsedEventHandler ProgressElapsed
{
add { this.progressTimer.Elapsed += value; }
remove { this.progressTimer.Elapsed -= value; }
}

private readonly Timer mainTimer;
private readonly Timer progressTimer;
private readonly Stopwatch stopWatch;
}
}

0 comments on commit 9aed531

Please sign in to comment.