From 81848ad2ac9d7a1a88ff7f687d8eabcdc480b3c5 Mon Sep 17 00:00:00 2001 From: shriramthebeast Date: Mon, 27 Oct 2025 22:42:16 +0530 Subject: [PATCH] feat: Add Weekly Incident Trend Analysis snippet - week-over-week incident comparison --- .../Weekly Incident Trend Analysis/README.md | 86 +++++++++++++++++++ .../incident_trend_analyzer.js | 32 +++++++ 2 files changed, 118 insertions(+) create mode 100644 Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/README.md create mode 100644 Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/incident_trend_analyzer.js diff --git a/Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/README.md b/Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/README.md new file mode 100644 index 0000000000..34ef6d0d37 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/README.md @@ -0,0 +1,86 @@ +# Weekly Incident Trend Analysis + +## Overview +Compares incident volume week-over-week to track trends and identify anomalies in incident patterns. + +## What It Does +- Counts incidents created last week +- Counts incidents created this week +- Calculates the difference (increase/decrease) +- Logs the trend analysis result + +## Use Cases +- Monitor incident volume trends +- Identify weeks with unusual spike/drop in incidents +- Track service health over time +- Alert on incident volume anomalies +- Weekly reporting on incident patterns + +## Files +- `incident_trend_analyzer.js` - GlideAggregate-based trend analysis + +## How to Use + +### Option 1: Run as Scheduled Job +1. Go to **System Scheduler > Scheduled Jobs** +2. Create new Scheduled Job +3. Copy code from `incident_trend_analyzer.js` +4. Set to run weekly (e.g., every Monday morning) +5. Check logs for trend results + +### Option 2: Run from Background Script +1. Go to **System Diagnostics > Script Background** +2. Copy and execute the code +3. View results in logs + +### Example Usage +```javascript +// The script automatically: +// 1. Queries incidents from last week +// 2. Queries incidents from this week +// 3. Compares counts +// 4. Logs: "Incident count increased by X compared to last week." +``` + +## Output Examples +``` +"Incident count increased by 15 compared to last week." +"Incident count decreased by 8 compared to last week." +"No change in incident volume week-over-week." +``` + +## Key Features +- Uses `GlideAggregate` for efficient counting +- No heavy querying of individual records +- Date range filtering using ServiceNow helper functions +- Week-over-week comparison logic + +## Requirements +- ServiceNow instance with Incident table +- Access to run Background Scripts or create Scheduled Jobs +- Read access to incident records + +## Performance Notes +- Very efficient - uses aggregation not GlideRecord loops +- Minimal database impact +- Suitable for running on schedule without performance concerns + +## Customization +To track other tables (Change, Problem, etc.): +```javascript +// Change 'incident' to your table name +var lastWeekAgg = new GlideAggregate('change_request'); +var thisWeekAgg = new GlideAggregate('change_request'); +``` + +To track different time periods: +```javascript +// Use other helper functions: +// gs.beginningOfThisMonth(), gs.endOfThisMonth() +// gs.beginningOfThisYear(), gs.endOfThisYear() +// gs.addMonthsUTC(), gs.addDaysUTC() for custom ranges +``` + +## Related ServiceNow APIs +- [GlideAggregate](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_UsingGlideAggregate.html) - Used for efficient counting +- [GlideSystem Date Functions](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_SystemDateFunctions.html) diff --git a/Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/incident_trend_analyzer.js b/Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/incident_trend_analyzer.js new file mode 100644 index 0000000000..56bc32141e --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Weekly Incident Trend Analysis/incident_trend_analyzer.js @@ -0,0 +1,32 @@ +(function() { + // Get last week's incident count + var lastWeekAgg = new GlideAggregate('incident'); + lastWeekAgg.addAggregate('COUNT'); + lastWeekAgg.addEncodedQuery('opened_at>=javascript:gs.beginningOfLastWeek()^opened_at<=javascript:gs.endOfLastWeek()'); + lastWeekAgg.query(); + + var lastWeekCount = 0; + if (lastWeekAgg.next()) { + lastWeekCount = lastWeekAgg.getAggregate('COUNT'); + } + + // Get this week's incident count + var thisWeekAgg = new GlideAggregate('incident'); + thisWeekAgg.addAggregate('COUNT'); + thisWeekAgg.addEncodedQuery('opened_at>=javascript:gs.beginningOfThisWeek()^opened_at<=javascript:gs.endOfThisWeek()'); + thisWeekAgg.query(); + + var thisWeekCount = 0; + if (thisWeekAgg.next()) { + thisWeekCount = thisWeekAgg.getAggregate('COUNT'); + } + + // Compare and log + var diff = thisWeekCount - lastWeekCount; + if (diff > 0) + gs.info("Incident count increased by " + diff + " compared to last week."); + else if (diff < 0) + gs.info("Incident count decreased by " + Math.abs(diff) + " compared to last week."); + else + gs.info("No change in incident volume week-over-week."); +})();