Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reliable_report.rb « tools « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27e54c2d8bff3f214802b4747de8f61dab3af90a (plain)
1
2
3
4
5
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
101
102
103
104
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
131
132
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# frozen_string_literal: true

require "influxdb-client"
require "terminal-table"
require "slack-notifier"
require "colorize"

module QA
  module Tools
    class ReliableReport
      include Support::API

      # Project for report creation: https://gitlab.com/gitlab-org/gitlab
      PROJECT_ID = 278964

      def initialize(range)
        @range = range.to_i
        @influxdb_bucket = "e2e-test-stats"
        @slack_channel = "#quality-reports"
        @influxdb_url = ENV["QA_INFLUXDB_URL"] || raise("Missing QA_INFLUXDB_URL env variable")
        @influxdb_token = ENV["QA_INFLUXDB_TOKEN"] || raise("Missing QA_INFLUXDB_TOKEN env variable")
      end

      # Run reliable reporter
      #
      # @param [Integer] range amount of days for results range
      # @param [String] report_in_issue_and_slack
      # @return [void]
      def self.run(range: 14, report_in_issue_and_slack: "false")
        reporter = new(range)

        reporter.print_report
        reporter.report_in_issue_and_slack if report_in_issue_and_slack == "true"
      rescue StandardError => e
        reporter&.notify_failure(e)
        raise(e)
      end

      # Print top stable specs
      #
      # @return [void]
      def print_report
        puts "#{stable_summary_table}\n\n"
        stable_results_tables.each { |stage, table| puts "#{table}\n\n" }
        return puts("No unstable reliable tests present!".colorize(:yellow)) if unstable_reliable_test_runs.empty?

        puts "#{unstable_summary_table}\n\n"
        unstable_reliable_results_tables.each { |stage, table| puts "#{table}\n\n" }
      end

      # Create report issue
      #
      # @return [void]
      def report_in_issue_and_slack
        puts "Creating report".colorize(:green)
        response = post(
          "#{gitlab_api_url}/projects/#{PROJECT_ID}/issues",
          {
            title: "Reliable e2e test report",
            description: report_issue_body,
            labels: "Quality,test,type::maintenance,reliable test report,automation:devops-mapping-disable"
          },
          headers: { "PRIVATE-TOKEN" => gitlab_access_token }
        )
        web_url = parse_body(response)[:web_url]
        puts "Created report issue: #{web_url}"

        puts "Sending slack notification".colorize(:green)
        notifier.post(
          icon_emoji: ":tanuki-protect:",
          text: <<~TEXT
            ```#{stable_summary_table}```
            ```#{unstable_summary_table}```

            #{web_url}
          TEXT
        )
        puts "Done!"
      end

      # Notify failure
      #
      # @param [StandardError] error
      # @return [void]
      def notify_failure(error)
        notifier.post(
          text: "Reliable reporter failed to create report. Error: ```#{error}```",
          icon_emoji: ":sadpanda:"
        )
      end

      private

      attr_reader :range, :influxdb_bucket, :slack_channel, :influxdb_url, :influxdb_token

      # Markdown formatted report issue body
      #
      # @return [String]
      def report_issue_body
        execution_interval = "(#{Date.today - range} - #{Date.today})"

        issue = []
        issue << "[[_TOC_]]"
        issue << "# Candidates for promotion to reliable #{execution_interval}"
        issue << "Total amount: **#{stable_test_runs.sum { |_k, v| v.count }}**"
        issue << stable_summary_table(markdown: true).to_s
        issue << results_markdown(:stable)
        return issue.join("\n\n") if unstable_reliable_test_runs.empty?

        issue << "# Reliable specs with failures #{execution_interval}"
        issue << "Total amount: **#{unstable_reliable_test_runs.sum { |_k, v| v.count }}**"
        issue << unstable_summary_table(markdown: true).to_s
        issue << results_markdown(:unstable)
        issue.join("\n\n")
      end

      # Stable spec summary table
      #
      # @param [Boolean] markdown
      # @return [Terminal::Table]
      def stable_summary_table(markdown: false)
        terminal_table(
          rows: stable_test_runs.map { |stage, specs| [stage, specs.length] },
          title: "Stable spec summary for past #{range} days".ljust(50),
          headings: %w[STAGE COUNT],
          markdown: markdown
        )
      end

      # Unstable reliable summary table
      #
      # @param [Boolean] markdown
      # @return [Terminal::Table]
      def unstable_summary_table(markdown: false)
        terminal_table(
          rows: unstable_reliable_test_runs.map { |stage, specs| [stage, specs.length] },
          title: "Unstable spec summary for past #{range} days".ljust(50),
          headings: %w[STAGE COUNT],
          markdown: markdown
        )
      end

      # Result tables for stable specs
      #
      # @param [Boolean] markdown
      # @return [Hash]
      def stable_results_tables(markdown: false)
        results_tables(:stable, markdown: markdown)
      end

      # Result table for unstable specs
      #
      # @param [Boolean] markdown
      # @return [Hash]
      def unstable_reliable_results_tables(markdown: false)
        results_tables(:unstable, markdown: markdown)
      end

      # Markdown formatted tables
      #
      # @param [Symbol] type result type - :stable, :unstable
      # @return [String]
      def results_markdown(type)
        runs = type == :stable ? stable_test_runs : unstable_reliable_test_runs
        results_tables(type, markdown: true).map do |stage, table|
          <<~STAGE.strip
            ## #{stage} (#{runs[stage].count})

            <details>
            <summary>Executions table</summary>

            #{table}

            </details>
          STAGE
        end.join("\n\n")
      end

      # Results table
      #
      # @param [Symbol] type result type - :stable, :unstable
      # @param [Boolean] markdown
      # @return [Hash<Symbol, Terminal::Table>]
      def results_tables(type, markdown: false)
        (type == :stable ? stable_test_runs : unstable_reliable_test_runs).to_h do |stage, specs|
          headings = ["name", "runs", "failures", "failure rate"]

          [stage, terminal_table(
            title: "Top #{type} specs in '#{stage}' stage for past #{range} days",
            headings: headings.map(&:upcase),
            markdown: markdown,
            rows: specs.map do |k, v|
              [name_column(name: k, file: v[:file], markdown: markdown), *table_params(v.values)]
            end
          )]
        end
      end

      # Stable specs
      #
      # @return [Hash]
      def stable_test_runs
        @top_stable ||= begin
          stable_specs = test_runs(reliable: false).transform_values do |specs|
            specs
              .reject { |k, v| v[:failure_rate] != 0 }
              .sort_by { |k, v| -v[:runs] }
              .to_h
          end

          stable_specs.reject { |k, v| v.empty? }
        end
      end

      # Unstable reliable specs
      #
      # @return [Hash]
      def unstable_reliable_test_runs
        @top_unstable_reliable ||= begin
          unstable = test_runs(reliable: true).transform_values do |specs|
            specs
              .reject { |k, v| v[:failure_rate] == 0 }
              .sort_by { |k, v| -v[:failure_rate] }
              .to_h
          end

          unstable.reject { |k, v| v.empty? }
        end
      end

      # Terminal table for result formatting
      #
      # @param [Array] rows
      # @param [Array] headings
      # @param [String] title
      # @param [Boolean] markdown
      # @return [Terminal::Table]
      def terminal_table(rows:, headings:, title:, markdown:)
        Terminal::Table.new(
          headings: headings,
          title: markdown ? nil : title,
          rows: rows,
          style: markdown ? { border: :markdown } : { all_separators: true }
        )
      end

      # Spec parameters for table row
      #
      # @param [Array] parameters
      # @return [Array]
      def table_params(parameters)
        [*parameters[1..2], "#{parameters.last}%"]
      end

      # Name column content
      #
      # @param [String] name
      # @param [String] file
      # @param [Boolean] markdown
      # @return [String]
      def name_column(name:, file:, markdown: false)
        return "**name**: #{name}<br>**file**: #{file}" if markdown

        wrapped_name = name.length > 150 ? "#{name} ".scan(/.{1,150} /).map(&:strip).join("\n") : name
        "name: '#{wrapped_name}'\nfile: #{file.ljust(160)}"
      end

      # Test executions grouped by name
      #
      # @param [Boolean] reliable
      # @return [Hash<String, Hash>]
      def test_runs(reliable:)
        puts("Fetching data on #{reliable ? 'reliable ' : ''}test execution for past #{range} days\n".colorize(:green))

        all_runs = query_api.query(query: query(reliable)).values
        all_runs.each_with_object(Hash.new { |hsh, key| hsh[key] = {} }) do |table, result|
          records = table.records.sort_by { |record| record.values["_time"] }
          # skip specs that executed less time than defined by range or stopped executing before report date
          # offset 1 day due to how schedulers are configured and first run can be 1 day later
          next if (Date.today - Date.parse(records.first.values["_time"])).to_i < (range - 1)
          next if (Date.today - Date.parse(records.last.values["_time"])).to_i > 1

          last_record = records.last.values
          name = last_record["name"]
          file = last_record["file_path"].split("/").last
          stage = last_record["stage"] || "unknown"

          runs = records.count
          failed = records.count { |r| r.values["status"] == "failed" }
          failure_rate = (failed.to_f / runs.to_f) * 100

          result[stage][name] = {
            file: file,
            runs: runs,
            failed: failed,
            failure_rate: failure_rate == 0 ? failure_rate.round(0) : failure_rate.round(2)
          }
        end
      end

      # Flux query
      #
      # @param [Boolean] reliable
      # @return [String]
      def query(reliable)
        <<~QUERY
          from(bucket: "#{influxdb_bucket}")
            |> range(start: -#{range}d)
            |> filter(fn: (r) => r._measurement == "test-stats")
            |> filter(fn: (r) => r.run_type == "staging-full" or
              r.run_type == "staging-sanity" or
              r.run_type == "staging-sanity-no-admin" or
              r.run_type == "production-full" or
              r.run_type == "production-sanity" or
              r.run_type == "package-and-qa" or
              r.run_type == "nightly"
            )
            |> filter(fn: (r) => r.status != "pending" and
              r.merge_request == "false" and
              r.quarantined == "false" and
              r.reliable == "#{reliable}" and
              r._field == "id"
            )
            |> group(columns: ["name"])
        QUERY
      end

      # Query client
      #
      # @return [QueryApi]
      def query_api
        @query_api ||= influx_client.create_query_api
      end

      # InfluxDb client
      #
      # @return [InfluxDB2::Client]
      def influx_client
        @influx_client ||= InfluxDB2::Client.new(
          influxdb_url,
          influxdb_token,
          bucket: influxdb_bucket,
          org: "gitlab-qa",
          precision: InfluxDB2::WritePrecision::NANOSECOND
        )
      end

      # Slack notifier
      #
      # @return [Slack::Notifier]
      def notifier
        @notifier ||= Slack::Notifier.new(
          slack_webhook_url,
          channel: slack_channel,
          username: "Reliable Spec Report"
        )
      end

      # Gitlab access token
      #
      # @return [String]
      def gitlab_access_token
        @gitlab_access_token ||= ENV["GITLAB_ACCESS_TOKEN"] || raise("Missing GITLAB_ACCESS_TOKEN env variable")
      end

      # Gitlab api url
      #
      # @return [String]
      def gitlab_api_url
        @gitlab_api_url ||= ENV["CI_API_V4_URL"] || raise("Missing CI_API_V4_URL env variable")
      end

      # Slack webhook url
      #
      # @return [String]
      def slack_webhook_url
        @slack_webhook_url ||= ENV["SLACK_WEBHOOK"] || raise("Missing SLACK_WEBHOOK env variable")
      end
    end
  end
end