Skip to content

Commit ef5bc7d

Browse files
committed
Refactor performance metrics method names for clarity in CQL::Performance module
- Renamed performance metrics methods for consistency and improved readability, changing `get_metrics`, `get_metrics_summary`, `get_critical_issues`, and `get_high_priority_issues` to `metrics`, `metrics_summary`, `critical_issues`, and `high_priority_issues` respectively. - Updated method definitions and calls throughout the performance metrics implementation to reflect these changes. - Enhanced the structure of hash outputs in the `to_h` methods for better organization and clarity.
1 parent 6e5ade8 commit ef5bc7d

File tree

2 files changed

+94
-94
lines changed

2 files changed

+94
-94
lines changed

src/performance.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,23 @@ module CQL::Performance
108108
end
109109

110110
# Performance Metrics convenience methods
111-
def self.get_metrics : PerformanceMetrics
111+
def self.metrics : PerformanceMetrics
112112
monitor.metrics
113113
end
114114

115-
def self.get_metrics_summary : Hash(String, String | Int64 | Float64)
115+
def self.metrics_summary : Hash(String, String | Int64 | Float64)
116116
monitor.metrics_summary
117117
end
118118

119119
def self.is_healthy? : Bool
120120
monitor.healthy?
121121
end
122122

123-
def self.get_critical_issues : Array(Issue)
123+
def self.critical_issues : Array(Issue)
124124
monitor.critical_issues
125125
end
126126

127-
def self.get_high_priority_issues : Array(Issue)
127+
def self.high_priority_issues : Array(Issue)
128128
monitor.high_priority_issues
129129
end
130130

src/performance/performance_metrics.cr

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ module CQL::Performance
5353

