Skip to content

Instantly share code, notes, and snippets.

@owenThurm
Last active July 14, 2021 18:59
Show Gist options
  • Save owenThurm/345ef2d8fe23e162bf73c82b0122a1ef to your computer and use it in GitHub Desktop.
Save owenThurm/345ef2d8fe23e162bf73c82b0122a1ef to your computer and use it in GitHub Desktop.
Ordered and Viewed Product custom events csv
from app.services.events import EventLogService
from app.services.plans import AVAILABLE_PLANS
from app.models import Company, CompanyStatistic
from collections import namedtuple
import csv
event_service = EventLogService()
custom_stats = []
viewed_and_ordered_stats = []
high_plans = []
rows = []
row = namedtuple("Row", ["company_id", "company_mrr", "statistic_id", "statistic_name", "missing_keys", "event_time", "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 'viewed' in metric.name.lower() or 'ordered' in metric.name.lower():
viewed_and_ordered_stats.append(metric)
for stat in viewed_and_ordered_stats:
if stat.service.name == 'API':
custom_stats.append(stat)
i=0
product_id_identifiers = ['ProductID', 'Product ID', 'productID', 'product_id', 'item_id', 'item_ID', 'ItemID', 'itemID', 'itemid', 'variantID', 'variant_id', 'VariantID', 'ItemId', 'itemId']
product_price_identifiers = ['ProductPrice', 'price', 'Price', 'product_price', 'item_price', 'ItemPrice', 'itemprice', 'productprice', 'productPrice', 'item', '$value', 'Itemprice']
for stat in custom_stats:
try:
stat_timeline = event_service.statistic_timeline(stat)
stat_payload = stat_timeline._get_event_at_index(0)
missing_items = []
has_id = False
has_price = False
for id_identifier in product_id_identifiers:
if stat_payload.data.get(id_identifier) is not None:
has_id = True
break
for price_identifier in product_price_identifiers:
if stat_payload.data.get(price_identifier) is not None:
has_price = True
break
if not has_id:
missing_items.append('product id')
if not has_price:
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,
event_time=stat_payload.timestamp,
latest_event_payload=stat_payload.data,
missing_keys=missing_items,
)
rows.append(statistic_row)
except IndexError:
i+=1
print(f'skipping {i}')
with open('OrderedAndViewed.csv', 'w') as f:
w = csv.writer(f)
w.writerow(('Company ID', 'MRR', 'Statistic ID', 'Statistic Name', 'Missing Keys', 'Event Time', 'Latest Payload'))
w.writerows([(data.company_id, data.company_mrr, data.statistic_id, data.statistic_name, data.missing_keys, data.event_time, data.latest_event_payload) for data in rows])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment