@@ -11,20 +11,30 @@ export const useFormattedData = (query: CHQuery, datasource: CHDataSource, optio
11
11
const [ error , setError ] = useState < string | null > ( null ) ;
12
12
13
13
useEffect ( ( ) => {
14
- if ( ( datasource . options || options ) && datasource . templateSrv ) {
15
- datasource . replace ( datasource . options || options , query ) . then ( ( replaced ) => {
16
- setFormattedData ( replaced . stmt ) ;
17
- setError ( null ) ;
18
- } ) . catch ( ( e ) => {
19
- setFormattedData ( query . query ) ;
20
- // Display the error we received from backend
21
- const errorStr = e . data ?. error || e . toString ( ) ;
22
- setError ( errorStr ) ;
23
- } )
24
- } else {
25
- // Only set error if datasource is genuinely missing required properties
26
- setError ( 'No datasource is defined or datasource not fully initialized. Please try to force reload' ) ;
27
- }
14
+ // Determine if we're in a context where template replacement is possible
15
+ const hasExecutionContext = ( datasource . options ?. range || options ?. range ) ;
16
+ const hasTemplateService = ! ! datasource . templateSrv ;
17
+
18
+ if ( hasExecutionContext && hasTemplateService ) {
19
+ // Normal dashboard mode - perform replacement
20
+ datasource . replace ( datasource . options || options , query ) . then ( ( replaced ) => {
21
+ setFormattedData ( replaced . stmt ) ;
22
+ setError ( null ) ;
23
+ } ) . catch ( ( e ) => {
24
+ setFormattedData ( query . query ) ;
25
+ const errorStr = e . data ?. error || e . toString ( ) ;
26
+ setError ( errorStr ) ;
27
+ } ) ;
28
+ } else if ( hasTemplateService ) {
29
+ // Alerts/Explore mode - no execution context yet
30
+ // This is EXPECTED behavior, not an error
31
+ setFormattedData ( query . query ) ;
32
+ setError ( null ) ;
33
+ } else {
34
+ // Critical error - no template service available
35
+ setFormattedData ( query . query ) ;
36
+ setError ( 'Grafana template service unavailable. Please refresh the page.' ) ;
37
+ }
28
38
29
39
// eslint-disable-next-line
30
40
} , [ query , datasource . name , datasource . options , options , datasource . templateSrv ] ) ;
0 commit comments