Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Issue #61: Support json stats. #3

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions do_times.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ maybe_create_file ${run_log}
cwd=`pwd`
cd ${log_dir}
git add *.csv
git add *.json
git add *.log
git commit -m "${log_base} update stats"
git push fenix-mobile master -q
Expand Down
38 changes: 31 additions & 7 deletions times.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import glob
from enum import Enum
import re
from typing import Pattern, List, MutableMapping
from typing import Any, Callable, List, Mapping, MutableMapping, Pattern
import os
import argparse

Expand Down Expand Up @@ -106,13 +106,31 @@ def convert_displayed_line_to_time(displayed_line: str, product: str) -> float:

return result

def format_calculations(calculations) -> str:
def csv_format_calculations(calculations: Mapping[str, str]) -> str:
result: str = ""
for date in sorted(calculations.keys()):
result += date + ", " + calculations[date] + "\n"
return result

def calculate(dirname: str, tipe: Type, product: str, result_file: str):
def json_format_calculations(calculations: Mapping[str, str]) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not worth changing now but this might be safer & more intuitive to do with the built-in json library (which I think can automatically convert python data types to valid JSON strings).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point. I will definitely do that on a subsequent pass. I honestly didn't expect @ecsmyth would have success with this output so I didn't anticipate using the code as it stands. But, it works and we are going with it.

result: str = ""
firstresult: bool = True
result = "["
for key in sorted(calculations.keys()):
if firstresult:
firstresult = False
else:
result += ","

value: str = calculations[key] if calculations[key] != "NA" else "0"
date: str = key.replace(".", "-")

result += "\n{ \"date\": \"" + date + "\", \"value\": " + value + "}"

result += "\n]"
return result

def calculate(dirname: str, tipe: Type, product: str, formatter: Callable[[Mapping[str, str]], str], result_file: str):
stats_filename: str = ""
calculations: MutableMapping[str, str] = {}
for stats_filename in glob.glob(dirname + "/*-" + str(tipe) + ".log"):
Expand All @@ -126,7 +144,7 @@ def calculate(dirname: str, tipe: Type, product: str, result_file: str):

try:
with open(result_file, "w+") as result_fd:
result_fd.write(format_calculations(calculations))
result_fd.write(formatter(calculations))
except IOError as ioerror:
pass

Expand All @@ -148,7 +166,13 @@ def calculate(dirname: str, tipe: Type, product: str, result_file: str):
if (not validate_product(product)):
print("Cannot run with invalid product: " + product + ".")
else:
calculate(input_dir, Type.HA, product, output_dir + "/" + "ha-results.csv")
calculate(input_dir, Type.AL, product, output_dir + "/" + "al-results.csv")
calculate(input_dir, Type.HANOOB, product, output_dir + "/" + "hanoob-results.csv")
# Print results in csv format.
calculate(input_dir, Type.HA, product, csv_format_calculations, output_dir + "/" + "ha-results.csv")
calculate(input_dir, Type.AL, product, csv_format_calculations, output_dir + "/" + "al-results.csv")
calculate(input_dir, Type.HANOOB, product, csv_format_calculations, output_dir + "/" + "hanoob-results.csv")

# Print results in json format.
calculate(input_dir, Type.HA, product, json_format_calculations, output_dir + "/" + "ha-results.json")
calculate(input_dir, Type.AL, product, json_format_calculations, output_dir + "/" + "al-results.json")
calculate(input_dir, Type.HANOOB, product, json_format_calculations, output_dir + "/" + "hanoob-results.json")
pass