Repository navigation
feat(metrics): add metric evidence drilldowns #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
78403be
feat(metrics): add metric evidence drilldowns
aleksdotbar 2c8fb36
fix(metrics): widen evidence reference column
aleksdotbar 83024b6
Merge origin/main into feat/metric-drilldown
aleksdotbar 74743f0
fix(metrics): harden evidence downloads
aleksdotbar 0a2b81d
fix(metrics): stabilize evidence dialog lifecycle
aleksdotbar 2309143
fix(metrics): preserve details during close transition
aleksdotbar 2d9cedb
fix(metrics): retain export download URLs
aleksdotbar ee90d60
Merge remote-tracking branch 'origin/main' into feat/metric-drilldown
aleksdotbar aa59a04
fix(metrics): preserve missing data presentation
aleksdotbar 1de440c
fix(metrics): harden evidence interactions
aleksdotbar 183f2cc
Merge remote-tracking branch 'origin/main' into feat/metric-drilldown
aleksdotbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,110 @@ | ||
| import { AnalyticsApiError } from "@/api/analytics-client"; | ||
| import { fetchWithAuth } from "@/api/fetch-with-auth"; | ||
| import type { | ||
| MetricCanonicalSelection, | ||
| MetricDimensionFilter, | ||
| } from "@/api/metric-results-client"; | ||
| import { downloadBlob } from "@/lib/download"; | ||
|
|
||
| const BASE = | ||
| (import.meta.env.VITE_API_BASE as string | undefined) ?? "/api/analytics/v1"; | ||
|
|
||
| export interface MetricEvidenceSelection { | ||
| metric_key: string; | ||
| entity: { type: "person"; id: string }; | ||
| period: { from: string; to: string }; | ||
| filters: MetricDimensionFilter[]; | ||
| display_dimensions: string[]; | ||
| } | ||
|
|
||
| export interface MetricEvidenceColumn { | ||
| key: string; | ||
| label: string; | ||
| type: "string" | "number" | "date"; | ||
| } | ||
|
|
||
| export interface MetricEvidenceRow { | ||
| values: Record<string, unknown>; | ||
| } | ||
|
|
||
| export interface MetricDrilldownResponse { | ||
| selection: MetricEvidenceSelection; | ||
| columns: MetricEvidenceColumn[]; | ||
| rows: MetricEvidenceRow[]; | ||
| next_cursor: string | null; | ||
| } | ||
|
|
||
| export interface MetricDrilldownRequest extends MetricEvidenceSelection { | ||
| cursor?: string; | ||
| limit: number; | ||
| } | ||
|
|
||
| async function errorFor(res: Response): Promise<AnalyticsApiError> { | ||
| const body = await res.json().catch(() => null); | ||
| return new AnalyticsApiError(res.status, body); | ||
| } | ||
|
|
||
| function exportFilename(disposition: string | null, fallback: string): string { | ||
| const encoded = disposition?.match(/filename\*=UTF-8''([^;]+)/i)?.[1]; | ||
| if (encoded) { | ||
| try { | ||
| return decodeURIComponent(encoded); | ||
| } catch { | ||
| return disposition?.match(/filename="?([^";]+)"?/i)?.[1] ?? fallback; | ||
| } | ||
| } | ||
| return disposition?.match(/filename="?([^";]+)"?/i)?.[1] ?? fallback; | ||
| } | ||
|
|
||
| export async function queryMetricDrilldown( | ||
| request: MetricDrilldownRequest, | ||
| signal?: AbortSignal | ||
| ): Promise<MetricDrilldownResponse> { | ||
| const res = await fetchWithAuth(`${BASE}/metric-drilldown`, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify(request), | ||
| signal, | ||
| }); | ||
| if (!res.ok) throw await errorFor(res); | ||
| return (await res.json()) as MetricDrilldownResponse; | ||
| } | ||
|
|
||
| export async function downloadMetricDrilldown( | ||
| selection: MetricEvidenceSelection, | ||
| format: "csv" | "xlsx", | ||
| signal?: AbortSignal | ||
| ): Promise<void> { | ||
| const res = await fetchWithAuth(`${BASE}/metric-drilldown/export`, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ ...selection, format }), | ||
| signal, | ||
| }); | ||
| if (!res.ok) throw await errorFor(res); | ||
| const blob = await res.blob(); | ||
| downloadBlob( | ||
| blob, | ||
| exportFilename( | ||
| res.headers.get("content-disposition"), | ||
| `${selection.metric_key}.${format}` | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| export function evidenceSelection( | ||
| canonical: MetricCanonicalSelection | undefined, | ||
| entityId: string, | ||
| period?: { from: string; to: string }, | ||
| filters?: MetricDimensionFilter[], | ||
| displayDimensions: string[] = [] | ||
| ): MetricEvidenceSelection | null { | ||
| if (!canonical) return null; | ||
| return { | ||
| metric_key: canonical.metric_key, | ||
| entity: { type: "person", id: entityId }, | ||
| period: period ?? canonical.period, | ||
| filters: filters ?? canonical.filters, | ||
| display_dimensions: [...new Set(displayDimensions)].sort(), | ||
| }; | ||
| } | ||
This file contains hidden or 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 hidden or 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,13 @@ | ||
| import type { Session } from "@/auth/types"; | ||
|
|
||
| export function sessionAuthorizationScope( | ||
| session: Session | null | ||
| ): string | null { | ||
| if (!session) return null; | ||
| return JSON.stringify({ | ||
| tenantId: session.tenantId, | ||
| personId: session.personId, | ||
| impersonatorEmail: session.impersonatorEmail, | ||
| roles: [...session.roles].sort(), | ||
| }); | ||
| } |
This file contains hidden or 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,42 @@ | ||
| import { createContext, useContext } from "react"; | ||
|
|
||
| import type { MetricEvidenceSelection } from "@/api/metric-drilldown-client"; | ||
|
|
||
| export interface EvidenceDialogTarget { | ||
| selection: MetricEvidenceSelection; | ||
| label: string; | ||
| } | ||
|
|
||
| export interface EvidenceDialogState { | ||
| targets: readonly [EvidenceDialogTarget, ...EvidenceDialogTarget[]]; | ||
| activeMetricKey: string; | ||
| title?: string; | ||
| } | ||
|
|
||
| export interface EvidenceDialogContextValue { | ||
| openEvidence: (selection: MetricEvidenceSelection, label: string) => void; | ||
| openEvidenceTargets: ( | ||
| targets: readonly EvidenceDialogTarget[], | ||
| title?: string | ||
| ) => void; | ||
| } | ||
|
|
||
| export const EvidenceDialogContext = createContext< | ||
| EvidenceDialogContextValue | undefined | ||
| >(undefined); | ||
|
|
||
| export function useMetricEvidence(): EvidenceDialogContextValue { | ||
| const context = useContext(EvidenceDialogContext); | ||
| if (!context) { | ||
| throw new Error( | ||
| "useMetricEvidence must be used within MetricEvidenceDialogProvider" | ||
| ); | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| export function useMetricEvidenceOptional(): | ||
| | EvidenceDialogContextValue | ||
| | undefined { | ||
| return useContext(EvidenceDialogContext); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.