Skip to content

Commit

Permalink
Add raw_output to facts
Browse files Browse the repository at this point in the history
  • Loading branch information
fliepeltje committed Nov 21, 2023
1 parent 8d0ccb7 commit f93ee76
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 9 additions & 2 deletions humitifier/db/queries.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
-- name: bulk_insert_host_facts*!
insert into
host_facts (host, timestamp, facts, exceptions)
host_facts (host, timestamp, raw_output, facts, exceptions)
values
(:host, :timestamp, :facts, :exceptions);
(
:host,
:timestamp,
:raw_output,
:facts,
:exceptions
);

-- name: get_latest_host_facts
select
host,
timestamp,
raw_output,
facts,
exceptions
from
Expand Down
1 change: 1 addition & 0 deletions humitifier/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ create table if not exists host_facts (
id integer primary key autoincrement,
host text,
timestamp integer,
raw_output text,
facts text,
exceptions text
);
5 changes: 2 additions & 3 deletions humitifier/infra/facts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import time
from .blocks import Blocks
from .groups import Groups
from .hostnamectl import HostnameCtl
Expand Down Expand Up @@ -66,11 +65,11 @@ async def element_to_fact(el: str) -> Fact:
try:
fact = FACT_TABLE[alias]
except KeyError as e:
return str(e)
return f"ParseError(KeyError): {str(e)}"
try:
return fact.from_stdout(stdout)
except Exception as e:
return str(e)
return f"ParseError: {str(e)}"


async def query_inventory_outputs(hosts) -> list[HostOutput]:
Expand Down
10 changes: 6 additions & 4 deletions humitifier/infra/models/hostfacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
class HostFacts:
fqdn: str
timestamp: int
raw_output: str
facts: dict
exceptions: list[str] | None = None

@classmethod
async def from_output(cls, output: HostOutput, ts) -> "HostFacts":
async def from_output(cls, output: HostOutput, ts: int) -> "HostFacts":
if output.exception:
return cls(output.host, ts, {}, [str(output.exception)])
stdout = "\n".join(list(output.stdout)).strip()
Expand All @@ -24,23 +25,24 @@ async def from_output(cls, output: HostOutput, ts) -> "HostFacts":
parsed = await asyncio.gather(*tasks)
facts = [f for f in parsed if not isinstance(f, str)]
excpetions = [f for f in parsed if isinstance(f, str)]
return cls(output.host, ts, {f.alias: f for f in facts}, excpetions or None)
return cls(output.host, ts, stdout, {f.alias: f for f in facts}, excpetions or None)

@property
async def sql_row(self) -> str:
fact_data = {k: fact.to_sql() for k, fact in self.facts.items()}
return (
self.fqdn,
self.timestamp,
self.raw_output,
json.dumps(fact_data),
json.dumps(self.exceptions),
)

@classmethod
async def from_sql_row(cls, row) -> "HostFacts":
fqdn, timestamp, facts, exceptions = row
fqdn, timestamp, raw_output, facts, exceptions = row
fact_data = {k: FACT_TABLE[k].from_sql(v) for k, v in json.loads(facts).items()}
return cls(fqdn, timestamp, fact_data, json.loads(exceptions))
return cls(fqdn, timestamp, raw_output, fact_data, json.loads(exceptions))

@property
def last_scan(self) -> str:
Expand Down

0 comments on commit f93ee76

Please sign in to comment.