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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
__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: dict[str, str] | str | None = None, time_window: tuple[str, str] | None = None, period_type: PeriodType = '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
|
dict[str, str] | str | None
|
Segment to apply. Two forms are accepted:
Only one segment per |
None
|
time_window
|
tuple[str, str] | None
|
(start_date, end_date) ISO strings for period filter.
Requires |
None
|
period_type
|
PeriodType
|
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 more than one segment is provided. |
QueryBuildError
|
if the join key in |
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. |
Source code in aitaem/insights.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
scan
Introspect source schemas and return a compatibility matrix for all loaded specs.
For each loaded metric, checks every loaded slice and segment:
- Slice: compatible when all referenced columns exist in the metric's source table.
- Segment: compatible when at least one join key (from
join_keys, orentity_idwhenjoin_keysis empty) exists in the metric's source table.
Schema introspection is batched by unique source URI — each table is queried once regardless of how many metrics share it. Metrics whose source connection is unavailable are skipped with a warning.
Returns:
| Type | Description |
|---|---|
ScanResult
|
ScanResult with one CompatibilityResult per metric × slice and per metric × segment. |
Source code in aitaem/insights.py
scan()
Introspects source table schemas and returns a compatibility matrix for all specs loaded in the
SpecCache. For each metric, every loaded slice and segment is checked:
- Slice — compatible when all columns referenced in
values[].where(or the barecolumnfield for wildcard slices) exist in the metric's source table. Composite slices are resolved transitively via their component leaf/wildcard slices. - Segment — compatible when at least one join-key candidate exists in the metric's source
table. Candidates are taken from
join_keys(if non-empty) orentity_id. Only the fact-table side is checked; DIM-table columns are not.
Schema introspection is batched by unique source URI — each table is queried once regardless of how many metrics share it. Metrics whose source connection is unavailable are skipped with a warning; all other metrics are still processed.
Returns a ScanResult with one CompatibilityResult per metric × slice and per metric × segment.
See the Specs API reference for field descriptions.