- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 12:13 PM
At the bottom is a snippet from my script include which when I run in a scheduled job I get negative values in logs
Where am I going wrong?
var DynatraceAlertToIncidentCreator = Class.create();
DynatraceAlertToIncidentCreator.prototype = {
initialize: function() {},
processAlerts: function() {
try {
gs.info("Starting the processing of alerts.");
var now = new GlideDateTime(); // Already in the correct time zone
gs.info("Current system time: " + now.getDisplayValue());
var businessStartTime = this._getTimeForToday('06:30:00');
var businessEndTime = this._getTimeForToday('19:30:00');
var alerts = new GlideRecord('u_dynatrace_alerts');
alerts.addActiveQuery();
alerts.addQuery('state', 'IN', '1,2');
alerts.query();
gs.info("Number of alerts fetched: " + alerts.getRowCount());
while (alerts.next()) {
var createdTime = new GlideDateTime(alerts.sys_created_on);
gs.info("Processing Alert: " + alerts.sys_id + " created at " + createdTime.getDisplayValue());
var minutesSinceCreated = this._calculateMinutesDifference(createdTime, now);
gs.info("Alert " + alerts.sys_id + ": Description = " + alerts.short_description + ", Minutes Since Created = " + minutesSinceCreated + ", In Business Hours = " + (now.compareTo(businessStartTime) >= 0 && now.compareTo(businessEndTime) <= 0) + ", Is Active = " + alerts.active);
if (this._isValidForProcessing(alerts, minutesSinceCreated, now, businessStartTime, businessEndTime)) {
this._createOrUpdateIncident(alerts, minutesSinceCreated, now, businessStartTime, businessEndTime);
}
}
} catch (e) {
gs.error("Error in processAlerts: " + e.message);
}
},
_getTimeForToday: function(timeString) {
var gdt = new GlideDateTime();
gdt.setValue(gs.nowDateTime()); // This sets gdt to the current date and time
gdt.setDisplayValue(gdt.getLocalDate().toString().split(' ')[0] + ' ' + timeString);
return gdt;
},
_calculateMinutesDifference: function(earlierTime, laterTime) {
if (earlierTime.getNumericValue() > laterTime.getNumericValue()) {
gs.info("Alert creation time is in the future compared to 'now': " + earlierTime.getDisplayValue() + " vs. current time " + laterTime.getDisplayValue());
return -1;
}
var minutesDifference = GlideDateTime.subtract(laterTime, earlierTime).getNumericValue() / 60000; // Convert ms to minutes
gs.info("Calculated Minutes Difference: " + minutesDifference + " minutes");
return minutesDifference;
},
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 12:57 PM
Hi @VarunS,
Shouldn't the 2 parameters (laterTime and earlierTime) be swapped around?
i.e.
Seems like you are subtracting the created time (which is in the past) from the current time.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 12:57 PM
Hi @VarunS,
Shouldn't the 2 parameters (laterTime and earlierTime) be swapped around?
i.e.
Seems like you are subtracting the created time (which is in the past) from the current time.
Cheers