def remove_redundant_delays(conn, delays):
if len(delays.values()) == 0:
return delays # no new delays to add
ids = delays.keys()
cur = conn.cursor(cursor_factory=RealDictCursor)
query = """ SELECT * FROM realtime_updates WHERE id IN %(ids)s """
cur.execute(query, {'ids': tuple(ids)})
results = cur.fetchall()
cur.close()
existing_delays = {get_delay_id(r): r for r in results}
new_delays = {}
for key, delay in delays.iteritems():
# if stop already registered in datbase
if key in existing_delays:
current = existing_delays[key]
# and the file we're reading now is newer than the file in the database
if current['s3_path'] < delay['s3_path']:
# and the timings is different
if current['arrival_delay'] != delay['arrival_delay'] \
or current['departure_delay'] != delay['departure_delay']:
# then store the change
new_delays[key] = delay
else:
new_delays[key] = delay
return new_delays
评论列表
文章目录