1
1
import _ from 'lodash' ;
2
- import { Instance , SnapshotIn , getParent , getRoot , types } from 'mobx-state-tree' ;
2
+ import { Instance , SnapshotIn , flow , getParent , getRoot , types } from 'mobx-state-tree' ;
3
3
import { CURRENT_SCHEMA_VERSION , QueryMetaSnapshotIn } from '~/model/meta-model' ;
4
4
import { downloadDataAsCSV , downloadDataListAsZip , downloadJSON } from '~/utils/download' ;
5
5
import { QueryRenderModel , QueryRenderModelInstance } from './query' ;
@@ -35,11 +35,11 @@ export const QueriesRenderModel = types
35
35
return this . root . name ;
36
36
} ,
37
37
get contentModel ( ) {
38
- return getParent ( self , 1 ) ;
38
+ // FIXME: type
39
+ return getParent ( self , 1 ) as any ;
39
40
} ,
40
41
get visibleQueryIDSet ( ) {
41
- // FIXME: type
42
- const { views, filters, queries } = this . contentModel as any ;
42
+ const { views, filters } = this . contentModel ;
43
43
const queryIDs : string [ ] = [ ] ;
44
44
45
45
views . visibleViews . forEach ( ( v : any ) => {
@@ -62,7 +62,7 @@ export const QueriesRenderModel = types
62
62
}
63
63
} ) ;
64
64
65
- queries . findByIDSet ( new Set ( queryIDs ) ) . forEach ( ( q : QueryRenderModelInstance ) => {
65
+ this . findByIDSet ( new Set ( queryIDs ) ) . forEach ( ( q : QueryRenderModelInstance ) => {
66
66
const config = q . config as TransformQueryMetaInstance ;
67
67
if ( ! q . isTransform || config . dep_query_ids . length === 0 ) {
68
68
return ;
@@ -77,6 +77,59 @@ export const QueriesRenderModel = types
77
77
isQueryInUse ( queryID : string ) {
78
78
return this . visibleQueryIDSet . has ( queryID ) ;
79
79
} ,
80
+ addTransformDepQueryIDs ( targetSet : Set < string > , excludeSet ?: Set < string > ) {
81
+ this . findByIDSet ( targetSet ) . forEach ( ( q : QueryRenderModelInstance ) => {
82
+ const config = q . config as TransformQueryMetaInstance ;
83
+ if ( ! q . isTransform || config . dep_query_ids . length === 0 ) {
84
+ return ;
85
+ }
86
+ config . dep_query_ids . forEach ( ( id ) => {
87
+ if ( excludeSet ?. has ( id ) ) {
88
+ return ;
89
+ }
90
+ targetSet . add ( id ) ;
91
+ } ) ;
92
+ } ) ;
93
+ } ,
94
+ get querisToForceReload ( ) {
95
+ const filterQueryIDSet = new Set < string > ( ) ;
96
+ const panelQueryIDSet = new Set < string > ( ) ;
97
+
98
+ const { views, filters } = this . contentModel ;
99
+ const visibleViewIDs : string [ ] = _ . uniq ( views . visibleViews . map ( ( v : any ) => v . renderViewIDs ) . flat ( ) ) ;
100
+
101
+ // make filterQueryIDSet
102
+ filters . current . forEach ( ( f : any ) => {
103
+ const id = _ . get ( f , 'config.options_query_id' ) ;
104
+ if ( ! id ) {
105
+ return ;
106
+ }
107
+
108
+ const visible = visibleViewIDs . some ( ( viewID ) => f . visibleInViewsIDSet . has ( viewID ) ) ;
109
+ if ( visible ) {
110
+ filterQueryIDSet . add ( id ) ;
111
+ }
112
+ } ) ;
113
+ this . addTransformDepQueryIDs ( filterQueryIDSet ) ;
114
+
115
+ // make panelQueryIDSet
116
+ views . visibleViews . forEach ( ( v : any ) => {
117
+ v . panels . forEach ( ( p : any ) => {
118
+ p . queryIDs . forEach ( ( id : string ) => {
119
+ if ( filterQueryIDSet . has ( id ) ) {
120
+ return ;
121
+ }
122
+ panelQueryIDSet . add ( id ) ;
123
+ } ) ;
124
+ } ) ;
125
+ } ) ;
126
+ this . addTransformDepQueryIDs ( panelQueryIDSet , filterQueryIDSet ) ;
127
+
128
+ return {
129
+ filterQueries : this . findByIDSet ( filterQueryIDSet ) ,
130
+ panelQueries : this . findByIDSet ( panelQueryIDSet ) ,
131
+ } ;
132
+ } ,
80
133
} ) )
81
134
. actions ( ( self ) => {
82
135
return {
@@ -134,6 +187,22 @@ export const QueriesRenderModel = types
134
187
const filename = 'Queries' ;
135
188
downloadJSON ( filename , schema ) ;
136
189
} ,
190
+ forceReloadVisibleQueries : flow ( function * ( ) {
191
+ const { filterQueries, panelQueries } = self . querisToForceReload ;
192
+ console . log ( '🟡 Force reloading queries' ) ;
193
+ if ( filterQueries . length > 0 ) {
194
+ const result = yield Promise . allSettled ( filterQueries . map ( ( q ) => q . fetchData ( true ) ) ) ;
195
+ console . log ( '🟡 Queries from filters reloaded' , result ) ;
196
+ } else {
197
+ console . log ( '🟡 Found no query from visible filters, skipping' ) ;
198
+ }
199
+ if ( panelQueries . length > 0 ) {
200
+ const result = yield Promise . allSettled ( panelQueries . map ( ( q ) => q . fetchData ( true ) ) ) ;
201
+ console . log ( '🟡 Queries from filters reloaded' , result ) ;
202
+ } else {
203
+ console . log ( '🟡 Found no query from visible panels, skipping' ) ;
204
+ }
205
+ } ) ,
137
206
} ;
138
207
} ) ;
139
208
0 commit comments