Skip to content

Instantly share code, notes, and snippets.

@owenThurm
Last active July 14, 2021 17:04
Show Gist options
  • Save owenThurm/6caa845028f9944ede9bbb12f9eef426 to your computer and use it in GitHub Desktop.
Save owenThurm/6caa845028f9944ede9bbb12f9eef426 to your computer and use it in GitHub Desktop.
Large customer custom started checkout data csv snippet
from app.services.events import EventLogService
from app.services.plans import AVAILABLE_PLANS
from app.models import Company, CompanyStatistic
from collections import namedtuple
event_service = EventLogService()
custom_stats = []
started_checkout_stats = []
high_plans = []
rows = []
row = namedtuple("Row", ["company_id", "company_mrr", "statistic_id", "statistic_name", "missing_keys", "latest_event_payload"])
for key, plan in AVAILABLE_PLANS.items():
if plan.price >= 5000:
high_plans.append(key)
companies = Company.objects.filter(plan_key__in=high_plans) # len(companies) == 312
filtered_companies = list(filter(lambda company: company.id != '9BX3wh', companies))
high_mrr_stats = CompanyStatistic.objects.filter(company__in=filtered_companies)
for metric in high_mrr_stats:
if 'started' in metric.name.lower() or 'checkout' in metric.name.lower():
started_checkout_stats.append(metric)
for stat in started_checkout_stats:
if stat.service.name == 'API':
custom_stats.append(stat)
i=0
product_list_names = ['product_list', 'basket_items', 'products', 'items', 'Items', 'line_items', 'items_list']
for stat in custom_stats:
try:
stat_timeline = event_service.statistic_timeline(stat)
stat_payload = stat_timeline._get_event_at_index(0)
missing_items = []
for i, product_list_name in enumerate(product_list_names):
if stat_payload.data.get(product_list_name):
break
if i == len(product_list_names)-1:
missing_items.append('products list')
if not missing_items:
for item in stat_payload.data.get(product_list_name):
if not item or isinstance(item, str) or isinstance(item, int):
print(item)
missing_items.extend(['product id', 'product price'])
break
if not (item.get('product_id') or item.get('Product ID') or item.get('ID') or item.get('productID') or item.get('item_id')):
missing_items.append('product id')
if not (item.get('ItemPrice') is None or item.get('Line Price') is None or item.get('Price') is None or item.get('product_price') is None or item.get('item_price') is None or item.g
et('price') is None or item.get('line_price') is None):
missing_items.append('product price')
statistic_row = row(
company_id=stat.company.id ,
company_mrr=stat.company.total_plan_payment_amount,
statistic_id=stat.id,
statistic_name=stat.name,
latest_event_payload=stat_payload.data,
missing_keys=missing_items,
)
rows.append(statistic_row)
except IndexError:
i+=1
print(f'skipping {i}')
with open('output2.csv', 'w') as f:
w = csv.writer(f)
w.writerow(('Company ID', 'MRR', 'Statistic ID', 'Statistic Name', 'Missing Keys', 'Latest Payload'))
w.writerows([(data.company_id, data.company_mrr, data.statistic_id, data.statistic_name, data.missing_keys, data.latest_event_payload) for data in rows])
# On Local:
scp <Your ondemand user>:/home/ubuntu/deployment_staging/app/red/output2.csv ~/Desktop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment