File size: 1,665 Bytes
c912a35 | 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 | import json
import pandas as pd
from operator import itemgetter
from datetime import timezone
import math
from datetime import datetime
import os
def add_unix_time_to_data(prs_metric_data):
new_data = []
for data in prs_metric_data:
if data['time'] == None:
data['time'] = '00:00'
data['timestamp'] = int(datetime.strptime('{} {}'.format(data['date'], data['time']), '%d/%m/%Y %H:%M').timestamp())
new_data.append(data)
new_data = sorted(new_data, key=itemgetter('timestamp'))
return new_data
def metric_data_to_df(prs_metric_data):
"""
@gagan: TODO
this function takes a list of dicts (each dict is the output of getMetricData from pms)
and converts it into
a df to be used in the above ml functions
"""
data_value_list = []
prs_metric_data = sorted(prs_metric_data, key=itemgetter('timestamp'))
for metric_data in prs_metric_data:
for kpi in metric_data['kpiValues']:
if metric_data['kpiValues'][kpi]!=None and not(math.isnan(metric_data['kpiValues'][kpi])):
data_value_list.append({
'value': metric_data['kpiValues'][kpi],
'timestamp': metric_data['timestamp']
})
prs_metric_df = pd.DataFrame(data_value_list)
return prs_metric_df
#%%
from glob import glob
files = glob('./json/*.json')
content = []
for file in files:
with open(file, 'r') as f:
content = json.load(f)
content = add_unix_time_to_data(content)
df = metric_data_to_df(content)
df.to_csv('./json_to_csv/'+file.split('/')[-1].split('.')[0]+'.csv', index=False)
|