Negative minutes in logs

VarunS
Kilo Sage

At the bottom is a snippet from my script include which when I run in a scheduled job I get negative values in logs 

VarunS_0-1715627568070.png

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;
    },

 

1 ACCEPTED SOLUTION

James Chun
Kilo Patron

Hi @VarunS,

 

Shouldn't the 2 parameters (laterTime and earlierTime) be swapped around?

i.e.

JamesChun_0-1715630229942.png

Seems like you are subtracting the created time (which is in the past) from the current time.

 

Cheers

View solution in original post

1 REPLY 1

James Chun
Kilo Patron

Hi @VarunS,

 

Shouldn't the 2 parameters (laterTime and earlierTime) be swapped around?

i.e.

JamesChun_0-1715630229942.png

Seems like you are subtracting the created time (which is in the past) from the current time.

 

Cheers