5454
def to_h : Hash(String, JSON::Any)
5555
{
56-
"total_queries" => JSON::Any.new(@total_queries),
57-
"slow_queries" => JSON::Any.new(@slow_queries),
58-
"very_slow_queries" => JSON::Any.new(@very_slow_queries),
59-
"error_queries" => JSON::Any.new(@error_queries),
56+
"total_queries" => JSON::Any.new(@total_queries),
57+
"slow_queries" => JSON::Any.new(@slow_queries),
58+
"very_slow_queries" => JSON::Any.new(@very_slow_queries),
59+
"error_queries" => JSON::Any.new(@error_queries),
6060
"total_execution_time_ms" => JSON::Any.new(@total_execution_time.total_milliseconds),
6161
"avg_execution_time_ms" => JSON::Any.new(@avg_execution_time.total_milliseconds),
6262
"min_execution_time_ms" => JSON::Any.new(@min_execution_time == Time::Span::MAX ? 0.0 : @min_execution_time.total_milliseconds),
@@ -142,11 +142,11 @@ module CQL::Performance
142142

143143
def to_h : Hash(String, JSON::Any)
144144
{
145-
"uptime_seconds" => JSON::Any.new(@uptime.total_seconds),
146-
"memory_usage_mb" => JSON::Any.new(@memory_usage_mb),
147-
"cpu_usage_percent" => JSON::Any.new(@cpu_usage_percent),
148-
"active_connections" => JSON::Any.new(@active_connections),
149-
"max_connections" => JSON::Any.new(@max_connections),
145+
"uptime_seconds" => JSON::Any.new(@uptime.total_seconds),
146+
"memory_usage_mb" => JSON::Any.new(@memory_usage_mb),
147+
"cpu_usage_percent" => JSON::Any.new(@cpu_usage_percent),
148+
"active_connections" => JSON::Any.new(@active_connections),
149+
"max_connections" => JSON::Any.new(@max_connections),
150150
"connection_pool_utilization_percent" => JSON::Any.new(@connection_pool_utilization),
151151
}
152152
end
@@ -172,16 +172,16 @@ module CQL::Performance
172172

173173
def to_h : Hash(String, JSON::Any)
174174
{
175-
"overall_health_score" => JSON::Any.new(@overall_health_score),
176-
"query_health_score" => JSON::Any.new(@query_health_score),
177-
"n_plus_one_health_score" => JSON::Any.new(@n_plus_one_health_score),
178-
"cache_health_score" => JSON::Any.new(@cache_health_score),
179-
"system_health_score" => JSON::Any.new(@system_health_score),
180-
"critical_issues" => JSON::Any.new(@critical_issues),
181-
"high_issues" => JSON::Any.new(@high_issues),
182-
"medium_issues" => JSON::Any.new(@medium_issues),
183-
"low_issues" => JSON::Any.new(@low_issues),
184-
"total_issues" => JSON::Any.new(@total_issues),
175+
"overall_health_score" => JSON::Any.new(@overall_health_score),
176+
"query_health_score" => JSON::Any.new(@query_health_score),
177+
"n_plus_one_health_score" => JSON::Any.new(@n_plus_one_health_score),
178+
"cache_health_score" => JSON::Any.new(@cache_health_score),
179+
"system_health_score" => JSON::Any.new(@system_health_score),
180+
"critical_issues" => JSON::Any.new(@critical_issues),
181+
"high_issues" => JSON::Any.new(@high_issues),
182+
"medium_issues" => JSON::Any.new(@medium_issues),
183+
"low_issues" => JSON::Any.new(@low_issues),
184+
"total_issues" => JSON::Any.new(@total_issues),
185185
}
186186
end
187187
end
@@ -198,10 +198,10 @@ module CQL::Performance
198198

199199
def to_h : Hash(String, JSON::Any)
200200
{
201-
"slowest_queries" => JSON::Any.new(@slowest_queries.map { |q| PerformanceMetrics.to_json_any(q.to_h) }),
202-
"most_frequent_queries" => JSON::Any.new(@most_frequent_queries.map { |q| PerformanceMetrics.to_json_any(q) }),
203-
"highest_error_queries" => JSON::Any.new(@highest_error_queries.map { |q| PerformanceMetrics.to_json_any(q) }),
204-
"most_expensive_queries" => JSON::Any.new(@most_expensive_queries.map { |q| PerformanceMetrics.to_json_any(q) }),
201+
"slowest_queries" => JSON::Any.new(@slowest_queries.map { |query| PerformanceMetrics.to_json_any(query.to_h) }),
202+
"most_frequent_queries" => JSON::Any.new(@most_frequent_queries.map { |query| PerformanceMetrics.to_json_any(query) }),
203+
"highest_error_queries" => JSON::Any.new(@highest_error_queries.map { |query| PerformanceMetrics.to_json_any(query) }),
204+
"most_expensive_queries" => JSON::Any.new(@most_expensive_queries.map { |query| PerformanceMetrics.to_json_any(query) }),
205205
}
206206
end
207207
end
@@ -218,10 +218,10 @@ module CQL::Performance
218218

219219
def to_h : Hash(String, JSON::Any)
220220
{
221-
"n_plus_one_patterns" => JSON::Any.new(@n_plus_one_patterns.map { |p| PerformanceMetrics.to_json_any(p.to_h) }),
222-
"query_patterns" => JSON::Any.new(@query_patterns.map { |p| PerformanceMetrics.to_json_any(p) }),
223-
"time_distribution" => PerformanceMetrics.to_json_any(@time_distribution),
224-
"error_patterns" => JSON::Any.new(@error_patterns.map { |p| PerformanceMetrics.to_json_any(p) }),
221+
"n_plus_one_patterns" => JSON::Any.new(@n_plus_one_patterns.map { |pattern| PerformanceMetrics.to_json_any(pattern.to_h) }),
222+
"query_patterns" => JSON::Any.new(@query_patterns.map { |pattern| PerformanceMetrics.to_json_any(pattern) }),
223+
"time_distribution" => PerformanceMetrics.to_json_any(@time_distribution),
224+
"error_patterns" => JSON::Any.new(@error_patterns.map { |pattern| PerformanceMetrics.to_json_any(pattern) }),
225225
}
226226
end
227227
end
@@ -247,7 +247,7 @@ module CQL::Performance
247247
@top_queries : TopQueries,
248248
@patterns : PerformancePatterns,
249249
@issues : Array(Issue),
250-
@collection_duration : Time::Span = Time::Span.zero
250+
@collection_duration : Time::Span = Time::Span.zero,
251251
)
252252
end
253253

