Skip to content

Commit 97b5e29

Browse files
authored
feat: Add Weekly Incident Trend Analysis snippet - week-over-week incident comparison (#2546)
1 parent 037ef75 commit 97b5e29

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Weekly Incident Trend Analysis
2+
3+
## Overview
4+
Compares incident volume week-over-week to track trends and identify anomalies in incident patterns.
5+
6+
## What It Does
7+
- Counts incidents created last week
8+
- Counts incidents created this week
9+
- Calculates the difference (increase/decrease)
10+
- Logs the trend analysis result
11+
12+
## Use Cases
13+
- Monitor incident volume trends
14+
- Identify weeks with unusual spike/drop in incidents
15+
- Track service health over time
16+
- Alert on incident volume anomalies
17+
- Weekly reporting on incident patterns
18+
19+
## Files
20+
- `incident_trend_analyzer.js` - GlideAggregate-based trend analysis
21+
22+
## How to Use
23+
24+
### Option 1: Run as Scheduled Job
25+
1. Go to **System Scheduler > Scheduled Jobs**
26+
2. Create new Scheduled Job
27+
3. Copy code from `incident_trend_analyzer.js`
28+
4. Set to run weekly (e.g., every Monday morning)
29+
5. Check logs for trend results
30+
31+
### Option 2: Run from Background Script
32+
1. Go to **System Diagnostics > Script Background**
33+
2. Copy and execute the code
34+
3. View results in logs
35+
36+
### Example Usage
37+
```javascript
38+
// The script automatically:
39+
// 1. Queries incidents from last week
40+
// 2. Queries incidents from this week
41+
// 3. Compares counts
42+
// 4. Logs: "Incident count increased by X compared to last week."
43+
```
44+
45+
## Output Examples
46+
```
47+
"Incident count increased by 15 compared to last week."
48+
"Incident count decreased by 8 compared to last week."
49+
"No change in incident volume week-over-week."
50+
```
51+
52+
## Key Features
53+
- Uses `GlideAggregate` for efficient counting
54+
- No heavy querying of individual records
55+
- Date range filtering using ServiceNow helper functions
56+
- Week-over-week comparison logic
57+
58+
## Requirements
59+
- ServiceNow instance with Incident table
60+
- Access to run Background Scripts or create Scheduled Jobs
61+
- Read access to incident records
62+
63+
## Performance Notes
64+
- Very efficient - uses aggregation not GlideRecord loops
65+
- Minimal database impact
66+
- Suitable for running on schedule without performance concerns
67+
68+
## Customization
69+
To track other tables (Change, Problem, etc.):
70+
```javascript
71+
// Change 'incident' to your table name
72+
var lastWeekAgg = new GlideAggregate('change_request');
73+
var thisWeekAgg = new GlideAggregate('change_request');
74+
```
75+
76+
To track different time periods:
77+
```javascript
78+
// Use other helper functions:
79+
// gs.beginningOfThisMonth(), gs.endOfThisMonth()
80+
// gs.beginningOfThisYear(), gs.endOfThisYear()
81+
// gs.addMonthsUTC(), gs.addDaysUTC() for custom ranges
82+
```
83+
84+
## Related ServiceNow APIs
85+
- [GlideAggregate](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_UsingGlideAggregate.html) - Used for efficient counting
86+
- [GlideSystem Date Functions](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_SystemDateFunctions.html)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function() {
2+
// Get last week's incident count
3+
var lastWeekAgg = new GlideAggregate('incident');
4+
lastWeekAgg.addAggregate('COUNT');
5+
lastWeekAgg.addEncodedQuery('opened_at>=javascript:gs.beginningOfLastWeek()^opened_at<=javascript:gs.endOfLastWeek()');
6+
lastWeekAgg.query();
7+
8+
var lastWeekCount = 0;
9+
if (lastWeekAgg.next()) {
10+
lastWeekCount = lastWeekAgg.getAggregate('COUNT');
11+
}
12+
13+
// Get this week's incident count
14+
var thisWeekAgg = new GlideAggregate('incident');
15+
thisWeekAgg.addAggregate('COUNT');
16+
thisWeekAgg.addEncodedQuery('opened_at>=javascript:gs.beginningOfThisWeek()^opened_at<=javascript:gs.endOfThisWeek()');
17+
thisWeekAgg.query();
18+
19+
var thisWeekCount = 0;
20+
if (thisWeekAgg.next()) {
21+
thisWeekCount = thisWeekAgg.getAggregate('COUNT');
22+
}
23+
24+
// Compare and log
25+
var diff = thisWeekCount - lastWeekCount;
26+
if (diff > 0)
27+
gs.info("Incident count increased by " + diff + " compared to last week.");
28+
else if (diff < 0)
29+
gs.info("Incident count decreased by " + Math.abs(diff) + " compared to last week.");
30+
else
31+
gs.info("No change in incident volume week-over-week.");
32+
})();

0 commit comments

Comments
 (0)