Example #1
0
def stats(request, timeframe_url):
    regular_form = StatsForm()
    custom_form = CustomStatsForm()

    #get form data if user already has been on stats page
    if request.method == 'POST':
        form = StatsForm(request.POST)
        if form.is_valid():
            timeframe = form.cleaned_data["timeframe"]
            period = form.cleaned_data["period"]

            if(timeframe == "timeframe_cus"):
                custom_stats_form = CustomStatsForm(request.POST)
                if custom_stats_form.is_valid():
                    start = datetime.datetime.strptime(request.POST.get('startfrom', datetime.datetime.now()+relativedelta(day=1, hour=0, minute=0, second=0, microsecond=0)), "%m/%d/%Y")
                    end = datetime.datetime.strptime(request.POST.get('endby', datetime.datetime.now()), "%m/%d/%Y")
                    #we usually mean the end of that day
                    end += relativedelta(days=1, hour=0, minute=0, second=0, microsecond=0) 
                    end -= relativedelta(day=0, hour=0, minute=0, seconds=1, microsecond=0)
                else:
                    print "Invalid form input"
                    print form.errors
                    return render_to_response('charts/stats.html', vars(), RequestContext(request))
            else:
                if timeframe == "timeframe_hrs":
                    start = datetime.datetime.utcnow()+relativedelta(minute=0, second=0, microsecond=0)
                elif timeframe == "timeframe_day":
                    if period == 'period_min' or period == 'period_hrs':
                        localTime = datetime.datetime.now()
                        localMidnight = datetime.datetime.combine(localTime, datetime.time(0))
                        timeSinceStartOfDay = localTime - localMidnight
                        start = datetime.datetime.utcnow() - timeSinceStartOfDay # local midnight in UTC
                    else:
                        start = datetime.datetime.utcnow() + relativedelta(hour=0, minute=0, second=0, microsecond=0) # UTC midnight (1:00 here in Germany))
                elif timeframe == "timeframe_mon":
                    start = datetime.datetime.utcnow()+relativedelta(day=1, hour=0, minute=0, second=0, microsecond=0)
                elif timeframe == "timeframe_yrs":
                    start = datetime.datetime.utcnow()+relativedelta(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
                end = datetime.datetime.utcnow()
        else:
            print "Invalid form input"
            print form.errors
            return render_to_response('charts/stats.html', vars(), RequestContext(request))

    #user navigates to stats from main menu
    else:        
        timeframe = timeframe_url
        
        #determine start and end date for the chart
        if timeframe != "timeframe_cus":
            if timeframe == "timeframe_hrs":
                start = datetime.datetime.utcnow()+relativedelta(minute=0, second=0, microsecond=0)
                period = 'period_min'
            elif timeframe == "timeframe_day":
                start = datetime.datetime.utcnow()+relativedelta(hour=0, minute=0, second=0, microsecond=0)
                period = 'period_hrs'
            elif timeframe == "timeframe_mon":
                start = datetime.datetime.utcnow()+relativedelta(day=1, hour=0, minute=0, second=0, microsecond=0)
                period = 'period_day'
            elif timeframe == "timeframe_yrs":
                start = datetime.datetime.utcnow()+relativedelta(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
                period = 'period_mon'  
            end = datetime.datetime.utcnow()
        else:
            regular_form.fields["timeframe"].initial = timeframe   
            return render_to_response('charts/stats.html', vars(), RequestContext(request))

    #TODO: DRY
    regular_form.fields["timeframe"].initial = timeframe   
    regular_form.fields["period"].initial = period

    #create a chart and fetch its data
    chart = Chart(start, end, period)
    graphs = []

    for i in chart.getDeviceIDList():
        chart.fetchTimeSeries(i)
        timetuples = chart.getTimeSeries(i)
        graphs.append({"label":"Einspeisung WR"+str(i), "data":timetuples})

    timeseries = json.dumps(graphs)
    plotsettings = json.dumps(chart.chartOptions())

    #TODO: the next couple of lines are pretty ugly
    stats = chart.getStatTable()
    hd =str(start)+" - "+str(end)+" | "+str(period)
    
    return render_to_response('charts/stats.html', vars(), RequestContext(request))