Skip to content

Commit 0559167

Browse files
committed
Properly format dates for queries
1 parent 2aa6059 commit 0559167

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

paramgenerator/generateparamsbi.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
import json
66
import os
77
import codecs
8-
from datetime import date
8+
import calendar
9+
import time
10+
from datetime import date,datetime,timedelta
911
from timeparameters import *
1012
from calendar import timegm
1113

14+
START_DATE=datetime.strptime("2010-01-01","%Y-%m-%d")
15+
END_DATE=datetime.strptime("2013-01-01","%Y-%m-%d")
16+
17+
def format_date(date):
18+
return int(time.mktime(date.timetuple())*1000)
19+
1220
# class ParamsWriter:
1321
# def __init__(self, name, num_params):
1422
# self.files = []
@@ -105,7 +113,6 @@ def post_month_params(sample, lower_bound, upper_bound):
105113
# results.append([[start_day, end_day], count_sum])
106114
# return results
107115

108-
109116
def key_params(sample, lower_bound, upper_bound):
110117
results = []
111118
for key, count in sample:
@@ -124,7 +131,7 @@ def serialize_q2(country_sets, post_day_ranges):
124131
for country_set, count_country in country_sets:
125132
for day_range, count_post in post_day_ranges:
126133
if random.randint(0,len(country_sets) + len(post_day_ranges)) == 0:
127-
writer.append([str(day_range[0]), str(day_range[1]), "2013-01-01", ",".join(country_set)], [count_post,count_post,count_country,333])
134+
writer.append([str(day_range[0]), str(day_range[1]), ",".join(country_set), str(format_date(END_DATE))], [count_post,count_post,count_country,333])
128135

129136
def serialize_q3(post_months):
130137
writer = ParamsWriter("q3", ["todo1","todo2"])
@@ -237,15 +244,23 @@ def serialize_q24(tagclasses):
237244
for tagclass, count in tagclasses:
238245
writer.append([tagclass], [count])
239246

247+
def add_months(sourcedate,months):
248+
month = sourcedate.month - 1 + months
249+
year = int(sourcedate.year + month / 12 )
250+
month = month % 12 + 1
251+
day = min(sourcedate.day,calendar.monthrange(year,month)[1])
252+
return sourcedate.replace(year, month, day)
253+
240254
def convert_posts_histo(histogram):
241255
week_posts = []
242256
month = 0
243257
while (histogram.existParam(month)):
244258
monthTotal = histogram.getValue(month, "p")
245-
week_posts.append([month*30, monthTotal/4])
246-
week_posts.append([month*30+7, monthTotal/4])
247-
week_posts.append([month*30+14, monthTotal/4])
248-
week_posts.append([month*30+21, monthTotal/4])
259+
baseDate=add_months(START_DATE,month)
260+
week_posts.append([format_date(baseDate), monthTotal/4])
261+
week_posts.append([format_date(baseDate+timedelta(days=7)), monthTotal/4])
262+
week_posts.append([format_date(baseDate+timedelta(days=14)), monthTotal/4])
263+
week_posts.append([format_date(baseDate+timedelta(days=21)), monthTotal/4])
249264
month = month + 1
250265
return week_posts
251266

@@ -299,8 +314,10 @@ def main(argv=None):
299314
post_upper_threshold = 0.1*total_posts*1.1
300315
post_day_ranges = post_date_range_params(week_posts, post_lower_threshold, post_upper_threshold)
301316

302-
post_lower_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*0.8
303-
post_upper_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*1.2
317+
#post_lower_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*0.8
318+
#post_upper_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*1.2
319+
post_lower_threshold = (total_posts/(len(week_posts)/4))*0.8
320+
post_upper_threshold = (total_posts/(len(week_posts)/4))*1.2
304321
post_months = post_month_params(week_posts, post_lower_threshold, post_upper_threshold)
305322

306323
serialize_q2(country_sets, post_day_ranges)

0 commit comments

Comments
 (0)