forked from alphagov/government-frontend-survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-comparisons.js
211 lines (179 loc) · 5.47 KB
/
generate-comparisons.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const fs = require('fs')
const outdent = require('outdent')
const GithubSlugger = require('github-slugger')
let slugger = new GithubSlugger()
function getTotals (data) {
return data.map(response => {
const total = (
Object
.entries(response.answers)
.map(answer => answer[1])
// Add all answer results together
.reduce((a, b) => a + b, 0)
)
return {
...response,
total
}
})
}
const UNDEFINED_LABEL = 'N/A'
function mergeAndSum (start, end) {
// Warning, crap lazy code incoming...
Object
.entries(end)
.forEach(obj => {
let key = obj[0]
let value = obj[1]
end[key] = [UNDEFINED_LABEL, value]
})
Object
.entries(start)
.forEach(obj => {
let key = obj[0]
let value = obj[1]
start[key] = [value, UNDEFINED_LABEL]
})
let mergedObject = start
// Sum up shared keys
Object
.entries(end)
.forEach(obj => {
let key = obj[0]
let value = obj[1]
if (typeof mergedObject[key] !== 'undefined') {
mergedObject[key][1] = value[1]
}
})
Object
.entries(end)
.forEach(obj => {
let key = obj[0]
let value = obj[1]
if (typeof mergedObject[key] !== 'undefined') {
mergedObject[key].push(value[0])
}
})
// Merge in any keys that were not shared
return Object.assign({}, end, mergedObject)
}
function generateComparison (startYear, endYear) {
const startData = require(`./data/${startYear}.json`)
const endData = require(`./data/${endYear}.json`)
const notes = require(`./data/${startYear}-${endYear}-notes.json`)
const startResponses = getTotals(startData)
const endResponses = getTotals(endData)
let output = outdent`
# Government frontend survey comparison between ${startYear} and ${endYear}
`
startResponses.forEach(response => {
slugger.reset()
let endResponse = endResponses.find(endResponse => endResponse.id === response.id)
const mergedAndSummedAnswers = mergeAndSum(
response.answers,
endResponse.answers
)
const title = `Question ${response.id}: ${response.question}`
const slug = slugger.slug(title)
const noteText = notes.filter(note => note.id === response.id).map(note => note.text)
let answers
if (response.type === 'open') {
const formattedAnswers =
Object
.entries(mergedAndSummedAnswers)
.map(answer => {
if (answer[1][0] !== UNDEFINED_LABEL) {
if (answer[1][0] === 'Done') {
answer[1][0] = 'N/A (Done)'
} else {
answer[1][0] = 'Yes'
}
} else {
answer[1][0] = 'No'
}
if (answer[1][1] !== UNDEFINED_LABEL) {
if (answer[1][1] === 'Done') {
answer[1][1] = 'N/A (Done)'
} else {
answer[1][1] = 'Yes'
}
} else {
answer[1][1] = 'No'
}
return answer
})
.sort((a, b) => {
const valueA = (a[1][0])
const valueB = (b[1][0])
const valueEndA = (a[1][1])
const valueEndB = (b[1][1])
if (
(valueB + valueEndB) <
(valueA + valueEndA)
) {
return -1;
}
return 1;
})
.map(answer => {
let key = answer[0]
let value = answer[1][0]
let endValue = answer[1][1]
return `| ${key} | ${value} | ${endValue} |`
}).join('\n')
answers = outdent`
| Name | ${startYear} | ${endYear} |
| --- | --- | --- |
${formattedAnswers}
`
} else {
const formattedAnswers =
Object
.entries(mergedAndSummedAnswers)
.map(answer => {
let value = answer[1][0]
let endValue = answer[1][1]
let percentage = !isNaN(value) ? ((value / response.total) * 100).toFixed(1) : 0
let endPercentage = !isNaN(endValue) ? ((endValue / endResponse.total) * 100).toFixed(1) : 0
answer[1][0] = percentage
answer[1][1] = endPercentage
return answer
})
.sort((a, b) => {
const valueA = parseInt(a[1][1])
const valueB = parseInt(b[1][1])
if (valueA > valueB) {
return -1;
}
return 1;
})
.map(answer => {
let key = answer[0]
let value = answer[1][0] ? answer[1][0] + '%' : UNDEFINED_LABEL
let endValue = answer[1][1] ? answer[1][1] + '%' : UNDEFINED_LABEL
return `| ${key} | ${value} | ${endValue} |`
}).join('\n')
answers = outdent`
| Name | Percentage (${startYear}) | Percentage (${endYear}) |
| --- | --- | --- |
${formattedAnswers}
`
}
output += outdent`
## Question ${response.id}: ${response.question}
${noteText}
### Answers
${answers}
### Sources
- [${startYear} results for question ${response.id}](./results-${startYear}.md#${slug})
- [${endYear} results for question ${response.id}](./results-${endYear}.md#${slug})
`
})
fs.writeFile(`./comparison-${startYear}-${endYear}.md`, output, (error) => {
if (error) {
return console.log(err)
}
console.log(`comparison-${startYear}-${endYear}.md updated`)
})
}
generateComparison(2016, 2018)