build_statistic.py 1.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import json
import sys
import glob
import time
import os
import argparse

def parseArguments():
  """
  Parse arguments for monitor statistics.
  """
  parser = argparse.ArgumentParser()
  parser.add_argument('--history_folder',
                      help='Path where history files are located and where stats will be generated.')

  return parser

18

19
def buildStatistic(history_folder):
20
  now = time.time()
21 22 23 24
  for p in glob.glob("%s/*.history.json" % history_folder):
    result = {}
    stats_list = []
    promise_name = p.split("/")[-1].replace(".history.json", "")
25 26 27 28 29 30 31 32 33 34 35

    stat_file_path = p.replace(".history.json", ".stats.json")
    if os.path.exists(stat_file_path):
      with open(stat_file_path) as f:
        stats_dict = json.load(f)
        f.close()
    else:
      stats_dict = {"date": now, "data": []}

    last_date = None
    if stats_dict["data"]:
36
      if "start-date" in stats_dict["data"][-1]:
37
        last_date = stats_dict["data"][-1]["start-date"]
38 39
      else:
        last_date = stats_dict["data"][-1]["date"]
40

41 42 43
    with open(p) as f:
      j = json.load(f)

44
      j_last_date = j['data'][-1]["date"]
45 46 47 48
      if last_date == j_last_date:
        # This file was already loaded, so skip
        continue

49
      for entry in j['data']:
50
        day = entry["date"].split("T")[0]
51 52 53 54 55 56
        result.setdefault(day, {"ERROR": 0, "OK": 0})
        result[day][str(entry["status"])] += 1
      f.close()

    for date, stat in result.iteritems():
      stats_list.append(
57 58
        {"status": "ERROR" if stat["ERROR"] > 0 else "OK",
         "change-time": now,
59
         "date": j_last_date,
60
         "message": stat})
61 62 63 64 65 66 67 68 69 70 71 72 73 74


    stats_dict["data"].extend(stats_list)

    with open(stat_file_path, "w+") as f:
      f.write(json.dumps(stats_dict))
      f.truncate()
      f.close()

def main():
  arg_parser = parseArguments()
  config = arg_parser.parse_args()
  buildStatistic(config.history_folder)
  sys.exit(0)