Minor fixes

This commit is contained in:
Alban Bronisz 2021-11-21 13:36:54 +01:00
parent 74db578072
commit 1f3cf30127

View File

@ -13,8 +13,8 @@ from typing import Dict
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
HERE = Path(os.path.dirname(os.path.realpath(__file__))) HERE = Path(os.path.dirname(os.path.realpath(__file__)))
# DOCKER_BIN = "/snap/bin/docker" DOCKER_BIN = "/snap/bin/docker"
DOCKER_BIN = "/usr/bin/docker" # DOCKER_BIN = "/usr/bin/docker"
TABLE_NAME = "dc_stats" TABLE_NAME = "dc_stats"
@ -39,7 +39,7 @@ def init_db(sqlite_fn: Path):
if sqlite_fn.exists(): if sqlite_fn.exists():
return return
logging.info("Create sqlite file: '%s'", sqlite_fn) log.info("Create sqlite file: '%s'", sqlite_fn)
con = sqlite3.connect(str(sqlite_fn)) con = sqlite3.connect(str(sqlite_fn))
cur = con.cursor() cur = con.cursor()
cur.execute(f"CREATE TABLE {TABLE_NAME} (date timestamp)") cur.execute(f"CREATE TABLE {TABLE_NAME} (date timestamp)")
@ -55,22 +55,22 @@ def add_row(sqlite_fn: Path, stats: Dict):
existing_cols = [ existing_cols = [
c[1] for c in cur.execute(f"PRAGMA table_info({TABLE_NAME});").fetchall() c[1] for c in cur.execute(f"PRAGMA table_info({TABLE_NAME});").fetchall()
] ]
logging.debug("existing_cols: %s", existing_cols) log.debug("existing_cols: %s", existing_cols)
for k in stats: for k in stats:
if k in existing_cols: if k in existing_cols:
continue continue
logging.debug("Create '%s' col", k) log.debug("Create '%s' col", k)
cur.execute(f"ALTER TABLE {TABLE_NAME} ADD COLUMN '{k}' 'float';") cur.execute(f"ALTER TABLE {TABLE_NAME} ADD COLUMN '{k}' 'float';")
# Insert row # Insert row
# ts = stats.pop("date") # ts = stats.pop("date")
cols = ", ".join(stats.keys()) cols = "`, `".join(stats.keys())
values = tuple(stats.values()) values = tuple(stats.values())
v = ", ".join(["?" for v in values]) v = ", ".join(["?" for v in values])
cmd = f"INSERT INTO {TABLE_NAME} ({cols}) VALUES ({v});" cmd = f"INSERT INTO {TABLE_NAME} (`{cols}`) VALUES ({v});"
logging.debug("SQLITE3: %s", cmd) log.debug("SQLITE3: %s", cmd)
logging.debug("SQLITE3: values: %s", values) log.debug("SQLITE3: values: %s", values)
cur.execute(cmd, values) cur.execute(cmd, values)
cur.execute("COMMIT") cur.execute("COMMIT")
con.close() con.close()
@ -97,12 +97,11 @@ def main():
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
else: else:
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logging.info("Export stats in %s", sqlite_fn) log.info("Export stats in %s", sqlite_fn)
out = check_output( out = check_output(
[DOCKER_BIN, "stats", "--no-stream", "--format", '"{{.Name}}": "{{.MemUsage}}"'] [DOCKER_BIN, "stats", "--no-stream", "--format", '"{{.Name}}": "{{.MemUsage}}"']
) )
stats = format_stats(out) stats = format_stats(out)
logging.info(stats)
init_db(sqlite_fn) init_db(sqlite_fn)
add_row(sqlite_fn, stats) add_row(sqlite_fn, stats)