diff --git a/front/src/config/i18n/en.json b/front/src/config/i18n/en.json index 4522280095..f29899bbd5 100644 --- a/front/src/config/i18n/en.json +++ b/front/src/config/i18n/en.json @@ -1581,7 +1581,9 @@ "monthly-device-state-aggregate": "Monthly sensors aggregation", "daily-device-state-aggregate": "Daily sensors aggregation", "hourly-device-state-aggregate": "Hourly sensors aggregation", - "gladys-gateway-backup": "Gladys Plus backup" + "gladys-gateway-backup": "Gladys Plus backup", + "device-state-purge-single-feature": "Single device feature states clean", + "vacuum": "Database cleaning" }, "jobErrors": { "purged-when-restarted": "Gladys Assistant restarted while this job was still running, so it was purged. It doesn't mean the job has failed, it's a normal behavior." @@ -1615,6 +1617,10 @@ "timezoneText": "The timezone is used in scheduled scene.", "deviceStateRetentionTime": "Keep Device State History", "deviceStateRetentionTimeDescription": "How long Gladys will keep your device states in database.", + "vacuumDatabaseTitle": "Database cleaning", + "vacuumDatabaseDescription": "By clicking on this button, Gladys will start cleaning the database to free up space. This does not delete any data, it just physically remove old deleted data. Be careful, this task can take some time to run, and during that time Gladys will not be available.", + "vacuumDatabaseButton": "Start database cleaning", + "vacuumDatabaseStarted": "Database cleaning has started... Gladys will not be available until this job finishes.", "containerState": { "created": "Created", "restarting": "Restarting", diff --git a/front/src/config/i18n/fr.json b/front/src/config/i18n/fr.json index 2204edc808..6b8297ee82 100644 --- a/front/src/config/i18n/fr.json +++ b/front/src/config/i18n/fr.json @@ -1581,7 +1581,9 @@ "monthly-device-state-aggregate": "Aggrégation donnée capteur mensuelle", "daily-device-state-aggregate": "Aggrégation donnée capteur journalière", "hourly-device-state-aggregate": "Aggrégation donnée capteur horaire", - "gladys-gateway-backup": "Sauvegarde Gladys Plus" + "gladys-gateway-backup": "Sauvegarde Gladys Plus", + "device-state-purge-single-feature": "Nettoyage des états d'un appareil", + "vacuum": "Nettoyage de la base de donnée" }, "jobErrors": { "purged-when-restarted": "Gladys Assistant a redémarré alors que cette tâche était en cours. Cela ne veut pas dire que cette tâche a échouée, c'est un comportement normal." @@ -1615,6 +1617,10 @@ "timezoneText": "Le fuseau horaire est utilisé dans les scènes programmées.", "deviceStateRetentionTime": "Conserver l'historique des états des appareils", "deviceStateRetentionTimeDescription": "La période pendant laquelle Gladys gardera en base de données les états des appareils.", + "vacuumDatabaseTitle": "Nettoyer la base de donnée", + "vacuumDatabaseDescription": "En cliquant sur ce bouton, Gladys lancera un nettoyage de la base de donnée, ce qui libère de l'espace disque. Cette tâche ne supprime pas de données, elle re-arrange juste la donnée existante sur le disque et vide des vieilles lignes qui ne sont plus utilisées. Attention, cette opération peut prendre un certain temps sur les grosses bases de données, et pendant ce temps Gladys sera indisponible. Ne lancez pas cette commande si vous avez besoin de Gladys dans la prochaine heure.", + "vacuumDatabaseButton": "Lancer le nettoyage", + "vacuumDatabaseStarted": "Le nettoyage a commencé... Gladys sera indisponible jusqu'à la fin du nettoyage.", "containerState": { "created": "Créé", "restarting": "Redémarrage", diff --git a/front/src/routes/settings/settings-system/SettingsSystemPage.jsx b/front/src/routes/settings/settings-system/SettingsSystemPage.jsx index d277468374..08051246b0 100644 --- a/front/src/routes/settings/settings-system/SettingsSystemPage.jsx +++ b/front/src/routes/settings/settings-system/SettingsSystemPage.jsx @@ -200,6 +200,26 @@ const SystemPage = ({ children, ...props }) => ( +
diff --git a/front/src/routes/settings/settings-system/index.js b/front/src/routes/settings/settings-system/index.js index 8d484974ed..fcc4d4b94b 100644 --- a/front/src/routes/settings/settings-system/index.js +++ b/front/src/routes/settings/settings-system/index.js @@ -26,6 +26,18 @@ class SettingsSystem extends Component { } }; + vacuumDatabase = async e => { + e.preventDefault(); + this.setState({ + vacuumStarted: true + }); + try { + await this.props.httpClient.post('/api/v1/system/vacuum'); + } catch (e) { + console.error(e); + } + }; + getTimezone = async () => { try { const { value } = await this.props.httpClient.get(`/api/v1/variable/${SYSTEM_VARIABLE_NAMES.TIMEZONE}`); @@ -86,7 +98,14 @@ class SettingsSystem extends Component { clearInterval(this.refreshPingIntervalId); } - render(props, { selectedTimezone, deviceStateHistoryInDays }) { + constructor(props) { + super(props); + this.state = { + vacuumStarted: false + }; + } + + render(props, { selectedTimezone, deviceStateHistoryInDays, vacuumStarted }) { const isDocker = get(props, 'systemInfos.is_docker'); const upgradeDownloadInProgress = props.downloadUpgradeStatus === RequestStatus.Getting; const upgradeDownloadFinished = props.downloadUpgradeStatus === RequestStatus.Success; @@ -104,6 +123,8 @@ class SettingsSystem extends Component { selectedTimezone={selectedTimezone} deviceStateHistoryInDays={deviceStateHistoryInDays} updateDeviceStateHistory={this.updateDeviceStateHistory} + vacuumDatabase={this.vacuumDatabase} + vacuumStarted={vacuumStarted} /> ); } diff --git a/package-lock.json b/package-lock.json index bc4403b3f9..6dbecb42ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gladys", - "version": "4.10.1", + "version": "4.10.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2d0a0c16d1..8a7c6a52e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gladys", - "version": "4.10.1", + "version": "4.10.2", "description": "A privacy-first, open-source home assistant", "main": "index.js", "engines": { diff --git a/server/api/controllers/system.controller.js b/server/api/controllers/system.controller.js index c844b3ef66..e64a10748d 100644 --- a/server/api/controllers/system.controller.js +++ b/server/api/controllers/system.controller.js @@ -71,6 +71,19 @@ module.exports = function SystemController(gladys) { gladys.system.shutdown(); } + /** + * @api {post} /api/v1/system/vacuum + * @apiName vacuumSystem + * @apiGroup System + */ + async function vacuum(req, res) { + gladys.event.emit(EVENTS.SYSTEM.VACUUM); + res.json({ + success: true, + message: 'Vacuum started, system might be unresponsive for a while', + }); + } + return Object.freeze({ downloadUpgrade: asyncMiddleware(downloadUpgrade), getSystemInfos: asyncMiddleware(getSystemInfos), @@ -78,5 +91,6 @@ module.exports = function SystemController(gladys) { getContainers: asyncMiddleware(getContainers), shutdown: asyncMiddleware(shutdown), getUpgradeDownloadStatus: asyncMiddleware(getUpgradeDownloadStatus), + vacuum: asyncMiddleware(vacuum), }); }; diff --git a/server/api/routes.js b/server/api/routes.js index 24367a6d98..7b761a47d9 100644 --- a/server/api/routes.js +++ b/server/api/routes.js @@ -496,6 +496,11 @@ function getRoutes(gladys) { authenticated: true, controller: systemController.getUpgradeDownloadStatus, }, + 'post /api/v1/system/vacuum': { + authenticated: true, + admin: true, + controller: systemController.vacuum, + }, // user 'post /api/v1/user': { authenticated: true, diff --git a/server/lib/device/device.create.js b/server/lib/device/device.create.js index 82b94bfda3..2e74266914 100644 --- a/server/lib/device/device.create.js +++ b/server/lib/device/device.create.js @@ -2,7 +2,6 @@ const Promise = require('bluebird'); const { BadParameters } = require('../../utils/coreErrors'); const db = require('../../models'); const { EVENTS } = require('../../utils/constants'); -const logger = require('../../utils/logger'); const getByExternalId = async (externalId) => { return db.Device.findOne({ @@ -154,17 +153,13 @@ async function create(device) { }); // We purge states of all device features that were marked as "keep_history = false" - await Promise.each(deviceFeaturesIdsToPurge, async (deviceFeaturesIdToPurge) => { - await this.purgeStatesByFeatureId(deviceFeaturesIdToPurge); + // We do this asynchronously with an event, so it doesn't block the current request + // Also, the function called will delete as slowly as possible the event + // To make sure that Gladys is not locked during this time + deviceFeaturesIdsToPurge.forEach((deviceFeatureIdToPurge) => { + this.eventManager.emit(EVENTS.DEVICE.PURGE_STATES_SINGLE_FEATURE, deviceFeatureIdToPurge); }); - if (deviceFeaturesIdsToPurge.length > 0) { - // If we don't run a VACUUM, the database file size will stay the same - // Read: https://www.sqlite.org/lang_vacuum.html - logger.info('Running VACUUM command to free up space.'); - await db.sequelize.query('VACUUM;'); - } - // we get the whole device from the DB to avoid // having a partial final object const newDevice = (await getByExternalId(device.external_id)).get({ plain: true }); diff --git a/server/lib/device/device.purgeStatesByFeatureId.js b/server/lib/device/device.purgeStatesByFeatureId.js index 7853ed6c54..aaf742fd13 100644 --- a/server/lib/device/device.purgeStatesByFeatureId.js +++ b/server/lib/device/device.purgeStatesByFeatureId.js @@ -1,22 +1,105 @@ +const { QueryTypes } = require('sequelize'); +const Promise = require('bluebird'); const db = require('../../models'); const logger = require('../../utils/logger'); /** * @description Purge device states of a specific feature * @param {string} deviceFeatureId - Id of a device feature. + * @param {string} jobId - Id of the job. * @returns {Promise} Resolve when finished. * @example * device.purgeStatesByFeatureId('d47b481b-a7be-4224-9850-313cdb8a4065'); */ -async function purgeStatesByFeatureId(deviceFeatureId) { +async function purgeStatesByFeatureId(deviceFeatureId, jobId) { logger.info(`Purging states of device feature ${deviceFeatureId}`); - const queryInterface = db.sequelize.getQueryInterface(); - await queryInterface.bulkDelete('t_device_feature_state', { - device_feature_id: deviceFeatureId, + + const numberOfDeviceFeatureStateToDelete = await db.DeviceFeatureState.count({ + where: { + device_feature_id: deviceFeatureId, + }, + }); + + const numberOfDeviceFeatureStateAggregateToDelete = await db.DeviceFeatureStateAggregate.count({ + where: { + device_feature_id: deviceFeatureId, + }, }); - await queryInterface.bulkDelete('t_device_feature_state_aggregate', { - device_feature_id: deviceFeatureId, + + logger.info( + `Purging "${deviceFeatureId}": ${numberOfDeviceFeatureStateToDelete} states & ${numberOfDeviceFeatureStateAggregateToDelete} aggregates to delete.`, + ); + + const numberOfIterationsStates = Math.ceil( + numberOfDeviceFeatureStateToDelete / this.STATES_TO_PURGE_PER_DEVICE_FEATURE_CLEAN_BATCH, + ); + const iterator = [...Array(numberOfIterationsStates)]; + + const numberOfIterationsStatesAggregates = Math.ceil( + numberOfDeviceFeatureStateAggregateToDelete / this.STATES_TO_PURGE_PER_DEVICE_FEATURE_CLEAN_BATCH, + ); + const iteratorAggregates = [...Array(numberOfIterationsStatesAggregates)]; + + const total = numberOfIterationsStates + numberOfIterationsStatesAggregates; + let currentBatch = 0; + let currentProgressPercent = 0; + + // We only save progress to DB if it changed + // Because saving progress is expensive (DB write + Websocket call) + const updateProgressIfNeeded = async () => { + currentBatch += 1; + const newProgressPercent = Math.round((currentBatch * 100) / total); + if (currentProgressPercent !== newProgressPercent) { + currentProgressPercent = newProgressPercent; + await this.job.updateProgress(jobId, currentProgressPercent); + } + }; + + await Promise.each(iterator, async () => { + await db.sequelize.query( + ` + DELETE FROM t_device_feature_state WHERE id IN ( + SELECT id FROM t_device_feature_state + WHERE device_feature_id = :id + LIMIT :limit + ); + `, + { + replacements: { + id: deviceFeatureId, + limit: this.STATES_TO_PURGE_PER_DEVICE_FEATURE_CLEAN_BATCH, + }, + type: QueryTypes.SELECT, + }, + ); + await updateProgressIfNeeded(); + await Promise.delay(this.WAIT_TIME_BETWEEN_DEVICE_FEATURE_CLEAN_BATCH); + }); + + await Promise.each(iteratorAggregates, async () => { + await db.sequelize.query( + ` + DELETE FROM t_device_feature_state_aggregate WHERE id IN ( + SELECT id FROM t_device_feature_state_aggregate + WHERE device_feature_id = :id + LIMIT :limit + ); + `, + { + replacements: { + id: deviceFeatureId, + limit: this.STATES_TO_PURGE_PER_DEVICE_FEATURE_CLEAN_BATCH, + }, + type: QueryTypes.SELECT, + }, + ); + await updateProgressIfNeeded(); + await Promise.delay(this.WAIT_TIME_BETWEEN_DEVICE_FEATURE_CLEAN_BATCH); }); + return { + numberOfDeviceFeatureStateToDelete, + numberOfDeviceFeatureStateAggregateToDelete, + }; } module.exports = { diff --git a/server/lib/device/index.js b/server/lib/device/index.js index 7b0ecb484b..97c1037983 100644 --- a/server/lib/device/index.js +++ b/server/lib/device/index.js @@ -1,4 +1,4 @@ -const { EVENTS } = require('../../utils/constants'); +const { EVENTS, JOB_TYPES } = require('../../utils/constants'); const { eventFunctionWrapper } = require('../../utils/functionsWrapper'); // Categories of DeviceFeatures @@ -51,12 +51,20 @@ const DeviceManager = function DeviceManager( this.variable = variable; this.job = job; + this.STATES_TO_PURGE_PER_DEVICE_FEATURE_CLEAN_BATCH = 1000; + this.WAIT_TIME_BETWEEN_DEVICE_FEATURE_CLEAN_BATCH = 100; + // initialize all types of device feature categories this.camera = new CameraManager(this.stateManager, messageManager, eventManager, this); this.lightManager = new LightManager(eventManager, messageManager, this); this.temperatureSensorManager = new TemperatureSensorManager(eventManager, messageManager, this); this.humiditySensorManager = new HumiditySensorManager(eventManager, messageManager, this); + this.purgeStatesByFeatureId = this.job.wrapper( + JOB_TYPES.DEVICE_STATES_PURGE_SINGLE_FEATURE, + this.purgeStatesByFeatureId.bind(this), + ); + this.devicesByPollFrequency = {}; // listen to events this.eventManager.on(EVENTS.DEVICE.NEW_STATE, this.newStateEvent.bind(this)); @@ -68,6 +76,10 @@ const DeviceManager = function DeviceManager( EVENTS.DEVICE.CALCULATE_HOURLY_AGGREGATE, eventFunctionWrapper(this.onHourlyDeviceAggregateEvent.bind(this)), ); + this.eventManager.on( + EVENTS.DEVICE.PURGE_STATES_SINGLE_FEATURE, + eventFunctionWrapper(this.purgeStatesByFeatureId.bind(this)), + ); }; DeviceManager.prototype.add = add; diff --git a/server/lib/index.js b/server/lib/index.js index 33914a7e8f..4624532a7f 100644 --- a/server/lib/index.js +++ b/server/lib/index.js @@ -55,7 +55,7 @@ function Gladys(params = {}) { const area = new Area(event); const dashboard = new Dashboard(); const stateManager = new StateManager(event); - const system = new System(db.sequelize, event, config); + const system = new System(db.sequelize, event, config, job); const http = new Http(system); const house = new House(event, stateManager); const room = new Room(brain); diff --git a/server/lib/job/job.wrapper.js b/server/lib/job/job.wrapper.js index b50ab198bd..fcb4bb2316 100644 --- a/server/lib/job/job.wrapper.js +++ b/server/lib/job/job.wrapper.js @@ -14,8 +14,9 @@ function wrapper(type, func) { let job; try { job = await this.start(type); - await func(...args, job.id); + const res = await func(...args, job.id); await this.finish(job.id, JOB_STATUS.SUCCESS); + return res; } catch (error) { if (job) { const data = { diff --git a/server/lib/system/index.js b/server/lib/system/index.js index af6ba0e386..53afeb48be 100644 --- a/server/lib/system/index.js +++ b/server/lib/system/index.js @@ -1,6 +1,6 @@ const Docker = require('dockerode'); -const { EVENTS } = require('../../utils/constants'); +const { EVENTS, JOB_TYPES } = require('../../utils/constants'); const { eventFunctionWrapper } = require('../../utils/functionsWrapper'); const { downloadUpgrade } = require('./system.downloadUpgrade'); const { init } = require('./system.init'); @@ -21,10 +21,11 @@ const { restartContainer } = require('./system.restartContainer'); const { removeContainer } = require('./system.removeContainer'); const { stopContainer } = require('./system.stopContainer'); const { getNetworkMode } = require('./system.getNetworkMode'); +const { vacuum } = require('./system.vacuum'); const { shutdown } = require('./system.shutdown'); -const System = function System(sequelize, event, config) { +const System = function System(sequelize, event, config, job) { this.downloadUpgradeError = null; this.downloadUpgradeFinished = null; this.downloadUpgradeLastEvent = null; @@ -32,8 +33,11 @@ const System = function System(sequelize, event, config) { this.sequelize = sequelize; this.event = event; this.config = config; + this.job = job; this.dockerode = null; + this.vacuum = this.job.wrapper(JOB_TYPES.VACUUM, this.vacuum.bind(this)); this.event.on(EVENTS.SYSTEM.DOWNLOAD_UPGRADE, eventFunctionWrapper(this.downloadUpgrade.bind(this))); + this.event.on(EVENTS.SYSTEM.VACUUM, eventFunctionWrapper(this.vacuum.bind(this))); this.networkMode = null; }; @@ -56,6 +60,7 @@ System.prototype.restartContainer = restartContainer; System.prototype.removeContainer = removeContainer; System.prototype.stopContainer = stopContainer; System.prototype.getNetworkMode = getNetworkMode; +System.prototype.vacuum = vacuum; System.prototype.shutdown = shutdown; diff --git a/server/lib/system/system.vacuum.js b/server/lib/system/system.vacuum.js new file mode 100644 index 0000000000..10da0158b2 --- /dev/null +++ b/server/lib/system/system.vacuum.js @@ -0,0 +1,22 @@ +const db = require('../../models'); +const logger = require('../../utils/logger'); + +/** + * @description Vacuum Gladys database + * @example + * vacuum(); + */ +async function vacuum() { + // SQLite VACUUM is a blocking task that will clean + // the database from deleted data. It reduces the database size + // on disk. + // Warning: This operations is blocking and takes time to run + // on big databases + // Read: https://www.sqlite.org/lang_vacuum.html + logger.info('Running VACUUM command to free up space.'); + await db.sequelize.query('VACUUM;'); +} + +module.exports = { + vacuum, +}; diff --git a/server/test/controllers/device/device.controller.test.js b/server/test/controllers/device/device.controller.test.js index 207d535771..297350f538 100644 --- a/server/test/controllers/device/device.controller.test.js +++ b/server/test/controllers/device/device.controller.test.js @@ -4,6 +4,7 @@ const EventEmitter = require('events'); const { fake } = require('sinon'); const db = require('../../../models'); const Device = require('../../../lib/device'); +const Job = require('../../../lib/job'); const { authenticatedRequest } = require('../request.test'); @@ -67,7 +68,8 @@ describe('GET /api/v1/device_feature/aggregated_states', () => { getValue: fake.resolves(null), }; const event = new EventEmitter(); - const device = new Device(event, {}, {}, {}, {}, variable); + const job = new Job(event); + const device = new Device(event, {}, {}, {}, {}, variable, job); await device.calculateAggregate('hourly'); await device.calculateAggregate('daily'); await device.calculateAggregate('monthly'); diff --git a/server/test/controllers/system/system.controller.test.js b/server/test/controllers/system/system.controller.test.js index e936385f5c..85409b012a 100644 --- a/server/test/controllers/system/system.controller.test.js +++ b/server/test/controllers/system/system.controller.test.js @@ -26,3 +26,16 @@ describe('GET /api/v1/system/disk', () => { }); }); }); + +describe('POST /api/v1/system/vacuum', () => { + it('should vacuum database', async () => { + await authenticatedRequest + .post('/api/v1/system/vacuum') + .expect('Content-Type', /json/) + .expect(200) + .then((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('message'); + }); + }); +}); diff --git a/server/test/lib/device/camera/camera.command.test.js b/server/test/lib/device/camera/camera.command.test.js index 9490ec9f10..392b639b52 100644 --- a/server/test/lib/device/camera/camera.command.test.js +++ b/server/test/lib/device/camera/camera.command.test.js @@ -2,9 +2,12 @@ const EventEmitter = require('events'); const { assert, fake } = require('sinon'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); + const messageManager = { replyByIntent: fake.resolves(true), }; @@ -19,7 +22,7 @@ const RANDOM_IMAGE = describe('Camera.command', () => { it('should respond with image from camera', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const context = {}; await deviceManager.camera.command( message, @@ -46,7 +49,7 @@ describe('Camera.command', () => { }); it('should respond camera not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const context = {}; await deviceManager.camera.command( message, @@ -73,7 +76,7 @@ describe('Camera.command', () => { }); it('should respond camera not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const context = {}; await deviceManager.camera.command( message, diff --git a/server/test/lib/device/camera/camera.get.test.js b/server/test/lib/device/camera/camera.get.test.js index b896f9034d..ee91ad166c 100644 --- a/server/test/lib/device/camera/camera.get.test.js +++ b/server/test/lib/device/camera/camera.get.test.js @@ -2,13 +2,15 @@ const EventEmitter = require('events'); const { expect } = require('chai'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Camera.get', () => { it('should return list of cameras', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); const cameras = await deviceManager.camera.get(); expect(cameras).to.be.instanceOf(Array); cameras.forEach((camera) => { diff --git a/server/test/lib/device/camera/camera.getImage.test.js b/server/test/lib/device/camera/camera.getImage.test.js index 886d504098..4cb97b6d46 100644 --- a/server/test/lib/device/camera/camera.getImage.test.js +++ b/server/test/lib/device/camera/camera.getImage.test.js @@ -2,16 +2,18 @@ const EventEmitter = require('events'); const { expect, assert } = require('chai'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const RANDOM_IMAGE = 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=='; const event = new EventEmitter(); +const job = new Job(event); describe('Camera.getImage', () => { it('should return camera image', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { @@ -28,7 +30,7 @@ describe('Camera.getImage', () => { }); it('should return camera not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { @@ -45,7 +47,7 @@ describe('Camera.getImage', () => { }); it('should return camera not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera-2', { features: [ { diff --git a/server/test/lib/device/camera/camera.getImageInRoom.test.js b/server/test/lib/device/camera/camera.getImageInRoom.test.js index a8013ad8c0..7579ffdcde 100644 --- a/server/test/lib/device/camera/camera.getImageInRoom.test.js +++ b/server/test/lib/device/camera/camera.getImageInRoom.test.js @@ -2,8 +2,10 @@ const EventEmitter = require('events'); const { expect } = require('chai'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const RANDOM_IMAGE = 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=='; @@ -11,7 +13,7 @@ const RANDOM_IMAGE = describe('Camera.getImageInRoom', () => { it('should return image', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { diff --git a/server/test/lib/device/camera/camera.setImage.test.js b/server/test/lib/device/camera/camera.setImage.test.js index 06fb8d9b5e..0e51e0f3b3 100644 --- a/server/test/lib/device/camera/camera.setImage.test.js +++ b/server/test/lib/device/camera/camera.setImage.test.js @@ -2,16 +2,18 @@ const EventEmitter = require('events'); const { expect, assert } = require('chai'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const RANDOM_IMAGE = 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=='; const event = new EventEmitter(); +const job = new Job(event); describe('Camera.setImage', () => { it('should set image', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { @@ -29,7 +31,7 @@ describe('Camera.setImage', () => { }); it('should return camera not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { @@ -46,7 +48,7 @@ describe('Camera.setImage', () => { }); it('should return camera feature not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { @@ -61,7 +63,7 @@ describe('Camera.setImage', () => { }); it('should return image too big', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('device', 'test-camera', { features: [ { diff --git a/server/test/lib/device/device.addFeature.test.js b/server/test/lib/device/device.addFeature.test.js index 24d14ae209..b6ef8fc021 100644 --- a/server/test/lib/device/device.addFeature.test.js +++ b/server/test/lib/device/device.addFeature.test.js @@ -2,6 +2,7 @@ const { expect } = require('chai'); const EventEmitter = require('events'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); @@ -28,7 +29,8 @@ describe('Device.addFeature', () => { ], params: [], }); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const newDevice = await device.addFeature('test-device', { name: 'On/Off', external_id: 'philips-hue:1:new', @@ -70,7 +72,8 @@ describe('Device.addFeature', () => { ], params: [], }); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const newDevice = await device.addFeature('test-device', { name: 'NEW NAME, SHOULD NOT BE UPDATED', external_id: 'philips-hue:1:binary', diff --git a/server/test/lib/device/device.addParam.test.js b/server/test/lib/device/device.addParam.test.js index c9954b6875..92212821e6 100644 --- a/server/test/lib/device/device.addParam.test.js +++ b/server/test/lib/device/device.addParam.test.js @@ -2,6 +2,7 @@ const { expect } = require('chai'); const EventEmitter = require('events'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); @@ -20,7 +21,8 @@ describe('Device.addParam', () => { }, ], }); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const newDevice = await device.addParam('test-device', { name: 'NEW_VALUE', value: '10', @@ -46,7 +48,8 @@ describe('Device.addParam', () => { }, ], }); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const newDevice = await device.addParam('test-device', { name: 'TEST_PARAM', value: '1000', diff --git a/server/test/lib/device/device.calculateAggregate.test.js b/server/test/lib/device/device.calculateAggregate.test.js index ab69e10177..dfc843a3a9 100644 --- a/server/test/lib/device/device.calculateAggregate.test.js +++ b/server/test/lib/device/device.calculateAggregate.test.js @@ -57,7 +57,8 @@ describe('Device.calculateAggregate', function Before() { // we modify the retention policy to take the last 1000 days (it'll cover this last year) getValue: fake.resolves('1000'), }; - const device = new Device(event, {}, {}, {}, {}, variable); + const job = new Job(event); + const device = new Device(event, {}, {}, {}, {}, variable, job); await device.calculateAggregate('hourly'); const deviceFeatureStates = await db.DeviceFeatureStateAggregate.findAll({ raw: true, @@ -78,7 +79,8 @@ describe('Device.calculateAggregate', function Before() { // we modify the retention policy to take the last 5 days only (for testing) getValue: fake.resolves('5'), }; - const device = new Device(event, {}, {}, {}, {}, variable); + const job = new Job(event); + const device = new Device(event, {}, {}, {}, {}, variable, job); await device.calculateAggregate('hourly'); }); it('should calculate hourly aggregate with last aggregate from device', async () => { @@ -91,7 +93,8 @@ describe('Device.calculateAggregate', function Before() { // we modify the retention policy to take the last 1000 days (for testing) getValue: fake.resolves('1000'), }; - const device = new Device(event, {}, {}, {}, {}, variable); + const job = new Job(event); + const device = new Device(event, {}, {}, {}, {}, variable, job); await device.calculateAggregate('hourly'); }); it('should calculate daily aggregate', async () => { @@ -99,7 +102,8 @@ describe('Device.calculateAggregate', function Before() { const variable = { getValue: fake.resolves(null), }; - const device = new Device(event, {}, {}, {}, {}, variable); + const job = new Job(event); + const device = new Device(event, {}, {}, {}, {}, variable, job); await device.calculateAggregate('daily'); const deviceFeatureStates = await db.DeviceFeatureStateAggregate.findAll({ raw: true, @@ -114,7 +118,8 @@ describe('Device.calculateAggregate', function Before() { const variable = { getValue: fake.resolves(null), }; - const device = new Device(event, {}, {}, {}, {}, variable); + const job = new Job(event); + const device = new Device(event, {}, {}, {}, {}, variable, job); await device.calculateAggregate('monthly'); const deviceFeatureStates = await db.DeviceFeatureStateAggregate.findAll({ raw: true, diff --git a/server/test/lib/device/device.create.test.js b/server/test/lib/device/device.create.test.js index 59f8fc3146..804e9c5ac8 100644 --- a/server/test/lib/device/device.create.test.js +++ b/server/test/lib/device/device.create.test.js @@ -1,19 +1,22 @@ const { expect } = require('chai'); +const { fake, assert } = require('sinon'); const EventEmitter = require('events'); -const { DEVICE_POLL_FREQUENCIES } = require('../../../utils/constants'); +const { DEVICE_POLL_FREQUENCIES, EVENTS } = require('../../../utils/constants'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); const ServiceManager = require('../../../lib/service'); +const Job = require('../../../lib/job'); const db = require('../../../models'); const event = new EventEmitter(); +const job = new Job(event); describe('Device', () => { it('should create device alone', async () => { const stateManager = new StateManager(event); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); const newDevice = await device.create({ service_id: 'a810b8db-6d04-4697-bed3-c4b72c996279', name: 'Philips Hue 1', @@ -42,7 +45,7 @@ describe('Device', () => { ], }); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); const newDevice = await device.create({ id: '7f85c2f8-86cc-4600-84db-6c074dadb4e8', name: 'RENAMED_DEVICE', @@ -76,7 +79,7 @@ describe('Device', () => { it('should update device which already exist, update a feature and a param', async () => { const stateManager = new StateManager(event); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); const newDevice = await device.create({ id: '7f85c2f8-86cc-4600-84db-6c074dadb4e8', name: 'RENAMED_DEVICE', @@ -165,7 +168,7 @@ describe('Device', () => { ], }); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); await device.create({ id: '7f85c2f8-86cc-4600-84db-6c074dadb4e8', name: 'RENAMED_DEVICE', @@ -209,7 +212,7 @@ describe('Device', () => { ], }); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); const newDevice = await device.create({ id: '7f85c2f8-86cc-4600-84db-6c074dadb4e8', name: 'RENAMED_DEVICE', @@ -231,7 +234,7 @@ describe('Device', () => { it('should create device, one feature and one param', async () => { const stateManager = new StateManager(event); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); const newDevice = await device.create({ service_id: 'a810b8db-6d04-4697-bed3-c4b72c996279', name: 'Philips Hue 1', @@ -265,7 +268,7 @@ describe('Device', () => { params: [], }); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); await device.create({ id: 'f3525782-f513-4068-9f64-f3429756f99d', name: 'RENAMED_DEVICE', @@ -332,7 +335,7 @@ describe('Device', () => { params: [], }); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); await device.create({ id: 'f3525782-f513-4068-9f64-f3429756f99d', name: 'RENAMED_DEVICE', @@ -399,7 +402,7 @@ describe('Device', () => { params: [], }); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); await device.create({ id: 'f3525782-f513-4068-9f64-f3429756f99d', name: 'RENAMED_DEVICE', @@ -457,10 +460,14 @@ describe('Device', () => { expect(device.devicesByPollFrequency[DEVICE_POLL_FREQUENCIES.EVERY_MINUTES]).to.have.lengthOf(0); expect(device.devicesByPollFrequency[DEVICE_POLL_FREQUENCIES.EVERY_30_SECONDS]).to.have.lengthOf(1); }); - it('should update a feature with keep_history = false and check that states are deleted', async () => { + it('should update a feature with keep_history = false and check that background job to delete states is called', async () => { const stateManager = new StateManager(event); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const fakeEvent = { + emit: fake.returns(null), + on: fake.returns(null), + }; + const device = new Device(fakeEvent, {}, stateManager, serviceManager, {}, {}, job); await db.DeviceFeatureState.create({ device_feature_id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', value: 10, @@ -495,17 +502,10 @@ describe('Device', () => { ], }); expect(createdDevice.features[0]).to.have.property('id', 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4'); - const states = await db.DeviceFeatureState.findAll({ - where: { - device_feature_id: createdDevice.features[0].id, - }, - }); - expect(states).to.have.lengthOf(0); - const statesAggregates = await db.DeviceFeatureStateAggregate.findAll({ - where: { - device_feature_id: createdDevice.features[0].id, - }, - }); - expect(statesAggregates).to.have.lengthOf(0); + assert.calledWith( + fakeEvent.emit, + EVENTS.DEVICE.PURGE_STATES_SINGLE_FEATURE, + 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', + ); }); }); diff --git a/server/test/lib/device/device.destroy.test.js b/server/test/lib/device/device.destroy.test.js index 6610bb4c2a..c054b4aa90 100644 --- a/server/test/lib/device/device.destroy.test.js +++ b/server/test/lib/device/device.destroy.test.js @@ -3,14 +3,16 @@ const { assert } = require('chai'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); const ServiceManager = require('../../../lib/service'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Device.destroy', () => { it('should destroy device', async () => { const stateManager = new StateManager(event); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); device.devicesByPollFrequency[60000] = [ { selector: 'test-device', @@ -21,7 +23,7 @@ describe('Device.destroy', () => { it('should return device not found', async () => { const stateManager = new StateManager(event); const serviceManager = new ServiceManager({}, stateManager); - const device = new Device(event, {}, stateManager, serviceManager); + const device = new Device(event, {}, stateManager, serviceManager, {}, {}, job); const promise = device.destroy('doesnotexist'); return assert.isRejected(promise); }); diff --git a/server/test/lib/device/device.get.test.js b/server/test/lib/device/device.get.test.js index b75bab8683..fe8b53d236 100644 --- a/server/test/lib/device/device.get.test.js +++ b/server/test/lib/device/device.get.test.js @@ -3,8 +3,10 @@ const { expect, assert } = require('chai'); const { fake } = require('sinon'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Device.get', () => { it('should get devices', async () => { @@ -14,7 +16,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get(); expect(devices).to.be.instanceOf(Array); }); @@ -25,7 +27,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get({ service: 'test-service', }); @@ -38,7 +40,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get({ service: 'test-service', search: 'this name does not exist', @@ -54,7 +56,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get({ model: 'my-unknown-model', }); @@ -69,7 +71,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get({ device_feature_category: 'my-unknown-category', }); @@ -84,7 +86,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get({ device_feature_type: 'my-unknown-type', }); @@ -99,7 +101,7 @@ describe('Device.get', () => { id: 'a810b8db-6d04-4697-bed3-c4b72c996279', }), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const devices = await device.get({ take: 0, }); @@ -112,7 +114,7 @@ describe('Device.get', () => { const service = { getLocalServiceByName: fake.resolves(null), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const promise = device.get({ service: 'not-found-service', }); diff --git a/server/test/lib/device/device.getBySelector.test.js b/server/test/lib/device/device.getBySelector.test.js index e319fce967..2bc1820540 100644 --- a/server/test/lib/device/device.getBySelector.test.js +++ b/server/test/lib/device/device.getBySelector.test.js @@ -2,8 +2,10 @@ const EventEmitter = require('events'); const { expect } = require('chai'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Device.getBySelector', () => { it('should return device', () => { @@ -11,7 +13,7 @@ describe('Device.getBySelector', () => { stateManager.setState('device', 'test-device', { name: 'test', }); - const device = new Device(event, {}, stateManager, {}); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const oneDevice = device.getBySelector('test-device'); expect(oneDevice).to.deep.equal({ name: 'test', @@ -22,7 +24,7 @@ describe('Device.getBySelector', () => { stateManager.setState('device', 'test-device', { name: 'test', }); - const device = new Device(event, {}, stateManager, {}); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); expect(() => device.getBySelector('not-found')).to.throw('Device not found'); }); }); diff --git a/server/test/lib/device/device.getDeviceFeaturesAggregates.test.js b/server/test/lib/device/device.getDeviceFeaturesAggregates.test.js index 1bbceba946..b2daef1e8e 100644 --- a/server/test/lib/device/device.getDeviceFeaturesAggregates.test.js +++ b/server/test/lib/device/device.getDeviceFeaturesAggregates.test.js @@ -5,8 +5,10 @@ const uuid = require('uuid'); const { fake } = require('sinon'); const db = require('../../../models'); const Device = require('../../../lib/device'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const insertStates = async (intervalInMinutes) => { const queryInterface = db.sequelize.getQueryInterface(); @@ -62,7 +64,7 @@ describe('Device.getDeviceFeaturesAggregates', function Describe() { name: 'my-feature', }), }; - const deviceInstance = new Device(event, {}, stateManager, {}, {}, variable); + const deviceInstance = new Device(event, {}, stateManager, {}, {}, variable, job); await deviceInstance.calculateAggregate('hourly'); const { values, device, deviceFeature } = await deviceInstance.getDeviceFeaturesAggregates( 'test-device-feature', @@ -84,7 +86,7 @@ describe('Device.getDeviceFeaturesAggregates', function Describe() { name: 'my-feature', }), }; - const device = new Device(event, {}, stateManager, {}, {}, variable); + const device = new Device(event, {}, stateManager, {}, {}, variable, job); await device.calculateAggregate('hourly'); const { values } = await device.getDeviceFeaturesAggregates('test-device-feature', 24 * 60, 100); expect(values).to.have.lengthOf(100); @@ -100,7 +102,7 @@ describe('Device.getDeviceFeaturesAggregates', function Describe() { name: 'my-feature', }), }; - const device = new Device(event, {}, stateManager, {}, {}, variable); + const device = new Device(event, {}, stateManager, {}, {}, variable, job); await device.calculateAggregate('hourly'); const { values } = await device.getDeviceFeaturesAggregates('test-device-feature', 3 * 24 * 60, 100); expect(values).to.have.lengthOf(72); @@ -116,7 +118,7 @@ describe('Device.getDeviceFeaturesAggregates', function Describe() { name: 'my-feature', }), }; - const device = new Device(event, {}, stateManager, {}, {}, variable); + const device = new Device(event, {}, stateManager, {}, {}, variable, job); await device.calculateAggregate('hourly'); await device.calculateAggregate('daily'); const { values } = await device.getDeviceFeaturesAggregates('test-device-feature', 30 * 24 * 60, 100); @@ -133,7 +135,7 @@ describe('Device.getDeviceFeaturesAggregates', function Describe() { name: 'my-feature', }), }; - const device = new Device(event, {}, stateManager, {}, {}, variable); + const device = new Device(event, {}, stateManager, {}, {}, variable, job); await device.calculateAggregate('hourly'); await device.calculateAggregate('daily'); await device.calculateAggregate('monthly'); @@ -147,7 +149,7 @@ describe('Device.getDeviceFeaturesAggregates', function Describe() { const stateManager = { get: fake.returns(null), }; - const device = new Device(event, {}, stateManager, {}, {}, variable); + const device = new Device(event, {}, stateManager, {}, {}, variable, job); const promise = device.getDeviceFeaturesAggregates('this-device-does-not-exist', 365 * 24 * 60, 100); return assert.isRejected(promise, 'DeviceFeature not found'); }); diff --git a/server/test/lib/device/device.getDeviceFeaturesAggregatesMulti.test.js b/server/test/lib/device/device.getDeviceFeaturesAggregatesMulti.test.js index ee86edd7fa..34227090f2 100644 --- a/server/test/lib/device/device.getDeviceFeaturesAggregatesMulti.test.js +++ b/server/test/lib/device/device.getDeviceFeaturesAggregatesMulti.test.js @@ -4,8 +4,10 @@ const uuid = require('uuid'); const { fake } = require('sinon'); const db = require('../../../models'); const Device = require('../../../lib/device'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const insertStates = async (intervalInMinutes) => { const queryInterface = db.sequelize.getQueryInterface(); @@ -52,7 +54,7 @@ describe('Device.getDeviceFeaturesAggregatesMulti', function Describe() { name: 'my-feature', }), }; - const device = new Device(event, {}, stateManager, {}, {}, variable); + const device = new Device(event, {}, stateManager, {}, {}, variable, job); await device.calculateAggregate('hourly'); const response = await device.getDeviceFeaturesAggregatesMulti(['test-device-feature'], 60, 100); expect(response).to.be.instanceOf(Array); diff --git a/server/test/lib/device/device.newStateEvent.test.js b/server/test/lib/device/device.newStateEvent.test.js index ba9a037b83..abe34f6780 100644 --- a/server/test/lib/device/device.newStateEvent.test.js +++ b/server/test/lib/device/device.newStateEvent.test.js @@ -1,8 +1,10 @@ const EventEmitter = require('events'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Device', () => { it('should save new sate', async () => { @@ -24,7 +26,7 @@ describe('Device', () => { created_at: '2019-02-12 07:49:07.556 +00:00', updated_at: '2019-02-12 07:49:07.556 +00:00', }); - const device = new Device(event, {}, stateManager); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); await device.newStateEvent({ device_feature_external_id: 'hue:binary:1', state: 12 }); }); }); diff --git a/server/test/lib/device/device.notify.test.js b/server/test/lib/device/device.notify.test.js index af887d60bb..b6867ff58d 100644 --- a/server/test/lib/device/device.notify.test.js +++ b/server/test/lib/device/device.notify.test.js @@ -6,6 +6,7 @@ const { EVENTS } = require('../../../utils/constants'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); const ServiceManager = require('../../../lib/service'); +const Job = require('../../../lib/job'); const event = { emit: fake.returns(null), @@ -13,6 +14,8 @@ const event = { }; const messageManager = {}; +const job = new Job(event); + describe('Device.notify', () => { afterEach(() => { sinon.reset(); @@ -32,7 +35,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; stateManager.setState('serviceById', serviceId, service); - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', @@ -61,7 +64,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; stateManager.setState('serviceById', serviceId, service); - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', @@ -90,7 +93,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; stateManager.setState('serviceById', serviceId, service); - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', @@ -119,7 +122,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; stateManager.setState('serviceById', serviceId, service); - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', @@ -140,7 +143,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', @@ -160,7 +163,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; stateManager.setState('serviceById', serviceId, service); - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', @@ -180,7 +183,7 @@ describe('Device.notify', () => { const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; stateManager.setState('serviceById', serviceId, service); - const device = new Device(event, messageManager, stateManager, serviceManager); + const device = new Device(event, messageManager, stateManager, serviceManager, {}, {}, job); const newDevice = { service_id: serviceId, name: 'Philips Hue 1', diff --git a/server/test/lib/device/device.poll.test.js b/server/test/lib/device/device.poll.test.js index e8ed6c348b..957baf104f 100644 --- a/server/test/lib/device/device.poll.test.js +++ b/server/test/lib/device/device.poll.test.js @@ -5,8 +5,10 @@ const { assert } = require('chai'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const testService = { device: { @@ -26,7 +28,7 @@ describe('Device', () => { const service = { getService: () => testService, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); await device.poll({ service: { name: 'test', @@ -38,7 +40,7 @@ describe('Device', () => { const service = { getService: () => testServiceBroken, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); await device.poll({ service: { name: 'test', @@ -50,7 +52,7 @@ describe('Device', () => { const service = { getService: () => null, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const promise = device.poll({ service: { name: 'doesnotexist', @@ -63,7 +65,7 @@ describe('Device', () => { const service = { getService: () => ({}), }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const promise = device.poll({ service: { name: 'doesnotexist', diff --git a/server/test/lib/device/device.purgeStates.test.js b/server/test/lib/device/device.purgeStates.test.js index ff78f8e44a..3f300c7f92 100644 --- a/server/test/lib/device/device.purgeStates.test.js +++ b/server/test/lib/device/device.purgeStates.test.js @@ -5,8 +5,10 @@ const { fake } = require('sinon'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Device', () => { it('should purgeStates', async () => { @@ -15,7 +17,7 @@ describe('Device', () => { }; const stateManager = new StateManager(event); const service = {}; - const device = new Device(event, {}, stateManager, service, {}, variable); + const device = new Device(event, {}, stateManager, service, {}, variable, job); const devicePurged = await device.purgeStates(); expect(devicePurged).to.equal(true); }); @@ -25,7 +27,7 @@ describe('Device', () => { }; const stateManager = new StateManager(event); const service = {}; - const device = new Device(event, {}, stateManager, service, {}, variable); + const device = new Device(event, {}, stateManager, service, {}, variable, job); const devicePurged = await device.purgeStates(); expect(devicePurged).to.equal(false); }); @@ -35,7 +37,7 @@ describe('Device', () => { }; const stateManager = new StateManager(event); const service = {}; - const device = new Device(event, {}, stateManager, service, {}, variable); + const device = new Device(event, {}, stateManager, service, {}, variable, job); const devicePurged = await device.purgeStates(); expect(devicePurged).to.equal(false); }); diff --git a/server/test/lib/device/device.purgeStatesByFeatureId.test.js b/server/test/lib/device/device.purgeStatesByFeatureId.test.js index b2593be95f..fb5073e744 100644 --- a/server/test/lib/device/device.purgeStatesByFeatureId.test.js +++ b/server/test/lib/device/device.purgeStatesByFeatureId.test.js @@ -1,18 +1,30 @@ const { expect } = require('chai'); const EventEmitter = require('events'); +const uuid = require('uuid'); const Device = require('../../../lib/device'); const db = require('../../../models'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); -describe('device.purgeStatesByFeatureId', () => { +describe('device.purgeStatesByFeatureId', async function Describe() { + this.timeout(5000); beforeEach(async () => { - await db.DeviceFeatureState.create({ - device_feature_id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', - value: 12, - }); + const queryInterface = db.sequelize.getQueryInterface(); + const deviceFeatureStateToInsert = []; + for (let i = 1; i <= 110; i += 1) { + const date = new Date(); + deviceFeatureStateToInsert.push({ + id: uuid.v4(), + device_feature_id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', + value: i, + created_at: date, + updated_at: date, + }); + } + await queryInterface.bulkInsert('t_device_feature_state', deviceFeatureStateToInsert); await db.DeviceFeatureStateAggregate.create({ type: 'daily', device_feature_id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', @@ -32,8 +44,15 @@ describe('device.purgeStatesByFeatureId', () => { it('should purge states of a specific feature id', async () => { const stateManager = new StateManager(event); const service = {}; - const device = new Device(event, {}, stateManager, service, {}, {}); - await device.purgeStatesByFeatureId('ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4'); + const job = new Job(event); + const device = new Device(event, {}, stateManager, service, {}, {}, job); + device.STATES_TO_PURGE_PER_DEVICE_FEATURE_CLEAN_BATCH = 1; + device.WAIT_TIME_BETWEEN_DEVICE_FEATURE_CLEAN_BATCH = 1; + const res = await device.purgeStatesByFeatureId('ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4'); + expect(res).to.deep.equal({ + numberOfDeviceFeatureStateToDelete: 110, + numberOfDeviceFeatureStateAggregateToDelete: 3, + }); const states = await db.DeviceFeatureState.findAll({ where: { device_feature_id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', diff --git a/server/test/lib/device/device.saveState.test.js b/server/test/lib/device/device.saveState.test.js index ee77962c6a..c3c753ac43 100644 --- a/server/test/lib/device/device.saveState.test.js +++ b/server/test/lib/device/device.saveState.test.js @@ -2,6 +2,7 @@ const { expect } = require('chai'); const { assert, stub, useFakeTimers } = require('sinon'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const { BadParameters } = require('../../../utils/coreErrors'); describe('Device.saveState', () => { @@ -12,7 +13,8 @@ describe('Device.saveState', () => { }; const clock = useFakeTimers(new Date('2019-05-01T00:00:00Z').getTime()); const stateManager = new StateManager(event); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); await device.saveState( { id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', @@ -46,7 +48,8 @@ describe('Device.saveState', () => { }; const clock = useFakeTimers(new Date('2019-05-01T00:00:00Z').getTime()); const stateManager = new StateManager(event); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); stateManager.setState('deviceFeature', 'test-device-feature', { last_value: 5, }); @@ -82,7 +85,8 @@ describe('Device.saveState', () => { on: stub().returns(null), }; const stateManager = new StateManager(event); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); await device.saveState( { id: 'ca91dfdf-55b2-4cf8-a58b-99c0fbf6f5e4', @@ -101,7 +105,8 @@ describe('Device.saveState', () => { on: stub().returns(null), }; const stateManager = new StateManager(event); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const promises = []; for (let i = 0; i < 200; i += 1) { promises.push( @@ -123,7 +128,8 @@ describe('Device.saveState', () => { on: stub().returns(null), }; const stateManager = new StateManager(event); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const nanValue = parseInt('NaN value', 10); @@ -147,7 +153,8 @@ describe('Device.saveState', () => { on: stub().returns(null), }; const stateManager = new StateManager(event); - const device = new Device(event, {}, stateManager); + const job = new Job(event); + const device = new Device(event, {}, stateManager, {}, {}, {}, job); const stringValue = 'LOCK'; diff --git a/server/test/lib/device/device.setValue.test.js b/server/test/lib/device/device.setValue.test.js index c38a78668b..2e69f93edc 100644 --- a/server/test/lib/device/device.setValue.test.js +++ b/server/test/lib/device/device.setValue.test.js @@ -5,14 +5,10 @@ const { assert } = require('chai'); const Device = require('../../../lib/device'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); - -/* const testService = { - light: { - turnOn: fake.resolves(true), - }, -}; */ +const job = new Job(event); describe('Device', () => { it('should throw an error, service does not exist', async () => { @@ -20,7 +16,7 @@ describe('Device', () => { const service = { getService: () => null, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const promise = device.setValue( { service: { @@ -38,7 +34,7 @@ describe('Device', () => { const service = { getService: () => {}, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); const promise = device.setValue( { service: { diff --git a/server/test/lib/device/device.setupPoll.test.js b/server/test/lib/device/device.setupPoll.test.js index 285ec1e520..75b98c19b8 100644 --- a/server/test/lib/device/device.setupPoll.test.js +++ b/server/test/lib/device/device.setupPoll.test.js @@ -3,8 +3,10 @@ const { fake } = require('sinon'); const Device = require('../../../lib/device'); const { DEVICE_POLL_FREQUENCIES } = require('../../../utils/constants'); const StateManager = require('../../../lib/state'); +const Job = require('../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const testService = { device: { @@ -18,7 +20,7 @@ describe('Device', () => { const service = { getService: () => testService, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); device.setupPoll(); }); it('should poll all', async () => { @@ -26,7 +28,7 @@ describe('Device', () => { const service = { getService: () => testService, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); device.devicesByPollFrequency[DEVICE_POLL_FREQUENCIES.EVERY_MINUTES] = [ { service: { @@ -41,7 +43,7 @@ describe('Device', () => { const service = { getService: () => testService, }; - const device = new Device(event, {}, stateManager, service); + const device = new Device(event, {}, stateManager, service, {}, {}, job); device.devicesByPollFrequency[DEVICE_POLL_FREQUENCIES.EVERY_MINUTES] = [ { service: { diff --git a/server/test/lib/device/humidity-sensor/humidity-sensor.test.js b/server/test/lib/device/humidity-sensor/humidity-sensor.test.js index 1e66e3b265..ebf5329567 100644 --- a/server/test/lib/device/humidity-sensor/humidity-sensor.test.js +++ b/server/test/lib/device/humidity-sensor/humidity-sensor.test.js @@ -5,8 +5,10 @@ const sinon = require('sinon'); const Device = require('../../../../lib/device'); const Room = require('../../../../lib/room'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const messageManager = { replyByIntent: fake.resolves(true), }; @@ -14,7 +16,7 @@ const messageManager = { describe('HumiditySensor.getHumidityInRoom', () => { it('should get average humidity in room', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const result = await deviceManager.humiditySensorManager.getHumidityInRoom('2398c689-8b47-43cc-ad32-e98d9be098b5'); expect(result).to.deep.equal({ humidity: 56.2, @@ -23,7 +25,7 @@ describe('HumiditySensor.getHumidityInRoom', () => { }); it('should return not found error', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); const humidityResult = await deviceManager.humiditySensorManager.getHumidityInRoom( 'f08337ff-206e-4bd7-86c4-6d63d793d58e', ); @@ -40,7 +42,7 @@ describe('HumiditySensor.command', () => { }); it('should ask the humidity in a room', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const message = {}; await deviceManager.humiditySensorManager.command( message, @@ -74,7 +76,7 @@ describe('HumiditySensor.command', () => { }); it('should return room not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const message = {}; await deviceManager.humiditySensorManager.command( message, @@ -92,7 +94,7 @@ describe('HumiditySensor.command', () => { removeRoom: fake.returns(null), }; const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const room = new Room(brain); await room.create('test-house', { name: 'No value Room', @@ -168,7 +170,7 @@ describe('HumiditySensor.command', () => { removeRoom: fake.returns(null), }; const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const room = new Room(brain); await room.create('test-house', { name: 'No value Room', diff --git a/server/test/lib/device/light/light.buildLightObject.test.js b/server/test/lib/device/light/light.buildLightObject.test.js index d35ddbaaa2..e40e6d76c7 100644 --- a/server/test/lib/device/light/light.buildLightObject.test.js +++ b/server/test/lib/device/light/light.buildLightObject.test.js @@ -3,13 +3,15 @@ const EventEmitter = require('events'); const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Light.buildLightObject', () => { it('should should build light object with binary deviceFeature', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); const device = { id: '78ffc050-71c4-4dfe-8f3b-4b153e79c457', selector: 'test', diff --git a/server/test/lib/device/light/light.command.test.js b/server/test/lib/device/light/light.command.test.js index bed7efd666..ef98d6c0c5 100644 --- a/server/test/lib/device/light/light.command.test.js +++ b/server/test/lib/device/light/light.command.test.js @@ -2,8 +2,10 @@ const EventEmitter = require('events'); const { assert, fake } = require('sinon'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const testService = { device: { @@ -38,7 +40,7 @@ const context = {}; describe('Light.command', () => { it('should send a turn on command', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, service); + const deviceManager = new Device(event, messageManager, stateManager, service, {}, {}, job); await deviceManager.lightManager.command( message, { @@ -65,7 +67,7 @@ describe('Light.command', () => { }); it('should fail to send a turn on command', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, serviceBroken); + const deviceManager = new Device(event, messageManager, stateManager, serviceBroken, {}, {}, job); await deviceManager.lightManager.command( message, { @@ -92,7 +94,7 @@ describe('Light.command', () => { }); it('should fail to send a turn on command', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, serviceBroken); + const deviceManager = new Device(event, messageManager, stateManager, serviceBroken, {}, {}, job); await deviceManager.lightManager.command( message, { @@ -118,7 +120,7 @@ describe('Light.command', () => { }); it('should send a turn off command', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, service); + const deviceManager = new Device(event, messageManager, stateManager, service, {}, {}, job); await deviceManager.lightManager.command( message, { @@ -145,7 +147,7 @@ describe('Light.command', () => { }); it('should fail to send a command because no device with binary feature in this room', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, service); + const deviceManager = new Device(event, messageManager, stateManager, service, {}, {}, job); // Mock getDeviceFeature to answer no binay feature deviceManager.lightManager.getLightsInRoom = () => new Promise((resolve) => { @@ -182,7 +184,7 @@ describe('Light.command', () => { }); it('should fail to send a command because no device in this room', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, service); + const deviceManager = new Device(event, messageManager, stateManager, service, {}, {}, job); // Mock getLightsInRoom to answer no devices deviceManager.lightManager.getLightsInRoom = () => new Promise((resolve) => { @@ -213,7 +215,7 @@ describe('Light.command', () => { }); it('should fail to send a command because no room found in command', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, service); + const deviceManager = new Device(event, messageManager, stateManager, service, {}, {}, job); // Mock getLightsInRoom to answer no devices deviceManager.lightManager.getLightsInRoom = () => new Promise((resolve) => { diff --git a/server/test/lib/device/light/light.init.test.js b/server/test/lib/device/light/light.init.test.js index bc9cea9b9a..c4864afe5d 100644 --- a/server/test/lib/device/light/light.init.test.js +++ b/server/test/lib/device/light/light.init.test.js @@ -2,13 +2,15 @@ const EventEmitter = require('events'); const { expect } = require('chai'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); describe('Light', () => { it('should get all lights and store them in the stateManager', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); const lights = await deviceManager.lightManager.init(); lights.forEach((light) => { expect(light).to.have.property('id'); diff --git a/server/test/lib/device/light/light.turnOff.test.js b/server/test/lib/device/light/light.turnOff.test.js index 5322355cfc..2a384bb612 100644 --- a/server/test/lib/device/light/light.turnOff.test.js +++ b/server/test/lib/device/light/light.turnOff.test.js @@ -2,8 +2,10 @@ const EventEmitter = require('events'); const { assert, fake } = require('sinon'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const testService = { device: { @@ -31,7 +33,7 @@ const deviceFeature = { describe('Light', () => { it('should turnOff the light', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, service); + const deviceManager = new Device(event, {}, stateManager, service, {}, {}, job); await deviceManager.lightManager.turnOff(device, deviceFeature); assert.calledWith(testService.device.setValue, device, deviceFeature, 0); }); diff --git a/server/test/lib/device/light/light.turnOn.test.js b/server/test/lib/device/light/light.turnOn.test.js index 6563f5f1a5..64b0e8a25b 100644 --- a/server/test/lib/device/light/light.turnOn.test.js +++ b/server/test/lib/device/light/light.turnOn.test.js @@ -2,8 +2,10 @@ const EventEmitter = require('events'); const { assert, fake } = require('sinon'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const testService = { device: { @@ -31,7 +33,7 @@ const deviceFeature = { describe('Light', () => { it('should turnOn the light', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, service); + const deviceManager = new Device(event, {}, stateManager, service, {}, {}, job); await deviceManager.lightManager.turnOn(device, deviceFeature); assert.calledWith(testService.device.setValue, device, deviceFeature, 1); }); diff --git a/server/test/lib/device/temperature-sensor/temperature-sensor.test.js b/server/test/lib/device/temperature-sensor/temperature-sensor.test.js index 77ecc541b6..8e6505f22f 100644 --- a/server/test/lib/device/temperature-sensor/temperature-sensor.test.js +++ b/server/test/lib/device/temperature-sensor/temperature-sensor.test.js @@ -3,8 +3,10 @@ const { expect } = require('chai'); const { assert, fake } = require('sinon'); const Device = require('../../../../lib/device'); const StateManager = require('../../../../lib/state'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); +const job = new Job(event); const messageManager = { replyByIntent: fake.resolves(true), @@ -13,7 +15,7 @@ const messageManager = { describe('TemperatureSensor.getTemperatureInRoom', () => { it('should get average temperature in room in celsius', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const result = await deviceManager.temperatureSensorManager.getTemperatureInRoom( '2398c689-8b47-43cc-ad32-e98d9be098b5', { @@ -27,7 +29,7 @@ describe('TemperatureSensor.getTemperatureInRoom', () => { }); it('should get average temperature in room in fahrenheit', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); const result = await deviceManager.temperatureSensorManager.getTemperatureInRoom( '2398c689-8b47-43cc-ad32-e98d9be098b5', { @@ -41,7 +43,7 @@ describe('TemperatureSensor.getTemperatureInRoom', () => { }); it('should return not found error', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, {}, stateManager, {}); + const deviceManager = new Device(event, {}, stateManager, {}, {}, {}, job); const temperatureResult = await deviceManager.temperatureSensorManager.getTemperatureInRoom( 'f08337ff-206e-4bd7-86c4-6d63d793d58e', { @@ -58,7 +60,7 @@ describe('TemperatureSensor.getTemperatureInRoom', () => { describe('TemperatureSensor.command', () => { it('should ask the temperature in a room', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const message = { user: { temperature_unit_preference: 'celsius', @@ -132,7 +134,7 @@ describe('TemperatureSensor.command', () => { }); it('should return room not found', async () => { const stateManager = new StateManager(event); - const deviceManager = new Device(event, messageManager, stateManager, {}); + const deviceManager = new Device(event, messageManager, stateManager, {}, {}, {}, job); const message = { user: { temperature_unit_preference: 'celsius', diff --git a/server/test/lib/system/system.createContainer.test.js b/server/test/lib/system/system.createContainer.test.js index cecb00688a..8ef03f1996 100644 --- a/server/test/lib/system/system.createContainer.test.js +++ b/server/test/lib/system/system.createContainer.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -21,6 +22,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -29,7 +32,7 @@ describe('system.createContainer', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.downloadUpgrade.test.js b/server/test/lib/system/system.downloadUpgrade.test.js index 88ebf6f58e..54dc4084a6 100644 --- a/server/test/lib/system/system.downloadUpgrade.test.js +++ b/server/test/lib/system/system.downloadUpgrade.test.js @@ -12,6 +12,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -22,6 +23,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -30,7 +33,7 @@ describe('system.downloadUpgrade', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.exec.test.js b/server/test/lib/system/system.exec.test.js index 8b8fb293ab..5da24cde9f 100644 --- a/server/test/lib/system/system.exec.test.js +++ b/server/test/lib/system/system.exec.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -21,6 +22,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -29,7 +32,7 @@ describe('system.exec', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.getContainerMounts.test.js b/server/test/lib/system/system.getContainerMounts.test.js index c8cfef6265..f5652ddf6e 100644 --- a/server/test/lib/system/system.getContainerMounts.test.js +++ b/server/test/lib/system/system.getContainerMounts.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -18,8 +19,11 @@ const sequelize = { const event = { on: fake.resolves(null), + emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -28,7 +32,7 @@ describe('system.getContainerMounts', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.getContainers.test.js b/server/test/lib/system/system.getContainers.test.js index abe6f692b4..4d6fbb1e13 100644 --- a/server/test/lib/system/system.getContainers.test.js +++ b/server/test/lib/system/system.getContainers.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -18,8 +19,11 @@ const sequelize = { const event = { on: fake.resolves(null), + emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -28,7 +32,7 @@ describe('system.getContainers', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.getDiskSpace.test.js b/server/test/lib/system/system.getDiskSpace.test.js index 14c12f303e..4f84fc4205 100644 --- a/server/test/lib/system/system.getDiskSpace.test.js +++ b/server/test/lib/system/system.getDiskSpace.test.js @@ -22,14 +22,19 @@ const System = proxyquire('../../../lib/system', { './system.getDiskSpace': { getDiskSpace }, }); +const Job = require('../../../lib/job'); + const sequelize = { close: fake.resolves(null), }; const event = { on: fake.resolves(null), + emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -38,7 +43,7 @@ describe('system.getDiskSpace', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); // Reset all fakes invoked within init call sinon.reset(); }); diff --git a/server/test/lib/system/system.getGladysBasePath.test.js b/server/test/lib/system/system.getGladysBasePath.test.js index 8234ca73d8..bbe4a145c0 100644 --- a/server/test/lib/system/system.getGladysBasePath.test.js +++ b/server/test/lib/system/system.getGladysBasePath.test.js @@ -10,6 +10,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -20,6 +21,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -28,7 +31,7 @@ describe('system.getGladysBasePath', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); system.getGladysContainerId = fake.resolves('containerid'); system.getContainerMounts = fake.resolves([]); await system.init(); diff --git a/server/test/lib/system/system.getGladysContainerId.test.js b/server/test/lib/system/system.getGladysContainerId.test.js index effd04601d..1aba74cbca 100644 --- a/server/test/lib/system/system.getGladysContainerId.test.js +++ b/server/test/lib/system/system.getGladysContainerId.test.js @@ -7,6 +7,7 @@ const proxyquire = require('proxyquire').noCallThru(); const { PlatformNotCompatible } = require('../../../utils/coreErrors'); const DockerodeMock = require('./DockerodeMock.test'); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -17,6 +18,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -86,7 +89,7 @@ describe('system.getGladysContainerId', () => { './system.getGladysContainerId': getGladysContainerId, }); - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.getInfos.test.js b/server/test/lib/system/system.getInfos.test.js index 2fa6490741..07661b894d 100644 --- a/server/test/lib/system/system.getInfos.test.js +++ b/server/test/lib/system/system.getInfos.test.js @@ -4,6 +4,7 @@ const sinon = require('sinon'); const { fake, assert } = sinon; const System = require('../../../lib/system'); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -11,8 +12,11 @@ const sequelize = { const event = { on: fake.resolves(null), + emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -21,7 +25,7 @@ describe('system.getInfos', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.getNetworkMode.test.js b/server/test/lib/system/system.getNetworkMode.test.js index 3a4abefd09..ae1214ac9b 100644 --- a/server/test/lib/system/system.getNetworkMode.test.js +++ b/server/test/lib/system/system.getNetworkMode.test.js @@ -14,6 +14,7 @@ const System = proxyquire('../../../lib/system', { getGladysContainerId: fake.resolves('967ef3114fa2ceb8c4f6dbdbc78ee411a6f33fb1fe1d32455686ef6e89f41d1c'), }, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -24,6 +25,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -32,7 +35,7 @@ describe('system.getNetworkMode', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.init.test.js b/server/test/lib/system/system.init.test.js index d2d2dba222..016160cf2e 100644 --- a/server/test/lib/system/system.init.test.js +++ b/server/test/lib/system/system.init.test.js @@ -10,14 +10,19 @@ const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); + const sequelize = { close: fake.resolves(null), }; const event = { on: fake.resolves(null), + emit: fake.returns(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -26,7 +31,7 @@ describe('system.init', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); }); afterEach(() => { @@ -36,9 +41,9 @@ describe('system.init', () => { it('should init system', async () => { await system.init(); - assert.calledOnce(system.dockerode.listContainers); + assert.called(system.dockerode.listContainers); assert.notCalled(sequelize.close); - assert.calledOnce(event.on); + assert.called(event.on); }); }); diff --git a/server/test/lib/system/system.installUpgrade.test.js b/server/test/lib/system/system.installUpgrade.test.js index d36f1b47bf..3fafc67d9a 100644 --- a/server/test/lib/system/system.installUpgrade.test.js +++ b/server/test/lib/system/system.installUpgrade.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -18,8 +19,11 @@ const sequelize = { const event = { on: fake.resolves(null), + emit: fake.returns(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -28,7 +32,7 @@ describe('system.installUpgrade', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.isDocker.test.js b/server/test/lib/system/system.isDocker.test.js index bfd5833ebe..991ed4d34a 100644 --- a/server/test/lib/system/system.isDocker.test.js +++ b/server/test/lib/system/system.isDocker.test.js @@ -20,6 +20,7 @@ const { isDocker } = proxyquire('../../../lib/system/system.isDocker', { const System = proxyquire('../../../lib/system', { './system.isDocker': { isDocker }, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -27,8 +28,11 @@ const sequelize = { const event = { on: fake.resolves(null), + emit: fake.returns(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -37,7 +41,7 @@ describe('system.isDocker', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.pull.test.js b/server/test/lib/system/system.pull.test.js index 8dc26c41c0..6ffc3648e2 100644 --- a/server/test/lib/system/system.pull.test.js +++ b/server/test/lib/system/system.pull.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -21,6 +22,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -29,7 +32,7 @@ describe('system.pull', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.removeContainer.test.js b/server/test/lib/system/system.removeContainer.test.js index f49f979d31..a9798dd2fa 100644 --- a/server/test/lib/system/system.removeContainer.test.js +++ b/server/test/lib/system/system.removeContainer.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -21,6 +22,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -29,7 +32,7 @@ describe('system.removeContainer', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.restartContainer.test.js b/server/test/lib/system/system.restartContainer.test.js index d384d4f095..80cb6377cd 100644 --- a/server/test/lib/system/system.restartContainer.test.js +++ b/server/test/lib/system/system.restartContainer.test.js @@ -11,6 +11,7 @@ const DockerodeMock = require('./DockerodeMock.test'); const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -21,6 +22,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -29,7 +32,7 @@ describe('system.restartContainer', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.shutdown.test.js b/server/test/lib/system/system.shutdown.test.js index c1c85644d9..9599a22220 100644 --- a/server/test/lib/system/system.shutdown.test.js +++ b/server/test/lib/system/system.shutdown.test.js @@ -3,6 +3,7 @@ const sinon = require('sinon'); const { fake, assert } = sinon; const System = require('../../../lib/system'); +const Job = require('../../../lib/job'); const sequelize = { close: fake.resolves(null), @@ -13,6 +14,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -26,16 +29,16 @@ describe('system.shutdown', () => { processExistStub.restore(); }); it('should shutdown system', async () => { - const system = new System(sequelize, event, config); + const system = new System(sequelize, event, config, job); system.sequelize = { - close: fake.resolves(), + close: fake.resolves(null), }; await system.shutdown(); assert.calledOnce(system.sequelize.close); assert.calledOnce(processExistStub); }); it('should shutdown system even if DB was already closed', async () => { - const system = new System(sequelize, event, config); + const system = new System(sequelize, event, config, job); system.sequelize = { close: fake.rejects(), }; diff --git a/server/test/lib/system/system.stopContainer.test.js b/server/test/lib/system/system.stopContainer.test.js index f610219059..a38c08d0f8 100644 --- a/server/test/lib/system/system.stopContainer.test.js +++ b/server/test/lib/system/system.stopContainer.test.js @@ -12,6 +12,8 @@ const System = proxyquire('../../../lib/system', { dockerode: DockerodeMock, }); +const Job = require('../../../lib/job'); + const sequelize = { close: fake.resolves(null), }; @@ -21,6 +23,8 @@ const event = { emit: fake.resolves(null), }; +const job = new Job(event); + const config = { tempFolder: '/tmp/gladys', }; @@ -29,7 +33,7 @@ describe('system.stopContainer', () => { let system; beforeEach(async () => { - system = new System(sequelize, event, config); + system = new System(sequelize, event, config, job); await system.init(); // Reset all fakes invoked within init call sinon.reset(); diff --git a/server/test/lib/system/system.vacuum.test.js b/server/test/lib/system/system.vacuum.test.js new file mode 100644 index 0000000000..66bf2e73c6 --- /dev/null +++ b/server/test/lib/system/system.vacuum.test.js @@ -0,0 +1,24 @@ +const { fake } = require('sinon'); + +const System = require('../../../lib/system'); +const Job = require('../../../lib/job'); + +const sequelize = { + close: fake.resolves(null), +}; + +const event = { + on: fake.resolves(null), + emit: fake.resolves(null), +}; + +const job = new Job(event); + +const config = {}; + +describe('system.vacuum', () => { + it('should vacuum system', async () => { + const system = new System(sequelize, event, config, job); + await system.vacuum(); + }); +}); diff --git a/server/test/services/philips-hue/light/light.configureBridge.test.js b/server/test/services/philips-hue/light/light.configureBridge.test.js index b470260657..7ae5210a49 100644 --- a/server/test/services/philips-hue/light/light.configureBridge.test.js +++ b/server/test/services/philips-hue/light/light.configureBridge.test.js @@ -10,11 +10,13 @@ const PhilipsHueService = proxyquire('../../../../services/philips-hue/index', { const StateManager = require('../../../../lib/state'); const ServiceManager = require('../../../../lib/service'); const DeviceManager = require('../../../../lib/device'); +const Job = require('../../../../lib/job'); const event = new EventEmitter(); const stateManager = new StateManager(event); +const job = new Job(event); const serviceManager = new ServiceManager({}, stateManager); -const deviceManager = new DeviceManager(event, {}, stateManager, serviceManager); +const deviceManager = new DeviceManager(event, {}, stateManager, serviceManager, {}, {}, job); const gladys = { device: deviceManager, diff --git a/server/utils/constants.js b/server/utils/constants.js index 0587e423c5..677553a46c 100644 --- a/server/utils/constants.js +++ b/server/utils/constants.js @@ -73,6 +73,7 @@ const EVENTS = { NEW_STATE: 'device.new-state', PURGE_STATES: 'device.purge-states', CALCULATE_HOURLY_AGGREGATE: 'device.calculate-hourly-aggregate', + PURGE_STATES_SINGLE_FEATURE: 'device.purge-states-single-feature', }, GATEWAY: { CREATE_BACKUP: 'gateway.create-backup', @@ -157,6 +158,7 @@ const EVENTS = { DOWNLOAD_UPGRADE: 'system.download-upgrade', CHECK_UPGRADE: 'system.check-upgrade', TIMEZONE_CHANGED: 'system.timezone-changed', + VACUUM: 'system.vacuum', }, WEBSOCKET: { SEND: 'websocket.send', @@ -789,6 +791,8 @@ const JOB_TYPES = { DAILY_DEVICE_STATE_AGGREGATE: 'daily-device-state-aggregate', MONTHLY_DEVICE_STATE_AGGREGATE: 'monthly-device-state-aggregate', GLADYS_GATEWAY_BACKUP: 'gladys-gateway-backup', + DEVICE_STATES_PURGE_SINGLE_FEATURE: 'device-state-purge-single-feature', + VACUUM: 'vacuum', }; const JOB_STATUS = {