From 7c12ec3a79eb7b40eac93c46acee94400abcd0b9 Mon Sep 17 00:00:00 2001 From: James Tattersall <10601770+jamerst@users.noreply.github.com> Date: Sat, 15 Apr 2023 11:20:11 +0100 Subject: [PATCH] Fix Indeed salary data not being stored --- jobhunt/Controllers/RefreshController.cs | 43 +++++++++++++++++++ .../Extensions/ServiceCollectionExtensions.cs | 1 + .../Indeed/IndeedApiSearchProvider.cs | 2 + jobhunt/Services/CompanyService.cs | 5 +++ 4 files changed, 51 insertions(+) diff --git a/jobhunt/Controllers/RefreshController.cs b/jobhunt/Controllers/RefreshController.cs index f276978..efa86ed 100644 --- a/jobhunt/Controllers/RefreshController.cs +++ b/jobhunt/Controllers/RefreshController.cs @@ -4,6 +4,8 @@ using JobHunt.PageWatcher; using JobHunt.Searching.Indeed; using JobHunt.Workers; +using JobHunt.Searching.Indeed.GraphQL; +using JobHunt.Searching; namespace JobHunt.Controllers; [ApiController] @@ -16,6 +18,7 @@ public class RefreshController : ControllerBase private readonly IPageScreenshotWorker _screenshotWorker; private readonly IJobService _jobService; private readonly IWatchedPageService _wpService; + public RefreshController( IIndeedApiSearchProvider indeed, IPageWatcher pageWatcher, @@ -38,6 +41,46 @@ public async Task Indeed(CancellationToken token) await _indeed.SearchAllAsync(token); } + [HttpGet] + public async Task GetMissingSalaries(string country, [FromServices] IIndeedGraphQLService graphQL) + { + int updated = 0; + + DateTimeOffset start = new DateTime(2023, 02, 01, 0, 0, 0, DateTimeKind.Utc); + List missing = await _jobService.Set.Where(j => j.Provider == SearchProviderName.Indeed && j.Posted >= start && !j.AvgYearlySalary.HasValue).ToListAsync(); + List seen = new List(); + + int skip = 0; + var batch = missing.Take(500); + while (batch.Any()) + { + var results = await graphQL.GetJobDataAsync(batch.Select(b => b.ProviderId!), country); + if (results != null) + { + foreach (var result in results.JobData.Results) + { + var job = missing.First(j => j.ProviderId == result.Job.Key); + job.Salary = result.Job.Compensation?.GetFormattedText(); + job.AvgYearlySalary = result.Job.Compensation?.GetAvgYearlySalary(); + + updated++; + seen.Add(result.Job.Key); + } + } + + skip += 500; + batch = missing.Skip(skip).Take(500); + } + + await _jobService.SaveChangesAsync(); + + return new JsonResult(new + { + Updated = updated, + Missing = missing.Select(j => j.ProviderId!).Except(seen) + }); + } + [HttpGet] public async Task PageWatcher(CancellationToken token) { diff --git a/jobhunt/Extensions/ServiceCollectionExtensions.cs b/jobhunt/Extensions/ServiceCollectionExtensions.cs index 6070665..af13117 100644 --- a/jobhunt/Extensions/ServiceCollectionExtensions.cs +++ b/jobhunt/Extensions/ServiceCollectionExtensions.cs @@ -75,6 +75,7 @@ public static IServiceCollection AddIndeedApiSearchProvider(this IServiceCollect public static IServiceCollection AddIndeedGraphQLApi(this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); return services; } diff --git a/jobhunt/Searching/Indeed/IndeedApiSearchProvider.cs b/jobhunt/Searching/Indeed/IndeedApiSearchProvider.cs index d81595a..1f9ac91 100644 --- a/jobhunt/Searching/Indeed/IndeedApiSearchProvider.cs +++ b/jobhunt/Searching/Indeed/IndeedApiSearchProvider.cs @@ -170,6 +170,8 @@ async Task processResults(IEnumerable results) { Title = result.Title, Description = markdown, + Salary = result.FormattedSalary, + AvgYearlySalary = result.AvgYearlySalary, Location = result.Location, Latitude = result.Latitude, Longitude = result.Longitude, diff --git a/jobhunt/Services/CompanyService.cs b/jobhunt/Services/CompanyService.cs index a02024d..6503df7 100644 --- a/jobhunt/Services/CompanyService.cs +++ b/jobhunt/Services/CompanyService.cs @@ -79,6 +79,11 @@ await _context.WatchedPages await transaction.CommitAsync(); } + if (src.Name != dest.Name) + { + dest.AlternateNames.Add(new CompanyName { Name = src.Name }); + } + if (src.Recruiter && !dest.Recruiter) { dest.Recruiter = true;