def get_chart(request): if request.method != 'POST' or not request.is_ajax(): return HttpResponseForbidden() stop_id = request.POST.get('stop_id', None) if stop_id in [None, '']: raise ValueError("stop_id can't be None or empty") stop = get_object_or_404(Stop, pk=stop_id) date_from = request.POST.get('date_from') date_to = request.POST.get('date_to') frequency = request.POST.get('frequency', '1t') time_start = request.POST.get('time_start') time_end = request.POST.get('time_end') tzname = request.POST.get('timezone', 'America/Toronto') timezone.activate(pytz.timezone(tzname)) predictions = Prediction.objects.filter( stop=stop, posted_at__gt=date_from, posted_at__lte=date_to, ) if is_valid_time_format(time_start) and is_valid_time_format(time_end): predictions = predictions.exclude(posted_at__time__range=(time_end, time_start)) # Converting predictions to Pandas timeseries and resample by frequency dataframe = predictions.to_timeseries( fieldnames=['posted_at', 'seconds'], index='posted_at', values='seconds', freq=frequency, # resampling triggers deprecation warning to use .mean() ).dropna(how='any') # Drop nan predictions_json = dataframe.to_json(date_format='iso', orient='index') return HttpResponse(predictions_json, content_type='application/json')
def get_hourly_average_chart(request): if request.method != 'POST' or not request.is_ajax(): return HttpResponseForbidden() stop_id = request.POST.get('stop_id', None) if stop_id in [None, '']: raise ValueError("stop_id can't be None or empty") stop = get_object_or_404(Stop, pk=stop_id) time_start = request.POST.get('time_start') time_end = request.POST.get('time_end') tzname = request.POST.get('timezone', 'America/Toronto') timezone.activate(pytz.timezone(tzname)) avg_hourly = {} for hour in range(0, 24): prediction_avg = Prediction.objects.filter( stop=stop, posted_at__hour=hour ) if is_valid_time_format(time_start) and is_valid_time_format(time_end): prediction_avg = prediction_avg.exclude(posted_at__time__range=(time_end, time_start)) prediction_avg = prediction_avg.aggregate(Avg('seconds')) avg_hourly[hour] = prediction_avg['seconds__avg'] response = {'avg_hourly': avg_hourly} return HttpResponse(json.dumps(response), content_type='application/json')