@@ -32,65 +32,59 @@ def generate_sparkline_data
32
32
FROM min_max_dates
33
33
)
34
34
SELECT
35
- subquery.*
35
+ benchmark_result_type_id,
36
+ benchmark_type_id,
37
+ hstore_to_json(br.result) AS result,
38
+ category
36
39
FROM (
37
- SELECT
38
- benchmark_result_type_id,
39
- benchmark_type_id,
40
- array_to_json(array_agg(br.id)) AS ids
41
- FROM (
42
- SELECT id FROM (
43
- SELECT
44
- ROW_NUMBER() OVER(PARTITION BY w.weekstart ORDER BY c.created_at) AS row_num,
45
- id
46
- FROM weeks w
47
- INNER JOIN commits c
48
- ON w.weekstart = date_trunc('week', c.created_at) AND c.repo_id = #{ self . id }
49
- ) x
50
- WHERE row_num = 1
51
- ) cw
52
- INNER JOIN benchmark_runs br
53
- ON cw.id = br.initiator_id AND br.initiator_type = 'Commit'
54
- INNER JOIN benchmark_types bt
55
- ON bt.id = br.benchmark_type_id AND bt.repo_id = #{ self . id }
56
- GROUP BY benchmark_type_id, benchmark_result_type_id
57
- ) AS subquery
58
- INNER JOIN benchmark_types
59
- ON benchmark_types.id = subquery.benchmark_type_id
60
- INNER JOIN benchmark_result_types
61
- ON benchmark_result_types.id = subquery.benchmark_result_type_id
62
- ORDER BY category, name
40
+ SELECT id, commit_date FROM (
41
+ SELECT
42
+ ROW_NUMBER() OVER(PARTITION BY w.weekstart ORDER BY c.created_at) AS row_num,
43
+ id,
44
+ created_at AS commit_date
45
+ FROM weeks w
46
+ INNER JOIN commits c
47
+ ON w.weekstart = date_trunc('week', c.created_at) AND c.repo_id = #{ self . id }
48
+ ) x
49
+ WHERE row_num = 1
50
+ ) cw
51
+ INNER JOIN benchmark_runs br
52
+ ON cw.id = br.initiator_id AND br.initiator_type = 'Commit'
53
+ INNER JOIN benchmark_types bt
54
+ ON bt.id = br.benchmark_type_id AND bt.repo_id = #{ self . id }
55
+ ORDER BY category, commit_date
63
56
SQL
64
57
65
- results = self . class . connection . execute ( query ) . to_a
66
- results . each do |row |
67
- row [ 'ids ' ] = JSON . parse ( row [ 'ids ' ] )
58
+ raw_results = self . class . connection . execute ( query ) . to_a
59
+ raw_results . each do |row |
60
+ row [ 'result ' ] = JSON . parse ( row [ 'result ' ] )
68
61
end
69
62
70
- types = self . benchmark_types
71
- . where ( id : results . map { |row | row [ 'benchmark_type_id' ] } . uniq )
72
- . map { |type | [ type . id , type ] } . to_h
73
63
result_types = BenchmarkResultType
74
- . where ( id : results . map { |row | row [ 'benchmark_result_type_id' ] } . uniq )
64
+ . where ( id : raw_results . map { |row | row [ 'benchmark_result_type_id' ] } . uniq )
75
65
. map { |res_type | [ res_type . id , res_type ] } . to_h
76
- all_runs = BenchmarkRun
77
- . select ( :id , :initiator_id , :result , :initiator_type , 'c.created_at' )
78
- . joins ( "JOIN commits c ON c.id = benchmark_runs.initiator_id AND benchmark_runs.initiator_type = 'Commit'" )
79
- . where ( id : results . map { |row | row [ 'ids' ] } . flatten . uniq )
80
- . map { |run | [ run . id , run ] } . to_h
81
-
82
- results . each do |res |
83
- type = types [ res [ 'benchmark_type_id' ] ]
84
- result_type = result_types [ res [ 'benchmark_result_type_id' ] ]
85
- runs = all_runs . values_at ( *res [ 'ids' ] ) . sort_by ( &:created_at )
86
- next if !type || !result_type || runs . size == 0
87
66
88
- chart_builder = ChartBuilder . new ( runs , result_type ) . build_columns
89
- charts [ type . category ] ||= [ ]
90
- charts [ type . category ] << {
91
- benchmark_result_type : result_type . name ,
92
- columns : chart_builder . columns
93
- }
67
+ results = { }
68
+ raw_results . each do |res |
69
+ results [ res [ 'benchmark_type_id' ] ] ||= { }
70
+ results [ res [ 'benchmark_type_id' ] ] [ 'category' ] = res [ 'category' ]
71
+ hash = results [ res [ 'benchmark_type_id' ] ] [ 'res_types' ] ||= { }
72
+ arr = hash [ result_types [ res [ 'benchmark_result_type_id' ] ] . name ] ||= [ ]
73
+ arr << res [ 'result' ]
74
+ hash . sort_by { |k | k }
75
+ results [ res [ 'benchmark_type_id' ] ] [ 'res_types' ] = hash . sort_by { |k , v | k } . to_h
76
+ end
77
+ results . values . each do |res |
78
+ category = res [ 'category' ]
79
+ next unless category
80
+ res [ 'res_types' ] . each do |name , runs |
81
+ chart_builder = ChartBuilder . new ( runs , nil ) . build_columns_hash
82
+ charts [ category ] ||= [ ]
83
+ charts [ category ] << {
84
+ benchmark_result_type : name ,
85
+ columns : chart_builder . columns
86
+ }
87
+ end
94
88
end
95
89
96
90
$redis. setex ( "sparklines:#{ self . id } " , 1800 , charts . to_msgpack )
0 commit comments