MetricCompute
aitaem.insights.MetricCompute
Compute metrics from a SpecCache and ConnectionManager.
Primary user interface for aitaem. Resolves specs, builds SQL queries, executes them, and returns a standardized pandas DataFrame.
Source code in aitaem/insights.py
__init__
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec_cache
|
SpecCache
|
Loaded and validated metric, slice, and segment specs. |
required |
connection_manager
|
ConnectionManager
|
Backend connections for query execution. |
required |
Source code in aitaem/insights.py
compute
compute(metrics: str | list[str], slices: str | list[str] | None = None, segments: str | list[str] | None = None, time_window: tuple[str, str] | None = None, period_type: str = 'all_time', by_entity: str | None = None, output_format: str = 'pandas') -> pd.DataFrame
Compute one or more metrics with optional slicing and segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
str | list[str]
|
Metric name(s) to compute. |
required |
slices
|
str | list[str] | None
|
Slice name(s). Each slice is computed independently. |
None
|
segments
|
str | list[str] | None
|
Segment name(s). Each segment is computed independently. |
None
|
time_window
|
tuple[str, str] | None
|
(start_date, end_date) ISO strings for period filter.
Requires |
None
|
period_type
|
str
|
Granularity for time grouping. One of 'all_time', 'daily', 'weekly', 'monthly', 'yearly'. Non-'all_time' requires time_window and timestamp_col on every metric spec. |
'all_time'
|
by_entity
|
str | None
|
Column name to group by for entity-level metrics. When set,
every requested metric must list this column in its |
None
|
output_format
|
str
|
Output format — only 'pandas' is supported in Phase 1. |
'pandas'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: period_type, period_start_date, period_end_date, |
DataFrame
|
entity_id, metric_name, slice_type, slice_value, segment_name, segment_value, |
DataFrame
|
metric_value. |
Raises:
| Type | Description |
|---|---|
SpecNotFoundError
|
if any metric/slice/segment name is not in the cache. |
QueryBuildError
|
if time_window is set but a metric has no timestamp_col. |
QueryBuildError
|
if period_type is invalid or missing required time_window. |
QueryBuildError
|
if by_entity is set but a metric does not list it in entities. |
QueryExecutionError
|
if all query groups fail to execute. |