#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require 'time'
require "#{LKP_SRC}/lib/statistics"
require "#{LKP_SRC}/lib/string_ext"
require "#{LKP_SRC}/lib/array_ext"
require "#{LKP_SRC}/lib/tests/stats"

test_name = nil
subtest_name = nil
has_dynamic_subtest = false

def valid_datetime?(datetime_str)
  !!Time.parse(datetime_str)
rescue StandardError
  false
end

stats = LKP::Stats.new

while (line = STDIN.gets)
  line = line.resolve_invalid_bytes

  case line
  when /^(.*) build\/tests\/([^\s]+)$/
    # 2020-10-27 01:29:45 build/tests/core_getstats
    next unless valid_datetime?($1)

    test_name = $2
  when /^(.*) build\/tests\/([^\s]+) --run-subtest ([^\s]+)$/
    # 2020-11-17 03:02:16 build/tests/fbdev --run-subtest info
    next unless valid_datetime?($1)

    test_name = $2
    subtest_name = $3
  when /^(SUCCESS|FAIL|CRASH)/
    # 1 2020-10-27 01:29:45 build/tests/core_getstats
    # 2 IGT-Version: 1.25-ga15f8da1 (x86_64) (Linux: 5.9.0-wt-17357-g18b04b6d608b x86_64)
    # 3 SUCCESS (0.005s)
    next if test_name.nil?

    stats.add test_name, $1
  when /Starting subtest: (.*)$/
    # Starting subtest: invalid-buffer
    next if test_name.nil?

    subtest_name = $1
    has_dynamic_subtest = false
  when /Subtest (.+): (SUCCESS|FAIL|CRASH)/
    # [thread:4146] Subtest thread-performance-write: CRASH (0.580s)
    #
    # Subtest invalid-buffer: SUCCESS (0.000s)
    # one special case is
    # 953 2020-10-26 01:36:11 build/tests/gem_sync
    # ...
    # 956 Starting subtest: default
    # 957 Starting dynamic subtest: default
    # 958   3666944 cycles: 5.455 us
    # 959 Dynamic subtest default: SUCCESS (20.015s)
    # ...
    # 980 Subtest default: SUCCESS (100.073s)
    # ...
    # 1511 Starting subtest: default
    # 1512 Starting dynamic subtest: rcs0
    # 1513   3674112 cycles: 5.444 us
    # 1514 Dynamic subtest rcs0: SUCCESS (20.010s)
    # ...
    # 1521 Subtest default: SUCCESS (60.036s)
    # above line 953-980 and 1511-1521 could be regarded as two sections for
    # gem_sync.default, they include different dynamic subtests.
    # should ignore the 'Subtest default' results from 980 and 1521 to avoid duplicated stats
    next if test_name.nil? || has_dynamic_subtest

    if subtest_name != $1
      log_warn "subtest_name [#{subtest_name}] != $1 [#{$1}]"
      exit 1
    end

    if test_name == 'gem_shrink'
      # some subtests of gem_shrink has duplication issue since v5.11-rc2
      # Subtest execbuf1-oom: CRASH (0.021s)
      #  #1 [killpg+0x40]
      #  #2 [__real_main396+0x837]
      #  #3 [main+0x27]
      #  #4 [__libc_start_main+0xeb]
      #  #5 [_start+0x2a]
      # Subtest execbuf1-oom: CRASH (0.021s)
      #  #4 [__libc_start_main+0xeb]
      #  #5 [_start+0x2a]
      # Subtest execbuf1-oom: CRASH (0.021s)
      stats.add "#{test_name}.#{$1}", $2 unless stats.key? "#{test_name}.#{$1}"
    else
      stats.add "#{test_name}.#{$1}", $2
    end
  when /^Dynamic subtest (.*): (SUCCESS|FAIL|CRASH)/
    # a 'dynamic subtest' is such like a 2nd level subtest under 'subtest' as below
    # 2607 Starting subtest: exec-single-timeline
    # 2608 Starting dynamic subtest: rcs0
    # 2609 Dynamic subtest rcs0: SUCCESS (1.060s)
    next if test_name.nil? || subtest_name.nil?

    has_dynamic_subtest = true

    stats.add "#{test_name}.#{subtest_name}.dynamic.#{$1}", $2
  end
end

stats.dump('success' => 'pass', 'crash' => 'fail')
