Class: Tasker::Functions::FunctionBasedAnalyticsMetrics

Inherits:
FunctionWrapper show all
Defined in:
lib/tasker/functions/function_based_analytics_metrics.rb

Overview

Wrapper for the get_analytics_metrics_v01 SQL function

This function provides comprehensive analytics metrics for performance monitoring, including system overview, performance metrics, and duration calculations.

Examples:

Basic usage

metrics = Tasker::Functions::FunctionBasedAnalyticsMetrics.call
puts metrics.system_health_score
puts metrics.completion_rate

Time-bounded analysis

since_time = 4.hours.ago
metrics = Tasker::Functions::FunctionBasedAnalyticsMetrics.call(since_time)
puts metrics.task_throughput

Defined Under Namespace

Classes: AnalyticsMetrics

Class Method Summary collapse

Methods inherited from FunctionWrapper

connection, convert_array_bind, from_sql_function, #readonly?, single_from_sql_function

Class Method Details

.call(since_timestamp = nil) ⇒ AnalyticsMetrics

Call the get_analytics_metrics_v01 SQL function

Parameters:

  • since_timestamp (Time, nil) (defaults to: nil)

    Start time for analysis (defaults to 1 hour ago in SQL)

Returns:

Raises:

  • (ActiveRecord::StatementInvalid)

    If the SQL function fails



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tasker/functions/function_based_analytics_metrics.rb', line 53

def self.call(since_timestamp = nil)
  if since_timestamp
    sql = 'SELECT * FROM get_analytics_metrics_v01($1)'
    result = connection.select_all(sql, 'AnalyticsMetrics Load', [since_timestamp])
  else
    sql = 'SELECT * FROM get_analytics_metrics_v01()'
    result = connection.select_all(sql, 'AnalyticsMetrics Load')
  end

  if result.empty?
    # Return zero values if no data (shouldn't happen but defensive)
    AnalyticsMetrics.new(
      active_tasks_count: 0,
      total_namespaces_count: 0,
      unique_task_types_count: 0,
      system_health_score: 1.0,
      task_throughput: 0,
      completion_count: 0,
      error_count: 0,
      completion_rate: 0.0,
      error_rate: 0.0,
      avg_task_duration: 0.0,
      avg_step_duration: 0.0,
      step_throughput: 0,
      analysis_period_start: (since_timestamp || 1.hour.ago).to_s,
      calculated_at: Time.current.to_s
    )
  else
    # Convert the result to a structured AnalyticsMetrics object
    row = result.first
    AnalyticsMetrics.new(
      active_tasks_count: row['active_tasks_count'].to_i,
      total_namespaces_count: row['total_namespaces_count'].to_i,
      unique_task_types_count: row['unique_task_types_count'].to_i,
      system_health_score: row['system_health_score'].to_f,
      task_throughput: row['task_throughput'].to_i,
      completion_count: row['completion_count'].to_i,
      error_count: row['error_count'].to_i,
      completion_rate: row['completion_rate'].to_f,
      error_rate: row['error_rate'].to_f,
      avg_task_duration: row['avg_task_duration'].to_f,
      avg_step_duration: row['avg_step_duration'].to_f,
      step_throughput: row['step_throughput'].to_i,
      analysis_period_start: row['analysis_period_start'].to_s,
      calculated_at: row['calculated_at'].to_s
    )
  end
end