@@ -2,14 +2,16 @@ import { DashboardInfo, DashboardMetabaseState } from './types';
22import _ , { forEach , reduce , template , values } from 'lodash' ;
33import { MetabaseAppStateDashboard , MetabaseAppStateType } from '../DOMToState' ;
44import { getTablesWithFields } from '../getDatabaseSchema' ;
5- import { getDatabaseInfo , getFieldResolvedName } from '../metabaseAPIHelpers' ;
5+ import { getAllRelevantModelsForSelectedDb , getDatabaseInfo , getFieldResolvedName } from '../metabaseAPIHelpers' ;
66import { getDashboardState , getSelectedDbId } from '../metabaseStateAPI' ;
77import { getParsedIframeInfo , RPCs } from 'web' ;
88import { getSQLFromMBQL } from '../metabaseAPI' ;
99import { metabaseToMarkdownTable } from '../operations' ;
1010import { find , get } from 'lodash' ;
1111import { getTablesFromSqlRegex , TableAndSchema } from '../parseSql' ;
1212import { getTableContextYAML } from '../catalog' ;
13+ import { MetabaseModel } from '../metabaseAPITypes' ;
14+ import { getModelsFromSql , getModelsWithFields , modifySqlForMetabaseModels , replaceLLMFriendlyIdentifiersInSqlWithModels } from '../metabaseModels' ;
1315
1416// Removed: const { getMetabaseState } = RPCs - using centralized state functions instead
1517
@@ -194,7 +196,8 @@ async function substituteParameters(
194196 sql : string ,
195197 dashcard : DashboardMetabaseState [ 'dashcards' ] [ 0 ] ,
196198 dashboardParamFields : DashboardMetabaseState [ 'dashboards' ] [ 0 ] [ 'param_fields' ] ,
197- parameterValues : DashboardMetabaseState [ 'parameterValues' ] ) {
199+ parameterValues : DashboardMetabaseState [ 'parameterValues' ]
200+ ) {
198201 // Algo:
199202 // transitivity is: template-tags -> dashcard parameters -> dashcard parameter mappings -> dashboard parameters -> parameter values
200203 // |-> parameter values
@@ -268,9 +271,9 @@ export async function getDashboardAppState(): Promise<MetabaseAppStateDashboard
268271 }
269272 const selectedTabDashcardIds = getSelectedTabDashcardIds ( dashboardMetabaseState ) ;
270273// const dashboardParameters = _.get(dashboardMetabaseState, ['dashboards', dashboardId, 'parameters'], [])
271- const cards = await Promise . all ( selectedTabDashcardIds . map ( async dashcardId => await getDashcardInfoWithSQLAndOutputTableMd ( dashboardMetabaseState , dashcardId , dashboardId ) ) )
272-
273- const filteredCards = _ . compact ( cards ) ;
274+ const allModels = dbId ? await getAllRelevantModelsForSelectedDb ( dbId ) : [ ]
275+ const cards = await Promise . all ( selectedTabDashcardIds . map ( async dashcardId => await getDashcardInfoWithSQLAndOutputTableMd ( dashboardMetabaseState , dashcardId , dashboardId , allModels ) ) )
276+ let filteredCards = _ . compact ( cards ) ;
274277 let sqlTables : TableAndSchema [ ] = [ ]
275278 forEach ( filteredCards , ( card ) => {
276279 if ( card ) {
@@ -284,9 +287,26 @@ export async function getDashboardAppState(): Promise<MetabaseAppStateDashboard
284287 } )
285288 }
286289 } )
290+
291+
287292 sqlTables = _ . uniqBy ( sqlTables , ( table ) => `${ table . schema } ::${ table . name } ` )
288293 const relevantTablesWithFields = await getTablesWithFields ( appSettings . tableDiff , appSettings . drMode , ! ! selectedCatalog , sqlTables , [ ] )
289- const tableContextYAML = getTableContextYAML ( relevantTablesWithFields , selectedCatalog , appSettings . drMode ) ;
294+ // find a list of models from each native card using getModelsFromSql, and then merge them to get relevantModels
295+ const modelsFromAllCards = ( await Promise . all ( filteredCards . map ( async card => {
296+ if ( card . sql ) {
297+ return await getModelsFromSql ( card . sql , allModels )
298+ }
299+ return [ ]
300+ } ) ) ) . flat ( )
301+ const dedupedCardAndSelectedModels = _ . uniqBy ( [ ...modelsFromAllCards , ...appSettings . selectedModels ] , 'modelId' )
302+ const relevantModelsWithFields = await getModelsWithFields ( dedupedCardAndSelectedModels )
303+ const allFormattedTables = [ ...relevantTablesWithFields , ...relevantModelsWithFields ]
304+ const tableContextYAML = getTableContextYAML ( allFormattedTables , selectedCatalog , appSettings . drMode ) ;
305+ filteredCards = filteredCards . map ( card => {
306+ // replace model identifiers with model ids
307+ card . sql = modifySqlForMetabaseModels ( card . sql , allModels )
308+ return card
309+ } )
290310 dashboardInfo . cards = filteredCards
291311 // filter out dashcards with null names or ids
292312 . filter ( dashcard => dashcard . name !== null && dashcard . id !== null ) ;
0 commit comments