You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.6 KiB

  1. import csv
  2. import arrow
  3. import os
  4. import collections
  5. import json
  6. import re
  7. import matplotlib
  8. ref_time = arrow.get()
  9. data_dir = '/Users/fluffy/Library/Mobile Documents/iCloud~com~ifunography~HealthExport/Documents/'
  10. start_time, _ = ref_time.shift(days=-30).span('day')
  11. _, end_time = ref_time.span('day', bounds='[]')
  12. metrics = collections.defaultdict(list)
  13. def filter(date):
  14. if date >= start_time and date < end_time:
  15. return date
  16. return None
  17. def scan_dir(path):
  18. for file in os.scandir(path):
  19. if file.is_dir():
  20. scan_dir(file.path)
  21. elif file.is_file():
  22. scan_file(file)
  23. def scan_file(file):
  24. match = re.match(r'HealthAutoExport-([0-9\-]+)\.json', file.name)
  25. if not match:
  26. return
  27. file_date = arrow.get(match[1])
  28. if filter(file_date):
  29. file_data = json.load(open(file.path,'r'))
  30. scan_metrics(file_data['data']['metrics'])
  31. def get_value(item):
  32. if 'qty' in item:
  33. return item['qty']
  34. elif 'Avg' in item:
  35. return item['Avg']
  36. elif 'asleep' in item:
  37. print(item)
  38. return None
  39. else:
  40. raise KeyError(f"unknown format; keys: {item.keys()}")
  41. def scan_metrics(data):
  42. for metric in data:
  43. if len(data):
  44. sink = metrics[metric['name']]
  45. for item in metric['data']:
  46. if when := filter(arrow.get(item['date'], 'YYYY-MM-DD HH:mm:ss Z')):
  47. sink.append((when, item))
  48. scan_dir(data_dir)
  49. metrics = {k:sorted(v,key=lambda i:i[0]) for k,v in metrics.items() if len(v)}
  50. for key,vals in metrics.items():
  51. print(key,len(vals))