88using Azure . Monitor . Query . Logs ;
99using Azure . Monitor . Query . Logs . Models ;
1010using Azure . ResourceManager . OperationalInsights ;
11- using Microsoft . Extensions . Logging ;
1211using Microsoft . Extensions . Options ;
1312
1413namespace Altinn . Studio . Gateway . Api . Clients . MetricsClient ;
@@ -44,9 +43,16 @@ ILogger<AzureMonitorClient> _logger
4443
4544 internal static IReadOnlyCollection < string > OperationNameKeys { get ; } = _operationNames . Keys . ToArray ( ) ;
4645
46+ internal static int GetBucketSize ( int range )
47+ {
48+ var maxPoints = 12 ;
49+ return ( int ) Math . Max ( 1 , Math . Ceiling ( ( double ) range / maxPoints ) ) ;
50+ }
51+
4752 private static string GetInterval ( int range )
4853 {
49- return range < 360 ? "5m" : "1h" ;
54+ var bucketSize = GetBucketSize ( range ) ;
55+ return $ "{ bucketSize } m";
5056 }
5157
5258 private ResourceIdentifier GetApplicationLogAnalyticsWorkspaceId ( )
@@ -73,7 +79,7 @@ public async Task<IEnumerable<FailedRequest>> GetFailedRequests(int range, Cance
7379 | where ClientType != 'Browser'
7480 | where toint(ResultCode) >= 500
7581 | where OperationName in ('{ string . Join ( "','" , _operationNames . Values . SelectMany ( value => value ) ) } ')
76- | summarize Count = count() by AppRoleName, OperationName" ;
82+ | summarize Count = count() by AppRoleName, OperationName; " ;
7783
7884 Response < LogsQueryResult > response = await _logsQueryClient . QueryResourceAsync (
7985 logAnalyticsWorkspaceId ,
@@ -209,7 +215,7 @@ CancellationToken cancellationToken
209215 | where AppRoleName == '{ app . Replace ( "'" , "''" ) } '
210216 | where OperationName in ('{ string . Join ( "','" , _operationNames . Values . SelectMany ( value => value ) ) } ')
211217 | summarize Count = count() by OperationName, DateTimeOffset = bin(TimeGenerated, { interval } )
212- | order by DateTimeOffset desc; " ;
218+ | order by DateTimeOffset desc" ;
213219
214220 Response < LogsQueryResult > response = await _logsQueryClient . QueryResourceAsync (
215221 logAnalyticsWorkspaceId ,
@@ -219,28 +225,26 @@ CancellationToken cancellationToken
219225 ) ;
220226
221227 var metrics = response
222- . Value . Table . Rows . Select ( row =>
228+ . Value . Table . Rows . GroupBy ( row =>
229+ _operationNames . First ( n => n . Value . Contains ( row . GetString ( "OperationName" ) ) ) . Key
230+ )
231+ . Select ( group => new AppFailedRequest
223232 {
224- return new
225- {
226- Name = _operationNames . First ( n => n . Value . Contains ( row . GetString ( "OperationName" ) ) ) . Key ,
227- DateTimeOffset = row . GetDateTimeOffset ( "DateTimeOffset" ) . GetValueOrDefault ( ) ,
228- Count = row . GetDouble ( "Count" ) ?? 0 ,
229- } ;
230- } )
231- . GroupBy ( row => row . Name )
232- . Select ( row =>
233- {
234- return new AppFailedRequest
235- {
236- Name = row . Key ,
237- DataPoints = row . Select ( e => new DataPoint { DateTimeOffset = e . DateTimeOffset , Count = e . Count } ) ,
238- } ;
233+ Name = group . Key ,
234+ Timestamps = group . Select ( row =>
235+ row . GetDateTimeOffset ( "DateTimeOffset" ) ? . ToUnixTimeMilliseconds ( ) ?? 0
236+ ) ,
237+ Counts = group . Select ( row => row . GetDouble ( "Count" ) ?? 0 ) ,
239238 } ) ;
240239
241240 return _operationNames . Select ( name =>
242241 metrics . FirstOrDefault ( metric => metric . Name == name . Key )
243- ?? new AppFailedRequest { Name = name . Key , DataPoints = [ ] }
242+ ?? new AppFailedRequest
243+ {
244+ Name = name . Key ,
245+ Timestamps = [ ] ,
246+ Counts = [ ] ,
247+ }
244248 ) ;
245249 }
246250
@@ -261,7 +265,7 @@ public async Task<IEnumerable<AppMetric>> GetAppMetrics(string app, int range, C
261265 | where AppRoleName == '{ app . Replace ( "'" , "''" ) } '
262266 | where Name in ('{ string . Join ( "','" , names ) } ')
263267 | summarize Count = sum(Sum) by Name, DateTimeOffset = bin(TimeGenerated, { interval } )
264- | order by DateTimeOffset desc; " ;
268+ | order by DateTimeOffset desc" ;
265269
266270 Response < LogsQueryResult > response = await _logsQueryClient . QueryResourceAsync (
267271 logAnalyticsWorkspaceId ,
@@ -271,27 +275,24 @@ public async Task<IEnumerable<AppMetric>> GetAppMetrics(string app, int range, C
271275 ) ;
272276
273277 var metrics = response
274- . Value . Table . Rows . Select ( row =>
278+ . Value . Table . Rows . GroupBy ( row => row . GetString ( "Name" ) )
279+ . Select ( group => new AppMetric
275280 {
276- return new
277- {
278- Name = row . GetString ( "Name" ) ,
279- DateTimeOffset = row . GetDateTimeOffset ( "DateTimeOffset" ) . GetValueOrDefault ( ) ,
280- Count = row . GetDouble ( "Count" ) ?? 0 ,
281- } ;
282- } )
283- . GroupBy ( row => row . Name )
284- . Select ( row =>
285- {
286- return new AppMetric
287- {
288- Name = row . Key ,
289- DataPoints = row . Select ( e => new DataPoint { DateTimeOffset = e . DateTimeOffset , Count = e . Count } ) ,
290- } ;
281+ Name = group . Key ,
282+ Timestamps = group . Select ( row =>
283+ row . GetDateTimeOffset ( "DateTimeOffset" ) ? . ToUnixTimeMilliseconds ( ) ?? 0
284+ ) ,
285+ Counts = group . Select ( row => row . GetDouble ( "Count" ) ?? 0 ) ,
291286 } ) ;
292287
293288 return names . Select ( name =>
294- metrics . FirstOrDefault ( metric => metric . Name == name ) ?? new AppMetric { Name = name , DataPoints = [ ] }
289+ metrics . FirstOrDefault ( metric => metric . Name == name )
290+ ?? new AppMetric
291+ {
292+ Name = name ,
293+ Timestamps = [ ] ,
294+ Counts = [ ] ,
295+ }
295296 ) ;
296297 }
297298
0 commit comments