-
Notifications
You must be signed in to change notification settings - Fork 919
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (51 loc) · 2.07 KB
/
script.js
File metadata and controls
64 lines (51 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Virtual Agent Conversation Analytics
// Analyzes VA conversation logs to identify the most common topics
var daysBack = 7; // Analyze conversations from the past 7 days
// Calculate date range
var startDate = new GlideDateTime();
startDate.addDaysLocalTime(-daysBack);
gs.info('=== Virtual Agent Conversation Analytics ===');
gs.info('Analyzing conversations from: ' + startDate.getDisplayValue());
// Get conversation logs
var convGr = new GlideRecord('sys_cs_conversation');
convGr.addQuery('sys_created_on', '>=', startDate);
convGr.query();
var totalConversations = convGr.getRowCount();
gs.info('Total Conversations: ' + totalConversations);
// Auto-detect topic field (handles schema variations)
var topicField = convGr.isValidField('topic') ? 'topic' :
(convGr.isValidField('selected_topic') ? 'selected_topic' : null);
if (!topicField) {
gs.warn('No topic field found on sys_cs_conversation table');
} else {
// Track topic counts
var topicCounts = {};
while (convGr.next()) {
var topicId = convGr.getValue(topicField);
if (topicId) {
var topicGr = new GlideRecord('sys_cs_topic');
if (topicGr.get(topicId)) {
var topicName = topicGr.getValue('name');
if (!topicCounts[topicName]) {
topicCounts[topicName] = 0;
}
topicCounts[topicName]++;
}
}
}
// Sort and display most common topics
gs.info('\n=== Most Common Topics ===');
var sortedTopics = [];
for (var topic in topicCounts) {
sortedTopics.push({name: topic, count: topicCounts[topic]});
}
sortedTopics.sort(function(a, b) { return b.count - a.count; });
if (sortedTopics.length === 0) {
gs.info('No topics found in the selected time range');
} else {
for (var i = 0; i < Math.min(10, sortedTopics.length); i++) {
gs.info((i + 1) + '. ' + sortedTopics[i].name + ': ' + sortedTopics[i].count + ' conversations');
}
}
}
gs.info('\n=== Analysis Complete ===');