70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
|
const axios = require('axios');
|
||
|
|
||
|
|
||
|
const host = 'http://localhost:3000';
|
||
|
// const host = 'https://definma-api.apps.de1.bosch-iot-cloud.com';
|
||
|
|
||
|
let errors = [];
|
||
|
|
||
|
async function fix() {
|
||
|
let res = await axios({
|
||
|
method: 'get',
|
||
|
url: host + '/samples?status[]=new&status[]=validated&status[]=deleted&fields[]=_id&fields[]=number',
|
||
|
auth: {
|
||
|
username: 'admin',
|
||
|
password: 'Abc123!#'
|
||
|
}
|
||
|
}).catch(err => {
|
||
|
if (err.response) {
|
||
|
console.error(err.response.data);
|
||
|
errors.push(`Could not fetch samples: ${JSON.stringify(err.response.data)}`);
|
||
|
}
|
||
|
});
|
||
|
console.log(res.data);
|
||
|
// for all samples
|
||
|
for (let sampleIndex in res.data) {
|
||
|
console.log(`SAMPLE ${sampleIndex}/${res.data.length}`);
|
||
|
const measurements = await axios({ // get all measurements
|
||
|
method: 'get',
|
||
|
url: host + '/measurement/sample/' + res.data[sampleIndex]._id,
|
||
|
auth: {
|
||
|
username: 'admin',
|
||
|
password: 'Abc123!#'
|
||
|
}
|
||
|
}).catch(err => {
|
||
|
if (err.response) {
|
||
|
console.error(err.response.data);
|
||
|
errors.push(`Could not fetch measurements: ${JSON.stringify(err.response.data)}`);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
if (measurements && measurements.data) { // found measurements
|
||
|
for (let measurementIndex in measurements.data) {
|
||
|
console.log(`${measurementIndex}/${measurements.data.length}`);
|
||
|
const measurement = measurements.data[measurementIndex];
|
||
|
if (measurement.values.hasOwnProperty('dpt')) { // is spectrum
|
||
|
await axios({ // get all measurements
|
||
|
method: 'put',
|
||
|
url: host + '/measurement/' + measurement._id,
|
||
|
auth: {
|
||
|
username: 'admin',
|
||
|
password: 'Abc123!#'
|
||
|
},
|
||
|
data: {values: {dpt: measurement.values.dpt
|
||
|
.filter(e => e.length === 2)
|
||
|
.map(e => e.map(el => parseFloat(el)))
|
||
|
}}
|
||
|
}).catch(err => {
|
||
|
if (err.response) {
|
||
|
console.error(err.response.data);
|
||
|
errors.push(`Could not save measurements: ${JSON.stringify(err.response.data)}`);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
console.log(errors);
|
||
|
}
|
||
|
|
||
|
fix();
|