-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-changes.js
More file actions
82 lines (69 loc) · 2.62 KB
/
analyze-changes.js
File metadata and controls
82 lines (69 loc) · 2.62 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import fs from 'fs'
const before = JSON.parse(fs.readFileSync('schedule-before.json', 'utf8'))
const after = JSON.parse(fs.readFileSync('schedule-after.json', 'utf8'))
// Group services by tech
const beforeByTech = {}
const afterByTech = {}
before.scheduledServices.forEach(service => {
if (!beforeByTech[service.techId]) beforeByTech[service.techId] = []
beforeByTech[service.techId].push(service)
})
after.scheduledServices.forEach(service => {
if (!afterByTech[service.techId]) afterByTech[service.techId] = []
afterByTech[service.techId].push(service)
})
// Analyze changes for each tech
Object.keys(beforeByTech).forEach(techId => {
const beforeServices = beforeByTech[techId]
const afterServices = afterByTech[techId]
if (!afterServices) {
console.log(`Tech ${techId} missing from after schedule!`)
return
}
console.log(`\nAnalyzing ${techId}:`)
console.log('Before:', beforeServices.length, 'services')
console.log('After:', afterServices.length, 'services')
// Compare service times
beforeServices.forEach(beforeService => {
const afterService = afterServices.find(s => s.id === beforeService.id)
if (!afterService) {
console.log(`Service ${beforeService.id} missing from after schedule!`)
return
}
const beforeStart = new Date(beforeService.start)
const afterStart = new Date(afterService.start)
const timeDiff = (afterStart - beforeStart) / (60 * 1000) // in minutes
if (Math.abs(timeDiff) > 1) { // Only show changes > 1 minute
console.log(
`Service ${beforeService.id} moved by ${Math.round(timeDiff)} minutes`,
timeDiff > 0 ? 'later' : 'earlier'
)
}
})
// Calculate gaps
const calculateGaps = services => {
const gaps = []
for (let i = 1; i < services.length; i++) {
const prevEnd = new Date(services[i-1].end)
const currentStart = new Date(services[i].start)
const gap = (currentStart - prevEnd) / (60 * 1000) // in minutes
if (gap > 15) { // Only count gaps > 15 minutes
gaps.push(gap)
}
}
return gaps
}
const beforeGaps = calculateGaps(beforeServices)
const afterGaps = calculateGaps(afterServices)
if (beforeGaps.length > 0 || afterGaps.length > 0) {
console.log('\nGaps analysis:')
if (beforeGaps.length > 0) {
console.log('Before:', beforeGaps.length, 'gaps,',
'Average:', Math.round(beforeGaps.reduce((a,b) => a+b, 0) / beforeGaps.length), 'minutes')
}
if (afterGaps.length > 0) {
console.log('After:', afterGaps.length, 'gaps,',
'Average:', Math.round(afterGaps.reduce((a,b) => a+b, 0) / afterGaps.length), 'minutes')
}
}
})