def main(unused_argv):
  current_trx = None
  current_ts = None
  prev_ts = None
  query = []

  # A single typical transaction looks like this:
  #
  #  1. # at 385 330500161
  #  2. #120105 14:52:55 server id 242880000  end_log_pos 452
  #       group_id 330500161       Query   thread_id=324436
  #       exec_time=8     error_code=0
  #  3. SET TIMESTAMP=1325803975/*!*/;
  #  4. BEGIN/*!*/;
  #  5. # at 452 330500161
  #  6. #120105 14:52:55 server id 242880000  end_log_pos 637
  #       group_id 330500161       Query   thread_id=324436
  #       exec_time=8     error_code=0
  #  7. use admin/*!*/;
  #  8. SET TIMESTAMP=1325803975/*!*/;
  #  9. UPDATE Heartbeat
  # 10.     SET LastHeartbeat = UNIX_TIMESTAMP()
  # 11.     WHERE LastHeartbeat <= UNIX_TIMESTAMP() - 0.000000/*!*/;
  # 12. # at 637 330500161
  # 13. #120105 14:52:55 server id 242880000  end_log_pos 672
  #        group_id 330500161       Xid = 3037797
  # 14. COMMIT/*!*/;

  for line in sys.stdin:
    if line.startswith('# '):
      ts_line = sys.stdin.next()
      v = ts_line.split()

      if 'Query' in v:
        trx = v[8]
        date = datetime.datetime.strptime(ts_line[1:16], '%y%m%d %H:%M:%S')

        # exec_time can be at slightly different positions.
        exec_time = int(
            [i for i in v if i.startswith('exec_time=')][0].split('=')[1])
        ts = int(time.mktime(date.timetuple())) + exec_time

        if trx != current_trx:
          if current_trx:
            diff = current_ts - prev_ts
            if FLAGS.cutoff_seconds > diff:
              query = ''.join(query)
              print 'timestamp: %d trx: %s delay: %d bytes: %d' % (
                  current_ts, current_trx, diff, len(query))
              print
              print query
              print '*' * 80
              print
          else:
            current_ts = ts

          prev_ts = current_ts
          query = []
        query.append(line)
        query.append(ts_line)
        current_trx = trx
        current_ts = ts
    else:
      query.append(line)
dataset/ETHPy150Open google/mysql-tools/extract_slow_queries.py/main/swapped operands of `>`