@@ -257,7 +257,7 @@ module CQL::Performance
257257
detector : NPlusOneDetectorInterface? = nil,
258258
cache : Cache? = nil,
259259
start_time : Time? = nil,
260-
config : Config? = nil
260+
config : Config? = nil,
261261
) : self
262262
start_time ||= Time.utc
263263
collection_duration = Time.utc - start_time
@@ -302,16 +302,16 @@ module CQL::Performance
302302
# Export all metrics as a comprehensive hash
303303
def to_h : Hash(String, JSON::Any)
304304
{
305-
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
305+
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
306306
"collection_duration_seconds" => JSON::Any.new(@collection_duration.total_seconds),
307-
"query_metrics" => JSON::Any.new(@query_metrics.to_h),
308-
"n_plus_one_metrics" => JSON::Any.new(@n_plus_one_metrics.to_h),
309-
"cache_metrics" => JSON::Any.new(@cache_metrics.to_h),
310-
"system_metrics" => JSON::Any.new(@system_metrics.to_h),
311-
"health_metrics" => JSON::Any.new(@health_metrics.to_h),
312-
"top_queries" => JSON::Any.new(@top_queries.to_h),
313-
"patterns" => JSON::Any.new(@patterns.to_h),
314-
"issues" => JSON::Any.new(@issues.map { |i| PerformanceMetrics.to_json_any(i.to_h) }),
307+
"query_metrics" => JSON::Any.new(@query_metrics.to_h),
308+
"n_plus_one_metrics" => JSON::Any.new(@n_plus_one_metrics.to_h),
309+
"cache_metrics" => JSON::Any.new(@cache_metrics.to_h),
310+
"system_metrics" => JSON::Any.new(@system_metrics.to_h),
311+
"health_metrics" => JSON::Any.new(@health_metrics.to_h),
312+
"top_queries" => JSON::Any.new(@top_queries.to_h),
313+
"patterns" => JSON::Any.new(@patterns.to_h),
314+
"issues" => JSON::Any.new(@issues.map { |i| PerformanceMetrics.to_json_any(i.to_h) }),
315315
}
316316
end
317317

@@ -323,14 +323,14 @@ module CQL::Performance
323323
# Get summary metrics for quick overview
324324
def summary : Hash(String, String | Int32 | Int64 | Float64)
325325
{
326-
"total_queries" => @query_metrics.total_queries,
327-
"slow_queries" => @query_metrics.slow_queries,
328-
"error_rate_percent" => @query_metrics.error_rate,
329-
"avg_query_time_ms" => @query_metrics.avg_execution_time.total_milliseconds,
326+
"total_queries" => @query_metrics.total_queries,
327+
"slow_queries" => @query_metrics.slow_queries,
328+
"error_rate_percent" => @query_metrics.error_rate,
329+
"avg_query_time_ms" => @query_metrics.avg_execution_time.total_milliseconds,
330330
"n_plus_one_patterns" => @n_plus_one_metrics.total_patterns,
331-
"health_score" => @health_metrics.overall_health_score.to_s,
332-
"total_issues" => @health_metrics.total_issues,
333-
"uptime_seconds" => @system_metrics.uptime.total_seconds,
331+
"health_score" => @health_metrics.overall_health_score.to_s,
332+
"total_issues" => @health_metrics.total_issues,
333+
"uptime_seconds" => @system_metrics.uptime.total_seconds,
334334
}
335335
end
336336

@@ -400,7 +400,7 @@ module CQL::Performance
400400

401401
# Count slow queries
402402
slow_queries = profiler.slowest_queries(1000).size.to_i64
403-
very_slow_queries = profiler.slowest_queries(1000).select { |q| q.execution_time > 1.second }.size.to_i64
403+
very_slow_queries = profiler.slowest_queries(1000).count { |query| query.execution_time > 1.second }
404404

