Skip to content

Commit 4a94aaa

Browse files
authored
Add logging for slow operations in profile data services (#309)
1 parent e511c0b commit 4a94aaa

15 files changed

Lines changed: 62 additions & 28 deletions

aspnetcore/src/api/Models/Common/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,10 @@ public static class FactContributionTypes
198198
public const string ACTIVITY_TYPE = "activity_type";
199199
public const string RESEARCHER_NAME_ACTIVITY = "researcher_name_activity";
200200
}
201+
202+
public static class LoggingParameters
203+
{
204+
public const int SLOW_OPERATION_MS_THRESHOLD = 2000;
205+
}
201206
}
202207
}

aspnetcore/src/api/Services/Profiledata/AffiliationService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ public async Task<List<ProfileEditorAffiliation>> GetProfileEditorAffiliations(i
353353
}
354354

355355
stopwatch.Stop();
356-
_logger.LogInformation($"GetProfileEditorAffiliations. {affiliations.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
356+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
357+
{
358+
_logger.LogWarning($"GetProfileEditorAffiliations is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {affiliations.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
359+
}
360+
357361
return affiliations;
358362
}
359363
}

aspnetcore/src/api/Services/Profiledata/EducationService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ public async Task<List<ProfileEditorEducation>> GetProfileEditorEducations(int u
9999
}
100100

101101
stopwatch.Stop();
102-
_logger.LogInformation($"GetProfileEditorEducations. {educations.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
102+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
103+
{
104+
_logger.LogWarning($"GetProfileEditorEducations is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {educations.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
105+
}
103106

104107
return educations;
105108
}

aspnetcore/src/api/Services/Profiledata/EmailService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ public async Task<List<ProfileEditorEmail>> GetProfileEditorEmails(int userprofi
7777
}
7878

7979
stopwatch.Stop();
80-
_logger.LogInformation($"GetProfileEditorEmails. {emails.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
80+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
81+
{
82+
_logger.LogWarning($"GetProfileEditorEmails is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {emails.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
83+
}
8184

8285
return emails;
8386
}

aspnetcore/src/api/Services/Profiledata/ExternalIdentifierService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public async Task<List<ProfileEditorExternalIdentifier>> GetProfileEditorExterna
7878
}
7979

8080
stopwatch.Stop();
81-
_logger.LogInformation($"GetProfileEditorExternalIdentifiers. {externalIdentifiers.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
81+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
82+
{
83+
_logger.LogWarning($"GetProfileEditorExternalIdentifiers is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {externalIdentifiers.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
84+
}
8285

8386
return externalIdentifiers;
8487
}

aspnetcore/src/api/Services/Profiledata/FundingDecisionService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,10 @@ public async Task<List<ProfileEditorFundingDecision>> GetProfileEditorFundingDec
357357
}
358358

359359
stopwatch.Stop();
360-
_logger.LogInformation($"GetProfileEditorFundingDecisions. {fundingDecisions.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
360+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
361+
{
362+
_logger.LogWarning($"GetProfileEditorFundingDecisions is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {fundingDecisions.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
363+
}
361364

362365
return fundingDecisions;
363366
}

aspnetcore/src/api/Services/Profiledata/KeywordService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ public async Task<List<ProfileEditorKeyword>> GetProfileEditorKeywords(int userp
8181
}
8282

8383
stopwatch.Stop();
84-
_logger.LogInformation($"GetProfileEditorKeywords. {keywords.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
84+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
85+
{
86+
_logger.LogWarning($"GetProfileEditorKeywords is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {keywords.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
87+
}
8588

8689
return keywords;
8790
}

aspnetcore/src/api/Services/Profiledata/NameService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ public async Task<List<ProfileEditorName>> GetProfileEditorOtherNames(int userpr
136136
}
137137

138138
stopwatch.Stop();
139-
_logger.LogInformation($"GetProfileEditorOtherNames. {otherNames.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
139+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
140+
{
141+
_logger.LogWarning($"GetProfileEditorOtherNames is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {otherNames.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
142+
}
140143

141144
return otherNames;
142145
}

aspnetcore/src/api/Services/Profiledata/PublicationService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ void ProcessItem(PublicationDto newPublication)
336336
}
337337

338338
stopwatch.Stop();
339-
_logger.LogInformation($"GetProfileEditorPublications. {publications.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
339+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
340+
{
341+
_logger.LogWarning($"GetProfileEditorPublications is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {publications.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
342+
}
340343

341344
return publications;
342345
}

aspnetcore/src/api/Services/Profiledata/ResearchActivityService.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
114114
/*
115115
* DimResearchActivity => DTO
116116
*/
117-
var stopwatch_researchActivityDtos = Stopwatch.StartNew();
118117
List<ResearchActivityDto> researchActivityDtos = await _ttvContext.FactFieldValues.Where(ffv => ffv.DimUserProfileId == userprofileId
119118
&& ffv.DimResearchActivityId > 0
120119
&& ffv.DimFieldDisplaySettings.FieldIdentifier == Constants.FieldIdentifiers.ACTIVITY_RESEARCH_ACTIVITY
@@ -216,13 +215,10 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
216215
LinkType = wl.LinkType ?? ""
217216
}).ToList()
218217
}).AsNoTracking().ToListAsync();
219-
stopwatch_researchActivityDtos.Stop();
220-
_logger.LogInformation($"GetProfileEditorResearchActivities. SQL query for researchActivityDtos got {researchActivityDtos.Count} items and took {stopwatch_researchActivityDtos.ElapsedMilliseconds}ms.");
221218

222219
/*
223220
* DimProfileOnlyResearchActivity => DTO
224221
*/
225-
var stopwatch_profileOnlyResearchActivityDtos = Stopwatch.StartNew();
226222
List<ResearchActivityDto> profileOnlyResearchActivityDtos = await _ttvContext.FactFieldValues.Where(ffv => ffv.DimUserProfileId == userprofileId
227223
&& ffv.DimProfileOnlyResearchActivityId > 0
228224
&& ffv.DimFieldDisplaySettings.FieldIdentifier == Constants.FieldIdentifiers.ACTIVITY_RESEARCH_ACTIVITY
@@ -297,14 +293,11 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
297293
LinkType = wl.LinkType ?? ""
298294
}).ToList()
299295
}).AsNoTracking().ToListAsync();
300-
stopwatch_profileOnlyResearchActivityDtos.Stop();
301-
_logger.LogInformation($"GetProfileEditorResearchActivities. SQL query for profileOnlyResearchActivityDtos got {profileOnlyResearchActivityDtos.Count} items and took {stopwatch_profileOnlyResearchActivityDtos.ElapsedMilliseconds}ms.");
302296

