detectkit 0.68.0__tar.gz → 0.70.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {detectkit-0.68.0/detectkit.egg-info → detectkit-0.70.0}/PKG-INFO +1 -1
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/__init__.py +1 -1
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/cli.md +1 -1
- detectkit-0.70.0/detectkit/serve/api_v1.py +335 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/app.py +29 -5
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/server.py +1 -1
- detectkit-0.70.0/detectkit/serve/web.py +162 -0
- detectkit-0.70.0/detectkit/ui/assets/ui.js +569 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/ui/html.py +3 -1
- {detectkit-0.68.0 → detectkit-0.70.0/detectkit.egg-info}/PKG-INFO +1 -1
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit.egg-info/SOURCES.txt +2 -0
- detectkit-0.68.0/detectkit/ui/assets/ui.js +0 -564
- {detectkit-0.68.0 → detectkit-0.70.0}/LICENSE +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/MANIFEST.in +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/README.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/base.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/branding.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/discord.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/email.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/factory.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/googlechat.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/mattermost.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/ntfy.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/slack.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/teams.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/telegram.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/channels/webhook.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_base.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_cooldown.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_decision.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_dispatch.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_recovery.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_replay.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/_types.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/alerting/orchestrator/orchestrator.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/_base.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/_types.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/alert_sim.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/autotuner.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/axis_spec.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/config_emitter.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/crossval.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/detector_select.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/distribution.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/grid_search.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/key_select.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/labels.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/profile_engine.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/result.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/runner.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/scoring.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/seasonality_search.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/settings.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/autotune/window_select.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/_output.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/CLAUDE.section.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/alerting.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/autotune.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/detectors.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/metrics.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/overview.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/rules/project.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/skills/dtk-autotune/SKILL.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/skills/dtk-feedback/SKILL.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/skills/dtk-new-metric/SKILL.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/skills/dtk-setup-project/SKILL.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/assets/claude/skills/dtk-tune/SKILL.md +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/autotune.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/clean.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/init.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/init_claude.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/mcp.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/osi.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/run.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/serve.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/test_alert.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/tune.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/ui.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/commands/unlock.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/cli/main.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/config/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/config/metric_config.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/config/metric_io.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/config/profile.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/config/project_config.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/config/validator.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/actions.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/actor.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/advise.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/commands.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/confirm.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/drafts.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/errors.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/health.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/jobs.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/metric_files.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/overview.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/project.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/proposals.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/queries.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/registry.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/roles.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/serialize.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/control/yaml_patch.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/core/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/core/interval.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/core/models.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/_sql_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/bigquery_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/clickhouse_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/duckdb_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_alert_states.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_autotune_runs.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_base.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_datapoints.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_detections.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_maintenance.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_metrics.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_platform.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_schema.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/_tasks.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/internal_tables/manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/mysql_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/postgres_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/snowflake_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/source_manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/database/tables.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/base.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/factory.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/seasonality.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/_baseline.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/_windowed.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/autoreg.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/iqr.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/mad.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/manual_bounds.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/detectors/statistical/zscore.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/loaders/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/loaders/errors.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/loaders/metric_loader.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/loaders/query_template.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/mcp/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/mcp/context.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/mcp/descriptions.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/mcp/errors.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/mcp/server.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/mcp/tools.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/error_dispatch.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/_alert_step.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/_base.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/_detect_step.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/_load_step.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/_types.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/orchestration/task_manager/manager.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/reporting/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/reporting/assets/report.js +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/reporting/builder.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/reporting/html_report.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/semantic/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/semantic/errors.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/semantic/exporter.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/semantic/importer.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/semantic/osi_model.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/semantic/query_gen.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/errors.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/forge.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/mcp_app.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/platform.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/projects.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/settings.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/serve/write_tools.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/tuning/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/tuning/assets/tune.js +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/tuning/config_writer.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/tuning/html.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/tuning/payload.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/tuning/server.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/ui/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/ui/server.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/utils/__init__.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/utils/datetime_utils.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/utils/env_interpolation.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/utils/json_utils.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit/utils/stats.py +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit.egg-info/dependency_links.txt +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit.egg-info/entry_points.txt +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit.egg-info/requires.txt +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/detectkit.egg-info/top_level.txt +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/pyproject.toml +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/requirements.txt +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/setup.cfg +0 -0
- {detectkit-0.68.0 → detectkit-0.70.0}/setup.py +0 -0
|
@@ -4,7 +4,7 @@ detectk - Anomaly Detection for Time-Series Metrics
|
|
|
4
4
|
A Python library for data analysts and engineers to monitor metrics with automatic anomaly detection.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
__version__ = "0.
|
|
7
|
+
__version__ = "0.70.0"
|
|
8
8
|
|
|
9
9
|
from detectkit.core.interval import Interval
|
|
10
10
|
from detectkit.core.models import ColumnDefinition, TableModel
|
|
@@ -19,7 +19,7 @@ Run all commands from a project directory (the one containing
|
|
|
19
19
|
| `dtk osi import <model>` | Scaffold a native metric from an OSI semantic-model metric (see "OSI interop") |
|
|
20
20
|
| `dtk osi export` | Publish metrics into an OSI fragment (config in `custom_extensions[detectkit]`) |
|
|
21
21
|
| `dtk mcp` | Serve a **read-only** MCP server for an AI assistant (metrics, data, alerts, autotune history — no writes) |
|
|
22
|
-
| `dtk serve` | Hosted platform: the same read-only MCP tools over HTTP for a team, across several projects, behind an MCP gateway |
|
|
22
|
+
| `dtk serve` | Hosted platform: the same read-only MCP tools over HTTP for a team, across several projects, behind an MCP gateway — plus the same reads as a JSON API at `/api/v1` and a read-only web app at `/ui` |
|
|
23
23
|
| `dtk --version` | Show installed detectkit version |
|
|
24
24
|
|
|
25
25
|
## Selectors (`--select` / `-s`)
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
"""``/api/v1`` — the platform's read-only JSON API (RFC 0001 phase 4a).
|
|
2
|
+
|
|
3
|
+
The HTTP twin of the hosted MCP read set, for the platform's web app (the later
|
|
4
|
+
phase-4 PRs) and for scripts. Every route is a ``GET`` over the same
|
|
5
|
+
control-layer query the matching MCP tool calls, so the two doors answer with the
|
|
6
|
+
same data, the same refusals and the same ISO-8601 / JSON-safe contract
|
|
7
|
+
(:mod:`detectkit.control.serialize`). The project is always named in the path —
|
|
8
|
+
the web is multi-project — and ``GET /api/v1/projects`` lists them.
|
|
9
|
+
|
|
10
|
+
::
|
|
11
|
+
|
|
12
|
+
GET /api/v1/projects list_projects
|
|
13
|
+
GET /api/v1/projects/{p} get_server_info
|
|
14
|
+
GET /api/v1/projects/{p}/status get_project_status
|
|
15
|
+
GET /api/v1/projects/{p}/metrics list_metrics
|
|
16
|
+
GET /api/v1/projects/{p}/metrics/{m} get_metric
|
|
17
|
+
GET /api/v1/projects/{p}/metrics/{m}/status get_metric_status
|
|
18
|
+
GET /api/v1/projects/{p}/metrics/{m}/datapoints query_datapoints
|
|
19
|
+
GET /api/v1/projects/{p}/metrics/{m}/detections query_detections
|
|
20
|
+
GET /api/v1/projects/{p}/metrics/{m}/alerts replay_alerts
|
|
21
|
+
GET /api/v1/projects/{p}/metrics/{m}/autotune-runs get_autotune_history
|
|
22
|
+
GET /api/v1/projects/{p}/metrics/{m}/incidents get_incidents
|
|
23
|
+
GET /api/v1/projects/{p}/metrics/{m}/source the YAML text + digest
|
|
24
|
+
GET /api/v1/projects/{p}/metrics/{m}/report the HTML report
|
|
25
|
+
GET /api/v1/projects/{p}/jobs list_jobs (--enable-writes)
|
|
26
|
+
GET /api/v1/projects/{p}/jobs/{id} get_job (--enable-writes)
|
|
27
|
+
|
|
28
|
+
**Nothing here writes.** Mutations stay on the MCP write tools until a later
|
|
29
|
+
phase gives the web its own; any other method answers 405.
|
|
30
|
+
|
|
31
|
+
**The same front as ``/mcp``.** The ASGI gate in :mod:`detectkit.serve.app`
|
|
32
|
+
checks the bearer before a request reaches this handler; the identity header is
|
|
33
|
+
read per request and logged (``[serve] GET /api/v1/… actor=…``). Each request
|
|
34
|
+
runs its query in a worker thread under ``tool_timeout`` (504 when it runs out),
|
|
35
|
+
exactly like a tool call.
|
|
36
|
+
|
|
37
|
+
**Strict query strings.** An unknown parameter or a malformed number / boolean
|
|
38
|
+
is a 400 naming what the route accepts, so a typo can't silently fall back to a
|
|
39
|
+
default. Errors are ``{"error": "<message>"}`` with the control layer's own
|
|
40
|
+
status (400 / 403 / 404 / 409); an unexpected failure is a 500 and is logged.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
from __future__ import annotations
|
|
44
|
+
|
|
45
|
+
import functools
|
|
46
|
+
import json
|
|
47
|
+
from collections.abc import Callable
|
|
48
|
+
from dataclasses import dataclass
|
|
49
|
+
from typing import TYPE_CHECKING, Any
|
|
50
|
+
from urllib.parse import parse_qsl, unquote
|
|
51
|
+
|
|
52
|
+
from detectkit.control import queries as q
|
|
53
|
+
from detectkit.control.errors import ControlError, InvalidRequestError, NotFoundError
|
|
54
|
+
from detectkit.control.project import ProjectHandle
|
|
55
|
+
from detectkit.control.registry import ProjectRegistry
|
|
56
|
+
from detectkit.serve.settings import ServeSettings
|
|
57
|
+
|
|
58
|
+
if TYPE_CHECKING:
|
|
59
|
+
from detectkit.serve.app import Receive, Scope, Send
|
|
60
|
+
from detectkit.serve.platform import Platform
|
|
61
|
+
|
|
62
|
+
PREFIX = "/api/v1"
|
|
63
|
+
|
|
64
|
+
# A parameter: (query-string name, kind, default). Kinds: "str", "int", "bool".
|
|
65
|
+
_Param = tuple[str, str, Any]
|
|
66
|
+
_FROM_TO: tuple[_Param, ...] = (("from", "str", None), ("to", "str", None))
|
|
67
|
+
_TRUE = {"1", "true", "yes", "on"}
|
|
68
|
+
_FALSE = {"0", "false", "no", "off"}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True)
|
|
72
|
+
class _Route:
|
|
73
|
+
"""One endpoint: its path pattern, its query parameters and what it runs.
|
|
74
|
+
|
|
75
|
+
``run(handle, path_args, params)`` returns a JSON-able dict, or a ``str`` for
|
|
76
|
+
an ``html`` route. ``handle`` is ``None`` only for the project list.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
pattern: tuple[str, ...]
|
|
80
|
+
params: tuple[_Param, ...]
|
|
81
|
+
run: Callable[..., Any]
|
|
82
|
+
html: bool = False
|
|
83
|
+
needs_platform: bool = False
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
_NO_PARAMS = _Route((), (), lambda _h, _a, _p: None)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _metric_routes() -> list[_Route]:
|
|
90
|
+
base = ("projects", "{project}", "metrics", "{metric}")
|
|
91
|
+
return [
|
|
92
|
+
_Route(base, (), lambda h, a, p: q.get_metric(h, a["metric"])),
|
|
93
|
+
_Route(
|
|
94
|
+
(*base, "status"),
|
|
95
|
+
(("window", "str", "7d"),),
|
|
96
|
+
lambda h, a, p: q.get_metric_status(h, a["metric"], p["window"]),
|
|
97
|
+
),
|
|
98
|
+
_Route(
|
|
99
|
+
(*base, "datapoints"),
|
|
100
|
+
(*_FROM_TO, ("limit", "int", 1000)),
|
|
101
|
+
lambda h, a, p: q.query_datapoints(h, a["metric"], p["from"], p["to"], p["limit"]),
|
|
102
|
+
),
|
|
103
|
+
_Route(
|
|
104
|
+
(*base, "detections"),
|
|
105
|
+
(
|
|
106
|
+
("detector_id", "str", None),
|
|
107
|
+
*_FROM_TO,
|
|
108
|
+
("anomalies_only", "bool", False),
|
|
109
|
+
("limit", "int", 1000),
|
|
110
|
+
),
|
|
111
|
+
lambda h, a, p: q.query_detections(
|
|
112
|
+
h,
|
|
113
|
+
a["metric"],
|
|
114
|
+
p["detector_id"],
|
|
115
|
+
p["from"],
|
|
116
|
+
p["to"],
|
|
117
|
+
p["anomalies_only"],
|
|
118
|
+
p["limit"],
|
|
119
|
+
),
|
|
120
|
+
),
|
|
121
|
+
_Route(
|
|
122
|
+
(*base, "alerts"),
|
|
123
|
+
_FROM_TO,
|
|
124
|
+
lambda h, a, p: q.replay_alerts(h, a["metric"], p["from"], p["to"]),
|
|
125
|
+
),
|
|
126
|
+
_Route(
|
|
127
|
+
(*base, "autotune-runs"),
|
|
128
|
+
(("limit", "int", 5), ("include_decision_log", "bool", False)),
|
|
129
|
+
lambda h, a, p: q.get_autotune_history(
|
|
130
|
+
h, a["metric"], p["limit"], p["include_decision_log"]
|
|
131
|
+
),
|
|
132
|
+
),
|
|
133
|
+
_Route((*base, "incidents"), (), lambda h, a, p: q.get_incidents(h, a["metric"])),
|
|
134
|
+
_Route((*base, "source"), (), lambda h, a, p: q.metric_source(h, a["metric"])),
|
|
135
|
+
_Route(
|
|
136
|
+
(*base, "report"),
|
|
137
|
+
(("window", "str", "7d"),),
|
|
138
|
+
lambda h, a, p: q.metric_report_html(h, a["metric"], p["window"]),
|
|
139
|
+
html=True,
|
|
140
|
+
),
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def build_routes(platform: Platform | None) -> list[_Route]:
|
|
145
|
+
"""Every ``/api/v1`` route; the job routes run against *platform* when present."""
|
|
146
|
+
proj = ("projects", "{project}")
|
|
147
|
+
|
|
148
|
+
def list_jobs(h: ProjectHandle, _a: dict[str, str], p: dict[str, Any]) -> dict[str, Any]:
|
|
149
|
+
from detectkit.serve import write_tools as w # noqa: PLC0415
|
|
150
|
+
|
|
151
|
+
assert platform is not None
|
|
152
|
+
return w.list_jobs(platform, project=h.name, limit=p["limit"])
|
|
153
|
+
|
|
154
|
+
def get_job(h: ProjectHandle, a: dict[str, str], p: dict[str, Any]) -> dict[str, Any]:
|
|
155
|
+
from detectkit.serve import write_tools as w # noqa: PLC0415
|
|
156
|
+
|
|
157
|
+
assert platform is not None
|
|
158
|
+
return w.get_job(platform, project=h.name, job_id=a["job"], tail=p["tail"])
|
|
159
|
+
|
|
160
|
+
return [
|
|
161
|
+
_Route(proj, (), lambda h, a, p: q.get_server_info(h)),
|
|
162
|
+
_Route(
|
|
163
|
+
(*proj, "status"),
|
|
164
|
+
(("window", "str", "7d"), ("selector", "str", "*"), ("limit", "int", 50)),
|
|
165
|
+
lambda h, a, p: q.get_project_status(h, p["window"], p["selector"], p["limit"]),
|
|
166
|
+
),
|
|
167
|
+
_Route(
|
|
168
|
+
(*proj, "metrics"),
|
|
169
|
+
(("selector", "str", "*"),),
|
|
170
|
+
lambda h, a, p: q.list_metrics(h, p["selector"]),
|
|
171
|
+
),
|
|
172
|
+
*_metric_routes(),
|
|
173
|
+
_Route((*proj, "jobs"), (("limit", "int", 20),), list_jobs, needs_platform=True),
|
|
174
|
+
_Route((*proj, "jobs", "{job}"), (("tail", "int", 50),), get_job, needs_platform=True),
|
|
175
|
+
]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _match(routes: list[_Route], segments: list[str]) -> tuple[_Route, dict[str, str]] | None:
|
|
179
|
+
for route in routes:
|
|
180
|
+
if len(route.pattern) != len(segments):
|
|
181
|
+
continue
|
|
182
|
+
args: dict[str, str] = {}
|
|
183
|
+
for want, got in zip(route.pattern, segments, strict=True):
|
|
184
|
+
if want.startswith("{"):
|
|
185
|
+
if not got:
|
|
186
|
+
break
|
|
187
|
+
args[want[1:-1]] = got
|
|
188
|
+
elif want != got:
|
|
189
|
+
break
|
|
190
|
+
else:
|
|
191
|
+
return route, args
|
|
192
|
+
return None
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _parse_params(route: _Route, raw_query: bytes) -> dict[str, Any]:
|
|
196
|
+
allowed = {name: (kind, default) for name, kind, default in route.params}
|
|
197
|
+
values: dict[str, Any] = {name: default for name, (_k, default) in allowed.items()}
|
|
198
|
+
pairs = parse_qsl(raw_query.decode("latin-1"), keep_blank_values=True)
|
|
199
|
+
for name, value in pairs:
|
|
200
|
+
if name not in allowed:
|
|
201
|
+
accepted = ", ".join(sorted(allowed)) or "none"
|
|
202
|
+
raise InvalidRequestError(
|
|
203
|
+
f"unknown query parameter {name!r} (this route accepts: {accepted})"
|
|
204
|
+
)
|
|
205
|
+
kind, _default = allowed[name]
|
|
206
|
+
if kind == "int":
|
|
207
|
+
try:
|
|
208
|
+
values[name] = int(value)
|
|
209
|
+
except ValueError:
|
|
210
|
+
raise InvalidRequestError(f"{name} must be an integer, not {value!r}") from None
|
|
211
|
+
elif kind == "bool":
|
|
212
|
+
low = value.strip().lower()
|
|
213
|
+
if low not in _TRUE | _FALSE:
|
|
214
|
+
raise InvalidRequestError(f"{name} must be true or false, not {value!r}")
|
|
215
|
+
values[name] = low in _TRUE
|
|
216
|
+
else:
|
|
217
|
+
values[name] = value
|
|
218
|
+
return values
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _json_default(o: Any) -> Any:
|
|
222
|
+
"""Numpy scalars/arrays that slip past the serialize contract still encode."""
|
|
223
|
+
import numpy as np # noqa: PLC0415
|
|
224
|
+
|
|
225
|
+
if isinstance(o, np.generic):
|
|
226
|
+
return o.item()
|
|
227
|
+
if isinstance(o, np.ndarray):
|
|
228
|
+
return o.tolist()
|
|
229
|
+
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
async def _reply(send: Send, status: int, body: bytes, content_type: str, **extra: str) -> None:
|
|
233
|
+
headers = [
|
|
234
|
+
(b"content-type", content_type.encode()),
|
|
235
|
+
(b"content-length", str(len(body)).encode()),
|
|
236
|
+
(b"cache-control", b"no-store"),
|
|
237
|
+
]
|
|
238
|
+
headers += [(k.encode(), v.encode()) for k, v in extra.items()]
|
|
239
|
+
await send({"type": "http.response.start", "status": status, "headers": headers})
|
|
240
|
+
await send({"type": "http.response.body", "body": body})
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
async def _reply_json(send: Send, status: int, payload: Any, **extra: str) -> None:
|
|
244
|
+
body = json.dumps(payload).encode("utf-8")
|
|
245
|
+
await _reply(send, status, body, "application/json", **extra)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class ApiV1:
|
|
249
|
+
"""The ``/api/v1`` ASGI handler (mounted behind the bearer gate)."""
|
|
250
|
+
|
|
251
|
+
def __init__(
|
|
252
|
+
self,
|
|
253
|
+
registry: ProjectRegistry,
|
|
254
|
+
settings: ServeSettings,
|
|
255
|
+
*,
|
|
256
|
+
platform: Platform | None = None,
|
|
257
|
+
log: Callable[[str], None] | None = None,
|
|
258
|
+
) -> None:
|
|
259
|
+
self._registry = registry
|
|
260
|
+
self._settings = settings
|
|
261
|
+
self._platform = platform
|
|
262
|
+
self._routes = build_routes(platform)
|
|
263
|
+
self._log = log or (lambda _line: None)
|
|
264
|
+
|
|
265
|
+
def _identity(self, scope: Scope) -> str | None:
|
|
266
|
+
key = self._settings.identity_header_key
|
|
267
|
+
if key is None:
|
|
268
|
+
return None
|
|
269
|
+
raw = dict(scope.get("headers") or []).get(key.encode("latin-1"))
|
|
270
|
+
if not raw:
|
|
271
|
+
return None
|
|
272
|
+
return raw.decode("latin-1").strip() or None
|
|
273
|
+
|
|
274
|
+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
275
|
+
import anyio # noqa: PLC0415 - a dependency of the serve extra
|
|
276
|
+
|
|
277
|
+
path: str = scope.get("path") or ""
|
|
278
|
+
who = self._identity(scope) or "anonymous"
|
|
279
|
+
if scope.get("method") != "GET":
|
|
280
|
+
await _reply_json(
|
|
281
|
+
send,
|
|
282
|
+
405,
|
|
283
|
+
{"error": f"{scope.get('method')} not allowed: /api/v1 is read-only"},
|
|
284
|
+
allow="GET",
|
|
285
|
+
)
|
|
286
|
+
return
|
|
287
|
+
# Split the *raw* path, then decode each segment: ASGI's ``path`` is already
|
|
288
|
+
# percent-decoded, so a name holding an encoded "/" would split wrongly.
|
|
289
|
+
raw = scope.get("raw_path") or path.encode("utf-8")
|
|
290
|
+
rest = raw.decode("latin-1").split("?", 1)[0][len(PREFIX) :].strip("/")
|
|
291
|
+
segments = [unquote(s) for s in rest.split("/")] if rest else []
|
|
292
|
+
self._log(f" [serve] GET {path} actor={who}")
|
|
293
|
+
try:
|
|
294
|
+
if segments == ["projects"]:
|
|
295
|
+
_parse_params(_NO_PARAMS, scope.get("query_string") or b"")
|
|
296
|
+
await _reply_json(send, 200, self._registry.list_projects())
|
|
297
|
+
return
|
|
298
|
+
matched = _match(self._routes, segments)
|
|
299
|
+
if matched is None:
|
|
300
|
+
raise NotFoundError(f"no such endpoint: {path}")
|
|
301
|
+
route, args = matched
|
|
302
|
+
params = _parse_params(route, scope.get("query_string") or b"")
|
|
303
|
+
handle = self._registry.get(args["project"])
|
|
304
|
+
if route.needs_platform and self._platform is None:
|
|
305
|
+
raise NotFoundError("jobs exist only when the server runs with --enable-writes")
|
|
306
|
+
with anyio.fail_after(self._settings.tool_timeout):
|
|
307
|
+
result = await anyio.to_thread.run_sync(
|
|
308
|
+
functools.partial(route.run, handle, args, params), abandon_on_cancel=True
|
|
309
|
+
)
|
|
310
|
+
if route.html:
|
|
311
|
+
body = str(result).encode("utf-8")
|
|
312
|
+
content_type = "text/html; charset=utf-8"
|
|
313
|
+
else:
|
|
314
|
+
# allow_nan=False: a NaN must fail loudly here, not reach a client as `NaN`.
|
|
315
|
+
body = json.dumps(result, default=_json_default, allow_nan=False).encode()
|
|
316
|
+
content_type = "application/json"
|
|
317
|
+
except ControlError as exc:
|
|
318
|
+
await _reply_json(send, exc.http_status, {"error": str(exc)})
|
|
319
|
+
return
|
|
320
|
+
except TimeoutError:
|
|
321
|
+
await _reply_json(
|
|
322
|
+
send,
|
|
323
|
+
504,
|
|
324
|
+
{
|
|
325
|
+
"error": f"the request did not finish within "
|
|
326
|
+
f"{self._settings.tool_timeout:g}s; narrow it (a smaller window or "
|
|
327
|
+
"limit) and retry"
|
|
328
|
+
},
|
|
329
|
+
)
|
|
330
|
+
return
|
|
331
|
+
except Exception as exc: # noqa: BLE001 - an API must answer, not drop the connection
|
|
332
|
+
self._log(f" [serve] 500 {path}: {type(exc).__name__}: {exc}")
|
|
333
|
+
await _reply_json(send, 500, {"error": f"internal error: {type(exc).__name__}"})
|
|
334
|
+
return
|
|
335
|
+
await _reply(send, 200, body, content_type)
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
"""The ``dtk serve`` ASGI app: ``/health`` + the bearer-guarded ``/mcp``.
|
|
1
|
+
"""The ``dtk serve`` ASGI app: ``/health`` + the bearer-guarded ``/mcp`` and ``/api/v1``.
|
|
2
2
|
|
|
3
3
|
One process, one address. Phase 2 of RFC 0001 serves the read-only MCP endpoint;
|
|
4
|
-
|
|
4
|
+
phase 4a adds ``/api/v1``, the same reads as JSON over plain HTTP
|
|
5
|
+
(:mod:`detectkit.serve.api_v1`), behind the same bearer; phase 4b adds ``/ui``,
|
|
6
|
+
the ``dtk ui`` frontend reading that API (:mod:`detectkit.serve.web`).
|
|
5
7
|
|
|
6
|
-
**Auth: the trusted gateway.** ``/mcp``
|
|
8
|
+
**Auth: the trusted gateway.** ``/mcp``, ``/api/v1`` and ``/ui`` require ``Authorization: Bearer <token>``
|
|
7
9
|
matching ``DTK_SERVE_MCP_TOKEN`` (compared in constant time) — the credential the
|
|
8
10
|
MCP gateway presents. The gateway authenticates the *person* and forwards their
|
|
9
11
|
identity in the configured header, which this service then trusts. That is safe
|
|
@@ -24,8 +26,12 @@ from typing import TYPE_CHECKING, Any
|
|
|
24
26
|
|
|
25
27
|
from detectkit import __version__
|
|
26
28
|
from detectkit.control.registry import ProjectRegistry
|
|
29
|
+
from detectkit.serve.api_v1 import PREFIX as API_PREFIX
|
|
30
|
+
from detectkit.serve.api_v1 import ApiV1
|
|
27
31
|
from detectkit.serve.mcp_app import build_hosted_mcp
|
|
28
32
|
from detectkit.serve.settings import ServeSettings
|
|
33
|
+
from detectkit.serve.web import PREFIX as WEB_PREFIX
|
|
34
|
+
from detectkit.serve.web import Web
|
|
29
35
|
|
|
30
36
|
if TYPE_CHECKING:
|
|
31
37
|
from detectkit.serve.platform import Platform
|
|
@@ -49,9 +55,18 @@ class _Gate:
|
|
|
49
55
|
"""Pure-ASGI front: ``/health`` unauthenticated, everything else behind the bearer."""
|
|
50
56
|
|
|
51
57
|
def __init__(
|
|
52
|
-
self,
|
|
58
|
+
self,
|
|
59
|
+
app: ASGIApp,
|
|
60
|
+
registry: ProjectRegistry,
|
|
61
|
+
token: str | None,
|
|
62
|
+
*,
|
|
63
|
+
writes: bool,
|
|
64
|
+
api: ASGIApp | None = None,
|
|
65
|
+
web: ASGIApp | None = None,
|
|
53
66
|
) -> None:
|
|
54
67
|
self._app = app
|
|
68
|
+
self._api = api
|
|
69
|
+
self._web = web
|
|
55
70
|
self._registry = registry
|
|
56
71
|
self._expected = f"Bearer {token}".encode() if token else None
|
|
57
72
|
self._writes = writes
|
|
@@ -82,6 +97,13 @@ class _Gate:
|
|
|
82
97
|
**{"www-authenticate": 'Bearer realm="detectkit"'},
|
|
83
98
|
)
|
|
84
99
|
return
|
|
100
|
+
path = scope.get("path") or ""
|
|
101
|
+
if self._api is not None and (path == API_PREFIX or path.startswith(API_PREFIX + "/")):
|
|
102
|
+
await self._api(scope, receive, send)
|
|
103
|
+
return
|
|
104
|
+
if self._web is not None and (path == WEB_PREFIX or path.startswith(WEB_PREFIX + "/")):
|
|
105
|
+
await self._web(scope, receive, send)
|
|
106
|
+
return
|
|
85
107
|
await self._app(scope, receive, send)
|
|
86
108
|
|
|
87
109
|
|
|
@@ -120,7 +142,9 @@ def build_app(
|
|
|
120
142
|
|
|
121
143
|
inner.router.lifespan_context = lifespan
|
|
122
144
|
token = None if settings.insecure_no_auth else settings.token
|
|
123
|
-
|
|
145
|
+
api = ApiV1(registry, settings, platform=platform, log=log)
|
|
146
|
+
web = Web(registry, settings, log=log)
|
|
147
|
+
return _Gate(inner, registry, token, writes=platform is not None, api=api, web=web)
|
|
124
148
|
|
|
125
149
|
|
|
126
150
|
def _build_platform(
|
|
@@ -33,7 +33,7 @@ def run_serve(settings: ServeSettings, *, echo: Callable[[str], None] | None = N
|
|
|
33
33
|
auth = "none (insecure, loopback only)" if settings.insecure_no_auth else "bearer"
|
|
34
34
|
mode = "writes enabled" if settings.enable_writes else "read-only"
|
|
35
35
|
say(
|
|
36
|
-
f"dtk serve: http://{settings.host}:{settings.port} /mcp ({mode}) /health "
|
|
36
|
+
f"dtk serve: http://{settings.host}:{settings.port} /mcp ({mode}) /api/v1 (read-only) /ui (read-only) /health "
|
|
37
37
|
f"projects={','.join(registry.names)} auth={auth} "
|
|
38
38
|
f"identity_header={settings.identity_header or '-'}"
|
|
39
39
|
)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""The platform's web pages (RFC 0001 phase 4b, read side).
|
|
2
|
+
|
|
3
|
+
``dtk serve`` serves the ``dtk ui`` frontend — the same committed bundle — in its
|
|
4
|
+
*platform* mode: the page reads ``/api/v1`` instead of the local ``dtk ui``
|
|
5
|
+
routes, switches between projects, and offers no write action (writes reach the
|
|
6
|
+
web in phase 4c). One page per project, so a link names what it shows::
|
|
7
|
+
|
|
8
|
+
GET /ui -> 307 to the first project's page
|
|
9
|
+
GET /ui/{project} the overview + detail for one project (?window= presets)
|
|
10
|
+
|
|
11
|
+
The page sits behind the same bearer gate as ``/mcp`` and ``/api/v1``, and every
|
|
12
|
+
read it makes goes through ``/api/v1``: a browser reaches it through a front
|
|
13
|
+
proxy that adds the bearer (or with ``--insecure-no-auth`` on loopback, for a
|
|
14
|
+
trial). The person's own identity is phase 4e.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import functools
|
|
20
|
+
from collections.abc import Callable, MutableMapping
|
|
21
|
+
from typing import Any
|
|
22
|
+
from urllib.parse import parse_qsl, quote, unquote
|
|
23
|
+
|
|
24
|
+
from detectkit import __version__
|
|
25
|
+
from detectkit.control import queries as q
|
|
26
|
+
from detectkit.control.errors import ControlError, NotFoundError
|
|
27
|
+
from detectkit.control.overview import ALL_WINDOW_PRESETS
|
|
28
|
+
from detectkit.control.registry import ProjectRegistry
|
|
29
|
+
from detectkit.serve.api_v1 import PREFIX as API_PREFIX
|
|
30
|
+
from detectkit.serve.settings import ServeSettings
|
|
31
|
+
from detectkit.utils.datetime_utils import now_utc_naive
|
|
32
|
+
|
|
33
|
+
Scope = MutableMapping[str, Any]
|
|
34
|
+
|
|
35
|
+
PREFIX = "/ui"
|
|
36
|
+
DEFAULT_WINDOW = "7d"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def boot_payload(registry: ProjectRegistry, project: str, window: str) -> dict[str, Any]:
|
|
40
|
+
"""The page's boot data: the project's metric list plus where its reads live.
|
|
41
|
+
|
|
42
|
+
Built from :func:`~detectkit.control.queries.list_metrics` — config only, no
|
|
43
|
+
state database — so the page renders its table at once and fills each row
|
|
44
|
+
from ``/api/v1/…/status``.
|
|
45
|
+
"""
|
|
46
|
+
handle = registry.get(project)
|
|
47
|
+
listing = q.list_metrics(handle)
|
|
48
|
+
metrics = [
|
|
49
|
+
{
|
|
50
|
+
"name": m["name"],
|
|
51
|
+
"dir": m["dir"],
|
|
52
|
+
"file": m["file"],
|
|
53
|
+
"tags": m["tags"],
|
|
54
|
+
"interval_seconds": m["interval_seconds"],
|
|
55
|
+
"enabled": m["enabled"],
|
|
56
|
+
}
|
|
57
|
+
for m in listing["metrics"]
|
|
58
|
+
]
|
|
59
|
+
return {
|
|
60
|
+
"project": project,
|
|
61
|
+
"initial_window": window,
|
|
62
|
+
"version": __version__,
|
|
63
|
+
"metrics": metrics,
|
|
64
|
+
"generated_at": int(now_utc_naive().timestamp() * 1000),
|
|
65
|
+
"platform": {
|
|
66
|
+
"projects": list(registry.names),
|
|
67
|
+
"api_base": f"{API_PREFIX}/projects/{quote(project, safe='')}",
|
|
68
|
+
"ui_base": f"{PREFIX}/",
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _window(query_string: bytes) -> str:
|
|
74
|
+
"""``?window=`` when it names a preset; anything else falls back to the default."""
|
|
75
|
+
for key, value in parse_qsl(query_string.decode("latin-1")):
|
|
76
|
+
if key == "window" and value in ALL_WINDOW_PRESETS:
|
|
77
|
+
return value
|
|
78
|
+
return DEFAULT_WINDOW
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class Web:
|
|
82
|
+
"""The ``/ui`` ASGI handler (mounted behind the bearer gate)."""
|
|
83
|
+
|
|
84
|
+
def __init__(
|
|
85
|
+
self,
|
|
86
|
+
registry: ProjectRegistry,
|
|
87
|
+
settings: ServeSettings,
|
|
88
|
+
*,
|
|
89
|
+
log: Callable[[str], None] | None = None,
|
|
90
|
+
) -> None:
|
|
91
|
+
self._registry = registry
|
|
92
|
+
self._settings = settings
|
|
93
|
+
self._log = log or (lambda _line: None)
|
|
94
|
+
|
|
95
|
+
def _identity(self, scope: Scope) -> str | None:
|
|
96
|
+
key = self._settings.identity_header_key
|
|
97
|
+
if key is None:
|
|
98
|
+
return None
|
|
99
|
+
raw = dict(scope.get("headers") or []).get(key.encode("latin-1"))
|
|
100
|
+
return (raw.decode("latin-1").strip() or None) if raw else None
|
|
101
|
+
|
|
102
|
+
async def __call__(self, scope: Scope, receive: Any, send: Any) -> None:
|
|
103
|
+
import anyio # noqa: PLC0415 - a dependency of the serve extra
|
|
104
|
+
|
|
105
|
+
from detectkit.ui.html import render_ui_html # noqa: PLC0415
|
|
106
|
+
|
|
107
|
+
path: str = scope.get("path") or ""
|
|
108
|
+
if scope.get("method") != "GET":
|
|
109
|
+
await _reply(send, 405, b"method not allowed", "text/plain", allow="GET")
|
|
110
|
+
return
|
|
111
|
+
raw = scope.get("raw_path") or path.encode("utf-8")
|
|
112
|
+
rest = raw.decode("latin-1").split("?", 1)[0][len(PREFIX) :].strip("/")
|
|
113
|
+
segments = [unquote(s) for s in rest.split("/")] if rest else []
|
|
114
|
+
query = scope.get("query_string") or b""
|
|
115
|
+
self._log(f" [serve] GET {path} actor={self._identity(scope) or 'anonymous'}")
|
|
116
|
+
|
|
117
|
+
if not segments:
|
|
118
|
+
names = self._registry.names
|
|
119
|
+
if not names:
|
|
120
|
+
await _reply(send, 404, b"no projects are served", "text/plain")
|
|
121
|
+
return
|
|
122
|
+
target = f"{PREFIX}/{quote(names[0], safe='')}"
|
|
123
|
+
if query:
|
|
124
|
+
target += "?" + query.decode("latin-1")
|
|
125
|
+
await _reply(send, 307, b"", "text/plain", location=target)
|
|
126
|
+
return
|
|
127
|
+
if len(segments) != 1:
|
|
128
|
+
await _reply(send, 404, f"no such page: {path}".encode(), "text/plain")
|
|
129
|
+
return
|
|
130
|
+
|
|
131
|
+
project = segments[0]
|
|
132
|
+
try:
|
|
133
|
+
with anyio.fail_after(self._settings.tool_timeout):
|
|
134
|
+
payload = await anyio.to_thread.run_sync(
|
|
135
|
+
functools.partial(boot_payload, self._registry, project, _window(query)),
|
|
136
|
+
abandon_on_cancel=True,
|
|
137
|
+
)
|
|
138
|
+
except NotFoundError as exc:
|
|
139
|
+
await _reply(send, 404, str(exc).encode(), "text/plain; charset=utf-8")
|
|
140
|
+
return
|
|
141
|
+
except ControlError as exc:
|
|
142
|
+
await _reply(send, exc.http_status, str(exc).encode(), "text/plain; charset=utf-8")
|
|
143
|
+
return
|
|
144
|
+
except TimeoutError:
|
|
145
|
+
await _reply(send, 504, b"the page did not build in time", "text/plain")
|
|
146
|
+
return
|
|
147
|
+
except Exception as exc: # noqa: BLE001 - a page must answer, not drop the connection
|
|
148
|
+
self._log(f" [serve] 500 {path}: {type(exc).__name__}: {exc}")
|
|
149
|
+
await _reply(send, 500, f"internal error: {type(exc).__name__}".encode(), "text/plain")
|
|
150
|
+
return
|
|
151
|
+
await _reply(send, 200, render_ui_html(payload).encode("utf-8"), "text/html; charset=utf-8")
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
async def _reply(send: Any, status: int, body: bytes, content_type: str, **extra: str) -> None:
|
|
155
|
+
headers = [
|
|
156
|
+
(b"content-type", content_type.encode()),
|
|
157
|
+
(b"content-length", str(len(body)).encode()),
|
|
158
|
+
(b"cache-control", b"no-store"),
|
|
159
|
+
]
|
|
160
|
+
headers += [(k.encode(), v.encode("latin-1")) for k, v in extra.items()]
|
|
161
|
+
await send({"type": "http.response.start", "status": status, "headers": headers})
|
|
162
|
+
await send({"type": "http.response.body", "body": body})
|