예제 #1
0
 def test_average_height(self):
     client = MongoClient('localhost', 27017)
     collection = client['GroupB']['farmers']
     test_d1 = dt.datetime(2015, 11, 14, 0, 0, 0)
     test_d2 = dt.datetime(2015, 11, 16, 0, 0, 0)
     test_btc_addr = "1H6HBTUCVpZMLBqSUgrb9bZHnPTE4uSiKV"
     height = payments.average_height(test_btc_addr, test_d1, test_d2, collection)
     self.assertTrue(height > 0)
예제 #2
0
def create_daily_summary(conn, cursor, collection, date):
    """
    Inserts the farmers' summaries for the specified date.

    Args:
        conn: sqlite db connection
        cursor: conn's cursor
        collection: MongoDB farmers collection
        date: generate summary entries for this date
    """
    next_date = date + timedelta(days = 1)
    first_dates = {}
    last_dates = {}
    payout_addresses = {}
    uptimes = {}
    previous_time = 0
    for doc in collection.find({'time': {'$gte': date, '$lt': next_date}}):
        doc_time = time.mktime(doc['time'].timetuple())
        for farmer in doc['farmers']:
            auth_address = farmer['btc_addr']
            if (auth_address in first_dates):
                if last_dates[auth_address] == previous_time:
                    uptimes[auth_address] += doc_time - previous_time
                last_dates[auth_address] = doc_time
            else:
                first_dates[auth_address] = doc_time
                last_dates[auth_address] = doc_time
                payout_addresses[auth_address] = farmer['payout_addr']
                uptimes[auth_address] = 0
        previous_time = doc_time
    for key in first_dates:
        auth_address = key
        summary_date = date
        payout_address = payout_addresses[key]
        uptime = uptimes[key]
        duration = last_dates[key] - first_dates[key]
        height = payments.average_height(auth_address, date, next_date, collection)
        cursor.execute('''INSERT INTO summaries (auth_address, date,
                          payout_address, uptime, duration, height)
                          VALUES (?, ?, ?, ?, ? ,?)''',
                       (str(auth_address), str(summary_date), payout_address, uptime,
                       duration, height))
        conn.commit()