@@ -13,6 +13,9 @@ import { getTableContextYAML } from '../catalog';
1313import { getModelsFromSql , getModelsWithFields , modifySqlForMetabaseModels , replaceLLMFriendlyIdentifiersInSqlWithModels } from '../metabaseModels' ;
1414import { MetabaseAppStateType } from '../analystModeTypes' ;
1515import { MetabaseTableOrModel } from '../metabaseAPITypes' ;
16+ import { processCard } from '../analystModeTypes' ;
17+ import { Card , SavedCard } from '../types' ;
18+ import { getLimitedEntitiesFromQueries , getLimitedEntitiesFromMBQLQueries } from '../utils' ;
1619
1720// Removed: const { getMetabaseState } = RPCs - using centralized state functions instead
1821
@@ -77,14 +80,7 @@ export type DashboardInfoForModelling = {
7780 type : string ,
7881 value ?: string | null
7982 } [ ] ;
80- cards : {
81- id : number ,
82- name : string ,
83- sql : string ,
84- databaseId : number ,
85- description ?: string | undefined ,
86- outputTableMarkdown ?: string ,
87- } [ ]
83+ cards : SavedCard [ ]
8884}
8985
9086function substituteParameterMappings (
@@ -103,65 +99,30 @@ function substituteParameterMappings(
10399 return sql
104100}
105101
106- async function getDashcardInfoWithSQLAndOutputTableMd (
102+ async function getDashcardwithOutputTableMd (
107103 dashboardMetabaseState : DashboardMetabaseState ,
108104 dashcardId : number ,
109105 dashboardId : number ) : Promise < DashboardInfoForModelling [ 'cards' ] [ number ] | null > {
110- const dashcard = dashboardMetabaseState . dashcards [ dashcardId ] ;
106+ const dashcard = dashboardMetabaseState . dashcards [ dashcardId ] . card ;
111107 if ( ! dashcard ) {
112108 return null ;
113109 }
114- const cardId = _ . get ( dashcard , 'card_id' , '' ) ;
115- const databaseId = _ . get ( dashcard , 'card.database_id' , 0 ) ;
116- const id = _ . get ( dashcard , 'id' ) ;
117- const query_type = _ . get ( dashcard , 'card.query_type' , 'unknown' ) ;
118- const name = _ . get ( dashcard , 'card.name' , '' ) ;
119- const description = _ . get ( dashcard , 'card.description' , '' ) ;
120- const visualizationType = _ . get ( dashcard , 'card.display' , '' ) ;
121- if ( ! name )
110+ const cardID = _ . get ( dashcard , 'id' , null ) ;
111+ if ( ! cardID ) {
122112 return null ;
123- let sql = ''
124- if ( query_type == 'native' ) {
125- sql = _ . get ( dashcard , 'card.dataset_query.native.query' , '' ) ;
126- }
127- else if ( query_type == 'query' ) {
128- // mbql query
129- const mbqlQuery = _ . get ( dashcard , 'card.dataset_query.query' , { } ) ;
130- if ( mbqlQuery ) {
131- const sql_from_mbql = await getSQLFromMBQL ( {
132- database : databaseId ,
133- type : 'query' ,
134- query : mbqlQuery
135- } ) ;
136- sql = sql_from_mbql . query || '' ;
137- }
138113 }
139-
140- if ( ! sql || sql == '' )
141- return null ;
114+ const card = processCard ( dashcard ) as SavedCard ;
115+ card . id = cardID ;
142116
143- // replace parameters
144- try {
145- sql = await substituteParameters ( sql , dashcard , dashboardMetabaseState [ 'dashboards' ] [ dashboardId ] ?. param_fields , dashboardMetabaseState . parameterValues )
146- } catch ( e ) {
147- }
148- const obj = {
149- id,
150- name,
151- sql,
152- databaseId,
153- visualizationType,
154- ...( description ? { description } : { } ) ,
155- }
156117 // dashcardData
157- const data = _ . get ( dashboardMetabaseState , [ 'dashcardData' , dashcardId , cardId , 'data' ] ) ;
118+ const data = _ . get ( dashboardMetabaseState , [ 'dashcardData' , dashcardId , card . id , 'data' ] ) ;
158119 if ( ! data ) {
159- return obj
120+ return card
160121 }
161122 const dataAsMarkdown = metabaseToMarkdownTable ( data , 1000 ) ;
162123 return {
163- ...obj ,
164- outputTableMarkdown : dataAsMarkdown
124+ ...card ,
125+ outputTableMarkdown : dataAsMarkdown
165126 }
166127}
167128/*
@@ -278,75 +239,33 @@ export async function getDashboardAppState(): Promise<MetabaseAppStateDashboard
278239 }
279240 const selectedTabDashcardIds = getSelectedTabDashcardIds ( dashboardMetabaseState ) ;
280241// const dashboardParameters = _.get(dashboardMetabaseState, ['dashboards', dashboardId, 'parameters'], [])
281- const allModels = dbId ? await getAllRelevantModelsForSelectedDb ( dbId ) : [ ]
282- const cards = await Promise . all ( selectedTabDashcardIds . map ( async dashcardId => await getDashcardInfoWithSQLAndOutputTableMd ( dashboardMetabaseState , dashcardId , dashboardId , allModels ) ) )
242+ const cards = await Promise . all ( selectedTabDashcardIds . map ( async dashcardId => await getDashcardwithOutputTableMd ( dashboardMetabaseState , dashcardId , dashboardId ) ) )
283243 let filteredCards = _ . compact ( cards ) ;
284- let sqlTables : TableAndSchema [ ] = [ ]
285- forEach ( filteredCards , ( card ) => {
286- if ( card ) {
287- getTablesFromSqlRegex ( card . sql ) . forEach ( ( table ) => {
288- if ( defaultSchema ) {
289- if ( table . schema === undefined || table . schema === '' ) {
290- table . schema = defaultSchema
291- }
292- }
293- sqlTables . push ( table )
294- } )
295- }
296- } )
297-
298-
299- sqlTables = _ . uniqBy ( sqlTables , ( table ) => `${ table . schema } ::${ table . name } ` )
300- const relevantTablesWithFields = await getTablesWithFields ( appSettings . tableDiff , appSettings . drMode , ! ! selectedCatalog , sqlTables , [ ] )
301- // find a list of models from each native card using getModelsFromSql, and then merge them to get relevantModels
302- const modelsFromAllCards = ( await Promise . all ( filteredCards . map ( async card => {
303- if ( card . sql ) {
304- return await getModelsFromSql ( card . sql , allModels )
305- }
306- return [ ]
307- } ) ) ) . flat ( )
308- const dedupedCardAndSelectedModels = _ . uniqBy ( [ ...modelsFromAllCards , ...appSettings . selectedModels ] , 'modelId' )
309- const relevantModelsWithFields = await getModelsWithFields ( dedupedCardAndSelectedModels )
310- const allFormattedTables = [ ...relevantTablesWithFields , ...relevantModelsWithFields ]
311- const tableContextYAML = getTableContextYAML ( allFormattedTables , selectedCatalog , appSettings . drMode ) ;
312- filteredCards = filteredCards . map ( card => {
313- // replace model identifiers with model ids
314- card . sql = modifySqlForMetabaseModels ( card . sql , allModels )
315- return card
316- } )
317- dashboardInfo . cards = filteredCards
318- // filter out dashcards with null names or ids
319- . filter ( dashcard => dashcard . name !== null && dashcard . id !== null ) ;
320- // remove description if it's null or undefined
321- if ( ! dashboardInfo . description ) {
322- delete dashboardInfo . description ;
323- }
244+ const limitedEntitiesSQL = await getLimitedEntitiesFromQueries (
245+ filteredCards . flatMap ( card =>
246+ card ?. dataset_query ?. native ?. query ? [ card . dataset_query . native . query ] : [ ]
247+ )
248+ ) ;
249+ const limitedEntitiesMBQL = await getLimitedEntitiesFromMBQLQueries (
250+ filteredCards . flatMap ( card =>
251+ card ?. dataset_query ?. query ? [ card . dataset_query . query ] : [ ]
252+ )
253+ ) ;
254+ const limitedEntities = [ ...limitedEntitiesSQL , ...limitedEntitiesMBQL ] ;
255+ // remove duplicates based on id and type
256+ const uniqueEntities = Array . from ( new Map ( limitedEntities . map ( entity => [ entity . id , entity ] ) ) . values ( ) ) ;
324257 const dashboardAppState : MetabaseAppStateDashboard = {
325258 ...dashboardInfo ,
326259 type : MetabaseAppStateType . Dashboard ,
327- tableContextYAML,
328260 selectedDatabaseInfo,
329261 metabaseOrigin : url ,
330262 metabaseUrl : fullUrl ,
331263 isEmbedded : getParsedIframeInfo ( ) . isEmbedded ,
332264 } ;
333- if ( appSettings . analystMode && appSettings . manuallyLimitContext ) {
334- const limitToTables : MetabaseTableOrModel [ ] = relevantTablesWithFields . map ( table => ( {
335- type : 'table' ,
336- id : table . id ,
337- name : table . name ,
338- schema : table . schema ,
339- description : table . description ,
340- } ) )
341- const limitToModels : MetabaseTableOrModel [ ] = dedupedCardAndSelectedModels . map ( model => ( {
342- type : 'model' ,
343- id : model . modelId ,
344- name : model . name ,
345- description : model . description ,
346- } ) )
347- dashboardAppState . limitedEntities = [ ...limitToTables , ...limitToModels ] ;
348- }
349- return dashboardAppState
265+ dashboardAppState . cards = filteredCards as SavedCard [ ] ;
266+ dashboardAppState . limitedEntities = uniqueEntities ;
267+ dashboardAppState . parameterValues = dashboardMetabaseState . parameterValues || { } ;
268+ return dashboardAppState ;
350269}
351270
352271
0 commit comments