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