one step closer to the singularity

This commit is contained in:
stupidcomputer 2024-06-24 04:57:53 -05:00
parent 23e771c981
commit 258295390b
1 changed files with 39 additions and 12 deletions

51
main.py
View File

@ -1,5 +1,6 @@
import json import json
import common import common
import interndata
from openpyxl import Workbook from openpyxl import Workbook
from openpyxl.utils import get_column_letter from openpyxl.utils import get_column_letter
from openpyxl.styles import Font from openpyxl.styles import Font
@ -17,14 +18,20 @@ def mapper(string):
except KeyError: except KeyError:
return "WARNING: {}".format(string) return "WARNING: {}".format(string)
def aggregate_demographic_totals(payload, adultonly, childonly): def aggregate_demographic_totals(payload, adultonly, childonly, internonly, withoutinterns):
results = {"age": {}, "gender": {}, "ethnicity": {}, "race": {}} results = {"age": {}, "gender": {}, "ethnicity": {}, "race": {}}
for i in payload: for i in payload:
if adultonly and i["age"] == "c": if adultonly and not i["age"] == "a":
continue continue
if childonly and i["age"] == "a": if childonly and not i["age"] == "c":
continue
if internonly and not i["age"] == "i":
continue
if withoutinterns and i["age"] == "i":
continue continue
# for every entry we get # for every entry we get
@ -35,7 +42,7 @@ def aggregate_demographic_totals(payload, adultonly, childonly):
return results return results
def get_totals_for_specific_attribute_on_specific_site(site, adultonly=False, childonly=False): def get_totals_for_specific_attribute_on_specific_site(site, adultonly=False, childonly=False, internonly=False, withoutinterns=False):
manifest = open("data/manifest.json") manifest = open("data/manifest.json")
manifest_data = json.loads(manifest.read()) manifest_data = json.loads(manifest.read())
@ -49,6 +56,13 @@ def get_totals_for_specific_attribute_on_specific_site(site, adultonly=False, ch
raise TypeError("site argument is not valid") raise TypeError("site argument is not valid")
used_filename = site_object["usedrecord"] used_filename = site_object["usedrecord"]
if not used_filename: # it's just a placeholder, so we should return no records
return {"age": {}, "gender": {}, "ethnicity": {}, "race": {}}
present_interns = site_object["present"]
intern_serialized = interndata.get_staff_records()
intern_data = interndata.return_intern_data_from_present_array(present_interns, intern_serialized)
site_specific_data_file = open("data/" + used_filename) site_specific_data_file = open("data/" + used_filename)
@ -56,16 +70,13 @@ def get_totals_for_specific_attribute_on_specific_site(site, adultonly=False, ch
payload = demographic_data_json["payload"] payload = demographic_data_json["payload"]
return aggregate_demographic_totals(payload, adultonly, childonly) return aggregate_demographic_totals(payload + intern_data, adultonly, childonly, internonly, withoutinterns)
def generate_table_for_attr(attr, totals): def generate_table_for_attr(attr, totals):
header = [mapper(attr), "Count"] header = [mapper(attr), "Count"]
data = [] data = []
print(totals)
for key, value in totals[attr].items(): for key, value in totals[attr].items():
print(key, value)
data.append([mapper(key), value]) data.append([mapper(key), value])
data.sort() data.sort()
@ -91,7 +102,10 @@ def generate_provider_string(providers):
else: else:
and_string = ' and {}'.format(used_providers[-1]) and_string = ' and {}'.format(used_providers[-1])
return before_and + and_string return before_and + and_string
return used_providers[0] try:
return used_providers[0]
except: # there's no documented communications coordinator or whatever
return "an Unknown Person"
def handle_writing_of_attrs(worksheet, totals, offset): def handle_writing_of_attrs(worksheet, totals, offset):
count = 0 count = 0
@ -112,8 +126,8 @@ def adjust_column_width(worksheet, times):
def write_information_to_spreadsheet(totals, worksheet, offset): def write_information_to_spreadsheet(totals, worksheet, offset):
return handle_writing_of_attrs(worksheet, totals, offset) return handle_writing_of_attrs(worksheet, totals, offset)
def handle_spreadsheet_decoration(ws, providers, persons, commleads, onsiteleads): def handle_spreadsheet_decoration(ws, providers, persons, commleads, onsiteleads, time, date):
ws["A1"].value = "Information for {} site attendance.".format(ws.title) ws["A1"].value = "Information for {} site attendance at {} on {}".format(ws.title, time, date)
ws["A2"].value = "Fields not present should be assumed 0." ws["A2"].value = "Fields not present should be assumed 0."
ws["A3"].value = "Data submitted by {} in person after conclusion of site operations.".format(generate_provider_string(providers)) ws["A3"].value = "Data submitted by {} in person after conclusion of site operations.".format(generate_provider_string(providers))
ws["A4"].value = "Interns present: {}".format(generate_provider_string(persons)) ws["A4"].value = "Interns present: {}".format(generate_provider_string(persons))
@ -131,6 +145,7 @@ json_data = json.loads(fd.read())
wb = Workbook() wb = Workbook()
for site in json_data["sites"]: for site in json_data["sites"]:
print(site["name"])
providers = [] providers = []
for record in site["records"]: for record in site["records"]:
providers.append(record['submitter']) providers.append(record['submitter'])
@ -138,6 +153,8 @@ for site in json_data["sites"]:
persons = site["present"] persons = site["present"]
commleads = site["commleads"] commleads = site["commleads"]
onsiteleads = site["onsiteleads"] onsiteleads = site["onsiteleads"]
time = site["time"]
date = site["date"]
length = 8 length = 8
ws = wb.create_sheet(site["name"]) ws = wb.create_sheet(site["name"])
@ -147,6 +164,11 @@ for site in json_data["sites"]:
ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True) ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True)
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"]), ws, length) + 2 length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"]), ws, length) + 2
ws.cell(row=length, column=1, value="Combined Totals (without interns)")
ws.merge_cells(convert_x_y_range_to_cols(1, length, 11, length))
ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True)
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"], withoutinterns=True), ws, length) + 2
ws.cell(row=length, column=1, value="Adults") ws.cell(row=length, column=1, value="Adults")
ws.merge_cells(convert_x_y_range_to_cols(1, length, 11, length)) ws.merge_cells(convert_x_y_range_to_cols(1, length, 11, length))
ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True) ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True)
@ -157,8 +179,13 @@ for site in json_data["sites"]:
ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True) ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True)
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"], childonly=True), ws, length) + 3 length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"], childonly=True), ws, length) + 3
ws.cell(row=length, column=1, value="Interns")
ws.merge_cells(convert_x_y_range_to_cols(1, length, 11, length))
ws[convert_x_y_to_col_id(1, length)].font = Font(italic=True)
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"], internonly=True), ws, length) + 3
adjust_column_width(ws, 4) adjust_column_width(ws, 4)
handle_spreadsheet_decoration(ws, providers, persons, commleads, onsiteleads) handle_spreadsheet_decoration(ws, providers, persons, commleads, onsiteleads, time, date)
del wb["Sheet"] del wb["Sheet"]
wb.save("test.xlsx") wb.save("test.xlsx")