A complete log processing pipeline that parses multiple lines and calculates aggregate metrics like total requests, error count, and average response time.

ID: 1389

				
					using uCalcSoftware;

var uc = 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 (var t = new uCalc.Transformer(uc)) {
   var pattern = "{@String:request} {@Number:status} {@Number:time}ms";

   // 3. The replacement string uses @Exec for side-effects (updating variables)
   var 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
   var 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);
}

// 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");
				
			
--- Log Analysis Summary ---
Total Requests: 4
Total Errors: 1
Average Response Time: 41.25ms
Maximum Response Time: 120ms
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // 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
   {
      uCalc::Transformer t(uc);
      t.Owned(); // Causes t to be released when it goes out of scope
      auto pattern = "{@String:request} {@Number:status} {@Number:time}ms";

      // 3. The replacement string uses @Exec for side-effects (updating variables)
      auto replacement = R"(
{@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
      auto logText = R"(
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);
   }

   // 6. Display the final aggregated metrics
   cout << "--- Log Analysis Summary ---" << endl;
   cout << "Total Requests: " << uc.EvalStr("request_count") << endl;
   cout << "Total Errors: " << uc.EvalStr("error_count") << endl;
   cout << "Average Response Time: " << uc.EvalStr("total_response_time / request_count") << "ms" << endl;
   cout << "Maximum Response Time: " << uc.EvalStr("max_response_time") << "ms" << endl;
}
				
			
--- Log Analysis Summary ---
Total Requests: 4
Total Errors: 1
Average Response Time: 41.25ms
Maximum Response Time: 120ms
				
					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
				
			
--- Log Analysis Summary ---
Total Requests: 4
Total Errors: 1
Average Response Time: 41.25ms
Maximum Response Time: 120ms