Skip to content

Commit

Permalink
Purge states should purge duckDB states + remove purge state agregate…
Browse files Browse the repository at this point in the history
…s + improve shutdown DB closing
  • Loading branch information
Pierre-Gilles committed Aug 8, 2024
1 parent 2293a6c commit d64a365
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 222 deletions.
6 changes: 0 additions & 6 deletions front/src/actions/signup/signupSetPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ function createActions(store) {
await state.httpClient.post(`/api/v1/variable/${SYSTEM_VARIABLE_NAMES.DEVICE_STATE_HISTORY_IN_DAYS}`, {
value: state.signupSystemPreferences[SYSTEM_VARIABLE_NAMES.DEVICE_STATE_HISTORY_IN_DAYS]
});
await state.httpClient.post(
`/api/v1/variable/${SYSTEM_VARIABLE_NAMES.DEVICE_AGGREGATE_STATE_HISTORY_IN_DAYS}`,
{
value: state.signupSystemPreferences[SYSTEM_VARIABLE_NAMES.DEVICE_STATE_HISTORY_IN_DAYS]
}
);
store.setState({
signupSaveSystemPreferences: RequestStatus.Success
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import SettingsSystemContainers from './SettingsSystemContainers';
import SettingsSystemOperations from './SettingsSystemOperations';
import SettingsSystemTimezone from './SettingsSystemTimezone';
import SettingsSystemKeepDeviceHistory from './SettingsSystemKeepDeviceHistory';
import SettingsSystemKeepAggregatedStates from './SettingsSystemKeepAggregatedStates';
import SettingsSystemTimeExpiryState from './SettingsSystemTimeExpiryState';
import SettingsSystemDatabaseCleaning from './SettingsSystemDatabaseCleaning';
import SettingsSystemDuckDbMigration from './SettingsSystemDuckDbMigration';
Expand Down Expand Up @@ -89,7 +88,6 @@ const SystemPage = ({ children, ...props }) => (
<SettingsSystemOperations />
<SettingsSystemDuckDbMigration />
<SettingsSystemKeepDeviceHistory />
<SettingsSystemKeepAggregatedStates />
<SettingsSystemTimeExpiryState />
</div>
<div class="col-lg-6">
Expand Down
25 changes: 17 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,35 @@ process.on('uncaughtException', (error, promise) => {
logger.error(error);
});

const shutdown = async (signal) => {
logger.info(`${signal} received.`);
// We give Gladys 10 seconds to properly shutdown, otherwise we do it
setTimeout(() => {
logger.info('Timeout to shutdown expired, forcing shut down.');
process.exit();
}, 10 * 1000);
logger.info('Closing database connections.');
const closeSQLite = async () => {
try {
await db.sequelize.close();
logger.info('SQLite closed.');
} catch (e) {
logger.info('SQLite database is probably already closed');
logger.warn(e);
}
};

const closeDuckDB = async () => {
try {
await db.duckDb.close();
logger.info('DuckDB closed.');
} catch (e) {
logger.info('DuckDB database is probably already closed');
logger.warn(e);
}
};

const shutdown = async (signal) => {
logger.info(`${signal} received.`);
// We give Gladys 10 seconds to properly shutdown, otherwise we do it
setTimeout(() => {
logger.info('Timeout to shutdown expired, forcing shut down.');
process.exit();
}, 10 * 1000);
logger.info('Closing database connections.');
await Promise.all([closeSQLite(), closeDuckDB()]);
process.exit();
};

Expand Down
1 change: 0 additions & 1 deletion server/lib/device/device.onPurgeStatesEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { JOB_TYPES } = require('../../utils/constants');
async function onPurgeStatesEvent() {
const purgeAllStates = this.job.wrapper(JOB_TYPES.DEVICE_STATES_PURGE, async () => {
await this.purgeStates();
await this.purgeAggregateStates();
});
await purgeAllStates();
}
Expand Down
44 changes: 0 additions & 44 deletions server/lib/device/device.purgeAggregateStates.js

This file was deleted.

13 changes: 6 additions & 7 deletions server/lib/device/device.purgeStates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { Op } = require('sequelize');
const db = require('../../models');
const logger = require('../../utils/logger');
const { SYSTEM_VARIABLE_NAMES } = require('../../utils/constants');
Expand All @@ -21,19 +20,19 @@ async function purgeStates() {
logger.debug('Not purging device feature states, deviceStateHistoryInDays = -1');
return Promise.resolve(false);
}
const queryInterface = db.sequelize.getQueryInterface();
const now = new Date().getTime();
// all date before this timestamp will be removed
const timstampLimit = now - deviceStateHistoryInDaysInt * 24 * 60 * 60 * 1000;
const dateLimit = new Date(timstampLimit);
logger.info(
`Purging device feature states of the last ${deviceStateHistoryInDaysInt} days. States older than ${dateLimit} will be purged.`,
);
await queryInterface.bulkDelete('t_device_feature_state', {
created_at: {
[Op.lte]: dateLimit,
},
});
await db.duckDbWriteConnectionAllAsync(
`
DELETE FROM t_device_feature_state WHERE created_at < ?
`,
dateLimit,
);
return true;
}

Expand Down
2 changes: 0 additions & 2 deletions server/lib/device/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const { getDeviceFeaturesAggregates } = require('./device.getDeviceFeaturesAggre
const { getDeviceFeaturesAggregatesMulti } = require('./device.getDeviceFeaturesAggregatesMulti');
const { onPurgeStatesEvent } = require('./device.onPurgeStatesEvent');
const { purgeStates } = require('./device.purgeStates');
const { purgeAggregateStates } = require('./device.purgeAggregateStates');
const { purgeStatesByFeatureId } = require('./device.purgeStatesByFeatureId');
const { poll } = require('./device.poll');
const { pollAll } = require('./device.pollAll');
Expand Down Expand Up @@ -114,7 +113,6 @@ DeviceManager.prototype.getDeviceFeaturesAggregates = getDeviceFeaturesAggregate
DeviceManager.prototype.getDeviceFeaturesAggregatesMulti = getDeviceFeaturesAggregatesMulti;
DeviceManager.prototype.onPurgeStatesEvent = onPurgeStatesEvent;
DeviceManager.prototype.purgeStates = purgeStates;
DeviceManager.prototype.purgeAggregateStates = purgeAggregateStates;
DeviceManager.prototype.purgeStatesByFeatureId = purgeStatesByFeatureId;
DeviceManager.prototype.poll = poll;
DeviceManager.prototype.pollAll = pollAll;
Expand Down
54 changes: 0 additions & 54 deletions server/test/lib/device/device.purgeAggregateStates.test.js

This file was deleted.

19 changes: 19 additions & 0 deletions server/test/lib/device/device.purgeStates.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { expect } = require('chai');
const EventEmitter = require('events');
const { fake } = require('sinon');
const db = require('../../../models');

const Device = require('../../../lib/device');

Expand All @@ -15,11 +16,29 @@ describe('Device', () => {
const variable = {
getValue: fake.resolves(30),
};
const currentDate = new Date();
const fortyDaysAgo = new Date(currentDate);
fortyDaysAgo.setDate(currentDate.getDate() - 40);
await db.duckDbBatchInsertState('ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', [
{
value: 1,
created_at: fortyDaysAgo,
},
{
value: 10,
created_at: currentDate,
},
]);
const stateManager = new StateManager(event);
const service = {};
const device = new Device(event, {}, stateManager, service, {}, variable, job);
const devicePurged = await device.purgeStates();
expect(devicePurged).to.equal(true);
const res = await db.duckDbReadConnectionAllAsync(
'SELECT value FROM t_device_feature_state WHERE device_feature_id = ?',
'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4',
);
expect(res).to.deep.equal([{ value: 10 }]);
});
it('should not purgeStates, invalid date', async () => {
const variable = {
Expand Down

0 comments on commit d64a365

Please sign in to comment.