koder aka kdanilov | 203eff5 | 2015-02-09 12:42:44 +0200 | [diff] [blame] | 1 | import sys |
| 2 | import json |
| 3 | import sqlite3 |
| 4 | import contextlib |
| 5 | |
| 6 | |
| 7 | def connect(url): |
| 8 | return sqlite3.connect(url) |
| 9 | |
| 10 | |
| 11 | create_db_sql_templ = """ |
| 12 | CREATE TABLE build (id integer primary key, |
| 13 | build text, |
| 14 | type text, |
| 15 | md5 text); |
| 16 | |
| 17 | CREATE TABLE params_combination (id integer primary key, {params}); |
| 18 | |
| 19 | CREATE TABLE result (build_id integer, |
| 20 | params_combination integer, |
| 21 | bandwith float, |
| 22 | deviation float); |
| 23 | """ |
| 24 | |
| 25 | |
| 26 | PARAM_COUNT = 20 |
| 27 | |
| 28 | |
| 29 | def get_all_tables(conn): |
| 30 | cursor = conn.cursor() |
| 31 | cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") |
| 32 | return cursor.fetchall() |
| 33 | |
| 34 | |
| 35 | def drop_database(conn): |
| 36 | cursor = conn.cursor() |
| 37 | cursor.execute("drop table result") |
| 38 | cursor.execute("drop table params_combination") |
| 39 | cursor.execute("drop table build") |
| 40 | |
| 41 | |
| 42 | def init_database(conn): |
| 43 | cursor = conn.cursor() |
| 44 | |
| 45 | params = ["param_{0} text".format(i) for i in range(PARAM_COUNT)] |
| 46 | create_db_sql = create_db_sql_templ.format(params=",".join(params)) |
| 47 | |
| 48 | for sql in create_db_sql.split(";"): |
| 49 | cursor.execute(sql) |
| 50 | |
| 51 | |
| 52 | def insert_build(cursor, build_id, build_type, iso_md5): |
| 53 | cursor.execute("insert into build (build, type, md5) values (?, ?, ?)", |
| 54 | (build_id, build_type, iso_md5)) |
| 55 | return cursor.lastrowid |
| 56 | |
| 57 | |
| 58 | def insert_params(cursor, *param_vals): |
| 59 | param_vals = param_vals + ("",) * (PARAM_COUNT - len(param_vals)) |
| 60 | |
| 61 | params = ",".join(['?'] * PARAM_COUNT) |
| 62 | select_templ = "select id from params_combination where {params_where}" |
| 63 | |
| 64 | params_where = ["param_{0}=?".format(i) for i in range(PARAM_COUNT)] |
| 65 | req = select_templ.format(params_where=" AND ".join(params_where)) |
| 66 | cursor.execute(req, param_vals) |
| 67 | res = cursor.fetchall() |
| 68 | if [] != res: |
| 69 | return res[0][0] |
| 70 | |
| 71 | params = ",".join(['?'] * PARAM_COUNT) |
| 72 | param_insert_templ = "insert into params_combination ({0}) values ({1})" |
| 73 | param_names = ",".join("param_{0}".format(i) for i in range(PARAM_COUNT)) |
| 74 | req = param_insert_templ.format(param_names, params) |
| 75 | cursor.execute(req, param_vals) |
| 76 | return cursor.lastrowid |
| 77 | |
| 78 | |
| 79 | def insert_results(cursor, build_id, params_id, bw, dev): |
| 80 | req = "insert into result values (?, ?, ?, ?)" |
| 81 | cursor.execute(req, (build_id, params_id, bw, dev)) |
| 82 | |
| 83 | |
| 84 | @contextlib.contextmanager |
| 85 | def transaction(conn): |
| 86 | try: |
| 87 | cursor = conn.cursor() |
| 88 | yield cursor |
| 89 | except: |
| 90 | conn.rollback() |
| 91 | raise |
| 92 | else: |
| 93 | conn.commit() |
| 94 | |
| 95 | |
| 96 | def json_to_db(json_data, conn): |
| 97 | data = json.loads(json_data) |
| 98 | with transaction(conn) as cursor: |
| 99 | for build_data in data: |
| 100 | build_id = insert_build(cursor, |
| 101 | build_data.pop("build_id"), |
| 102 | build_data.pop("type"), |
| 103 | build_data.pop("iso_md5")) |
| 104 | |
| 105 | for params, (bw, dev) in build_data.items(): |
| 106 | param_id = insert_params(cursor, *params.split(" ")) |
| 107 | insert_results(cursor, build_id, param_id, bw, dev) |
| 108 | |
| 109 | |
| 110 | conn = sqlite3.connect(sys.argv[1]) |
| 111 | json_data = open(sys.argv[2]).read() |
| 112 | |
| 113 | if len(get_all_tables(conn)) == 0: |
| 114 | init_database(conn) |
| 115 | |
| 116 | json_to_db(json_data, conn) |