|
1 | | -const { TestResultsDB, ObjectID } = require('../Database'); |
2 | | - |
3 | | -/** |
4 | | - * updateStats updates the cached statistics for a build based on excluded runs. |
5 | | - * |
6 | | - * @route POST /api/updateStats |
7 | | - * @group TrafficLight - Operations related to traffic light stats |
8 | | - * @param {string} id.body.required - build ID |
9 | | - * @param {string} benchmarkName.body.required - name of the benchmark |
10 | | - * @param {object} excludedRuns.body.required - map of build names to arrays of excluded iteration indices |
11 | | - * @return {object} 200 - success message |
12 | | - */ |
| 1 | +const { TestResultsDB } = require('../Database'); |
| 2 | +const ObjectID = require('mongodb').ObjectID; |
13 | 3 |
|
14 | 4 | module.exports = async (req, res) => { |
| 5 | + const db = new TestResultsDB(); |
| 6 | + |
| 7 | + const { |
| 8 | + testId, |
| 9 | + baselineId, |
| 10 | + benchmarkName, |
| 11 | + testStats, |
| 12 | + baselineStats, |
| 13 | + testDisabledIterations, |
| 14 | + baselineDisabledIterations |
| 15 | + } = req.body; |
| 16 | + |
| 17 | + console.log('Received update request:', { |
| 18 | + testId, |
| 19 | + baselineId, |
| 20 | + benchmarkName, |
| 21 | + testDisabledIterations, |
| 22 | + baselineDisabledIterations |
| 23 | + }); |
| 24 | + |
15 | 25 | try { |
16 | | - const { id, benchmarkName, excludedRuns } = req.body; |
17 | | - if (!id || !benchmarkName) { |
18 | | - return res |
19 | | - .status(400) |
20 | | - .send({ |
21 | | - error: 'Missing required parameters: id, benchmarkName', |
22 | | - }); |
23 | | - } |
24 | | - |
25 | | - const testResultsDB = new TestResultsDB(); |
26 | | - const build = await testResultsDB.findOne({ _id: new ObjectID(id) }); |
27 | | - |
28 | | - if (!build || !build.aggregateInfo) { |
29 | | - return res |
30 | | - .status(404) |
31 | | - .send({ error: 'Build or aggregated info not found' }); |
32 | | - } |
33 | | - |
34 | | - const aggregateInfo = build.aggregateInfo; |
35 | | - const keys = Object.keys(aggregateInfo); |
36 | | - |
37 | | - let updated = false; |
38 | | - for (const key of keys) { |
39 | | - const item = aggregateInfo[key]; |
40 | | - if ( |
41 | | - item.benchmarkName === benchmarkName && |
42 | | - excludedRuns[item.buildName] |
43 | | - ) { |
44 | | - // Update the item in aggregateInfo |
45 | | - // We'll store the excluded runs indices so the frontend can retrieve them later |
46 | | - item.excludedRuns = excludedRuns[item.buildName]; |
47 | | - updated = true; |
| 26 | + // Helper function to update a document |
| 27 | + const updateDocument = async (docId, stats, disabledIterations, buildType) => { |
| 28 | + // Find the document |
| 29 | + const doc = await db.findOne({ _id: new ObjectID(docId) }); |
| 30 | + |
| 31 | + if (!doc) { |
| 32 | + console.error(`Document not found: ${docId}`); |
| 33 | + return { success: false, error: `Document ${docId} not found` }; |
48 | 34 | } |
49 | | - } |
50 | | - |
51 | | - if (updated) { |
52 | | - await testResultsDB.update( |
53 | | - { _id: new ObjectID(id) }, |
54 | | - { $set: { aggregateInfo } } |
| 35 | + |
| 36 | + if (!doc.aggregateInfo) { |
| 37 | + console.error(`No aggregateInfo in document: ${docId}`); |
| 38 | + return { success: false, error: `No aggregateInfo in document` }; |
| 39 | + } |
| 40 | + |
| 41 | + // Find and update the matching aggregateInfo entry |
| 42 | + let updated = false; |
| 43 | + for (const key in doc.aggregateInfo) { |
| 44 | + const item = doc.aggregateInfo[key]; |
| 45 | + if (item.benchmarkName === benchmarkName && |
| 46 | + item.buildName && |
| 47 | + item.buildName.includes(buildType)) { |
| 48 | + |
| 49 | + if (item.metrics) { |
| 50 | + // Update each metric in this aggregateInfo |
| 51 | + item.metrics = item.metrics.map(metric => { |
| 52 | + return { |
| 53 | + ...metric, |
| 54 | + |
| 55 | + // original rawValues |
| 56 | + rawValues: metric.rawValues, |
| 57 | + |
| 58 | + //original statValues |
| 59 | + statValues: metric.statValues, |
| 60 | + |
| 61 | + // new field - filteredStatValues |
| 62 | + filteredStatValues: { |
| 63 | + mean: stats.mean, |
| 64 | + max: stats.max, |
| 65 | + min: stats.min, |
| 66 | + median: stats.median, |
| 67 | + std: stats.std, |
| 68 | + CI: stats.CI |
| 69 | + }, |
| 70 | + |
| 71 | + // disablediterations |
| 72 | + disabledIterations: disabledIterations || [] |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + updated = true; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (!updated) { |
| 82 | + console.error(`Benchmark ${benchmarkName} with type ${buildType} not found`); |
| 83 | + return { success: false, error: `Benchmark not found` }; |
| 84 | + } |
| 85 | + |
| 86 | + // Update the document in database |
| 87 | + await db.update( |
| 88 | + { _id: new ObjectID(docId) }, |
| 89 | + { $set: { aggregateInfo: doc.aggregateInfo } } |
55 | 90 | ); |
56 | | - res.send({ status: 'success', message: 'Statistics updated' }); |
57 | | - } else { |
58 | | - res.status(404).send({ |
59 | | - error: 'Matching benchmark/build not found for update', |
60 | | - }); |
| 91 | + |
| 92 | + console.log(`Successfully updated document: ${docId}`); |
| 93 | + return { success: true }; |
| 94 | + }; |
| 95 | + |
| 96 | + // Update test document |
| 97 | + const testResult = await updateDocument( |
| 98 | + testId, |
| 99 | + testStats, |
| 100 | + testDisabledIterations, |
| 101 | + 'test' |
| 102 | + ); |
| 103 | + |
| 104 | + if (!testResult.success) { |
| 105 | + return res.status(400).json(testResult); |
| 106 | + } |
| 107 | + |
| 108 | + // Update baseline document |
| 109 | + const baselineResult = await updateDocument( |
| 110 | + baselineId, |
| 111 | + baselineStats, |
| 112 | + baselineDisabledIterations, |
| 113 | + 'baseline' |
| 114 | + ); |
| 115 | + |
| 116 | + if (!baselineResult.success) { |
| 117 | + return res.status(400).json(baselineResult); |
61 | 118 | } |
| 119 | + |
| 120 | + res.json({ |
| 121 | + success: true, |
| 122 | + message: 'Statistics updated successfully' |
| 123 | + }); |
| 124 | + |
62 | 125 | } catch (error) { |
63 | | - res.status(500).send({ error: error.message }); |
| 126 | + console.error('Error updating stats:', error); |
| 127 | + res.status(500).json({ |
| 128 | + success: false, |
| 129 | + error: error.message |
| 130 | + }); |
64 | 131 | } |
65 | 132 | }; |
0 commit comments