1- // Copyright (c) .NET Foundation. All rights reserved.
1+ // Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using Microsoft . Extensions . Logging ;
55using NuGet . Jobs . Extensions ;
66using NuGet . Services . Incidents ;
77using NuGet . Services . Status . Table ;
8+ using StatusAggregator . Factory ;
89using StatusAggregator . Parse ;
9- using StatusAggregator . Table ;
1010using System ;
1111using System . Linq ;
1212using System . Threading . Tasks ;
1313
14- namespace StatusAggregator
14+ namespace StatusAggregator . Collector
1515{
16- public class IncidentUpdater : IIncidentUpdater
16+ /// <summary>
17+ /// Fetches new <see cref="IncidentEntity"/>s using an <see cref="IIncidentApiClient"/>.
18+ /// </summary>
19+ public class IncidentEntityCollectorProcessor : IEntityCollectorProcessor
1720 {
18- private readonly ITableWrapper _table ;
21+ public const string IncidentsCollectorName = "incidents" ;
22+
1923 private readonly IAggregateIncidentParser _aggregateIncidentParser ;
2024 private readonly IIncidentApiClient _incidentApiClient ;
21- private readonly IIncidentFactory _incidentFactory ;
22- private readonly ILogger < IncidentUpdater > _logger ;
25+ private readonly IComponentAffectingEntityFactory < IncidentEntity > _incidentFactory ;
26+ private readonly ILogger < IncidentEntityCollectorProcessor > _logger ;
2327
2428 private readonly string _incidentApiTeamId ;
2529
26- public IncidentUpdater (
27- ITableWrapper table ,
30+ public IncidentEntityCollectorProcessor (
2831 IIncidentApiClient incidentApiClient ,
2932 IAggregateIncidentParser aggregateIncidentParser ,
30- IIncidentFactory incidentFactory ,
33+ IComponentAffectingEntityFactory < IncidentEntity > incidentFactory ,
3134 StatusAggregatorConfiguration configuration ,
32- ILogger < IncidentUpdater > logger )
35+ ILogger < IncidentEntityCollectorProcessor > logger )
3336 {
34- _table = table ?? throw new ArgumentNullException ( nameof ( table ) ) ;
3537 _incidentApiClient = incidentApiClient ?? throw new ArgumentNullException ( nameof ( incidentApiClient ) ) ;
3638 _aggregateIncidentParser = aggregateIncidentParser ?? throw new ArgumentNullException ( nameof ( aggregateIncidentParser ) ) ;
3739 _incidentFactory = incidentFactory ?? throw new ArgumentNullException ( nameof ( incidentFactory ) ) ;
3840 _incidentApiTeamId = configuration ? . TeamId ?? throw new ArgumentNullException ( nameof ( configuration ) ) ;
3941 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
4042 }
4143
42- public async Task RefreshActiveIncidents ( )
43- {
44- using ( _logger . Scope ( "Refreshing active incidents." ) )
45- {
46- var activeIncidentEntities = _table
47- . CreateQuery < IncidentEntity > ( )
48- . Where ( i => i . PartitionKey == IncidentEntity . DefaultPartitionKey && i . IsActive )
49- . ToList ( ) ;
50-
51- _logger . LogInformation ( "Refreshing {ActiveIncidentsCount} active incidents." , activeIncidentEntities . Count ( ) ) ;
52- foreach ( var activeIncidentEntity in activeIncidentEntities )
53- {
54- using ( _logger . Scope ( "Refreshing active incident '{IncidentRowKey}'." , activeIncidentEntity . RowKey ) )
55- {
56- var activeIncident = await _incidentApiClient . GetIncident ( activeIncidentEntity . IncidentApiId ) ;
57- activeIncidentEntity . MitigationTime = activeIncident . MitigationData ? . Date ;
58- _logger . LogInformation ( "Updated mitigation time of active incident to {MitigationTime}" , activeIncidentEntity . MitigationTime ) ;
59- await _table . InsertOrReplaceAsync ( activeIncidentEntity ) ;
60- }
61- }
62- }
63- }
44+ public string Name => IncidentsCollectorName ;
6445
65- public async Task < DateTime ? > FetchNewIncidents ( DateTime cursor )
46+ public async Task < DateTime ? > FetchSince ( DateTime cursor )
6647 {
6748 using ( _logger . Scope ( "Fetching all new incidents since {Cursor}." , cursor ) )
6849 {
@@ -73,12 +54,19 @@ public async Task RefreshActiveIncidents()
7354 . Where ( i => i . CreateDate > cursor )
7455 . ToList ( ) ;
7556
57+ _logger . LogInformation ( "Found {IncidentCount} incidents to parse." , incidents . Count ) ;
7658 var parsedIncidents = incidents
77- . SelectMany ( i => _aggregateIncidentParser . ParseIncident ( i ) )
59+ . SelectMany ( _aggregateIncidentParser . ParseIncident )
7860 . ToList ( ) ;
79- foreach ( var parsedIncident in parsedIncidents . OrderBy ( i => i . CreationTime ) )
61+
62+ _logger . LogInformation ( "Parsed {ParsedIncidentCount} incidents." , parsedIncidents . Count ) ;
63+ foreach ( var parsedIncident in parsedIncidents . OrderBy ( i => i . StartTime ) )
8064 {
81- await _incidentFactory . CreateIncident ( parsedIncident ) ;
65+ using ( _logger . Scope ( "Creating incident for parsed incident with ID {ParsedIncidentID} affecting {ParsedIncidentPath} at {ParsedIncidentStartTime} with status {ParsedIncidentStatus}." ,
66+ parsedIncident . Id , parsedIncident . AffectedComponentPath , parsedIncident . StartTime , parsedIncident . AffectedComponentStatus ) )
67+ {
68+ await _incidentFactory . CreateAsync ( parsedIncident ) ;
69+ }
8270 }
8371
8472 return incidents . Any ( ) ? incidents . Max ( i => i . CreateDate ) : ( DateTime ? ) null ;
@@ -97,4 +85,4 @@ private string GetRecentIncidentsQuery(DateTime cursor)
9785 return query ;
9886 }
9987 }
100- }
88+ }
0 commit comments