Skip to content

Commit 4149ad2

Browse files
author
Mattia Moretti
authored
Alert Prerequisites (#266)
* Alerting Support * problemi * alert prereq * working * other fixes * ready
1 parent 6e71936 commit 4149ad2

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

src/QueryEditor.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ export const QueryEditor: React.FC<Props> = (props) => {
3838
{ label: 'Builder', value: true },
3939
];
4040

41-
4241
const onApplyQueryChange = (changedQuery: OCIQuery, runQuery = true) => {
4342
if (runQuery) {
4443
const queryModel = new QueryModel(changedQuery, getTemplateSrv());
44+
4545
// for metrics
46-
if (datasource.isVariable(String(query.metric))) {
47-
let { [String(query.metric)]: var_metric } = datasource.interpolateProps({ [String(query.metric)]: query.metric });
46+
if (datasource.isVariable(String(changedQuery.metric))) {
47+
let { [String(changedQuery.metric)]: var_metric } = datasource.interpolateProps({ [String(changedQuery.metric)]: changedQuery.metric });
4848
if (var_metric !== "" && var_metric !== QueryPlaceholder.Metric) {
49-
query.metric = var_metric
49+
changedQuery.metric = var_metric
5050
}
5151
}
5252
if (queryModel.isQueryReady()) {
53-
54-
if (query.rawQuery === false){
55-
changedQuery.queryText = queryModel.buildQuery(String(query.queryTextRaw));
53+
54+
if (changedQuery.rawQuery === false){
55+
changedQuery.queryText = queryModel.buildQuery(String(changedQuery.queryTextRaw));
5656
} else {
57-
changedQuery.queryText = queryModel.buildQuery(String(query.metric));
57+
changedQuery.queryText = queryModel.buildQuery(String(changedQuery.metric));
5858
}
5959

6060
onChange({ ...changedQuery });
@@ -65,6 +65,7 @@ export const QueryEditor: React.FC<Props> = (props) => {
6565
}
6666
};
6767

68+
6869
const init = () => {
6970
let initialDimensions: any = [];
7071
let initialTags: any = [];
@@ -419,8 +420,8 @@ export const QueryEditor: React.FC<Props> = (props) => {
419420

420421

421422
const onMetricChange = (data: any) => {
422-
setMetricValue(data);
423-
onApplyQueryChange({ ...query, metric: data.value });
423+
setMetricValue(data);
424+
onApplyQueryChange({ ...query, metric: data.value }, true);
424425
};
425426
const onAggregationChange = (data: any) => {
426427
onApplyQueryChange({ ...query, statisticLabel: data.label, statistic: data.value });

src/datasource.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
tenanciesQueryRegex,
2727
DEFAULT_TENANCY,
2828
compartmentsQueryRegex,
29+
SetAutoInterval,
2930
} from "./types";
3031
import QueryModel from './query_model';
3132

@@ -51,20 +52,6 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
5152
return true;
5253
}
5354

54-
SetAutoInterval(timestamp1: number, timestamp2: number): string {
55-
const differenceInMs = timestamp2 - timestamp1;
56-
const differenceInHours = differenceInMs / (1000 * 60 * 60);
57-
58-
// use limits and defaults specified here: https://docs.oracle.com/en-us/iaas/Content/Monitoring/Reference/mql.htm#Interval
59-
if (differenceInHours <= 6) {
60-
return "[1m]"; // Equal or Less than 6 hours, set to 1 minute interval
61-
} else if (differenceInHours < 36) {
62-
return "[5m]"; // Between 6 and 36 hours, set to 5 minute interval
63-
} else {
64-
return "[1h]"; // More than 36 hours, set to 1 hour interval
65-
}
66-
}
67-
6855
compartmentFormatter = (value: string): string => {
6956
// if (typeof value === 'string') {
7057
// return value;
@@ -91,7 +78,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
9178
query.interval = templateSrv.replace(query.interval, scopedVars);
9279
}
9380
if (query.interval === QueryPlaceholder.Interval || query.interval === "auto" || query.interval === undefined){
94-
query.interval = this.SetAutoInterval(TimeStart, TimeEnd);
81+
query.interval = SetAutoInterval(TimeStart, TimeEnd);
9582
}
9683
query.region = templateSrv.replace(query.region, scopedVars);
9784
query.tenancy = templateSrv.replace(query.tenancy, scopedVars);

src/query_model.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
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';
77
import { ScopedVars } from '@grafana/data';
8-
import { TemplateSrv } from '@grafana/runtime';
8+
import { TemplateSrv, getTemplateSrv } from '@grafana/runtime';
99

1010
export 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 = '{';

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,17 @@ export interface OCISecureJsonData {
173173
fingerprint5: string;
174174
privkey5: string;
175175
}
176+
177+
export const SetAutoInterval = (timestamp1: number, timestamp2: number): string => {
178+
const differenceInMs = timestamp2 - timestamp1;
179+
const differenceInHours = differenceInMs / (1000 * 60 * 60);
180+
181+
// use limits and defaults specified here: https://docs.oracle.com/en-us/iaas/Content/Monitoring/Reference/mql.htm#Interval
182+
if (differenceInHours <= 6) {
183+
return "[1m]"; // Equal or Less than 6 hours, set to 1 minute interval
184+
} else if (differenceInHours < 36) {
185+
return "[5m]"; // Between 6 and 36 hours, set to 5 minute interval
186+
} else {
187+
return "[1h]"; // More than 36 hours, set to 1 hour interval
188+
}
189+
};

0 commit comments

Comments
 (0)