33** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44*/
55
6- import { OCIQuery , QueryPlaceholder , AggregationOptions } from './types' ;
6+ import { OCIQuery , QueryPlaceholder , AggregationOptions , SetAutoInterval } from './types' ;
77import { ScopedVars } from '@grafana/data' ;
8- import { TemplateSrv } from '@grafana/runtime' ;
8+ import { TemplateSrv , getTemplateSrv } from '@grafana/runtime' ;
99
1010export default class QueryModel {
1111 target : OCIQuery ;
@@ -47,7 +47,7 @@ export default class QueryModel {
4747 this . target . queryText =
4848 incomingQuery . queryTextRaw || 'metric[interval]{dimensionname="dimensionvalue"}.groupingfunction.statistic' ;
4949 } else {
50- this . target . queryText = incomingQuery . queryText || this . buildQuery ( String ( this . target . metric ) ) ;
50+ this . target . queryText = this . buildQuery ( String ( this . target . metric ) ) ;
5151 }
5252 }
5353
@@ -57,7 +57,7 @@ export default class QueryModel {
5757 this . target . tenancy === QueryPlaceholder . Tenancy ||
5858 this . target . region === QueryPlaceholder . Region ||
5959 this . target . namespace === QueryPlaceholder . Namespace ||
60- ( this . target . metric === QueryPlaceholder . Metric && this . target . queryTextRaw === '' )
60+ ( ( this . target . metric === QueryPlaceholder . Metric || this . target . metric === undefined ) && this . target . queryTextRaw === '' )
6161 ) {
6262 return false ;
6363 }
@@ -72,9 +72,27 @@ export default class QueryModel {
7272 queryText = String ( this . target . queryTextRaw ) ;
7373 } else {
7474 // if builder mode is used then:
75-
7675 // add interval
77- queryText += this . target . interval ;
76+ let convertedInterval ;
77+ try {
78+ // Check for special cases or undefined interval
79+ if ( this . target . interval === QueryPlaceholder . Interval || this . target . interval === "auto" || ! this . target . interval ) {
80+ const timeStart = parseInt ( getTemplateSrv ( ) . replace ( "${__from}" ) , 10 ) ;
81+ const timeEnd = parseInt ( getTemplateSrv ( ) . replace ( "${__to}" ) , 10 ) ;
82+
83+ if ( isNaN ( timeStart ) || isNaN ( timeEnd ) ) {
84+ convertedInterval = "[1m]" ; // Default interval if parsing fails
85+ } else {
86+ convertedInterval = SetAutoInterval ( timeStart , timeEnd ) ; // Use custom function
87+ }
88+ } else {
89+ convertedInterval = this . target . interval ; // Use existing interval for other cases
90+ }
91+ } catch ( error ) {
92+ convertedInterval = "[1m]" ; // Default interval on error
93+ }
94+
95+ queryText += convertedInterval ;
7896
7997 // add dimensions
8098 let dimensionParams = '{' ;
0 commit comments