Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
littleskunk committed Mar 21, 2016
1 parent 121ba34 commit 4fb5592
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
23 changes: 12 additions & 11 deletions driveshare_graph/farmer_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def init_summary_table(conn, cursor, collection):
for single_date in (first_date + timedelta(days=n) for n in range(day_count)):
create_daily_summary(conn, cursor, collection, single_date)
assign_points(conn, cursor, single_date)
conn.commit()


def update_table(conn, cursor, collection): # pragma: no cover
Expand All @@ -47,19 +48,19 @@ def update_table(conn, cursor, collection): # pragma: no cover
if len(cursor.fetchall()) == 0:
create_summary_table(conn, cursor)

cursor.execute("SELECT count(date) FROM summaries")
if cursor.fetchone()[0] == 0:
init_summary_table(conn, cursor, collection)

cursor.execute('SELECT MAX(date) FROM summaries')
date = cursor.fetchone()[0]
max_date = dt.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
next_date = max_date + timedelta(days = 1)
last_date = end_date(collection)
day_count = (last_date - next_date).days
for single_date in (next_date + timedelta(days=n) for n in range(day_count)):
create_daily_summary(conn, cursor, collection, single_date)
assign_points(conn, cursor, single_date)
if date is None:
init_summary_table(conn, cursor, collection)
else:
max_date = dt.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
next_date = max_date + timedelta(days = 1)
last_date = end_date(collection)
day_count = (last_date - next_date).days
for single_date in (next_date + timedelta(days=n) for n in range(day_count)):
create_daily_summary(conn, cursor, collection, single_date)
assign_points(conn, cursor, single_date)
conn.commit()


def create_daily_summary(conn, cursor, collection, date):
Expand Down
28 changes: 14 additions & 14 deletions driveshare_graph/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ def update_stats_table(conn, cursor, collection):
if len(cursor.fetchall()) == 0:
create_stats_table(conn, cursor)

cursor.execute("SELECT count(date) FROM stats")
if cursor.fetchone()[0] == 0:
init_stats_table(conn, cursor, collection)

cursor.execute('SELECT MAX(date) from stats')
last_time = cursor.fetchone()[0]
last_date = dt.datetime.fromtimestamp(int(last_time))
for doc in collection.find({'time': {'$gt': last_time}}):
tb = doc['total_TB']
farmers = doc['total_farmers']
date = time.mktime(doc['time'].timetuple())
if last_time == date:
cursor.execute('UPDATE stats set tb=?, farmers=? WHERE date=?',
(tb, farmers, date))
else:
cursor.execute('INSERT INTO stats(date, tb, farmers) VALUES (?, ?, ?)',
(date, tb, farmers))
if last_time is None:
init_stats_table(conn, cursor, collection)
else:
last_date = dt.datetime.fromtimestamp(int(last_time))
for doc in collection.find({'time': {'$gt': last_time}}):
tb = doc['total_TB']
farmers = doc['total_farmers']
date = time.mktime(doc['time'].timetuple())
if last_time == date:
cursor.execute('UPDATE stats set tb=?, farmers=? WHERE date=?',
(tb, farmers, date))
else:
cursor.execute('INSERT INTO stats(date, tb, farmers) VALUES (?, ?, ?)',
(date, tb, farmers))
conn.commit()


Expand Down
55 changes: 27 additions & 28 deletions driveshare_graph/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def init_farmers_table(conn, cursor, collection):
cursor.execute('''INSERT INTO farmers (nodeid, first_date, last_date,
uptime) VALUES(?, ?, ?, ?)''',
(str(nodeid), first_date, last_date, uptime))
conn.commit()
conn.commit()


def nodeid_in_db(cursor, nodeid):
Expand Down Expand Up @@ -92,37 +92,36 @@ def update_farmers_table(conn, cursor, collection):
if len(cursor.fetchall()) == 0:
create_farmers_table(conn, cursor)

cursor.execute("SELECT count(last_date) FROM farmers")
if cursor.fetchone()[0] == 0:
init_farmers_table(conn, cursor, collection)

cursor.execute('SELECT MAX(last_date) FROM farmers')
last_time = int(cursor.fetchone()[0])
last_date = dt_from_timestamp(last_time)

for doc in collection.find({'time': {'$gt': last_time}}).sort('time', 1):
doc_time = timestamp_from_dt(doc['time'])
for farmer in doc['farmers']:
nodeid = farmer['nodeid']
if nodeid_in_db(cursor, nodeid):
cursor.execute('''SELECT MAX(last_date) FROM farmers WHERE
nodeid=?''', (str(nodeid),))
farmer_time = int(cursor.fetchone()[0])
if farmer_time == last_time:
uptime = doc_time - farmer_time
cursor.execute('''UPDATE farmers SET last_date=?, uptime=uptime+?
WHERE nodeid=?''', (farmer_time, uptime, str(nodeid),))
conn.commit()
if last_time is None:
init_farmers_table(conn, cursor, collection)
else:
last_date = dt_from_timestamp(last_time)

for doc in collection.find({'time': {'$gt': last_time}}).sort('time', 1):
doc_time = timestamp_from_dt(doc['time'])
for farmer in doc['farmers']:
nodeid = farmer['nodeid']
if nodeid_in_db(cursor, nodeid):
cursor.execute('''SELECT MAX(last_date) FROM farmers WHERE
nodeid=?''', (str(nodeid),))
farmer_time = int(cursor.fetchone()[0])
if farmer_time == last_time:
uptime = doc_time - farmer_time
cursor.execute('''UPDATE farmers SET last_date=?, uptime=uptime+?
WHERE nodeid=?''', (farmer_time, uptime, str(nodeid),))
conn.commit()
else:
cursor.execute('''UPDATE farmers SET last_date=? WHERE
nodeid=?''', (farmer_time, str(nodeid),))
conn.commit()
else:
cursor.execute('''UPDATE farmers SET last_date=? WHERE
nodeid=?''', (farmer_time, str(nodeid),))
cursor.execute('''INSERT INTO farmers (nodeid, first_date,
last_date, uptime) VALUES (?, ?, ?, ?) ''',
(str(nodeid), doc_time, doc_time, 0))
conn.commit()
else:
cursor.execute('''INSERT INTO farmers (nodeid, first_date,
last_date, uptime) VALUES (?, ?, ?, ?) ''',
(str(nodeid), doc_time, doc_time, 0))
conn.commit()
last_time = doc_time
last_time = doc_time
conn.commit()


Expand Down

0 comments on commit 4fb5592

Please sign in to comment.