I trying to generate charts for the last four weeks from the specified week number(week by week)
So far I have models and view working fine, I able to query data from raw database Also I know how to create four list from query:
results = [i for i in chain.from_iterable(cursor.fetchall())]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
but I still unable to merge one list from this four lists, on the end the list should look like is below:
[["ZZZ", "AAA", "BBB", "CCC"], ["48", 21, 223, 232], ["47", 334, 343, 232], ["46", 345, 542, 245], ["45", 764, 463, 989]]
Where: ZZZ = Week number "48", "47", "46", "45" AAA, BBB, CCC.... = name from query
any suggestion?
my models.py
def four_weeks(year, week):
end = datetime(year, 1, 1) + relativedelta(weeks=week-1, weekday=SU)
start = end - relativedelta(weeks=4, weekday=MO)
mint, maxt = datetime.min.time(), datetime.max.time()
for dt in rrule(WEEKLY, start, count=4):
yield dt.combine(dt, mint), dt.combine(dt + timedelta(days=6), maxt)
views.py
def WeekCombo(request):
fweeks = []
year = 2013 #this is only for test
week = 48 #in future it will be integrated with form
cursor = connections['mydba'].cursor()
for start, end in four_weeks(year, week):
cursor.execute("SELECT DISTINCT (p.name) AS platform, count(e.id ) AS count FROM event e, lu_platform p WHERE e.platform_id = p.id AND e.sourcetype_id = 1 AND e.event_datetime BETWEEN %s AND %s AND e.sender_id NOT IN ( 759, 73 ) GROUP BY p.name ORDER BY p.name", [start, end] )
results = [i for i in chain.from_iterable(cursor.fetchall())]
fweeks.append(results)
return render_to_response('form.html', {'fweeks': fweeks}, context_instance=RequestContext(request))