|
| 1 | +// Virtual Agent Topic Coverage Report |
| 2 | +// Analyzes VA topic configuration health and usage patterns |
| 3 | + |
| 4 | +var daysBack = 30; // Analyze topic usage from the past 30 days |
| 5 | + |
| 6 | +// Calculate date range for usage analysis |
| 7 | +var startDate = new GlideDateTime(); |
| 8 | +startDate.addDaysLocalTime(-daysBack); |
| 9 | + |
| 10 | +gs.info('=== Virtual Agent Topic Coverage Report ==='); |
| 11 | +gs.info('Analyzing topics and usage from: ' + startDate.getDisplayValue()); |
| 12 | + |
| 13 | +// Get all VA topics |
| 14 | +var topicGr = new GlideRecord('sys_cs_topic'); |
| 15 | +if (!topicGr.isValid()) { |
| 16 | + gs.warn('Table sys_cs_topic not found. Virtual Agent may not be installed.'); |
| 17 | +} else { |
| 18 | + topicGr.query(); |
| 19 | + |
| 20 | + var totalTopics = topicGr.getRowCount(); |
| 21 | + gs.info('Total Topics: ' + totalTopics); |
| 22 | + |
| 23 | + var inactiveTopics = []; |
| 24 | + var unpublishedTopics = []; |
| 25 | + var zeroUsageTopics = []; |
| 26 | + var topicUsage = {}; |
| 27 | + |
| 28 | + // Auto-detect conversation table field name |
| 29 | + var convGr = new GlideRecord('sys_cs_conversation'); |
| 30 | + var topicField = null; |
| 31 | + if (convGr.isValid()) { |
| 32 | + topicField = convGr.isValidField('topic') ? 'topic' : |
| 33 | + (convGr.isValidField('selected_topic') ? 'selected_topic' : null); |
| 34 | + } |
| 35 | + |
| 36 | + while (topicGr.next()) { |
| 37 | + var topicId = topicGr.getUniqueValue(); |
| 38 | + var topicName = topicGr.getValue('name'); |
| 39 | + var isActive = topicGr.getValue('active') == 'true' || topicGr.getValue('active') == '1'; |
| 40 | + var isPublished = topicGr.getValue('published') == 'true' || topicGr.getValue('published') == '1'; |
| 41 | + |
| 42 | + // Track inactive topics |
| 43 | + if (!isActive) { |
| 44 | + inactiveTopics.push(topicName); |
| 45 | + } |
| 46 | + |
| 47 | + // Track unpublished topics |
| 48 | + if (!isPublished) { |
| 49 | + unpublishedTopics.push(topicName); |
| 50 | + } |
| 51 | + |
| 52 | + // Count conversations for this topic (if conversation table exists) |
| 53 | + var conversationCount = 0; |
| 54 | + if (topicField) { |
| 55 | + var convCountGr = new GlideAggregate('sys_cs_conversation'); |
| 56 | + convCountGr.addQuery(topicField, topicId); |
| 57 | + convCountGr.addQuery('sys_created_on', '>=', startDate); |
| 58 | + convCountGr.addAggregate('COUNT'); |
| 59 | + convCountGr.query(); |
| 60 | + if (convCountGr.next()) { |
| 61 | + conversationCount = parseInt(convCountGr.getAggregate('COUNT')) || 0; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + topicUsage[topicName] = conversationCount; |
| 66 | + |
| 67 | + // Track topics with zero usage |
| 68 | + if (isActive && isPublished && conversationCount === 0) { |
| 69 | + zeroUsageTopics.push(topicName); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Display results |
| 74 | + gs.info('\n=== Inactive Topics ==='); |
| 75 | + if (inactiveTopics.length > 0) { |
| 76 | + for (var i = 0; i < inactiveTopics.length; i++) { |
| 77 | + gs.info((i + 1) + '. ' + inactiveTopics[i]); |
| 78 | + } |
| 79 | + } else { |
| 80 | + gs.info('No inactive topics found'); |
| 81 | + } |
| 82 | + |
| 83 | + gs.info('\n=== Unpublished Topics ==='); |
| 84 | + if (unpublishedTopics.length > 0) { |
| 85 | + for (var j = 0; j < unpublishedTopics.length; j++) { |
| 86 | + gs.info((j + 1) + '. ' + unpublishedTopics[j]); |
| 87 | + } |
| 88 | + } else { |
| 89 | + gs.info('No unpublished topics found'); |
| 90 | + } |
| 91 | + |
| 92 | + gs.info('\n=== Topics with Zero Usage (Active & Published) ==='); |
| 93 | + if (zeroUsageTopics.length > 0) { |
| 94 | + for (var k = 0; k < zeroUsageTopics.length; k++) { |
| 95 | + gs.info((k + 1) + '. ' + zeroUsageTopics[k]); |
| 96 | + } |
| 97 | + } else { |
| 98 | + if (topicField) { |
| 99 | + gs.info('All active & published topics have been used'); |
| 100 | + } else { |
| 101 | + gs.info('Cannot analyze usage - conversation table not available'); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +gs.info('\n=== Analysis Complete ==='); |
0 commit comments