gateways.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import annotations
  2. import collections
  3. import json
  4. import typing
  5. def main() -> None:
  6. gateway_mats = [
  7. 'GWS', 'SST', 'PFG', 'SDM', 'TOR', 'TRS',
  8. 'VOE', 'VOR', 'HAM', 'BSU', 'CPU', 'VFT',
  9. 'SPT', 'ALR', 'WRH', 'PSH', 'TSH', 'RSH', 'LIT',
  10. ]
  11. companies: dict[str, collections.Counter] = collections.defaultdict(collections.Counter)
  12. mats: dict[str, collections.Counter] = collections.defaultdict(collections.Counter)
  13. for company_data in iter_months():
  14. for co_id, production in company_data.items():
  15. for mat in gateway_mats:
  16. if mat_prod := production.get(mat):
  17. companies[co_id][mat] += mat_prod['amount']
  18. mats[mat][co_id] += mat_prod['amount']
  19. with open('www/data/knownCompanies.json', 'r') as f:
  20. known_companies = json.load(f)
  21. for co_id, produced in companies.items():
  22. try:
  23. print(known_companies[co_id]['Username'])
  24. except KeyError:
  25. print('unknown user', co_id)
  26. for mat, amount in produced.items():
  27. print(f'\t{mat:3}: {amount * 30:8,.1f}')
  28. print()
  29. for mat, produced in mats.items():
  30. print(mat)
  31. for co_id, amount in produced.most_common(10):
  32. try:
  33. username = known_companies[co_id]['Username']
  34. except KeyError:
  35. username = 'unknown user ' + co_id
  36. print(f'\t{username:20}: {amount * 30:8,.1f}')
  37. def iter_months() -> typing.Iterator[dict[str, dict[str, dict[str, float]]]]:
  38. for year in ['25', '26']:
  39. for month in ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']:
  40. try:
  41. with open(f'www/data/company-data-{month}{year}.json', 'r') as f:
  42. print(month + year)
  43. yield json.load(f)['individual']
  44. except FileNotFoundError:
  45. print(month + year, "doesn't exist")
  46. if __name__ == '__main__':
  47. main()