def runTest(self): """Make sure speedtest table works as expected""" connection = sqlite3.connect(":memory:") connection.row_factory = sqlite3.Row table_speedtest.create(connection) table_speedtest.create(connection) v = map(None, ResultIterator()) for d in v: table_speedtest.insert(connection, d, override_timestamp=False) v1 = table_speedtest.listify(connection) self.assertEquals(sorted(v), sorted(v1)) since = utils.timestamp() - 7 * 24 * 60 * 60 until = utils.timestamp() - 3 * 24 * 60 * 60 v2 = table_speedtest.listify(connection, since=since, until=until) self.assertTrue(len(v2) < len(v)) table_speedtest.prune(connection, until) self.assertTrue(len(table_speedtest.listify(connection)) < len(v1))
def main(args): try: options, arguments = getopt.getopt(args[1:], "f:") except getopt.GetoptError: sys.stderr.write(USAGE) sys.exit(1) for key, value in options: if key == "-f": DATABASE.set_path(value) DATABASE.connect() if not arguments: sys.stdout.write('%s\n' % DATABASE.path) elif arguments[0] == "regen_uuid": if DATABASE.readonly: sys.exit('ERROR: readonly database') table_config.update(DATABASE.connection(), {"uuid": utils.get_uuid()}.iteritems()) elif arguments[0] == "prune": if DATABASE.readonly: sys.exit('ERROR: readonly database') table_speedtest.prune(DATABASE.connection()) elif arguments[0] == "delete_all": if DATABASE.readonly: sys.exit('ERROR: readonly database') table_speedtest.prune(DATABASE.connection(), until=utils.timestamp()) DATABASE.connection().execute("VACUUM;") elif arguments[0] in ("show", "dump"): d = { "config": table_config.dictionarize(DATABASE.connection()), "speedtest": table_speedtest.listify(DATABASE.connection()) } if arguments[0] == "show": compat.json.dump(d, sys.stdout, indent=4) elif arguments[0] == "dump": compat.json.dump(d, sys.stdout) else: sys.stdout.write(USAGE) sys.exit(0)
def _api_speedtest(self, stream, request, query): since, until = -1, -1 dictionary = cgi.parse_qs(query) if dictionary.has_key("since"): since = int(dictionary["since"][0]) if dictionary.has_key("until"): until = int(dictionary["until"][0]) indent, mimetype, sort_keys = None, "application/json", False if "debug" in dictionary and utils.intify(dictionary["debug"][0]): indent, mimetype, sort_keys = 4, "text/plain", True response = Message() lst = table_speedtest.listify(DATABASE.connection(), since, until) s = json.dumps(lst, indent=indent, sort_keys=sort_keys) stringio = StringIO.StringIO(s) response.compose(code="200", reason="Ok", body=stringio, mimetype=mimetype) stream.send_response(request, response)