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

View file

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