303297
/*
304298
* Batch load child identifierless data.
305299
* One query replaces 4 correlated subqueries per row across both DTO lists.
306300
*/
307-
var stopwatch_childLookup = Stopwatch.StartNew();
308301
List<int> allParentIds = researchActivityDtos
309302
.Concat(profileOnlyResearchActivityDtos)
310303
.Where(d => d.DimIdentifierlessData_Id > 0)
@@ -338,16 +331,13 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
338331
}
339332
}
340333
}
341-
stopwatch_childLookup.Stop();
342-
_logger.LogInformation($"GetProfileEditorResearchActivities. Batch child lookup took {stopwatch_childLookup.ElapsedMilliseconds}ms.");
343334

344335
/*
345336
* Research activity deduplication.
346337
* Deduplication is based on start year and name properties.
347338
* Remove items from profileOnlyResearchActivityDtos which duplicate items from researchActivityDtos.
348339
* Comparison is done by computing a key for each research activity based on start year and translated name FI, and comparing the keys.
349340
*/
350-
var stopwatch_deduplicateDtos = Stopwatch.StartNew();
351341
HashSet<string> uniqueKeys = new();
352342
foreach (ResearchActivityDto researchActivityDto in researchActivityDtos)
353343
{
@@ -392,7 +382,6 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
392382
uniqueKeys.Add(key);
393383
}
394384
}
395-
_logger.LogInformation($"GetProfileEditorResearchActivities. Deduplication took {stopwatch_deduplicateDtos.ElapsedMilliseconds}ms.");
396385

397386
/*
398387
* Process DTOs
@@ -402,7 +391,6 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
402391
* Destination:
403392
* - List<ProfileEditorActivityAndReward>
404393
*/
405-
var stopwatch_processDtos = Stopwatch.StartNew();
406394
List<ProfileEditorActivityAndReward> activitiesAndRewards = new();
407395
foreach (ResearchActivityDto dto in researchActivityDtos.Concat(profileOnlyResearchActivityDtos).Where(d => !d.IsDuplicate))
408396
{
@@ -617,11 +605,12 @@ public async Task<List<ProfileEditorActivityAndReward>> GetProfileEditorActivies
617605
}
618606
activitiesAndRewards.Add(activityAndReward);
619607
}
620-
stopwatch_processDtos.Stop();
621-
_logger.LogInformation($"GetProfileEditorResearchActivities. Processing DTOs took {stopwatch_processDtos.ElapsedMilliseconds}ms.");
622608

623609
stopwatch.Stop();
624-
_logger.LogInformation($"GetProfileEditorResearchActivities. {activitiesAndRewards.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
610+
if (stopwatch.ElapsedMilliseconds > Constants.LoggingParameters.SLOW_OPERATION_MS_THRESHOLD)
611+
{
612+
_logger.LogWarning($"GetProfileEditorResearchActivities is slow. userprofileId={userprofileId}, forElasticsearch={forElasticsearch}, {activitiesAndRewards.Count} items in {stopwatch.ElapsedMilliseconds}ms.");
613+
}
625614

626615
return activitiesAndRewards;
627616
}

0 commit comments

Comments
 (0)