alfraser commited on
Commit
843d9d3
·
1 Parent(s): 3853f7c

Adding the sqlite db where I will archive the test results and added the archiving code

Browse files
Files changed (2) hide show
  1. data/sqlite/test_records.db +3 -0
  2. src/testing.py +77 -1
data/sqlite/test_records.db ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d94143eec37c91e71cc3d39507a8c4741b2922848b7cfd33f48b1b5d4b7aebd0
3
+ size 53248
src/testing.py CHANGED
@@ -2,7 +2,11 @@ from __future__ import annotations # For self-referencing annotations
2
 
3
  import json
4
  import os
 
 
 
5
 
 
6
  from random import choices
7
  from typing import List, Dict, Optional
8
 
@@ -104,7 +108,7 @@ class ArchitectureRequestRecord:
104
 
105
 
106
  class TestGroup:
107
- all: Dict[str, List[ArchitectureRequestRecord]] = None
108
 
109
  def __init__(self, test_group:str):
110
  self.arch_request_records: List[ArchitectureRequestRecord] = []
@@ -183,3 +187,75 @@ class TestGroup:
183
  def for_test_group_tag(cls, test_group_tag: str) -> TestGroup:
184
  cls.load_all()
185
  return cls.all[test_group_tag]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import json
4
  import os
5
+ import shutil
6
+ import sqlite3
7
+ import sys
8
 
9
+ from huggingface_hub import Repository
10
  from random import choices
11
  from typing import List, Dict, Optional
12
 
 
108
 
109
 
110
  class TestGroup:
111
+ all: Dict[str, TestGroup] = None
112
 
113
  def __init__(self, test_group:str):
114
  self.arch_request_records: List[ArchitectureRequestRecord] = []
 
187
  def for_test_group_tag(cls, test_group_tag: str) -> TestGroup:
188
  cls.load_all()
189
  return cls.all[test_group_tag]
190
+
191
+
192
+ def move_test_records_to_db(hf_hub_token: str) -> None:
193
+ """
194
+ This is an offline utility to move the transaction logs from
195
+ a flat file to a database. To keep things simpler transaction logs
196
+ are initially stored into a json file as a Hugging Face dataset, but
197
+ this can get cumbersome, so this utility will move the records
198
+ into an sqlite database. It can be run periodically just to move
199
+ them across.
200
+ """
201
+ def download_latest_json_file(hf_hub_token: str) -> Repository:
202
+ if os.path.exists(Architecture.trace_dir):
203
+ shutil.rmtree(Architecture.trace_dir)
204
+ return Repository(local_dir=Architecture.trace_dir, clone_from=Architecture.save_repo_url, token=hf_hub_token)
205
+
206
+ def create_local_db():
207
+ db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
208
+ con = sqlite3.connect(db_file)
209
+
210
+ sql = "CREATE TABLE test_groups (id INTEGER PRIMARY KEY AUTOINCREMENT, test_group TEXT NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL);"
211
+ con.execute(sql)
212
+
213
+ sql = "CREATE TABLE arch_requests (id INTEGER PRIMARY KEY AUTOINCREMENT, arch_name TEXT NOT NULL, response_len INTEGER NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL, comment TEXT NOT NULL, test_group_id INTEGER NOT NULL, FOREIGN KEY (test_group_id) REFERENCES test_groups (id))"
214
+ con.execute(sql)
215
+
216
+ sql = "CREATE TABLE arch_req_steps (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL, arch_req_id INTEGER NOT NULL, FOREIGN KEY (arch_req_id) REFERENCES arch_requests(id))"
217
+ con.execute(sql)
218
+
219
+ def get_local_db() -> sqlite3.Connection:
220
+ db_file = os.path.join(data_dir, 'sqlite', 'test_records.db')
221
+ if not os.path.exists(db_file):
222
+ create_local_db()
223
+ return sqlite3.connect(db_file)
224
+
225
+ def load_test_group_to_db(test_group: TestGroup, con: sqlite3.Connection) -> None:
226
+ cur = con.cursor()
227
+ sql = f'SELECT count(*) from test_groups where test_group ="{test_group.test_group}"'
228
+ cur.execute(sql)
229
+ tg_not_in_db = cur.fetchall()[0][0] == 0
230
+ if tg_not_in_db:
231
+ sql = f'INSERT into test_groups (test_group, start, end) VALUES ("{test_group.test_group}", {test_group.start}, {test_group.end})'
232
+ tg_id = con.execute(sql).lastrowid
233
+
234
+ for arr in test_group.arch_request_records:
235
+ sql = f'INSERT INTO arch_requests (arch_name, response_len, start, end, comment, test_group_id) VALUES ("{arr.arch}", {arr.response_len}, {arr.start}, {arr.end}, "{arr.comment}", {tg_id})'
236
+ arr_id = con.execute(sql).lastrowid
237
+
238
+ for s in arr.steps:
239
+ sql= f'INSERT INTO arch_req_steps (name, start, end, arch_req_id) VALUES ("{s.name}", {s.start}, {s.end}, {arr_id})'
240
+ con.execute(sql)
241
+ con.commit()
242
+ else:
243
+ print(f"Warning TestGroup {tg_not_in_db} was not added to the DB as it already existed there")
244
+
245
+ def load_all_test_groups_to_db(con: sqlite3.Connection) -> None:
246
+ TestGroup.load_all()
247
+ for tg in TestGroup.all.values():
248
+ load_test_group_to_db(tg, con)
249
+
250
+ # Main control flow using utility local functions above for better structure
251
+ download_latest_json_file(hf_hub_token)
252
+ conn = get_local_db()
253
+ load_all_test_groups_to_db(conn)
254
+ Architecture.wipe_trace(hf_hub_token)
255
+ print("REMINDER: need to commit the local sqlite file to make it available to the server")
256
+
257
+
258
+ if __name__ == "__main__":
259
+ # Expected to only be directly called for the json to db transfer - arg should be the HF token
260
+ hf_token = sys.argv[1]
261
+ move_test_records_to_db(hf_token)