insight/backend/src/dashboard/services/json_reader.py

72 lines
2.2 KiB
Python

from pprint import pprint
from collections import defaultdict
import pandas as pd
from enum import Enum
class ProductType(Enum):
EMPTY_CAVITY_ECO_4 = "Empty Cavity - ECO4"
class jsonReader:
def __init__(self, json_data):
self.raw_data = json_data
self.deals_by_line_item = defaultdict(list)
self.line_item_names = list
self.initial_setup()
def initial_setup(self):
"""
Build a dictionary mapping line item names -> list of deals
"""
for deal in self.raw_data:
line_items = deal.get("line_items", [])
if not line_items:
# Store empty deals under a special key
self.deals_by_line_item["__empty__"].append(deal)
continue
# Add this deal under each line item name
for item in line_items:
name = item.get("name")
if name:
self.deals_by_line_item[name].append(deal)
self.line_item_names = list(self.deals_by_line_item.keys())
def generate_df_via_product_type(self, product_type):
rows = []
for deals in self.deals_by_line_item[product_type]:
row = self._return_df_from_deal_info(deals, product_type)
rows.append(row)
break
if rows:
return pd.concat(rows, ignore_index=True)
else:
return
def _return_df_from_deal_info(self, deal, product_type):
data = {
"submission_date": deal["deal_properties"].get("submission_date", None),
"expected_commencement_date": deal["deal_properties"].get("expected_commencement_date", None),
"work_type": product_type,
"price": next(
(item["price"] for item in deal["line_items"] if product_type in item["name"]),
None
)
}
return pd.DataFrame([data])
def find_all_job_with_line_item(self):
for i, deal in enumerate(self.raw_data):
if len(deal["line_items"])>0:
print(deal)
print(i)
break
def print_raw_data(self):
pprint(self.raw_data)