-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9182be8
commit 72dfec4
Showing
3 changed files
with
55 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from cognee.infrastructure.engine import DataPoint | ||
from cognee.modules.data.processing.document_types import Document | ||
from cognee.infrastructure.databases.relational import get_relational_engine | ||
from sqlalchemy import select | ||
from cognee.modules.data.models import Data | ||
from cognee.modules.data.models.MetricData import GraphMetricData | ||
import uuid | ||
from cognee.infrastructure.databases.graph import get_graph_engine | ||
|
||
|
||
async def fetch_token_count(db_engine) -> int: | ||
""" | ||
Fetches and sums token counts from the database. | ||
Returns: | ||
int: The total number of tokens across all documents. | ||
""" | ||
|
||
async with db_engine.get_async_session() as session: | ||
document_data_points = await session.execute(select(Data)) | ||
token_count_sum = sum(document.token_count for document in document_data_points.scalars()) | ||
|
||
return token_count_sum | ||
|
||
|
||
async def calculate_graph_metrics(graph_data): | ||
nodes, edges = graph_data | ||
graph_metrics = { | ||
"num_nodes": len(nodes), | ||
"num_edges": len(edges), | ||
} | ||
return graph_metrics | ||
|
||
|
||
async def store_descriptive_metrics(data_points: list[DataPoint]): | ||
db_engine = get_relational_engine() | ||
graph_engine = await get_graph_engine() | ||
graph_data = await graph_engine.get_graph_data() | ||
|
||
token_count_sum = await fetch_token_count(db_engine) | ||
graph_metrics = await calculate_graph_metrics(graph_data) | ||
|
||
table_name = "graph_metric_table" | ||
metrics_dict = {"id": uuid.uuid4(), "num_tokens": token_count_sum} | graph_metrics | ||
|
||
await db_engine.insert_data(table_name, metrics_dict) |