From ac99cc46825eed1b8b4a83e7dc20d8c056a34293 Mon Sep 17 00:00:00 2001 From: Maksim Andrianov Date: Tue, 18 Feb 2020 19:08:58 +0300 Subject: [PATCH] [python][generator] Added diff_stats.py. --- tools/python/maps_generator/diff_stats.py | 37 ++++++++++++++ .../maps_generator/generator/statistics.py | 50 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tools/python/maps_generator/diff_stats.py diff --git a/tools/python/maps_generator/diff_stats.py b/tools/python/maps_generator/diff_stats.py new file mode 100644 index 0000000000..53f174777a --- /dev/null +++ b/tools/python/maps_generator/diff_stats.py @@ -0,0 +1,37 @@ +import argparse + +from .generator.statistics import diff +from .generator.statistics import read_types + + +def get_args(): + parser = argparse.ArgumentParser( + description="This script prints the difference between old_stats.json and new_stats.json." + ) + parser.add_argument( + "--old", + default="", + type=str, + required=True, + help="Path to old file with map generation statistics.", + ) + parser.add_argument( + "--new", + default="", + type=str, + required=True, + help="Path to new file with map generation statistics.", + ) + return parser.parse_args() + + +def main(): + args = get_args() + old = read_types(args.old) + new = read_types(args.new) + for line in diff(new, old): + print(";".join(str(x) for x in line)) + + +if __name__ == "__main__": + main() diff --git a/tools/python/maps_generator/generator/statistics.py b/tools/python/maps_generator/generator/statistics.py index 9ae9195bb1..dcb3efac4a 100644 --- a/tools/python/maps_generator/generator/statistics.py +++ b/tools/python/maps_generator/generator/statistics.py @@ -1,10 +1,18 @@ import datetime +import json +import logging import os import re from collections import defaultdict +from typing import AnyStr +from typing import Dict +from typing import List +from .env import WORLDS_NAMES from .exceptions import ParseError +logger = logging.getLogger("maps_generator") + RE_STAT = re.compile( r"(?:\d+\. )?([\w:|-]+?)\|: " r"size = \d+; " @@ -128,3 +136,45 @@ def get_stages_info(log_path, ignored_stages=frozenset()): country = file.split(".")[0] result["countries"][country][stage_name] = dt return result + + +def read_types(path: AnyStr) -> Dict[AnyStr, Dict]: + """" + Reads and summarizes statistics for all countries, excluding World and + WorldCoast. + """ + with open(path) as f: + json_data = json.load(f) + all_types = {} + countries = json_data["countries"] + for country, json_value in countries.items(): + if country in WORLDS_NAMES: + continue + try: + json_types = json_value["types"] + except KeyError: + logger.exception(f"Cannot parse {json_value}") + continue + for t in json_types: + curr = all_types.get(t["type"], {}) + curr["quantity"] = curr.get("quantity", 0.0) + t["quantity"] + curr["unit"] = t["unit"] + all_types[t["type"]] = curr + return all_types + + +def diff(new: Dict[AnyStr, Dict], old: Dict[AnyStr, Dict]) -> List: + assert len(new) == len(old) + lines = [] + for key in new: + o = old[key]["quantity"] + n = new[key]["quantity"] + rel = 0 + if o != 0.0: + rel = int(((n - o) / o) * 100) + else: + if n != 0.0: + rel = 100 + + lines.append((key, o, n, rel, n - o, new[key]["unit"],)) + return lines