def runtest(dbhandle):
            for i in range(100):
                db.add_row(i, usage_from_timestamp(i), dbhandle)
            q1 = db.query_by_timestamp(10, dbhandle)
            self.assertEqual(q1, [(10, usage_from_timestamp(10))])

            q2 = db.query_by_timestamp((5, 15), dbhandle)
            expected_usages = []
            for i in range(5, 16):
                expected_usages.append((i, usage_from_timestamp(i)))
            for pair in zip(q2, expected_usages):
                self.assertEqual(pair[0], pair[1])
Exemplo n.º 2
0
        def runtest(dbhandle):
            for i in range(100):
                db.add_row(i, usage_from_timestamp(i), dbhandle)
            q1 = db.query_by_timestamp(10, dbhandle)
            self.assertEqual(q1, [(10, usage_from_timestamp(10))])

            q2 = db.query_by_timestamp((5, 15), dbhandle)
            expected_usages = []
            for i in range(5, 16):
                expected_usages.append((i, usage_from_timestamp(i)))
            for pair in zip(q2, expected_usages):
                self.assertEqual(pair[0], pair[1])
def handle_post_request(env):
    try:
        req_body_length = int(env.get('CONTENT_LENGTH', 0))
    except (ValueError):
        req_body_length = 0
    if req_body_length:
        req_body = json.loads(env['wsgi.input'].read(req_body_length))
        try:
            if req_body['function'] == 'insert':
                usage = req_body['usage']
                ts = req_body['timestamp']
                db.add_row(ts, usage)
        except:
            pass
        finally:
            return (True, req_body)
    return (False, None)
def handle_get_request(env):
    queries = parse_qs(env['QUERY_STRING'])
    if dict_contains(queries, 'insert', 'timestamp', 'usage'):
        ts = queries['timestamp'][0]
        usage = queries['usage'][0]
        db.add_row(ts, usage)
        ret = '<p>Inserted row: timestamp = {}, usage = {}</p>'.format(ts, usage)
        return (True, ret)
    elif dict_contains(queries, 'query', 'timestamp'):
        query = db.query_by_timestamp(int(queries['timestamp'][0]))
        timestamps = []
        usage = []
        for val in query:
            timestamps.append(int(val[0]))
            usage.append(int(val[1]))
        plot.make_plot(timestamps, usage)
        ret = "<img src='{}' alt='plot' />".format(TMP_PLOT_NAME)
        return (True, ret)
    return (False, None)
def handle_get_request(env):
    queries = parse_qs(env['QUERY_STRING'])
    if dict_contains(queries, 'insert', 'timestamp', 'usage'):
        ts = queries['timestamp'][0]
        usage = queries['usage'][0]
        db.add_row(ts, usage)
        ret = '<p>Inserted row: timestamp = {}, usage = {}</p>'.format(
            ts, usage)
        return (True, ret)
    elif dict_contains(queries, 'query', 'timestamp'):
        query = db.query_by_timestamp(int(queries['timestamp'][0]))
        timestamps = []
        usage = []
        for val in query:
            timestamps.append(int(val[0]))
            usage.append(int(val[1]))
        plot.make_plot(timestamps, usage)
        ret = "<img src='{}' alt='plot' />".format(TMP_PLOT_NAME)
        return (True, ret)
    return (False, None)
Exemplo n.º 6
0
## @file
# Script to add some random data to an Outletify database

import db_interface as db
import random

dbhandle = db.connect()
try:
    for i in range(0, 100):
        for _ in range(0, random.randint(5, 20)):
            db.add_row(i, random.randint(0, 2000), dbhandle)
finally:
    db.close_handle(dbhandle)