Skip to content

Commit

Permalink
Feat: cleanup auto export trigger code
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebonnici committed Mar 8, 2024
1 parent 7b125a2 commit ad410b3
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 148 deletions.
82 changes: 5 additions & 77 deletions src/actions/deviceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import {
AppThunk,
Device,
logger,
telemetry,
} from '@nordicsemiconductor/pc-nrfconnect-shared';
import describeError from '@nordicsemiconductor/pc-nrfconnect-shared/src/logging/describeError';
import { unit } from 'mathjs';
import path from 'path';

import { resetCache } from '../components/Chart/data/dataAccumulator';
import SerialDevice from '../device/serialDevice';
Expand Down Expand Up @@ -66,11 +64,7 @@ import {
import { updateGainsAction } from '../slices/gainsSlice';
import {
clearProgress,
deregisterSaveEvent,
getAutoExportTrigger,
registerSaveEvent,
resetTriggerOrigin,
setAutoExportTrigger,
setProgress,
setTriggerActive,
setTriggerOrigin,
Expand All @@ -81,7 +75,6 @@ import { convertTimeToSeconds } from '../utils/duration';
import { isDiskFull } from '../utils/fileUtils';
import { isDataLoggerPane } from '../utils/panes';
import { setSpikeFilter as persistSpikeFilter } from '../utils/persistentStore';
import saveData, { PPK2Metadata } from '../utils/saveFileHandler';

let device: null | SerialDevice = null;
let updateRequestInterval: NodeJS.Timeout | undefined;
Expand Down Expand Up @@ -535,72 +528,13 @@ export const processTrigger =
const recordingDuration = indexToTimestamp(
numberOfBytes / frameSize
);
const savePath = getState().app.trigger.savePath;
const shouldSave = getState().app.trigger.autoExportTrigger;

const createSessionData = DataManager().createSessionData;
let session:
| Awaited<ReturnType<typeof createSessionData>>
| undefined;
let savePromise: Promise<void> | undefined;

if (shouldSave && savePath) {
// createSession
session = await createSessionData(
buffer,
getSessionRootFolder(getState()),
info.absoluteTime
);

const dataToBeSaved: PPK2Metadata = {
metadata: {
samplesPerSecond: DataManager().getSamplesPerSecond(),
startSystemTime: info.absoluteTime,
},
};

dispatch(registerSaveEvent());
savePromise = saveData(
path.join(
savePath,
`${info.absoluteTime + info.timeRange.start} - ${
info.absoluteTime + info.timeRange.end
}.ppk2`
),
dataToBeSaved,
session.fileBuffer,
session.foldingBuffer
)
.then(async () => {
if (
(await isDiskFull(
getDiskFullTrigger(getState()),
getSessionRootFolder(getState())
)) &&
getAutoExportTrigger(getState())
) {
telemetry.sendEvent('Auto Export', {
state: false,
reason: 'Disk Full',
});
logger.warn(
'Auto export was turned off due disk being full'
);
dispatch(setAutoExportTrigger(false));
}
})
.finally(() => {
dispatch(deregisterSaveEvent());
});
}

// createSession
if (!session)
session = await createSessionData(
buffer,
getSessionRootFolder(getState()),
info.absoluteTime
);
const session = await createSessionData(
buffer,
getSessionRootFolder(getState()),
info.absoluteTime
);

dispatch(setTriggerOrigin(indexToTimestamp(info.triggerOrigin)));

Expand All @@ -621,12 +555,6 @@ export const processTrigger =
releaseLastSession?.();
releaseLastSession = undefined;
releaseLastSession = async () => {
try {
await savePromise;
} catch {
// do nothing
}

try {
await session?.fileBuffer.close();
session?.fileBuffer.release();
Expand Down
13 changes: 1 addition & 12 deletions src/components/SidePanel/StartStop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@ import {
dataLoggerState,
getSampleFrequency,
} from '../../slices/dataLoggerSlice';
import {
getAutoExportTrigger,
resetTriggerOrigin,
setTriggerSavePath,
} from '../../slices/triggerSlice';
import { resetTriggerOrigin } from '../../slices/triggerSlice';
import { convertTimeToSeconds, formatDuration } from '../../utils/duration';
import {
calcFileSize,
getFreeSpace,
remainingTime as calcRemainingTime,
selectDirectoryDialog,
} from '../../utils/fileUtils';
import { isDataLoggerPane, isScopePane } from '../../utils/panes';
import {
Expand All @@ -61,7 +56,6 @@ const calcFileSizeString = (sampleFreq: number, durationSeconds: number) => {
export default () => {
const dispatch = useDispatch();
const scopePane = useSelector(isScopePane);
const autoExport = useSelector(getAutoExportTrigger);
const dataLoggerPane = useSelector(isDataLoggerPane);
const recordingMode = useSelector(getRecordingMode);
const { samplingRunning } = useSelector(appState);
Expand All @@ -82,11 +76,6 @@ export default () => {
const [showDialog, setShowDialog] = useState(false);

const startAndClear = async () => {
if (scopePane && autoExport) {
const filePath = await selectDirectoryDialog();
dispatch(setTriggerSavePath(filePath));
}

await DataManager().reset();
dispatch(resetChartTime());
dispatch(resetTriggerOrigin());
Expand Down
20 changes: 0 additions & 20 deletions src/components/SidePanel/TriggerSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
DropdownItem,
logger,
NumberInput,
StateSelector,
telemetry,
} from '@nordicsemiconductor/pc-nrfconnect-shared';

import { appState } from '../../slices/appSlice';
import { getRecordingMode } from '../../slices/chartSlice';
import {
getAutoExportTrigger,
getSavingEventQueueLength,
getTriggerRecordingLength,
getTriggerType,
getTriggerValue,
setAutoExportTrigger,
setTriggerLevel,
setTriggerRecordingLength,
setTriggerType,
Expand Down Expand Up @@ -55,8 +50,6 @@ export default () => {
const recordingLength = useSelector(getTriggerRecordingLength);
const triggerValue = useSelector(getTriggerValue);
const triggerType = useSelector(getTriggerType);
const triggerSaveQueueLength = useSelector(getSavingEventQueueLength);
const autoExportTrigger = useSelector(getAutoExportTrigger);
const { samplingRunning } = useSelector(appState);
const dataLoggerActive =
useSelector(getRecordingMode) === 'DataLogger' && samplingRunning;
Expand All @@ -68,19 +61,6 @@ export default () => {
label: value,
}));

useEffect(() => {
if (triggerSaveQueueLength >= 10 && autoExportTrigger) {
dispatch(setAutoExportTrigger(false));
telemetry.sendEvent('Auto Export', {
state: false,
reason: 'excessive number of triggers',
});
logger.warn(
'Unable to keep up with saving triggers. Auto export was turned off due to excessive number of triggers'
);
}
}, [autoExportTrigger, dispatch, triggerSaveQueueLength]);

const [internalTriggerValue, setInternalTriggerValue] =
useState(triggerValue);
const [internalTriggerLength, setInternalTriggerLength] =
Expand Down
34 changes: 0 additions & 34 deletions src/slices/triggerSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

import {
getAutoExport as getPersistedAutoExport,
getRecordingLength as getPersistedRecordingLength,
getTriggerLevel as getPersistedTriggerLevel,
getTriggerType as getPersistedTriggerType,
setAutoExport as persistAutoExport,
setRecordingLength as persistRecordingLength,
setTriggerLevel as persistTriggerLevel,
setTriggerType as persistTriggerType,
Expand All @@ -24,23 +22,18 @@ export type TriggerType = (typeof TriggerTypeValues)[number];
export interface DataLoggerState {
level: number;
recordingLength: number;
autoExportTrigger: boolean;
savePath?: string;
active: boolean;
type: TriggerType;
progressMessage?: string;
progress?: number;
savingEventQueueLength: number;
triggerOrigin?: number;
}

const initialState = (): DataLoggerState => ({
level: getPersistedTriggerLevel(1000),
recordingLength: getPersistedRecordingLength(1000),
autoExportTrigger: getPersistedAutoExport(false),
active: false,
type: getPersistedTriggerType('Single'),
savingEventQueueLength: 0,
});

const triggerSlice = createSlice({
Expand All @@ -56,13 +49,6 @@ const triggerSlice = createSlice({
state.recordingLength = action.payload;
persistRecordingLength(action.payload);
},
setAutoExportTrigger: (state, action: PayloadAction<boolean>) => {
state.autoExportTrigger = action.payload;
persistAutoExport(action.payload);
},
setTriggerSavePath: (state, action: PayloadAction<string>) => {
state.savePath = action.payload;
},
setTriggerActive: (state, action: PayloadAction<boolean>) => {
state.active = action.payload;
},
Expand All @@ -84,15 +70,6 @@ const triggerSlice = createSlice({
state.progress = undefined;
state.progressMessage = undefined;
},
resetSaveEventQueueLength: state => {
state.savingEventQueueLength = 0;
},
registerSaveEvent: state => {
state.savingEventQueueLength += 1;
},
deregisterSaveEvent: state => {
state.savingEventQueueLength -= 1;
},
setTriggerOrigin: (state, action: PayloadAction<number>) => {
state.triggerOrigin = action.payload;
},
Expand All @@ -104,33 +81,22 @@ const triggerSlice = createSlice({
export const getTriggerValue = (state: RootState) => state.app.trigger.level;
export const getTriggerRecordingLength = (state: RootState) =>
state.app.trigger.recordingLength;
export const getAutoExportTrigger = (state: RootState) =>
state.app.trigger.autoExportTrigger;
export const getTriggerSavePath = (state: RootState) =>
state.app.trigger.savePath;
export const getTriggerActive = (state: RootState) => state.app.trigger.active;
export const getTriggerType = (state: RootState) => state.app.trigger.type;
export const getProgress = (state: RootState) => ({
progressMessage: state.app.trigger.progressMessage,
progress: state.app.trigger.progress,
});
export const getSavingEventQueueLength = (state: RootState) =>
state.app.trigger.savingEventQueueLength;
export const getTriggerOrigin = (state: RootState) =>
state.app.trigger.triggerOrigin;

export const {
setTriggerLevel,
setTriggerRecordingLength,
setAutoExportTrigger,
setTriggerSavePath,
setTriggerActive,
setTriggerType,
setProgress,
clearProgress,
resetSaveEventQueueLength,
registerSaveEvent,
deregisterSaveEvent,
setTriggerOrigin,
resetTriggerOrigin,
} = triggerSlice.actions;
Expand Down
5 changes: 0 additions & 5 deletions src/utils/persistentStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ export const getRecordingLength = (defaultValue: number) =>
export const setRecordingLength = (value: number) => {
store.set(`recording-length-ms`, value);
};
export const getAutoExport = (defaultValue: boolean) =>
store.get(`auto-export-trigger`, defaultValue);
export const setAutoExport = (value: boolean) => {
store.set(`auto-export-trigger`, value);
};
export const getTriggerType = (defaultValue: TriggerType) =>
store.get(`trigger-mode-type`, defaultValue);
export const setTriggerType = (value: TriggerType) => {
Expand Down

0 comments on commit ad410b3

Please sign in to comment.