Module: WebHelpers

Defined in:
lib/greenhat/web/api.rb,
lib/greenhat/web/gitaly.rb,
lib/greenhat/web/helpers.rb,
lib/greenhat/web/sidekiq.rb,
lib/greenhat/web/faststats.rb,
lib/greenhat/web/workhorse.rb,
lib/greenhat/web/production.rb,
lib/greenhat/web/stats_helpers.rb

Overview

General Web Helper

Instance Method Summary collapse

Instance Method Details

#api_avg_duration_seriesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/greenhat/web/api.rb', line 35

def api_avg_duration_series
  query = [
    'gitlab-rails/api_json.log',
    '--slice=time,path,duration_s',
    '--exists=duration_s',
    '--duration_s!=0',
    '--exact'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  # MS is hard
  results.each do |r|
    r[:duration_s] = r[:duration_s] * 1000
  end

  build_percentile_list(results, :duration_s)

  # build_time_avg(results, :duration_s).transform_values { |x| x.round(1) }
end

#api_avg_path_duration_seriesObject

[ API ]



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/greenhat/web/api.rb', line 6

def api_avg_path_duration_series
  query = [
    'gitlab-rails/api_json.log',
    '--slice=time,path,duration_s'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  method_key = :path
  duration_key = :duration_s

  # Collect Total by Duration
  top = results.each_with_object({}) do |v, obj|
    obj[v[method_key]] ||= []
    obj[v[method_key]] << v[duration_key]

    obj
  end

  # Average / Round
  top.transform_values! { |x| (x.sum / x.size.to_f).round(1) }

  # Only top by duration
  method_calls = top_method_calls(top)
  results.select! { |x| method_calls.any? x[method_key] }

  build_group_time_avg(results, method_key, duration_key) # 5 Minutes
end

#api_duration_pieObject



85
86
87
88
89
90
91
92
93
# File 'lib/greenhat/web/api.rb', line 85

def api_duration_pie
  all = faststats_run('gitlab-rails/api_json.log', 'top')[:totals]

  # Grab Specifics
  totals = all.except(:count, :fails).transform_values(&:round)

  # Organize
  totals.sort_by(&:last).reverse.to_h
end

#api_duration_series_stackedObject



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
# File 'lib/greenhat/web/api.rb', line 56

def api_duration_series_stacked
  stacks = i[
    redis_duration_s
    view_duration_s
    gitaly_duration_s
    redis_cache_duration_s
    duration_s
    db_duration_s
    redis_shared_state_duration_s
    queue_duration_s
    db_duration_s
  ]

  query = [
    'gitlab-rails/api_json.log',
    "--slice=time,path,#{stacks.join(',')}"
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)
  output = build_stack_time_avg(results, stacks)

  # MS are hard
  output.each do |dataset|
    dataset[:data].transform_values! { |x| (x * 1000).round(1) }
  end

  output
end

#arg_parseObject



3
4
5
# File 'lib/greenhat/web/helpers.rb', line 3

def arg_parse
  @files, @flags, @args = GreenHat::Args.parse Shellwords.split(params[:query])
end

#build_count_by_occurance(results, key, limit = 10) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/greenhat/web/helpers.rb', line 185

def build_count_by_occurance(results, key, limit = 10)
  output = results.each_with_object(Hash.new(0)) do |result, counts|
    counts[result[key]] += 1
  end

  return output if limit.zero?

  output.sort_by(&:last).reverse[0..(limit - 1)].to_h
end

#build_group_time_avg(results, group, sum, interval = 5.minutes) ⇒ Object

by Avg



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/greenhat/web/helpers.rb', line 105

def build_group_time_avg(results, group, sum, interval = 5.minutes)
  default_index = build_time_index(results, interval)
  list = {}

  results.map { |x| x[group] }.uniq.each do |key|
    key = 'None' if key.nil?
    list[key] = {
      name: key,
      data: default_index.clone.transform_values { |_y| [] }
    }
  end

  results.each do |r|
    key = r[group].nil? ? 'None' : r[group]
    list[key].data[r.time.floor(interval)] << r[sum]
  end

  # Transform / Calculate
  list.each_value do |v|
    v.data.transform_values! do |l|
      l.empty? ? 0 : (l.sum / l.size.to_f) # .round(1)
    end
  end

  list.values
end

#build_group_time_sum(results, group, sum, interval = 5.minutes) ⇒ Object

by Sum



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/greenhat/web/helpers.rb', line 77

def build_group_time_sum(results, group, sum, interval = 5.minutes)
  default_index = build_time_index(results, interval)
  list = {}

  results.map { |x| x[group] }.uniq.each do |key|
    key = 'None' if key.nil?
    list[key] = {
      name: key,
      data: default_index.clone.transform_values { |_y| [] }
    }
  end

  results.each do |r|
    key = r[group].nil? ? 'None' : r[group]
    list[key].data[r.time.floor(interval)] << r[sum]
  end

  # Transform / Calculate
  list.each_value do |v|
    v.data.transform_values! do |l|
      l.empty? ? 0 : l.sum
    end
  end

  list.values
end

#build_group_time_total(results, group, interval = 5.minutes) ⇒ Object

by Occurance/Count / Key to String



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/greenhat/web/helpers.rb', line 56

def build_group_time_total(results, group, interval = 5.minutes)
  default_index = build_time_index(results, interval)
  list = {}

  results.map { |x| x[group] }.uniq.each do |key|
    key = 'None' if key.nil?
    list[key.to_s] = {
      name: key.to_s,
      data: default_index.clone
    }
  end

  results.each do |r|
    key = r[group].nil? ? 'None' : r[group]
    list[key.to_s].data[r.time.floor(interval)] += 1
  end

  list.values
end

#build_percentile_list(results, key, round_value = 1) ⇒ Object

Helper to grab the most common desired results



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/greenhat/web/stats_helpers.rb', line 45

def build_percentile_list(results, key, round_value = 1)
  p99 = build_time_percentile(results, key, 0.99)
  p95 = build_time_percentile(results, key, 0.95)
  max = build_time_method(results, key, :max)
  mean = build_time_method(results, key, :mean)
  [
    { name: 'p99', data: round_values(p99, round_value) },
    { name: 'p95', data: round_values(p95, round_value) },
    { name: 'mean', data: round_values(mean, round_value) },
    { name: 'max', data: round_values(max, round_value) }
  ]
end

#build_stack_time_avg(results, stacks, interval = 5.minutes) ⇒ Object

Stack AVG Helper



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/greenhat/web/helpers.rb', line 133

def build_stack_time_avg(results, stacks, interval = 5.minutes)
  default_index = build_time_index(results, interval)
  list = {}

  # MS are Hard
  # results.each do |r|
  #   stacks.each do |stack|
  #     next unless r[stack]

  #     r[stack] = r[stack] * 1000
  #   end
  # end

  # Build List
  stacks.each do |stack|
    data = default_index.clone
    data.transform_values! { |_y| [] } # Ensure Uniq
    list[stack] = {        name: stack, data: data }
  end

  # Collect Stack Data
  results.each do |r|
    stacks.each do |stack|
      next unless r.key?(stack)

      list[stack].data[r.time.floor(interval)] << r[stack]
    end
  end

  # Transform / Calculate
  list.each_value do |v|
    v.data.transform_values! do |l|
      l.empty? ? 0 : (l.sum / l.size.to_f)
    end
  end

  list.values
end

#build_time_avg(results, sum, interval = 5.minutes) ⇒ Object

Without Grouping Avg



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/greenhat/web/helpers.rb', line 39

def build_time_avg(results, sum, interval = 5.minutes)
  index = build_time_index(results, interval)

  index.transform_values! { |_y| [] }

  results.each do |r|
    index[r.time.floor(interval)] << r[sum]
  end

  index.transform_values! do |l|
    (l.sum / l.size.to_f)
  end

  index
end

#build_time_index(results, interval = 5.minutes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/greenhat/web/helpers.rb', line 11

def build_time_index(results, interval = 5.minutes)
  index = {}
  start = results.min_by(&:time).time.floor(interval)
  finish = results.max_by(&:time).time.floor(interval)

  loop do
    index[start] = 0
    start += interval
    break if start > finish
  end

  index
rescue StandardError => e
  LogBot.fatal('Index Error', e.message)
end

#build_time_method(results, sum, method = :max, interval = 5.minutes) ⇒ Object

Time / Min/Max without Grouping



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/greenhat/web/stats_helpers.rb', line 21

def build_time_method(results, sum, method = :max, interval = 5.minutes)
  index = build_time_index(results, interval)

  index.transform_values! { |_y| [] }

  results.each do |r|
    index[r.time.floor(interval)] << r[sum]
  end

  index.transform_values! do |l|
    next if l.empty?

    l.send(method)
  end

  index
end

#build_time_percentile(results, sum, percentile = 0.90, interval = 5.minutes) ⇒ Object

Time / Percentile without Grouping



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/greenhat/web/stats_helpers.rb', line 4

def build_time_percentile(results, sum, percentile = 0.90, interval = 5.minutes)
  index = build_time_index(results, interval)

  index.transform_values! { |_y| [] }

  results.each do |r|
    index[r.time.floor(interval)] << r[sum]
  end

  index.transform_values! do |l|
    l.percentile(percentile)
  end

  index
end

#build_time_total(results, interval = 5.minutes) ⇒ Object

Without Grouping Just total count



28
29
30
31
32
33
34
35
36
# File 'lib/greenhat/web/helpers.rb', line 28

def build_time_total(results, interval = 5.minutes)
  index = build_time_index(results, interval)

  results.each do |r|
    index[r.time.floor(interval)] += 1
  end

  index
end

#faststats_field(file, field = :count) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/greenhat/web/faststats.rb', line 3

def faststats_field(file, field = :count)
  results = faststats_run(file).stats[0..10].map do |x|
    [x[:prim_field], x[field]]
  end

  # Sort and Round
  results = results.sort_by(&:last).reverse.to_h
  results.transform_values! { |v| v.round(1) }

  results
end

#faststats_run(file = 'sidekiq/current', cmd = '') ⇒ Object



15
16
17
# File 'lib/greenhat/web/faststats.rb', line 15

def faststats_run(file = 'sidekiq/current', cmd = '')
  GreenHat::ShellHelper::Faststats.run(Thing.find_by(type: file), cmd)
end

#faststats_top(file = 'sidekiq/current', kind = :project_stats) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/greenhat/web/faststats.rb', line 19

def faststats_top(file = 'sidekiq/current', kind = :project_stats)
  thing = Thing.find_by(type: file)
  results = GreenHat::ShellHelper::Faststats.run(thing, 'top')

  # -------------------------------------------------------
  # Calculate Total and Percentages
  list = results.dig(kind, :stats).sort_by { |_k, v| v[:duration] }.to_h
  count = list.values.sum { |x| x[:count] }
  # duration = list.values.sum { |x| x[:duration] }
  # -------------------------------------------------------

  # Handle Short Results
  top = results.dig(kind, :stats).sort_by { |_k, v| v[:duration] }
  top = top[-10..] if top.size >= 10

  top.to_h.map do |k, v|
    count_perc = percent(v[:count], count)
    {
      name: k,
      data: [
        [v[:duration].round, count_perc, v[:count]]
      ]
    }
  end
end

#gitaly_avg_method_seriesObject

[ Gitaly ]



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/greenhat/web/gitaly.rb', line 6

def gitaly_avg_method_series
  query = [
    'gitaly/current',
    '--slice=time,grpc.time_ms,grpc.method',
    '--exists=grpc.time_ms',
    '--exists=time',
    '--grpc.time_ms>=1'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  method_key = :'grpc.method'
  duration_key = :'grpc.time_ms'

  # Collect Total by Duration
  top = results.each_with_object({}) do |v, obj|
    obj[v[method_key]] ||= []
    obj[v[method_key]] << v[duration_key]

    obj
  end

  # Average / Round
  top.transform_values! { |x| (x.sum / x.size.to_f).round(1) }

  # Only top by duration
  method_calls = top.sort_by(&:last)[-10..].map(&:first)
  results.select! { |x| method_calls.any? x[method_key] }

  build_group_time_avg(results, method_key, duration_key) # 5 Minutes
end

#gitaly_durationObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/greenhat/web/gitaly.rb', line 51

def gitaly_duration
  query = [
    'gitaly/current',
    '--slice=time,grpc.time_ms',
    '--exists=grpc.time_ms',
    '--exists=time',
    '--grpc.time_ms!=0',
    '--exact'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_percentile_list(results, :'grpc.time_ms')
end

#gitaly_errors_seriesObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/greenhat/web/gitaly.rb', line 38

def gitaly_errors_series
  query = [
    'gitaly/current',
    '--slice=time,grpc.method,level',
    '--exists=time,level,time',
    '--level=error'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_group_time_total(results, :'grpc.method')
end

#percent(value, total) ⇒ Object



7
8
9
# File 'lib/greenhat/web/helpers.rb', line 7

def percent(value, total)
  ((value.to_i / total.to_f) * 100).round(2)
end

#prod_duration_pieObject



95
96
97
98
99
100
101
102
103
# File 'lib/greenhat/web/production.rb', line 95

def prod_duration_pie
  all = faststats_run('gitlab-rails/production_json.log', 'top')[:totals]

  # Grab Specifics
  totals = all.except(:count, :fails).transform_values(&:round)

  # Organize
  totals.sort_by(&:last).reverse.to_h
end

#prod_duration_seriesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/greenhat/web/production.rb', line 48

def prod_duration_series
  query = [
    'gitlab-rails/production_json.log',
    '--slice=time,path,duration_s',
    '--exists=duration_s'
  ].join(' ')

  # MS is hard
  results = GreenHat::ShellHelper.filter_internal(query)
  results.each do |r|
    r[:duration_s] = r[:duration_s] * 1000
  end

  # build_time_avg(results, :duration_s).transform_values { |x| x.round(1) }
  build_percentile_list(results, :duration_s)
end

#prod_duration_series_stackedObject



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
# File 'lib/greenhat/web/production.rb', line 65

def prod_duration_series_stacked
  stacks = i[
    redis_duration_s
    view_duration_s
    gitaly_duration_s
    redis_cache_duration_s
    duration_s
    db_duration_s
    redis_shared_state_duration_s
    queue_duration_s
    db_duration_s
  ]

  query = [
    'gitlab-rails/production_json.log',
    "--slice=time,#{stacks.join(',')}"
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  output = build_stack_time_avg(results, stacks)

  # MS are hard
  output.each do |dataset|
    dataset[:data].transform_values! { |x| (x * 1000).round(1) }
  end

  output
end

#prod_status_seriesObject

build_group_time_avg(results, method_key, duration_key) # 5 Minutes end



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/greenhat/web/production.rb', line 36

def prod_status_series
  query = [
    'gitlab-rails/production_json.log',
    '--slice=time,status',
    '--exists=status'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_group_time_total(results, :status)
end

#round_values(results, round_value = 1) ⇒ Object

Helper to make rounding Values Easier



40
41
42
# File 'lib/greenhat/web/stats_helpers.rb', line 40

def round_values(results, round_value = 1)
  results.transform_values { |x| x.round(round_value) }
end

#sidekiq_avg_duration_seriesObject

[ Sidekiq ]



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/greenhat/web/sidekiq.rb', line 6

def sidekiq_avg_duration_series
  query = [
    'sidekiq/current',
    '--slice=time,class,duration_s',
    '--duration_s>=1'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  method_key = :class
  duration_key = :duration_s

  # Collect Total by Duration
  top = results.each_with_object({}) do |v, obj|
    obj[v[method_key]] ||= []
    obj[v[method_key]] << v[duration_key]

    obj
  end

  # Average / Round
  top.transform_values! { |x| (x.sum / x.size.to_f).round(1) }

  # Only top by duration
  method_calls = top_method_calls(top)
  results.select! { |x| method_calls.any? x[method_key] }

  build_group_time_avg(results, method_key, duration_key) # 5 Minutes
end

#sidekiq_durationObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/greenhat/web/sidekiq.rb', line 36

def sidekiq_duration
  query = [
    'sidekiq/current',
    '--slice=time,duration_s',
    '--exists=duration_s',
    '--duration_s!=0',
    '--exact'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_percentile_list(results, :duration_s)
end

#sidekiq_errors_seriesObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/greenhat/web/sidekiq.rb', line 50

def sidekiq_errors_series
  query = [
    'sidekiq/current',
    '--slice=time,class',
    '--exists=errors'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_group_time_total(results, :class)
end

#sidekiq_job_latencyObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/greenhat/web/sidekiq.rb', line 62

def sidekiq_job_latency
  query = [
    'sidekiq/current',
    '--slice=time,class,scheduling_latency_s',
    '--exists=scheduling_latency_s'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_percentile_list(results, :scheduling_latency_s)
end

#top_method_calls(list) ⇒ Object

Helper to get the bottom Ten



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/greenhat/web/helpers.rb', line 173

def top_method_calls(list)
  sorted = list.sort_by(&:last)

  all = if sorted.length < 10
          sorted
        else
          sorted[-10..]
        end

  all.map(&:first)
end

#workhorse_client_totalObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/greenhat/web/workhorse.rb', line 18

def workhorse_client_total
  query = [
    'gitlab-workhorse/current',
    '--slice=user_agent',
    '--exists=user_agent'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_count_by_occurance(results, :user_agent, 5)
end

#workhorse_durationObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/greenhat/web/workhorse.rb', line 30

def workhorse_duration
  query = [
    'gitlab-workhorse/current',
    '--slice=time,duration_ms',
    '--exists=duration_ms',
    '--duration_ms!=0',
    '--exact'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_percentile_list(results, :duration_ms)
end

#workhorse_request_totalObject

[ API ]



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/greenhat/web/workhorse.rb', line 6

def workhorse_request_total
  query = [
    'gitlab-workhorse/current',
    '--slice=time,status',
    '--exists=status'
  ].join(' ')

  results = GreenHat::ShellHelper.filter_internal(query)

  build_group_time_total(results, :status)
end