Skip to content

Commit

Permalink
feat(db): Add command_meta and settings from argparser as json
Browse files Browse the repository at this point in the history
  • Loading branch information
peckto authored and rumpelsepp committed Feb 24, 2023
1 parent 25a131e commit bb09d80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/gallia/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from gallia.powersupply import PowerSupply, PowerSupplyURI
from gallia.services.uds.core.exception import UDSException
from gallia.transports import BaseTransport, TargetURI
from gallia.utils import camel_to_snake
from gallia.utils import camel_to_snake, dump_args


@unique
Expand Down Expand Up @@ -494,6 +494,8 @@ async def _db_insert_run_meta(self, args: Namespace) -> None:
await self.db_handler.insert_run_meta(
script=sys.argv[0].split()[-1],
arguments=sys.argv[1:],
command_meta=msgspec.json.encode(self.run_meta.command_meta),
settings=dump_args(args),
start_time=datetime.now(timezone.utc).astimezone(),
path=self.artifacts_dir,
)
Expand Down
20 changes: 15 additions & 5 deletions src/gallia/db/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def bytes_repr(data: bytes) -> str:
return bytes_repr_(data, False, None)


schema_version = "1.0"
schema_version = "2.0"

DB_SCHEMA = f"""
CREATE TABLE IF NOT EXISTS version (
Expand All @@ -50,6 +50,8 @@ def bytes_repr(data: bytes) -> str:
id integer primary key,
script text not null,
arguments json not null check(json_valid(arguments)),
command_meta json not null check(json_valid(arguments)),
settings json not null check(json_valid(arguments)),
start_time real not null,
start_timezone text not null,
end_time real,
Expand Down Expand Up @@ -191,20 +193,28 @@ async def check_version(self) -> None:
f"The version of the database schema is not supported! ({version} != {schema_version})"
)

async def insert_run_meta(
self, script: str, arguments: list[str], start_time: datetime, path: Path
async def insert_run_meta( # noqa: CODE
self,
script: str,
arguments: list[str],
command_meta: bytes,
settings: dict[str, str | int | float],
start_time: datetime,
path: Path,
) -> None:
assert self.connection is not None, "Not connected to the database"

query = (
"INSERT INTO run_meta(script, arguments, start_time, start_timezone, path) VALUES "
"(?, ?, ?, ?, ?)"
"INSERT INTO run_meta(script, arguments, command_meta, settings, start_time, start_timezone, path) VALUES "
"(?, ?, ?, ?, ?, ?, ?)"
)
cursor = await self.connection.execute(
query,
(
script,
json.dumps(arguments),
command_meta,
json.dumps(settings),
start_time.timestamp(),
start_time.tzname(),
str(path),
Expand Down
10 changes: 10 additions & 0 deletions src/gallia/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,13 @@ def lazy_import(name: str) -> ModuleType:
sys.modules[name] = module
loader.exec_module(module)
return module


def dump_args(args: Namespace) -> dict[str, str | int | float]:
settings = {}
for key, value in args.__dict__.items():
match value:
case str() | int() | float():
settings[key] = value

return settings

0 comments on commit bb09d80

Please sign in to comment.