from build.chart_data_functions import get_confirmed_cases_by_county
from build.chart_data_functions import get_county_by_day
from build.constants import CONFIRMED_CASES_BY_COUNTIES_PATH
from build.constants import COUNTY_MAPPING
from build.constants import COUNTY_POPULATION
from build.constants import DATE_SETTINGS
from build.constants import TEST_RESULTS_PATH
from build.constants import TODAY_DMYHM
from build.constants import YESTERDAY_YMD
from build.utils import analyze_memory
from build.utils import analyze_time
from build.utils import logger
from build.utils import read_json_from_file
from build.utils import save_as_json
import pandas as pd


@analyze_time
@analyze_memory
def main():
    # Log status
    logger.info("Loading local data files")
    test_results = read_json_from_file(TEST_RESULTS_PATH)

    # Log status
    logger.info("Calculating main statistics")

    # Create date ranges for charts
    case_dates = pd.date_range(start=DATE_SETTINGS["firstCaseDate"], end=YESTERDAY_YMD)

    # Get data for each chart
    logger.info("Calculating data for charts")
    county_by_day = get_county_by_day(
        test_results, case_dates, COUNTY_MAPPING, COUNTY_POPULATION
    )
    confirmed_cases_by_county = get_confirmed_cases_by_county(
        test_results, COUNTY_MAPPING
    )

    del county_by_day["mapPlayback"]
    del county_by_day["mapPlayback10k"]

    # Create dictionary for final JSON
    logger.info("Compiling final JSON")
    final_json = {
        "updatedOn": TODAY_DMYHM,
        "dataConfirmedCasesByCounties": confirmed_cases_by_county,
        "countyByDay": county_by_day,
    }

    # Dump JSON output
    save_as_json(CONFIRMED_CASES_BY_COUNTIES_PATH, final_json)

    # Log finish time
    logger.info("Finished update process")


if __name__ == "__main__":
    main()

[((22, 4, 22, 43), 'build.utils.logger.info', 'logger.info', ({(22, 16, 22, 42): '"""Loading local data files"""'}, {}), "('Loading local data files')", False, 'from build.utils import logger\n'), ((23, 19, 23, 57), 'build.utils.read_json_from_file', 'read_json_from_file', ({(23, 39, 23, 56): 'TEST_RESULTS_PATH'}, {}), '(TEST_RESULTS_PATH)', False, 'from build.utils import read_json_from_file\n'), ((26, 4, 26, 46), 'build.utils.logger.info', 'logger.info', ({(26, 16, 26, 45): '"""Calculating main statistics"""'}, {}), "('Calculating main statistics')", False, 'from build.utils import logger\n'), ((29, 17, 29, 87), 'pandas.date_range', 'pd.date_range', (), '', True, 'import pandas as pd\n'), ((32, 4, 32, 46), 'build.utils.logger.info', 'logger.info', ({(32, 16, 32, 45): '"""Calculating data for charts"""'}, {}), "('Calculating data for charts')", False, 'from build.utils import logger\n'), ((33, 20, 35, 5), 'build.chart_data_functions.get_county_by_day', 'get_county_by_day', ({(34, 8, 34, 20): 'test_results', (34, 22, 34, 32): 'case_dates', (34, 34, 34, 48): 'COUNTY_MAPPING', (34, 50, 34, 67): 'COUNTY_POPULATION'}, {}), '(test_results, case_dates, COUNTY_MAPPING, COUNTY_POPULATION)', False, 'from build.chart_data_functions import get_county_by_day\n'), ((36, 32, 38, 5), 'build.chart_data_functions.get_confirmed_cases_by_county', 'get_confirmed_cases_by_county', ({(37, 8, 37, 20): 'test_results', (37, 22, 37, 36): 'COUNTY_MAPPING'}, {}), '(test_results, COUNTY_MAPPING)', False, 'from build.chart_data_functions import get_confirmed_cases_by_county\n'), ((44, 4, 44, 39), 'build.utils.logger.info', 'logger.info', ({(44, 16, 44, 38): '"""Compiling final JSON"""'}, {}), "('Compiling final JSON')", False, 'from build.utils import logger\n'), ((52, 4, 52, 62), 'build.utils.save_as_json', 'save_as_json', ({(52, 17, 52, 49): 'CONFIRMED_CASES_BY_COUNTIES_PATH', (52, 51, 52, 61): 'final_json'}, {}), '(CONFIRMED_CASES_BY_COUNTIES_PATH, final_json)', False, 'from build.utils import save_as_json\n'), ((55, 4, 55, 42), 'build.utils.logger.info', 'logger.info', ({(55, 16, 55, 41): '"""Finished update process"""'}, {}), "('Finished update process')", False, 'from build.utils import logger\n')]