@@ -6,54 +6,40 @@ const log = require('../../../../api/utils/log.js')('alert:dataPoints');
66const moment = require ( 'moment-timezone' ) ;
77const common = require ( '../../../../api/utils/common.js' ) ;
88const commonLib = require ( "../parts/common-lib.js" ) ;
9- const { ObjectId } = require ( 'mongodb' ) ;
109
1110const DATA_POINT_PROPERTY = "dp" ;
1211
13- module . exports . check = async function ( { alertConfigs : alert , done, scheduledTo : date } ) {
14- const selectedApp = alert . selectedApps [ 0 ] ;
15- let apps ;
16- if ( selectedApp === "all" ) {
17- apps = await common . readBatcher . getMany ( "apps" , { } ) ;
18- }
19- else {
20- apps = [ await common . readBatcher . getOne ( "apps" , { _id : new ObjectId ( selectedApp ) } ) ] ;
21- }
12+ module . exports . check = async function ( { alert, app, done, scheduledTo : date } ) {
13+ let { period, compareType, compareValue } = alert ;
14+ compareValue = Number ( compareValue ) ;
2215
23- for ( let app of apps ) {
24- if ( ! app ) {
25- log . e ( `App ${ alert . selectedApps [ 0 ] } couldn't be found` ) ;
26- continue ;
27- }
16+ const metricValue = await getDataPointByDate ( app , date , period ) || 0 ;
17+ log . d ( alert . _id , "value on" , date , "is" , metricValue ) ;
2818
29- let { period, compareType, compareValue } = alert ;
30- compareValue = Number ( compareValue ) ;
31-
32- const metricValue = await getDataPointByDate ( app , date , period ) || 0 ;
33-
34- if ( compareType === commonLib . COMPARE_TYPE_ENUM . MORE_THAN ) {
35- if ( metricValue > compareValue ) {
36- await commonLib . trigger ( { alert, app, metricValue, date } , log ) ;
37- }
19+ if ( compareType === commonLib . COMPARE_TYPE_ENUM . MORE_THAN ) {
20+ if ( metricValue > compareValue ) {
21+ log . d ( alert . _id , "triggered because" , metricValue , "is more than" , compareValue ) ;
22+ await commonLib . trigger ( { alert, app, metricValue, date } , log ) ;
23+ }
24+ }
25+ else {
26+ const before = moment ( date ) . subtract ( 1 , commonLib . PERIOD_TO_DATE_COMPONENT_MAP [ period ] ) . toDate ( ) ;
27+ const metricValueBefore = await getDataPointByDate ( app , before , period ) ;
28+ log . d ( alert . _id , "value on" , before , "is" , metricValueBefore ) ;
29+ if ( ! metricValueBefore ) {
30+ return done ( ) ;
3831 }
39- else {
40- const before = moment ( date ) . subtract ( 1 , commonLib . PERIOD_TO_DATE_COMPONENT_MAP [ period ] ) . toDate ( ) ;
41- const metricValueBefore = await getDataPointByDate ( app , before , period ) ;
42- if ( ! metricValueBefore ) {
43- continue ;
44- }
4532
46- const change = ( metricValue / metricValueBefore - 1 ) * 100 ;
47- const shouldTrigger = compareType === commonLib . COMPARE_TYPE_ENUM . INCREASED_BY
48- ? change >= compareValue
49- : change <= - compareValue ;
33+ const change = ( metricValue / metricValueBefore - 1 ) * 100 ;
34+ const shouldTrigger = compareType === commonLib . COMPARE_TYPE_ENUM . INCREASED_BY
35+ ? change >= compareValue
36+ : change <= - compareValue ;
5037
51- if ( shouldTrigger ) {
52- await commonLib . trigger ( { alert, app , date , metricValue , metricValueBefore } , log ) ;
53- }
38+ if ( shouldTrigger ) {
39+ log . d ( alert . _id , "triggered because" , compareType , String ( change ) + "%" ) ;
40+ await commonLib . trigger ( { alert , app , date , metricValue , metricValueBefore } , log ) ;
5441 }
5542 }
56-
5743 done ( ) ;
5844} ;
5945
@@ -117,17 +103,3 @@ function dailySum(dailyData) {
117103 }
118104 return dailyValue ;
119105}
120- /*
121- (async function() {
122- await new Promise(res => setTimeout(res, 2000));
123- const app = { _id: ObjectId("65c1f875a12e98a328d5eb9e"), timezone: "Europe/Istanbul" };
124- const date1 = new Date("2024-01-07T10:00:00.000Z");
125- const date2 = new Date("2024-02-07T10:00:00.000Z");
126- const date3 = new Date("2024-03-07T10:00:00.000Z");
127- let monthlyData1 = await getDataPointByDate(app, date1, "monthly");
128- let monthlyData2 = await getDataPointByDate(app, date2, "monthly");
129- let monthlyData3 = await getDataPointByDate(app, date3, "monthly");
130- console.log("monthly:", monthlyData1, monthlyData2, monthlyData3);
131- console.log("all:", monthlyData1 + monthlyData2 + monthlyData3);
132- })();
133- */
0 commit comments