405405
# Calculate rates
406406
error_rate = 0.0 # TODO: Track errors properly
@@ -434,10 +434,10 @@ module CQL::Performance
434434
max_repetitions = patterns.max_of?(&.repetition_count) || 0
435435

436436
# Count by severity
437-
critical_patterns = patterns.count { |p| p.repetition_count > 50 }
438-
high_patterns = patterns.count { |p| p.repetition_count > 20 && p.repetition_count <= 50 }
439-
medium_patterns = patterns.count { |p| p.repetition_count > 5 && p.repetition_count <= 20 }
440-
low_patterns = patterns.count { |p| p.repetition_count > 2 && p.repetition_count <= 5 }
437+
critical_patterns = patterns.count { |pattern| pattern.repetition_count > 50 }
438+
high_patterns = patterns.count { |pattern| pattern.repetition_count > 20 && pattern.repetition_count <= 50 }
439+
medium_patterns = patterns.count { |pattern| pattern.repetition_count > 5 && pattern.repetition_count <= 20 }
440+
low_patterns = patterns.count { |pattern| pattern.repetition_count > 2 && pattern.repetition_count <= 5 }
441441

442442
# Detection rate (placeholder)
443443
detection_rate = 0.0
@@ -465,10 +465,10 @@ module CQL::Performance
465465
0_i64, # cache_hits
466466
0_i64, # cache_misses
467467
cache.size,
468-
1000, # max_cache_size
469-
0.0, # hit_rate
470-
0.0, # miss_rate
471-
0_i64 # evictions
468+
1000, # max_cache_size
469+
0.0, # hit_rate
470+
0.0, # miss_rate
471+
0_i64 # evictions
472472
)
473473
end
474474

@@ -478,11 +478,11 @@ module CQL::Performance
478478
# Placeholder values - would need system monitoring
479479
SystemMetrics.new(
480480
uptime,
481-
0.0, # memory_usage_mb
482-
0.0, # cpu_usage_percent
483-
0, # active_connections
484-
100, # max_connections
485-
0.0 # connection_pool_utilization
481+
0.0, # memory_usage_mb
482+
0.0, # cpu_usage_percent
483+
0, # active_connections
484+
100, # max_connections
485+
0.0 # connection_pool_utilization
486486
)
487487
end
488488

@@ -506,7 +506,7 @@ module CQL::Performance
506506
query_health_score,
507507
n_plus_one_health_score,
508508
cache_health_score,
509-
system_health_score
509+
system_health_score,
510510
].sum / 4).to_i
511511

