Files
bom-local-service/Services/Scraping/Workflows/RadarScrapingWorkflow.cs
T
alex 12dd0eedad Refactor scraping system to workflow-based architecture
Major architectural changes:
- Replace monolithic ScrapingService with workflow-based system
- Extract scraping logic into discrete, testable steps
- Add configuration-driven selectors, JavaScript templates, and text patterns
- Implement step registry and workflow factory for extensibility
- Add PageState tracking and step dependencies
- Make IWorkflow generic to support different response types

New components:
- Scraping steps organized by category (Navigation, Search, Map, Metadata, Capture)
- BaseScrapingStep base class with common dependencies
- ScrapingContext for shared state between steps
- RadarScrapingWorkflow orchestrating step execution
- SelectorService for configurable element selection
- Configuration models (SelectorConfig, TextPatternsConfig, JavaScriptTemplatesConfig)

Performance and monitoring:
- Add step-level timing metrics with historical tracking
- Log step durations and compare to averages
- Warn on slow steps/workflows (50%+ slower than average)
- Integrate metrics with CacheService

Configuration improvements:
- Externalize all selectors to appsettings.json
- Externalize JavaScript templates to appsettings.json
- Externalize text patterns to appsettings.json
- Support Docker configuration via mounted appsettings.json
- Remove hardcoded waits, use configurable timeouts

Bug fixes:
- Fix DebugService collection modification exception
- Fix ResetToFirstFrameStep click interception with JavaScript fallback
- Remove minutes ago fallback, use timestamp parsing only
- Update timestamp parsing for new BOM website format

Documentation:
- Update README with new architecture details
- Add configuration section with Docker guidance
- Add development guidelines for extending scraping system
- Add performance monitoring documentation
2025-12-18 00:54:57 +10:00

211 lines
8.7 KiB
C#

