ok metrics are working again

This commit is contained in:
dal 2025-08-15 16:18:14 -06:00
parent 3f3b9233f3
commit d11c62446a
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
4 changed files with 31 additions and 21 deletions

View File

@ -193,8 +193,8 @@ async function processDashboardFile(file: { id: string; yml_content: string }):
} }
// Collect all metric IDs from rows if they exist // Collect all metric IDs from rows if they exist
const metricIds: string[] = dashboard.config.rows const metricIds: string[] = dashboard.rows
? dashboard.config.rows.flatMap((row) => row.items).map((item) => item.id) ? dashboard.rows.flatMap((row) => row.items).map((item) => item.id)
: []; : [];
// Validate metric IDs if any exist // Validate metric IDs if any exist
@ -226,7 +226,7 @@ async function processDashboardFile(file: { id: string; yml_content: string }):
created_at: existingFile.createdAt, created_at: existingFile.createdAt,
updated_at: new Date().toISOString(), updated_at: new Date().toISOString(),
version_number: latestVersion, version_number: latestVersion,
content: dashboard.config, // Store the DashboardConfig directly content: { rows: dashboard.rows }, // Extract the config properties
}; };
return { return {
@ -346,7 +346,7 @@ const modifyDashboardFiles = wrapTraced(
await tx await tx
.update(dashboardFiles) .update(dashboardFiles)
.set({ .set({
content: sp.dashboard.config as DashboardConfig, content: { rows: sp.dashboard.rows } as DashboardConfig,
updatedAt: sp.dashboardFile.updated_at, updatedAt: sp.dashboardFile.updated_at,
versionHistory: updatedVersionHistory, versionHistory: updatedVersionHistory,
name: sp.dashboard.name, name: sp.dashboard.name,
@ -355,8 +355,8 @@ const modifyDashboardFiles = wrapTraced(
.execute(); .execute();
// Update metric associations // Update metric associations
const newMetricIds = sp.dashboard.config.rows const newMetricIds = sp.dashboard.rows
? sp.dashboard.config.rows.flatMap((row) => row.items).map((item) => item.id) ? sp.dashboard.rows.flatMap((row) => row.items).map((item) => item.id)
: []; : [];
const existingAssociations = await tx const existingAssociations = await tx

View File

@ -3,9 +3,10 @@ import type { DataSource } from '@buster/data-source';
import { assetPermissions, db, metricFiles, updateMessageEntries } from '@buster/database'; import { assetPermissions, db, metricFiles, updateMessageEntries } from '@buster/database';
import { import {
type ChartConfigProps, type ChartConfigProps,
ChartConfigPropsSchema,
type ColumnMetaData, type ColumnMetaData,
type DataMetadata, type DataMetadata,
type MetricYml,
MetricYmlSchema,
} from '@buster/server-shared/metrics'; } from '@buster/server-shared/metrics';
import { wrapTraced } from 'braintrust'; import { wrapTraced } from 'braintrust';
import * as yaml from 'yaml'; import * as yaml from 'yaml';
@ -51,7 +52,7 @@ interface MetricFileResult {
success: boolean; success: boolean;
error?: string; error?: string;
metricFile?: FileWithId; metricFile?: FileWithId;
metricYml?: ChartConfigProps; metricYml?: MetricYml;
message?: string; message?: string;
results?: Record<string, unknown>[]; results?: Record<string, unknown>[];
} }
@ -59,13 +60,10 @@ interface MetricFileResult {
type VersionHistory = (typeof metricFiles.$inferSelect)['versionHistory']; type VersionHistory = (typeof metricFiles.$inferSelect)['versionHistory'];
// Helper function to create initial version history // Helper function to create initial version history
function createInitialMetricVersionHistory( function createInitialMetricVersionHistory(metric: MetricYml, createdAt: string): VersionHistory {
metric: ChartConfigProps,
createdAt: string
): VersionHistory {
return { return {
'1': { '1': {
content: JSON.stringify(metric), content: metric,
updated_at: createdAt, updated_at: createdAt,
version_number: 1, version_number: 1,
}, },
@ -219,12 +217,12 @@ async function processMetricFile(
// Parse and validate YAML // Parse and validate YAML
const metricYml = yaml.parse(fixedYmlContent); const metricYml = yaml.parse(fixedYmlContent);
const validatedMetricYml = ChartConfigPropsSchema.parse(metricYml); const validatedMetricYml = MetricYmlSchema.parse(metricYml);
// Validate and adjust bar/line chart axes // Validate and adjust bar/line chart axes
let finalMetricYml: ChartConfigProps; let finalChartConfig: ChartConfigProps;
try { try {
finalMetricYml = validateAndAdjustBarLineAxes(validatedMetricYml); finalChartConfig = validateAndAdjustBarLineAxes(validatedMetricYml.chartConfig);
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
@ -232,12 +230,18 @@ async function processMetricFile(
}; };
} }
// Create the final metric YML with the adjusted chart config
const finalMetricYml: MetricYml = {
...validatedMetricYml,
chartConfig: finalChartConfig,
};
// Use provided metric ID from state or generate new one // Use provided metric ID from state or generate new one
const id = metricId || randomUUID(); const id = metricId || randomUUID();
// Validate SQL by running it // Validate SQL by running it
const sqlValidationResult = await validateSql( const sqlValidationResult = await validateSql(
metricYml.sql, finalMetricYml.sql,
dataSourceId, dataSourceId,
userId, userId,
dataSourceDialect dataSourceDialect
@ -254,7 +258,7 @@ async function processMetricFile(
const now = new Date().toISOString(); const now = new Date().toISOString();
const metricFile: FileWithId = { const metricFile: FileWithId = {
id, id,
name: metricYml.name, name: finalMetricYml.name,
file_type: 'metric', file_type: 'metric',
result_message: sqlValidationResult.message || '', result_message: sqlValidationResult.message || '',
results: sqlValidationResult.results || [], results: sqlValidationResult.results || [],
@ -552,7 +556,7 @@ const createMetricFiles = wrapTraced(
const successfulProcessing: Array<{ const successfulProcessing: Array<{
fileName: string; fileName: string;
metricFile: FileWithId; metricFile: FileWithId;
metricYml: ChartConfigProps; metricYml: MetricYml;
message: string; message: string;
results: Record<string, unknown>[]; results: Record<string, unknown>[];
}> = []; }> = [];

View File

@ -74,7 +74,7 @@ function addMetricVersionToHistory(
return { return {
...history, ...history,
[nextVersion.toString()]: { [nextVersion.toString()]: {
content: JSON.stringify(metric), content: metric,
updated_at: updatedAt, updated_at: updatedAt,
version_number: nextVersion, version_number: nextVersion,
}, },

View File

@ -1285,7 +1285,13 @@ export const metricFiles = pgTable(
Record< Record<
string, //version number as a string string, //version number as a string
{ {
content: string; content: string | {
name: string;
description: string;
timeFrame: string;
sql: string;
chartConfig: Record<string, unknown>;
};
updated_at: string; updated_at: string;
version_number: number; version_number: number;
} }