Fix most concurrent count with duplicate time entires

This commit is contained in:
Jonathan Wong 2015-12-20 03:14:39 -08:00
parent 0620ebebcf
commit 3eebb58da5

View file

@ -607,25 +607,27 @@ class DataFactory(object):
logger.warn("Unable to execute database query for get_home_stats: most_concurrent.") logger.warn("Unable to execute database query for get_home_stats: most_concurrent.")
return None return None
times = {} times = []
for item in result: for item in result:
times.update({str(item['stopped']) + 'A': -1, str(item['started']) + 'B': 1}) times.append({'time': str(item['started']) + 'B', 'count': 1})
times.append({'time': str(item['stopped']) + 'A', 'count': -1})
times = sorted(times, key=lambda k: k['time'])
count = 0 count = 0
last_start = 0 last_start = 0
most_concurrent = {'count': count} most_concurrent = {'count': count}
for key in sorted(times): for d in times:
if times[key] == 1: if d['count'] == 1:
count += times[key] count += d['count']
if count >= most_concurrent['count']: if count >= most_concurrent['count']:
last_start = key last_start = d['time']
else: else:
if count >= most_concurrent['count']: if count >= most_concurrent['count']:
most_concurrent = {'count': count, most_concurrent = {'count': count,
'started': last_start[:-1], 'started': last_start[:-1],
'stopped': key[:-1]} 'stopped': d['time'][:-1]}
count += times[key] count += d['count']
home_stats.append({'stat_id': stat, home_stats.append({'stat_id': stat,
'rows': [most_concurrent]}) 'rows': [most_concurrent]})