This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgranular_sos_reports_metadata.py
58 lines (48 loc) · 1.93 KB
/
granular_sos_reports_metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
from collections import defaultdict
import boto3
import json
class Granular_SOS_Reports_Metadata:
def __init__(self, file_name):
self.file_name = file_name
self.year_directory = defaultdict(lambda: defaultdict(int))
self.month_directory = defaultdict(lambda: defaultdict(int))
self.day_directory = defaultdict(lambda: defaultdict(int))
def save_to_file(self, granularity, directory):
file_name = '{}_{}'.format(self.file_name, granularity)
with open('{}.json'.format(file_name), 'w') as fp:
json.dump(directory, fp)
print('File: {} saved!'.format(file_name))
def add_to_year(self, key, value):
self.year_directory[key] = {
'size': value['size'],
'count': value['count']
}
def add_to_month(self, key, value):
self.month_directory[key] = {
'size': value['size'],
'count': value['count']
}
def add_to_day(self, key, value):
self.day_directory[key] = {
'size': value['size'],
'count': value['count']
}
def read_json(self):
with open(self.file_name, 'r') as json_file:
json_data = json.load(json_file)
for key in json_data.keys():
if 'created_day' in key:
self.add_to_day(key, json_data[key])
elif 'created_month' in key:
self.add_to_month(key, json_data[key])
elif 'created_year' in key:
self.add_to_year(key, json_data[key])
self.save_to_file('day', self.day_directory)
self.save_to_file('month', self.month_directory)
self.save_to_file('year', self.year_directory)
print('Finished saving all the granularities.')
metadata_json_file = input('Please enter the json file: \n')
granular_SOS_Reports_Metadata = Granular_SOS_Reports_Metadata(
metadata_json_file)
granular_SOS_Reports_Metadata.read_json()