Merge pull request #1886 from siiky/fix/lam-1455/diagnostics

LAM-1455 fix: don't try to show diagnostics photos if they're missing
This commit is contained in:
Rafael Taranto 2025-06-17 13:29:13 +01:00 committed by GitHub
commit 1c8d542080
2 changed files with 31 additions and 26 deletions

View file

@ -56,6 +56,7 @@ const createCsv = async ({ machineLogsCsv }) => {
const DiagnosticsModal = ({ onClose, deviceId, sendAction }) => {
const [state, setState] = useState(STATES.INITIAL)
const [timestamp, setTimestamp] = useState(null)
const [diagnosticTimestamps, setDiagnosticTimestamps] = useState({})
const timeoutRef = useRef(null)
const [fetchSummary, { loading }] = useLazyQuery(MACHINE_LOGS, {
@ -81,6 +82,10 @@ const DiagnosticsModal = ({ onClose, deviceId, sendAction }) => {
timeoutRef.current = null
}
setTimestamp(data.machine.diagnostics.timestamp)
setDiagnosticTimestamps({
front: data.machine.diagnostics.frontTimestamp,
scan: data.machine.diagnostics.scanTimestamp,
})
setState(STATES.FILLED)
stopPolling()
}
@ -95,8 +100,6 @@ const DiagnosticsModal = ({ onClose, deviceId, sendAction }) => {
}
}, [])
const path = `/operator-data/diagnostics/${deviceId}/`
const runDiagnostics = () => {
setState(STATES.RUNNING)
startPolling(2000)
@ -116,6 +119,18 @@ const DiagnosticsModal = ({ onClose, deviceId, sendAction }) => {
const messageClass = 'm-auto flex flex-col items-center justify-center'
const showPhoto = diagnosticName => {
console.log(diagnosticName, diagnosticTimestamps)
return diagnosticTimestamps[diagnosticName] ? (
<img
className="w-88"
src={`/operator-data/diagnostics/${deviceId}/${diagnosticName}.jpg?${Date.now()}`}
/>
) : (
<>Failed getting photo</>
)
}
return (
<Modal
closeOnBackdropClick={true}
@ -152,23 +167,14 @@ const DiagnosticsModal = ({ onClose, deviceId, sendAction }) => {
{state === STATES.FILLED && (
<div>
<div className="flex mt-6">
<div className="flex justify-around mt-6">
<div>
<H3>Scan</H3>
<img
className="w-88"
src={`${path}scan.jpg?${Date.now()}`}
alt="Failure getting photo"
/>
{showPhoto('scan')}
</div>
<div>
<H3>Front</H3>
<img
className="w-88"
src={`${path}front.jpg?${Date.now()}`}
alt="Failure getting photo"
/>
<P></P>
{showPhoto('front')}
</div>
</div>
<div>

View file

@ -61,11 +61,11 @@ function toMachineObject(r) {
timestamp: r.diagnostics_timestamp
? new Date(r.diagnostics_timestamp)
: null,
scanTimestamp: r.diagnostics_scan_timestamp
? new Date(r.diagnostics_scan_timestamp)
scanTimestamp: r.diagnostics_scan_updated_at
? new Date(r.diagnostics_scan_updated_at)
: null,
frontTimestamp: r.diagnostics_front_timestamp
? new Date(r.diagnostics_front_timestamp)
frontTimestamp: r.diagnostics_front_updated_at
? new Date(r.diagnostics_front_updated_at)
: null,
},
pairedAt: new Date(r.created),
@ -676,24 +676,23 @@ function updateDiagnostics(deviceId, images) {
['scan.jpg', scan],
['front.jpg', front],
])
.then(() => db.none(sql, [deviceId, !!scan, !!front]))
.then(([scan, front]) => db.none(sql, [deviceId, scan, front]))
.catch(err => logger.error('while running machine diagnostics: ', err))
}
const updateFailedQRScans = (deviceId, frames) => {
const timestamp = new Date().toISOString()
const directory = `${OPERATOR_DATA_DIR}/failedQRScans/${deviceId}/`
const filenames = _.map(
no => `${timestamp}-${no}.jpg`,
_.range(0, _.size(frames)),
return updatePhotos(
directory,
frames.map((frame, no) => [`${timestamp}-${no}.jpg`, frame]),
)
return updatePhotos(directory, _.zip(filenames, frames))
}
function createPhoto(name, data, dir) {
if (!data) {
logger.error(`Diagnostics error: No data to save for ${name} photo`)
return Promise.resolve()
return Promise.reject()
}
const decodedImageData = Buffer.from(data, 'base64')
@ -704,9 +703,9 @@ function createPhoto(name, data, dir) {
function updatePhotos(dir, photoPairs) {
const dirname = path.join(dir)
_.attempt(() => makeDir.sync(dirname))
return Promise.all(
return Promise.allSettled(
photoPairs.map(([filename, data]) => createPhoto(filename, data, dirname)),
)
).then(savedPhotos => savedPhotos.map(res => res.status === 'fulfilled'))
}
let pendingRecordPings = new Map()