Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// 1. Define variables to hold the metrics
      uc.DefineVariable("request_count = 0")
      uc.DefineVariable("error_count = 0")
      uc.DefineVariable("total_response_time = 0.0")
      uc.DefineVariable("max_response_time = 0.0")
      
      '// 2. Create the transformer and define the rule
      Using t As New uCalc.Transformer(uc)
         Dim pattern = "{@String:request} {@Number:status} {@Number:time}ms"
         
         '// 3. The replacement string uses @Exec for side-effects (updating variables)
         Dim replacement = "
{@Exec: request_count++}
{@Exec: total_response_time = total_response_time + Double(time)}
{@Exec: max_response_time = Max(max_response_time, Double(time))}
{@Exec: iif(Double(status) >= 400, error_count++, 0)}
"
         
         t.FromTo(pattern, replacement)
         
         '// 4. Define the multi-line log data
         Dim logText = "
2024-10-26 10:00:05 INFO    192.168.1.10   ""GET /api/users HTTP/1.1"" 200 15ms
2024-10-26 10:00:06 INFO    192.168.1.15   ""GET /api/products HTTP/1.1"" 200 22ms
2024-10-26 10:00:07 ERROR   192.168.1.22   ""POST /api/login HTTP/1.1"" 500 120ms
2024-10-26 10:00:08 INFO    192.168.1.10   ""GET /api/users/1 HTTP/1.1"" 200 8ms
"
         
         '// 5. Run the transformation (the output will be empty as we only use @Exec)
         t.Transform(logText)
      End Using
      
      '// 6. Display the final aggregated metrics
      Console.WriteLine("--- Log Analysis Summary ---")
      Console.WriteLine($"Total Requests: {uc.EvalStr("request_count")}")
      Console.WriteLine($"Total Errors: {uc.EvalStr("error_count")}")
      Console.WriteLine($"Average Response Time: {uc.EvalStr("total_response_time / request_count")}ms")
      Console.WriteLine($"Maximum Response Time: {uc.EvalStr("max_response_time")}ms")
   End Sub
End Module