rows=nm.query("""select et.customer_id,c.group_id,et.offer_json,s.status from enterprise_turns et join customers c on c.customer_id=et.customer_id left join subscriptions s on s.customer_id=et.customer_id where et.sender='agent' and et.offer_json not in ('{}','')""")['rows']
⋯ 40+ lines suppressed
def parse_prices(o):
try: d=json.loads(o)
except: return []
return [(x.get('plan'),x.get('price',x.get('price_per_seat',x.get('monthly_price')))) for x in ([d] if isinstance(d,dict) else d) if isinstance(x,dict) and x.get('plan')]
last={}; [last.setdefault(r['customer_id'],r) for r in rows]
for gid in set(r['group_id'] for r in last.values()):
lr=[r for r in last.values() if r['group_id']==gid]
for plan in ['A','B','C']:
by_status=collections.defaultdict(list)
for r in lr:
for p,v in parse_prices(r['offer_json']):
if p==plan and v is not None: by_status[r['status']].append(float(v))
if by_status:
print(gid,plan,{s:(len(v),round(statistics.mean(v),2)) for s,v in by_status.items()})
⋯ 20+ lines suppressed
Active enterprise accepted price distribution
{'group_id':'E3','plan':'B','n':154,'seats':167595,'minp':1.69,'avgp':56.01,'maxp':89.0}
{'group_id':'E3','plan':'C','n':3,'seats':3341,'minp':25.0,'avgp':101.0,'maxp':139.0}
Enterprise lost by latest agent offer and day groups summary
GID D_E02 n 413 statuses Counter({'lost':403,'lead':10})
B all avg 40.19 min 39.0 max 49.0 by_status {'lost':(403,40.22,39,49),'lead':(10,39,39,39)}
C all avg 61.15 min 59.0 max 79.0 by_status {'lost':(403,61.21,59,79),'lead':(10,59,59,59)}
⋯ 40+ lines suppressed