using BomLocalService.Models;
using BomLocalService.Services.Interfaces;
using BomLocalService.Services.Scraping;
using BomLocalService.Utilities;
namespace BomLocalService.Services.Scraping.Workflows;
public class RadarScrapingWorkflow : IWorkflow<RadarResponse>
{
private readonly ILogger<RadarScrapingWorkflow> _logger;
private readonly IScrapingStepRegistry _stepRegistry;
private readonly IConfiguration _configuration;
private readonly ICacheService _cacheService;
private readonly double _cacheExpirationMinutes;
private readonly int _cacheManagementCheckIntervalMinutes;
public string Name => "RadarScraping";
public string Description => "Scrapes radar images for a location";
// Fixed step sequence - order matters due to dependencies
public string[] StepNames => new[]
{
"NavigateHomepage",
"ClickSearchButton",
"FillSearchInput",
"WaitForSearchResults",
"SelectSearchResult",
"ClickRadarLink",
"WaitForMapReady",
"PauseRadar",
"ResetToFirstFrame",
"ExtractMetadata",
"CalculateMapBounds",
"CaptureFrames"
};
public RadarScrapingWorkflow(
ILogger<RadarScrapingWorkflow> logger,
IScrapingStepRegistry stepRegistry,
IConfiguration configuration,
ICacheService cacheService)
{
_logger = logger;
_stepRegistry = stepRegistry;
_configuration = configuration;
_cacheService = cacheService;
_cacheExpirationMinutes = configuration.GetValue<double>("CacheExpirationMinutes", 12.5);
_cacheManagementCheckIntervalMinutes = configuration.GetValue<int>("CacheManagement:CheckIntervalMinutes", 5);
}
public async Task<RadarResponse> ExecuteAsync(ScrapingContext context, CancellationToken cancellationToken)
{
var workflowConfig = _configuration.GetSection($"Scraping:Workflows:{Name}").Get<ScrapingWorkflowConfig>();
var workflowStartTime = DateTime.UtcNow;
var stepTimings = new List<(string stepName, double durationSeconds)>();
// Record Initializing phase start for metrics tracking
if (!string.IsNullOrEmpty(context.CacheFolderPath))
{
_cacheService.RecordUpdateProgressByFolder(context.CacheFolderPath, CacheUpdatePhase.Initializing);
}
foreach (var stepName in StepNames)
{
var stepConfig = workflowConfig?.Steps?.GetValueOrDefault(stepName) ?? new ScrapingStepConfig { Enabled = true };
if (!stepConfig.Enabled)
{
_logger.LogInformation("Step {Step} is disabled, skipping", stepName);
continue;
}
var step = _stepRegistry.GetStep(stepName);
if (step == null)
{
throw new InvalidOperationException($"Step {stepName} not found in registry");
}
if (!ValidatePrerequisites(step, context))
{
throw new InvalidOperationException(
$"Step {stepName} prerequisites not met. Required: {string.Join(", ", step.Prerequisites)}");
}
if (!step.CanExecute(context))
{
throw new InvalidOperationException(
$"Step {stepName} cannot execute in current page state: {context.CurrentState}");
}
var stepStartTime = DateTime.UtcNow;
_logger.LogInformation("Executing step {Step}", stepName);
var result = await step.ExecuteAsync(context, cancellationToken);
var stepDuration = (DateTime.UtcNow - stepStartTime).TotalSeconds;
stepTimings.Add((stepName, stepDuration));
// Record step timing in metrics
_cacheService.RecordStepCompletion(stepName, stepDuration);
// Get historical average for comparison
var avgDuration = _cacheService.GetAverageStepDuration(stepName);
if (avgDuration > 0)
{
var diff = stepDuration - avgDuration;
var diffPercent = (diff / avgDuration) * 100;
var isSlow = stepDuration > avgDuration * 1.5; // 50% slower than average
if (isSlow)
{
_logger.LogWarning("Step {Step} took significantly longer than average: {Duration:F2}s (avg: {Avg:F2}s, {Diff:+#.##}s, {DiffPercent:+#0.#}% slower)",
stepName, stepDuration, avgDuration, diff, diffPercent);
}
else if (Math.Abs(diffPercent) > 20) // More than 20% difference (faster or slower)
{
_logger.LogInformation("Step {Step} completed in {Duration:F2}s (avg: {Avg:F2}s, {Diff:+#.##;-#.##}s, {DiffPercent:+#0.#;-#0.#}%)",
stepName, stepDuration, avgDuration, diff, diffPercent);
}
else
{
_logger.LogInformation("Step {Step} completed in {Duration:F2}s (avg: {Avg:F2}s)", stepName, stepDuration, avgDuration);
}
}
else
{
_logger.LogInformation("Step {Step} completed in {Duration:F2}s", stepName, stepDuration);
}
if (!result.Success)
{
throw new Exception($"Step {stepName} failed: {result.ErrorMessage}");
}
context.CompletedSteps.Add(stepName);
}
var totalDuration = (DateTime.UtcNow - workflowStartTime).TotalSeconds;
// Check if workflow duration is significantly longer than average
var avgTotalDuration = _cacheService.GetAverageTotalDuration();
var isWorkflowSlow = avgTotalDuration > 0 && totalDuration > avgTotalDuration * 1.3; // 30% slower than average
// Log step breakdown
var stepBreakdown = string.Join(", ", stepTimings.Select(t => $"{t.stepName}={t.durationSeconds:F2}s"));
if (isWorkflowSlow)
{
var workflowDiff = totalDuration - avgTotalDuration;
var workflowDiffPercent = (workflowDiff / avgTotalDuration) * 100;
_logger.LogWarning("Workflow {Workflow} took significantly longer than average: {TotalDuration:F2}s (avg: {Avg:F2}s, {Diff:+#.##}s, {DiffPercent:+#0.#}% slower) for {Suburb}, {State}",
Name, totalDuration, avgTotalDuration, workflowDiff, workflowDiffPercent, context.Suburb, context.State);
}
else
{
_logger.LogInformation("Workflow {Workflow} completed in {TotalDuration:F2}s for {Suburb}, {State}. Step breakdown: {StepBreakdown}",
Name, totalDuration, context.Suburb, context.State, stepBreakdown);
}
// Log step performance summary if we have historical data
var stepMetrics = _cacheService.GetStepMetrics();
if (stepMetrics.Count > 0)
{
var slowSteps = stepTimings
.Where(t => stepMetrics.ContainsKey(t.stepName) && t.durationSeconds > stepMetrics[t.stepName] * 1.5)
.Select(t => $"{t.stepName} ({t.durationSeconds:F2}s vs avg {stepMetrics[t.stepName]:F2}s)")
.ToList();
if (slowSteps.Any())
{
_logger.LogWarning("Workflow {Workflow} had slower-than-average steps: {SlowSteps}",
Name, string.Join(", ", slowSteps));
}
}
return BuildResponse(context);
}
private bool ValidatePrerequisites(IScrapingStep step, ScrapingContext context)
{
return step.Prerequisites.All(prereq => context.CompletedSteps.Contains(prereq));
}
private RadarResponse BuildResponse(ScrapingContext context)
{
if (context.LastUpdatedInfo == null)
{
throw new InvalidOperationException("LastUpdatedInfo is required to build response");
}
if (context.Frames == null || context.Frames.Count == 0)
{
throw new InvalidOperationException("Frames are required to build response");
}
var cacheExpiresAt = context.LastUpdatedInfo.ObservationTime.AddMinutes(_cacheExpirationMinutes);
return ResponseBuilder.CreateRadarResponse(
context.CacheFolderPath,
context.Frames,
_cacheManagementCheckIntervalMinutes,
context.LastUpdatedInfo,
context.Suburb,
context.State,
cacheIsValid: true,
cacheExpiresAt: cacheExpiresAt,
isUpdating: false);
}
}