|
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) |
|
|