def graph_leads_won_rate(request): """Graph rates of won leads for given (or all) subsidiary""" graph_data = [] start_date = (datetime.today() - timedelta(3 * 365)) subsidiary = get_subsidiary_from_session(request) leads = Lead.objects.filter(creation_date__gt=start_date) if subsidiary: leads = leads.filter(subsidiary=subsidiary) leads = leads.annotate(month=TruncMonth("creation_date")).order_by("month") leads = leads.values("month", "state").annotate(Count("state")) leads_state = OrderedDict() won_rate = [] months = [] # compute lead state for each month for lead in leads: month = lead["month"] if month not in leads_state: leads_state[month] = {} leads_state[month][lead["state"]] = lead["state__count"] # compute won rate for month, lead_state in leads_state.items(): if lead_state.get("WON", 0) > 0: won_rate.append( round( 100 * lead_state.get("WON", 0) / (lead_state.get("LOST", 0) + lead_state.get("FORGIVEN", 0) + lead_state.get("WON", 0)), 2)) months.append(month.date().isoformat()) if len(months) > 0: graph_data.append(["x"] + months) graph_data.append(["won-rate"] + won_rate) graph_data.append(["won-rate-MA90"] + moving_average(won_rate, 3, round_digits=2)) graph_data.append(["won-rate-MA180"] + moving_average(won_rate, 6, round_digits=2)) return render( request, "leads/graph_won_rate.html", { "graph_data": json.dumps(graph_data), "series_colors": COLORS, "user": request.user })
def graph_leads_activity(request): """some graph and figures about current leads activity""" subsidiary = get_subsidiary_from_session(request) # lead stat current_leads = Lead.objects.active() if subsidiary: current_leads = current_leads.filter(subsidiary=subsidiary) leads_state_data = leads_state_stat(current_leads) leads = Lead.objects.all() if subsidiary: leads = leads.filter(subsidiary=subsidiary) # lead creation rate per week first_lead_creation_date = leads.aggregate(Min("creation_date")).get( "creation_date__min", datetime.now()).date() today = date.today() lead_creation_rate_data = [] max_creation_rate = 0 for timeframe in (30, 30 * 6, 365): start = today - timedelta(timeframe) if start > first_lead_creation_date: rate = 7 * leads.filter( creation_date__gte=start).count() / timeframe rate = round(rate, 2) lead_creation_rate_data.append( [_("Last %s days") % timeframe, rate]) max_creation_rate = max(rate, max_creation_rate) # lead duration d_leads = leads.filter(creation_date__gt=(datetime.today() - timedelta(2 * 365))) d_leads = d_leads.annotate( timesheet_start=Min("mission__timesheet__working_date")) leads_duration = defaultdict(list) for lead in d_leads: end_date = lead.timesheet_start or lead.start_date or lead.update_date.date( ) duration = (end_date - lead.creation_date.date()).days leads_duration[lead.creation_date.date().replace( day=1)].append(duration) leads_duration_per_month = to_int_or_round( [sum(i) / len(i) for i in sortedValues(leads_duration)], 1) leads_duration_data = [ ["x"] + [d.isoformat() for d in sorted(leads_duration.keys())], [_("duration")] + leads_duration_per_month, [_("average duration 6 months")] + moving_average(leads_duration_per_month, 6, round_digits=1) ] return render( request, "leads/graph_leads_activity.html", { "leads_state_data": leads_state_data, "leads_state_title": _("%s leads in progress") % len(current_leads), "lead_creation_rate_data": json.dumps(lead_creation_rate_data), "max_creation_rate": max_creation_rate, "leads_duration_data": json.dumps(leads_duration_data), "user": request.user })