Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions app/mcp/v1/tools/ch_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_health_summary_ch() -> dict[str, Any]:
try:
return get_health_summary_from_ch()
except Exception as e:
return {"error": str(e)}
return {"error": f"Failed to get health summary: {str(e)}"}


@ch_reader_router.tool
Expand Down Expand Up @@ -71,7 +71,7 @@ def search_health_records_ch(params: HealthRecordSearchParams) -> dict[str, Any]
try:
return search_health_records_from_ch(params)
except Exception as e:
return {"error": str(e)}
return {"error": f"Failed to search health records: {str(e)}"}


@ch_reader_router.tool
Expand Down Expand Up @@ -142,16 +142,26 @@ def get_trend_data_ch(

Returns:
- record_type: The analyzed record type
- device: The device on which the data was recorded
- interval: The time interval used
- trend_data: List of time buckets with statistics for each period:
* date: The time period (ISO string)
* value_sum: Sum of values for the period
* avg_value: Average value for the period
* min_value: Minimum value for the period
* max_value: Maximum value for the period
* count: Number of records in the period

Notes for LLMs:
- Use this to analyze trends, patterns, and seasonal variations in health data
- Keep in mind that when there is data from multiple devices spanning the same
time period, there is a possibility of data being duplicated. Inform the user
of this possibility if you see multiple devices in the same time period.
- If a user asks you to sum up some values from their health records, DO NOT
search for records and write a script to sum them, instead, use this tool:
if they ask to sum data from a year, use this tool with date_from set as the
beginning of the year and date_to as the end of the year, with an interval
of 'year'
- The function automatically handles date filtering if date_from/date_to are provided
- IMPORTANT - interval must be one of: "day", "week", "month", or "year".
Do not use other values.
Expand Down
16 changes: 13 additions & 3 deletions app/mcp/v1/tools/duckdb_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_health_summary_duckdb() -> list[dict[str, Any]]:
try:
return get_health_summary_from_duckdb()
except Exception as e:
return [{"error": str(e)}]
return [{"error": f"Failed to get health summary: {str(e)}"}]


@duckdb_reader_router.tool
Expand Down Expand Up @@ -71,7 +71,7 @@ def search_health_records_duckdb(params: HealthRecordSearchParams) -> list[dict[
try:
return search_health_records_from_duckdb(params)
except Exception as e:
return [{"error": str(e)}]
return [{"error": f"Failed to search health records: {str(e)}"}]


@duckdb_reader_router.tool
Expand Down Expand Up @@ -142,16 +142,26 @@ def get_trend_data_duckdb(

Returns:
- record_type: The analyzed record type
- device: The device on which the data was recorded
- interval: The time interval used
- trend_data: List of time buckets with statistics for each period:
* date: The time period (ISO string)
* value_sum: Sum of values for the period
* avg_value: Average value for the period
* min_value: Minimum value for the period
* max_value: Maximum value for the period
* count: Number of records in the period

Notes for LLMs:
- Use this to analyze trends, patterns, and seasonal variations in health data
- Keep in mind that when there is data from multiple devices spanning the same
time period, there is a possibility of data being duplicated. Inform the user
of this possibility if you see multiple devices in the same time period.
- If a user asks you to sum up some values from their health records, DO NOT
search for records and write a script to sum them, instead, use this tool:
if they ask to sum data from a year, use this tool with date_from set as the
beginning of the year and date_to as the end of the year, with an interval
of 'year'
- The function automatically handles date filtering if date_from/date_to are provided
- IMPORTANT - interval must be one of: "day", "week", "month", or "year".
Do not use other values.
Expand Down Expand Up @@ -207,4 +217,4 @@ def search_values_duckdb(
try:
return search_values_from_duckdb(record_type, value, date_from, date_to)
except Exception as e:
return [{"error": f"Failed to get trend data: {str(e)}"}]
return [{"error": f"Failed to search for values: {str(e)}"}]
14 changes: 12 additions & 2 deletions app/mcp/v1/tools/es_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_health_summary_es() -> dict[str, Any]:
try:
return get_health_summary_from_es()
except Exception as e:
return {"error": f"Failed to get health summary from ES: {str(e)}"}
return {"error": f"Failed to get health summary: {str(e)}"}


@es_reader_router.tool
Expand Down Expand Up @@ -142,16 +142,26 @@ def get_trend_data_es(

Returns:
- record_type: The analyzed record type
- device: The device on which the data was recorded
- interval: The time interval used
- trend_data: List of time buckets with statistics for each period:
* date: The time period (ISO string)
* value_sum: Sum of values for the period
* avg_value: Average value for the period
* min_value: Minimum value for the period
* max_value: Maximum value for the period
* count: Number of records in the period

Notes for LLMs:
- Use this to analyze trends, patterns, and seasonal variations in health data
- Keep in mind that when there is data from multiple devices spanning the same
time period, there is a possibility of data being duplicated. Inform the user
of this possibility if you see multiple devices in the same time period.
- If a user asks you to sum up some values from their health records, DO NOT
search for records and write a script to sum them, instead, use this tool:
if they ask to sum data from a year, use this tool with date_from set as the
beginning of the year and date_to as the end of the year, with an interval
of 'year'
- The function automatically handles date filtering if date_from/date_to are provided
- IMPORTANT - interval must be one of: "day", "week", "month", or "year".
Do not use other values.
Expand Down Expand Up @@ -207,4 +217,4 @@ def search_values_es(
try:
return search_values_logic(record_type, value, date_from, date_to)
except Exception as e:
return [{"error": f"Failed to get trend data: {str(e)}"}]
return [{"error": f"Failed to search for values: {str(e)}"}]
7 changes: 4 additions & 3 deletions app/services/health/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ def get_trend_data_from_ch(
date_to: str | None = None,
) -> dict[str, Any]:
return ch.inquire(f"""
SELECT toStartOfInterval(startDate, INTERVAL 1 {interval}) AS interval,
AVG(value), MIN(value), MAX(value), COUNT(*) FROM {ch.db_name}.{ch.table_name}
SELECT device, toStartOfInterval(startDate, INTERVAL 1 {interval}) AS interval,
AVG(value) AS average, SUM(value) AS sum, MIN(value) AS min,
MAX(value) AS max, COUNT(*) AS count FROM {ch.db_name}.{ch.table_name}
WHERE type = '{record_type}'
{f"AND startDate >= '{date_from}'" if date_from else ""}
{f"AND startDate <= '{date_to}'" if date_to else ""}
GROUP BY interval ORDER BY interval ASC
GROUP BY interval, device ORDER BY interval ASC
""")


Expand Down
11 changes: 6 additions & 5 deletions app/services/health/duckdb_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ def get_trend_data_from_duckdb(
date_to: str | None = None,
) -> list[dict[str, Any]]:
result = duckdb.sql(f"""
SELECT time_bucket(INTERVAL '1 {interval}', startDate) AS interval,
AVG(value) AS average, MIN(value) AS min, MAX(value) AS max, COUNT(*) AS count
SELECT device, time_bucket(INTERVAL '1 {interval}', startDate) AS interval,
AVG(value) AS average, SUM(value) AS sum,
MIN(value) AS min, MAX(value) AS max, COUNT(*) AS count
FROM read_parquet('{client.parquetpath}')
WHERE type = '{record_type}'
{f"AND startDate >= '{date_from}'" if date_from else ""}
{f"AND startDate <= '{date_to}'" if date_to else ""}
GROUP BY interval ORDER BY interval ASC
{f"AND startDate >= '{date_from}'" if date_from else ""}
{f"AND startDate <= '{date_to}'" if date_to else ""}
GROUP BY interval, device ORDER BY interval ASC
""")
return client.format_response(result)

Expand Down
2 changes: 2 additions & 0 deletions app/services/health/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def get_trend_data_logic(
"avg_value": {"avg": {"field": "value"}},
"min_value": {"min": {"field": "value"}},
"max_value": {"max": {"field": "value"}},
"value_sum": {"sum": {"field": "value"}},
"count": {"value_count": {"field": "value"}},
},
},
Expand All @@ -121,6 +122,7 @@ def get_trend_data_logic(
"avg_value": bucket["avg_value"]["value"],
"min_value": bucket["min_value"]["value"],
"max_value": bucket["max_value"]["value"],
"value_sum": bucket["value_sum"]["value"],
"count": bucket["count"]["value"],
},
)
Expand Down