feat: batch diagnostics script

This commit is contained in:
Rafael Taranto 2025-05-25 14:16:20 +01:00
parent 4c270dcd65
commit 4a00fad44c
3 changed files with 76 additions and 1 deletions

View file

@ -0,0 +1,36 @@
#!/usr/bin/env node
require('../lib/environment-helper')
const db = require('../lib/db')
const machineLoader = require('../lib/machine-loader')
const operator = require('../lib/operator')
console.log('Running diagnostics on all paired devices...\n')
operator.getOperatorId('middleware')
.then(operatorId => {
if (!operatorId) {
throw new Error('Operator ID not found in database')
}
return db.any('SELECT device_id, name FROM devices')
.then(devices => ({ operatorId, devices }))
})
.then(({ operatorId, devices }) => {
if (devices.length === 0) {
console.log('No paired devices found.')
process.exit(0)
}
const deviceIds = devices.map(d => d.device_id)
return machineLoader.batchDiagnostics(deviceIds, operatorId)
})
.then(() => {
console.log('\n✓ Diagnostics initiated for all devices. It can take a few minutes for the results to appear on the admin.')
process.exit(0)
})
.catch(err => {
console.error('Error:', err.message)
process.exit(1)
})

View file

@ -523,6 +523,43 @@ function diagnostics(rec) {
)
}
function batchDiagnostics(deviceIds, operatorId) {
const diagnosticsDir = `${OPERATOR_DATA_DIR}/diagnostics/`
const removeDir = fsPromises
.rm(diagnosticsDir, { recursive: true })
.catch(err => {
if (err.code !== 'ENOENT') {
throw err
}
})
const sql = `UPDATE devices
SET diagnostics_timestamp = NULL,
diagnostics_scan_updated_at = NULL,
diagnostics_front_updated_at = NULL
WHERE device_id = ANY($1)`
// Send individual notifications for each machine
const sendNotifications = deviceIds.map(deviceId =>
db.none('NOTIFY $1:name, $2', [
'machineAction',
JSON.stringify({
action: 'diagnostics',
value: {
deviceId,
operatorId,
action: 'diagnostics',
},
}),
]),
)
return removeDir
.then(() => db.none(sql, [deviceIds]))
.then(() => Promise.all(sendNotifications))
}
function setMachine(rec, operatorId) {
rec.operatorId = operatorId
switch (rec.action) {
@ -681,4 +718,5 @@ module.exports = {
refillMachineUnits,
updateDiagnostics,
updateFailedQRScans,
batchDiagnostics,
}

View file

@ -123,7 +123,8 @@
"lamassu-eth-recovery": "./bin/lamassu-eth-recovery",
"lamassu-trx-recovery": "./bin/lamassu-trx-recovery",
"lamassu-update-cassettes": "./bin/lamassu-update-cassettes",
"lamassu-clean-parsed-id": "./bin/lamassu-clean-parsed-id"
"lamassu-clean-parsed-id": "./bin/lamassu-clean-parsed-id",
"lamassu-batch-diagnostics": "./bin/lamassu-batch-diagnostics"
},
"scripts": {
"dev": "concurrently \"npm:server\" \"npm:admin-server\"",