/
StoryStats.js
241 lines (207 loc) · 6.79 KB
/
StoryStats.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/** @format */
'use strict'
const config = require('config')
const path = require('path')
const os = require('os')
const fs = require('fs')
const debug = require('debug')('StoryStats')
class StoryStats {
constructor(dbFilename = `${config.dataPath}${path.sep}jira-stats.db`) {
if (fs.existsSync(dbFilename)) {
this.db = require('better-sqlite3')(dbFilename, { readonly: true })
} else {
console.error(`@16: Couldn't find db file (${dbFilename}).`)
process.exit(10)
}
}
getChangeStats(
startDate,
endDate,
inComponent = false,
inFixVersion = false
) {
let prevRow, prevKey
let processRow = false
const componentsFound = []
let results = {}
let releaseFilter = ''
let componentFilter = ''
if (inComponent) {
componentFilter = ` AND Component like '%${inComponent}%'`
}
if (inFixVersion) {
releaseFilter = ` AND fixVersion='${inFixVersion}' `
}
// Additions
const addSql = `select * from 'story-stats' where date='${endDate}' ${componentFilter} ${releaseFilter} and key not in (select key from 'story-stats' where date='${startDate}' ${componentFilter} ${releaseFilter}) group by key order by key,date ASC`
debug(addSql)
const addRows = this.db.prepare(addSql).all()
addRows.forEach((row) => {
let components = ['none']
if (row.component) {
components = row.component.split(',')
}
components.forEach((component) => {
if (!inComponent || component == inComponent) {
if (!Object.keys(results).includes(component)) {
results[component] = {
count: 0,
changed: 0,
completed: 0,
total: 0,
changes: [],
additions: [],
}
}
results[component].count++
results[component].additions.push(row)
}
})
})
// Changes
const changeSql = `select * from 'story-stats' where ( date='${endDate}' and key in (select key from 'story-stats' where date='${startDate}' ${componentFilter} ${releaseFilter})) or (date='${startDate}' ${componentFilter} ${releaseFilter}) order by key,date ASC`
debug(changeSql)
debug(`startDate: ${startDate}; startYMD: ${startDate}`)
debug(`endDate: ${endDate}; endYMD: ${endDate}`)
const rows = this.db.prepare(changeSql).all()
debug(`Results count: `, rows.length)
rows.forEach((row) => {
processRow = row.key == prevKey
if (processRow) {
let components = ['none']
if (row.component) {
components = row.component.split(',')
}
components.forEach((component) => {
// debug(`Processing Component: ${component}`)
if (!componentsFound.includes(component)) {
componentsFound.push(component)
}
if (!inComponent || component == inComponent) {
// debug(`Continuing with inComponent ${inComponent}`)
if (!Object.keys(results).includes(component)) {
results[component] = {
count: 0,
changed: 0,
completed: 0,
total: 0,
changes: [],
additions: [],
}
}
results[component].count++
if (
row.total !== prevRow.total ||
row.progress !== prevRow.progress ||
row.fixVersion !== prevRow.fixVersion ||
row.status !== prevRow.status
) {
results[component].changed++
}
results[component].completed += row.progress - prevRow.progress
results[component].total += row.total
// Change in data
if (
prevRow.progress !== row.progress ||
prevRow.total !== row.total ||
prevRow.status !== row.status ||
prevRow.fixVersion !== row.fixVersion
) {
results[component].changes.push({
key: row.key,
dates: [prevRow.date, row.date],
progress:
prevRow.progress == row.progress
? row.progress
: [prevRow.progress, row.progress],
status:
prevRow.status == row.status
? row.status
: [prevRow.status, row.status],
total:
prevRow.total == row.total
? row.total
: [prevRow.total, row.total],
fixVersion:
prevRow.fixVersion == row.fixVersion
? row.fixVersion
: [prevRow.fixVersion, row.fixVersion],
})
}
}
})
}
prevRow = row
prevKey = row.key
})
return results
}
/**
*Return a top-level list of the components, story count, story mod count, and expended and total time
*
* @param {Date} startDate
* @param {Date} endDate
* @param {String} [component=false]
* @param {String} [fixVersion=false]
* @memberof StoryStats
*/
getSummaryReport(
startDate,
endDate,
inComponent = false,
inFixVersion = false
) {
debug(`inComponent: ${inComponent}`)
const changes = this.getChangeStats(
startDate,
endDate,
inComponent,
inFixVersion
)
return changes
}
/**
* Fetch the list of seen Components
*
* @param {boolean} [forceRefresh=false] Always refresh the list?
* @returns {array} List of components (alphabetically sorted)
* @memberof StoryStats
*/
getComponentList(forceRefresh = false) {
if (forceRefresh || !this.components || this.components.length == 0) {
this.components = []
// Get the complete list of components in the db
let results = this.db
.prepare(
`SELECT DISTINCT Component FROM 'story-stats' WHERE Component <> '' ORDER BY Component`
)
.all()
results.forEach((c) => {
if (c.component) {
// Handle multiple components in one string
let c2 = c.component.split(',')
c2.forEach((c3) => {
// Only add the component on first sighting
if (!this.components.includes(c3)) {
this.components.push(c3)
}
})
}
})
this.components.sort()
}
return this.components
}
getFullDailyReport(releaseName) {
const componentList = this.getComponentList()
const sql = `select date as keyDate, (sum(total)/28800) as 'total (d)', component
from 'story-stats'
where fixVersion=?
group by date,component`
const rows = this.db.prepare(sql).all(releaseName)
debug(`Results count: `, rows.length)
let data = []
rows.forEach((row) => {})
}
}
module.exports = StoryStats