512512
HealthMetrics.new(
@@ -540,23 +540,23 @@ module CQL::Performance
540540
stats = profiler.statistics
541541
most_frequent = stats.map do |sql, stat|
542542
{
543-
sql: sql,
544-
count: stat[:count].to_i64,
545-
avg_time: stat[:avg_ms].milliseconds
543+
sql: sql,
544+
count: stat[:count].to_i64,
545+
avg_time: stat[:avg_ms].milliseconds,
546546
}
547-
end.sort_by(&.[:count]).reverse!.first(10)
547+
end.sort_by!(&.[:count]).reverse!.first(10)
548548

549549
# Highest error queries (placeholder)
550550
highest_error_queries = [] of NamedTuple(sql: String, errors: Int64, error_rate: Float64)
551551

552552
# Most expensive queries (total time)
553553
most_expensive = stats.map do |sql, stat|
554554
{
555-
sql: sql,
555+
sql: sql,
556556
total_time: stat[:total_ms].milliseconds,
557-
count: stat[:count].to_i64
557+
count: stat[:count].to_i64,
558558
}
559-
end.sort_by(&.[:total_time]).reverse!.first(10)
559+
end.sort_by!(&.[:total_time]).reverse!.first(10)
560560

561561
TopQueries.new(slowest_queries, most_frequent, highest_error_queries, most_expensive)
562562
end
@@ -571,18 +571,18 @@ module CQL::Performance
571571
stats = profiler.statistics
572572
query_patterns = stats.map do |sql, stat|
573573
{
574-
pattern: sql,
575-
count: stat[:count].to_i64,
576-
avg_time: stat[:avg_ms].milliseconds
574+
pattern: sql,
575+
count: stat[:count].to_i64,
576+
avg_time: stat[:avg_ms].milliseconds,
577577
}
578-
end.sort_by(&.[:count]).reverse!.first(20)
578+
end.sort_by!(&.[:count]).reverse!.first(20)
579579
end
580580

581581
# Time distribution
582582
time_distribution = {
583-
"fast" => 0_i64,
584-
"slow" => 0_i64,
585-
"very_slow" => 0_i64
583+
"fast" => 0_i64,
584+
"slow" => 0_i64,
585+
"very_slow" => 0_i64,
586586
}
587587

588588
# Error patterns (placeholder)
@@ -606,7 +606,7 @@ module CQL::Performance
606606

607607
total_queries = stats.values.sum(&.[:count])
608608
slow_queries = profiler.slowest_queries(1000).size
609-
very_slow_queries = profiler.slowest_queries(1000).select { |q| q.execution_time > 1.second }.size
609+
very_slow_queries = profiler.slowest_queries(1000).count { |query| query.execution_time > 1.second }
610610

611611
score = 100
612612
score -= (slow_queries.to_f / total_queries * 20).to_i if total_queries > 0
@@ -620,8 +620,8 @@ module CQL::Performance
620620
patterns = detector.patterns
621621
return 100 if patterns.empty?
622622

623-
critical_patterns = patterns.count { |p| p.repetition_count > 50 }
624-
high_patterns = patterns.count { |p| p.repetition_count > 20 }
623+
critical_patterns = patterns.count { |pattern| pattern.repetition_count > 50 }
624+
high_patterns = patterns.count { |pattern| pattern.repetition_count > 20 }
625625

626626
score = 100
627627
score -= critical_patterns * 30
@@ -641,13 +641,13 @@ module CQL::Performance
641641
struct QueryData
642642
def to_h : Hash(String, JSON::Any)
643643
{
644-
"sql" => JSON::Any.new(@sql),
645-
"params" => PerformanceMetrics.to_json_any(@params),
644+
"sql" => JSON::Any.new(@sql),
645+
"params" => PerformanceMetrics.to_json_any(@params),
646646
"execution_time_ms" => JSON::Any.new(@execution_time.total_milliseconds),
647-
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
648-
"rows_affected" => JSON::Any.new(@rows_affected || 0_i64),
649-
"error" => JSON::Any.new(@error || ""),
650-
"normalized_sql" => JSON::Any.new(normalized_sql),
647+
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
648+
"rows_affected" => JSON::Any.new(@rows_affected || 0_i64),
649+
"error" => JSON::Any.new(@error || ""),
650+
"normalized_sql" => JSON::Any.new(normalized_sql),
651651
}
652652
end
653653
end
@@ -656,10 +656,10 @@ module CQL::Performance
656656
struct NPlusOnePattern
657657
def to_h : Hash(String, JSON::Any)
658658
{
659-
"parent_query" => JSON::Any.new(@parent_query),
660-
"repeated_query" => JSON::Any.new(@repeated_query),
659+
"parent_query" => JSON::Any.new(@parent_query),
660+
"repeated_query" => JSON::Any.new(@repeated_query),
661661
"repetition_count" => JSON::Any.new(@repetition_count),
662-
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
662+
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
663663
}
664664
end
665665
end
@@ -668,10 +668,10 @@ module CQL::Performance
668668
struct Issue
669669
def to_h : Hash(String, JSON::Any)
670670
{
671-
"type" => JSON::Any.new(@type.to_s),
672-
"severity" => JSON::Any.new(@severity.to_s),
673-
"message" => JSON::Any.new(@message),
674-
"details" => PerformanceMetrics.to_json_any(@details),
671+
"type" => JSON::Any.new(@type.to_s),
672+
"severity" => JSON::Any.new(@severity.to_s),
673+
"message" => JSON::Any.new(@message),
674+
"details" => PerformanceMetrics.to_json_any(@details),
675675
"timestamp" => JSON::Any.new(@timestamp.to_rfc3339),
676676
}
677677
end

0 commit comments

Comments
 (0)