Skip to content

Commit

Permalink
Feat: update free space and trigger check on every page write
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebonnici committed Apr 4, 2024
1 parent f0d2e82 commit 4deba50
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
36 changes: 17 additions & 19 deletions src/actions/deviceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import { setSpikeFilter as persistSpikeFilter } from '../utils/persistentStore';

let device: null | SerialDevice = null;
let updateRequestInterval: NodeJS.Timeout | undefined;
let releaseFileWriteListener: (() => void) | undefined;

export const setupOptions =
(recordingMode: RecordingMode): AppThunk<RootState, Promise<void>> =>
Expand All @@ -99,6 +100,21 @@ export const setupOptions =
DataManager().initializeTriggerSession(60);
break;
}

releaseFileWriteListener?.();
releaseFileWriteListener = DataManager().onFileWrite?.(() => {
isDiskFull(
getDiskFullTrigger(getState()),
getSessionRootFolder(getState())
).then(isFull => {
if (isFull) {
logger.warn(
'Session stopped. Disk full trigger detected'
);
dispatch(samplingStop());
}
});
});
} catch (err) {
logger.error(err);
}
Expand Down Expand Up @@ -145,6 +161,7 @@ export const samplingStop =
dispatch(samplingStoppedAction());
await device.ppkAverageStop();
stopPreventSleep();
releaseFileWriteListener?.();
};

export const updateSpikeFilter = (): AppThunk<RootState> => (_, getState) => {
Expand Down Expand Up @@ -207,7 +224,6 @@ export const open =
let prevBits = 0;
let nbSamples = 0;
let nbSamplesTotal = 0;
let lastDiskFullCheck = 0;

const onSample = ({ value, bits }: SampleValues) => {
const {
Expand Down Expand Up @@ -302,24 +318,6 @@ export const open =
dispatch(setSavePending(true));
}

const shouldCheckDiskFull =
performance.now() - lastDiskFullCheck > 10_000;

if (shouldCheckDiskFull) {
lastDiskFullCheck = performance.now();
isDiskFull(
getDiskFullTrigger(getState()),
getSessionRootFolder(getState())
).then(isFull => {
if (isFull) {
logger.warn(
'Session stopped. Disk full trigger detected'
);
dispatch(samplingStop());
}
});
}

const durationInMicroSeconds =
convertTimeToSeconds(
getState().app.dataLogger.duration,
Expand Down
13 changes: 11 additions & 2 deletions src/components/SidePanel/StartStop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
*/

import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
ConfirmationDialog,
Expand Down Expand Up @@ -57,6 +57,8 @@ const calcFileSizeString = (sampleFreq: number, durationSeconds: number) => {

export default () => {
const dispatch = useDispatch();

const onWriteListener = useRef<() => void>();
const scopePane = useSelector(isScopePane);
const dataLoggerPane = useSelector(isDataLoggerPane);
const recordingMode = useSelector(getRecordingMode);
Expand Down Expand Up @@ -103,8 +105,14 @@ export default () => {
return;
}

dispatch(samplingStart());
setShowDialog(false);
await dispatch(samplingStart());
onWriteListener.current?.();
onWriteListener.current = DataManager().onFileWrite(() => {
getFreeSpace(diskFullTrigger, sessionFolder).then(s => {
setFreeSpace(Math.max(0, s));
});
});
};

const [freeSpace, setFreeSpace] = useState<number>(0);
Expand Down Expand Up @@ -143,6 +151,7 @@ export default () => {
stopText="Stop"
onClick={async () => {
if (samplingRunning) {
onWriteListener.current?.();
dispatch(samplingStop());
telemetry.sendEvent('StopSampling', {
mode: recordingMode,
Expand Down
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ export const DataManager = () => ({
});
}),
hasPendingTriggers: () => options.timeReachedTriggers.length > 0,
onFileWrite: (listener: () => void) =>
options.fileBuffer?.onFileWrite(listener),
});

/**
Expand Down
20 changes: 19 additions & 1 deletion src/utils/FileBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class FileBuffer {
#fileBusy = false;
#beforeUnload: (event: BeforeUnloadEvent) => Promise<void>;
#bufferingListeners: ((event: Promise<void>) => void)[] = [];
#fileWriteListeners: (() => void)[] = [];
#freePageBuffers: Uint8Array[] = [];
#bufferingRequests: Promise<void>[] = [];
#cancelBufferOperations: WeakMap<Promise<void>, AbortController> =
Expand Down Expand Up @@ -125,7 +126,10 @@ export class FileBuffer {
throw new Error('Invalid File handle');
return fs
.appendFile(this.#fileHandle, activePage.page)
.finally(resolve);
.finally(() => {
this.#fileWriteListeners.forEach(l => l());
resolve();
});
});
} else {
this.fileOperationTasks.push(() => {
Expand Down Expand Up @@ -577,4 +581,18 @@ export class FileBuffer {
}
};
}

onFileWrite(listener: () => void) {
this.#fileWriteListeners.push(listener);

return () => {
const index = this.#fileWriteListeners.findIndex(
l => l === listener
);

if (index !== -1) {
this.#fileWriteListeners.splice(index, 1);
}
};
}
}

0 comments on commit 4deba50

Please sign in to comment.