Skip to content

Commit

Permalink
[resotocore][feat] Allow configuring history duration (#1909)
Browse files Browse the repository at this point in the history
* [resotocore][feat] Allow configuring history duration

* fix tests
  • Loading branch information
aquamatthias committed Feb 14, 2024
1 parent 5172f6c commit 9564bb3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 4 additions & 0 deletions resotocore/resotocore/core_config.py
Expand Up @@ -509,6 +509,10 @@ class GraphUpdateConfig(ConfigObject):
default=True,
metadata={"description": "If true, changes of the graph are stored and are available via history."},
)
keep_history_for_days: int = field(
default=180,
metadata={"description": "Duration to keep history entries in days. (default: 180)"},
)
parallel_imports: int = field(
default=5,
metadata={"description": "Number of parallel graph merge requests handled in parallel."},
Expand Down
19 changes: 11 additions & 8 deletions resotocore/resotocore/db/graphdb.py
Expand Up @@ -1286,17 +1286,20 @@ def create_update_collection_indexes(progress: StandardCollection, node_history:
log.info(f"Add index root_nodes on {progress.name}")
progress.add_persistent_index(["root_nodes[*]"], name="root_nodes")
# history indexes ------
node_history_indexes = {idx["name"]: idx for idx in cast(List[Json], node_history.indexes())}
if "history_access" not in node_history_indexes:
nh_idx = {idx["name"]: idx for idx in cast(List[Json], node_history.indexes())}
if "history_access" not in nh_idx:
node_history.add_persistent_index(
["id", "change", "changed_at", "kinds[*]", "reported.id", "reported.name", "reported.ctime"],
sparse=False,
name="history_access",
)
if "ttl_index" in node_history_indexes:
ttl_secs = self.config.keep_history_for_days * (24 * 60 * 60) # days to seconds
if "ttl_index" in nh_idx:
node_history.delete_index("ttl_index")
if "history_ttl" not in node_history_indexes:
node_history.add_ttl_index(["changed_at"], int(timedelta(days=14).total_seconds()), name="history_ttl")
if "history_ttl" not in nh_idx or value_in_path(nh_idx, ["history_ttl", "expiry_time"]) != ttl_secs:
if "history_ttl" in nh_idx:
node_history.delete_index("history_ttl")
node_history.add_ttl_index(["changed_at"], ttl_secs, name="history_ttl")

def create_update_edge_indexes(edges: EdgeCollection) -> None:
edge_idxes = {idx["name"]: idx for idx in cast(List[Json], edges.indexes())}
Expand Down Expand Up @@ -1366,13 +1369,13 @@ async def create_update_views(nodes: VertexCollection) -> None:
else:
in_progress = await create_collection(self.in_progress)
node_history_collection = await create_collection(self.node_history)
create_node_indexes(vertex)
create_update_collection_indexes(in_progress, node_history_collection)
await run_async(create_node_indexes, vertex)
await run_async(create_update_collection_indexes, in_progress, node_history_collection)
await self.usage_db.create_update_schema()

for edge_type in EdgeTypes.all:
edge_collection = db.graph(self.name).edge_collection(self.edge_collection(edge_type))
create_update_edge_indexes(edge_collection)
await run_async(create_update_edge_indexes, edge_collection)

await create_update_views(vertex)
if init_with_data:
Expand Down
1 change: 1 addition & 0 deletions resotocore/tests/resotocore/core_config_test.py
Expand Up @@ -210,6 +210,7 @@ def config_json() -> Json:
"abort_after_seconds": 1234,
"merge_max_wait_time_seconds": 4321,
"keep_history": True,
"keep_history_for_days": 180,
"parallel_imports": 5,
},
"runtime": {
Expand Down

0 comments on commit 9564bb3

Please sign in to comment.