-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.js
80 lines (72 loc) · 2.85 KB
/
log.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
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { PSDB } from 'planetscale-node'
const conn = new PSDB('main')
export default async (req, res) => {
const {
method,
query: { token }
} = req
switch (method) {
case 'POST':
try {
const { metrics, hash, timestamp, branch, baseMetrics, baseHash } = req.body
const [[project]] = await conn.query('select id from project where token = ?', token)
if (!project) {
throw 'Invalid token'
}
const [availableMetrics] = await conn.query('select * from metric where project_id = ?', project.id)
// No base perfs yet, fill base values.
const [basePerfs] = await conn.query('select * from perf where hash = ? and project_id = ?', [
baseHash,
project.id
])
const baseTuples = []
Object.entries(baseMetrics).forEach(([metricKey, metricValue]) => {
const availableMetric = availableMetrics.find((m) => m.key === metricKey)
const baseValue = availableMetric ? basePerfs.find((p) => p.metric_id === availableMetric.id) : null
if (availableMetric && !baseValue) {
baseTuples.push([project.id, baseHash, baseHash, availableMetric.id, metricValue, new Date(timestamp)])
}
})
for (const tuple of baseTuples) {
await conn.query(
'insert into perf (project_id, branch, hash, metric_id, value, measured_at) values (?,?,?,?,?,?)',
tuple
)
}
// Insert and normalize metrics
const tuples = []
Object.entries(metrics).forEach(([metricKey, metricValue]) => {
const availableMetric = availableMetrics.find((m) => m.key === metricKey)
const baseValue = availableMetric ? basePerfs.find((p) => p.metric_id === availableMetric.id) : null
if (availableMetric) {
tuples.push([
project.id,
branch,
hash,
availableMetric.id,
baseValue ? (metricValue * baseValue.value) / baseMetrics[availableMetric.key] : metricValue,
new Date(timestamp)
])
}
})
for (const tuple of tuples) {
await conn.query(
'insert into perf (project_id, branch, hash, metric_id, value, measured_at) values (?,?,?,?,?,?)',
tuple
)
}
res.statusCode = 200
res.json({ status: 'ok', count: tuples.length })
} catch (e) {
const error = new Error('An error occurred while connecting to the database')
error.status = 500
error.info = { message: 'An error occurred while connecting to the database' }
throw e
}
break
default:
res.setHeader('Allow', ['GET', 'PUT'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}