Skip to content

Commit

Permalink
better thinking
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapaglia committed Jun 22, 2021
1 parent 161f009 commit dab5d61
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 0 additions & 2 deletions OpenAlprWebhookProcessor/Hydration/HydrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ private async Task StartHydrationAsync()
}

await _processorHub.Clients.All.ScrapeFinished();

_logger.LogInformation("Finished OpenALPR Agent scrape.");
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions OpenAlprWebhookProcessor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public void ConfigureServices(IServiceCollection services)
{
options.SchedulePollingInterval = TimeSpan.FromSeconds(1);
});

services.AddMemoryCache();
}

public void Configure(
Expand Down
21 changes: 20 additions & 1 deletion OpenAlprWebhookProcessor/WebhookProcessor/GroupWebhookHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public GroupWebhookHandler(
public async Task HandleWebhookAsync(
Webhook webhook,
bool isBulkImport,
List<string> previouslyProcessedGroups,
CancellationToken cancellationToken)
{
var camera = await _processorContext.Cameras
Expand All @@ -67,7 +68,25 @@ public async Task HandleWebhookAsync(
return;
}

var alreadyProcessed = await _processorContext.PlateGroups.AnyAsync(x => x.OpenAlprUuid == webhook.Group.BestUuid);
var alreadyProcessed = false;

if (previouslyProcessedGroups != null)
{
alreadyProcessed = previouslyProcessedGroups
.Intersect(webhook.Group.Uuids)
.Any();

if (!alreadyProcessed)
{
previouslyProcessedGroups.AddRange(webhook.Group.Uuids);
}
}
else
{
alreadyProcessed = await _processorContext.PlateGroups.Select(x => x.OpenAlprUuid)
.Intersect(webhook.Group.Uuids)
.AnyAsync(cancellationToken);
}

if (alreadyProcessed)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebhook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -76,34 +77,43 @@ public async Task ScrapeAgentAsync(CancellationToken cancellationToken)

foreach (var metadata in metaDatasToQuery)
{
_logger.LogInformation("querying: " + metadata.Key);

var newGroup = await _httpClient.GetAsync(
agent.EndpointUrl + metadataUrl.Replace("{0}", metadata.Key),
cancellationToken);

var group = await JsonSerializer.DeserializeAsync<Group>(
await newGroup.Content.ReadAsStreamAsync(cancellationToken),
cancellationToken: cancellationToken);

_logger.LogInformation("date: " + DateTimeOffset.FromUnixTimeMilliseconds(group.EpochStart).ToString() + " querying: " + metadata.Key);

if (!newGroup.IsSuccessStatusCode)
{
_logger.LogError("Unable to parse Group with Id: " + metadata.Key);
continue;
}

var newGroupResult = await newGroup.Content.ReadAsStringAsync(cancellationToken);

var previouslyProcessedGroups = await _processorContext.PlateGroups
.Select(x => x.OpenAlprUuid)
.ToListAsync(cancellationToken);
try
{
await _groupWebhookHandler.HandleWebhookAsync(
new Webhook
{
Group = JsonSerializer.Deserialize<Group>(newGroupResult)
Group = group,
},
true,
previouslyProcessedGroups,
cancellationToken);
}
catch
{
_logger.LogError("Failed to parse bulk import request.");
}

agent.LastSuccessfulScrapeEpoch = group.EpochStart;
await _processorContext.SaveChangesAsync(cancellationToken);
}

lastSuccessfulScrape = lastSuccessfulScrape.AddMinutes(minutesToScrape);
Expand All @@ -112,10 +122,9 @@ await _groupWebhookHandler.HandleWebhookAsync(
{
lastSuccessfulScrape = startDate;
}

agent.LastSuccessfulScrapeEpoch = lastSuccessfulScrape.ToUnixTimeMilliseconds();
await _processorContext.SaveChangesAsync(cancellationToken);
}

_logger.LogInformation("Finished OpenALPR Agent scrape.");
}

private async Task<DateTimeOffset> GetEarliestGroupEpochAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public async Task<ActionResult> Post(CancellationToken cancellationToken)
await _groupWebhookHandler.HandleWebhookAsync(
alertGroupResult,
false,
null,
cancellationToken);
}
else if (rawWebhook.Contains("alpr_group"))
Expand All @@ -61,6 +62,7 @@ await _groupWebhookHandler.HandleWebhookAsync(
await _groupWebhookHandler.HandleWebhookAsync(
groupResult,
false,
null,
cancellationToken);
}
else if (rawWebhook.Contains("alpr_results"))
Expand Down

0 comments on commit dab5d61

Please sign in to comment.