aboutsummaryrefslogtreecommitdiff
path: root/Pages/JobCounter/Index.cshtml.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2024-09-20 16:21:09 +1000
committerJake Mannens <jake@asger.xyz>2024-10-29 11:42:54 +1100
commite8e3c4cba8ffa0056e984c113cfbb75319e00022 (patch)
tree8336315e61f9e77207276d478b25fe5dc7c3d06c /Pages/JobCounter/Index.cshtml.cs
parent0e21907c76dbefed11f382bcf949143f0716567f (diff)
v0.4-rc1v0.4-rc1
Diffstat (limited to 'Pages/JobCounter/Index.cshtml.cs')
-rw-r--r--Pages/JobCounter/Index.cshtml.cs74
1 files changed, 74 insertions, 0 deletions
diff --git a/Pages/JobCounter/Index.cshtml.cs b/Pages/JobCounter/Index.cshtml.cs
new file mode 100644
index 0000000..cec36fa
--- /dev/null
+++ b/Pages/JobCounter/Index.cshtml.cs
@@ -0,0 +1,74 @@
+using Microsoft.AspNetCore.Mvc.Filters;
+using Microsoft.AspNetCore.Mvc.RazorPages;
+using System.Text.RegularExpressions;
+
+namespace PagerParser.Pages;
+
+public class IndexModel : PageModel {
+ private readonly PagerContext db;
+ private readonly JobCounterConfiguration config = new();
+
+ private string pageDestination;
+
+ public IndexModel(PagerContext db, IConfiguration config) {
+ this.db = db;
+
+ config.Bind("PagerParser:JobCounter", this.config);
+ }
+
+ public int RefreshInterval => config.RefreshInterval ?? 0;
+ public string MessageFilter =>
+ $"^@@ALERT.*\\[{Regex.Escape(pageDestination)}\\]$";
+
+ public int TotalJobs {
+ get {
+ // Attempt to calculate the number of jobs attended based on the
+ // number of distinct FIRS report numbers for successfully-parsed
+ // pager messages plus the number of unparsed, but matched alert
+ // messages. We attempt to scale the number of unparsed messages
+ // based on the ratio of duplicate FIRS report numbers that appear
+ // in the parsed messages, to best predict and compensate for
+ // re-paged jobs, before they are added to the tally.
+
+ // TODO: filter parsed messages based on page destination
+
+ var date = new DateTime(DateTime.UtcNow.Year, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+ var matchedMessages = db.PagerMessages
+ .Where(m => m.Timestamp >= date)
+ .Where(m => Regex.IsMatch(m.Message, MessageFilter));
+ var parsedMessages = matchedMessages
+ .Where(m => m.ParsedMessage != null)
+ .Select(m => m.ParsedMessage!);
+
+ var matchedMessageCount = matchedMessages.Count();
+ var parsedMessageCount = parsedMessages.Count();
+
+ var jobNumberCount = parsedMessages
+ .Select(pm => pm.FirecomJobNo)
+ .Distinct()
+ .Count();
+
+ double uniqueJobRate =
+ (double) jobNumberCount / (double) parsedMessageCount;
+
+ // Scale the number of unparsed messages and add
+ // them to the tally of distinct FIRS job numbers
+ jobNumberCount +=
+ (int) Math.Round((matchedMessageCount - parsedMessageCount) * uniqueJobRate);
+
+ // Apply the manually-entered offset if needed
+ if(config.CountOffsets.TryGetValue(pageDestination, out int offset))
+ jobNumberCount += offset;
+
+ return jobNumberCount;
+ }
+ }
+
+ public override void OnPageHandlerExecuting(PageHandlerExecutingContext context) =>
+ pageDestination = (string) RouteData.Values["PageDestination"]!;
+
+ private record JobCounterConfiguration {
+ public int? RefreshInterval { get; set; } = 300;
+ public Dictionary<string, int> CountOffsets { get; set; } = new();
+ }
+}