From 7bc2ccb5ae5172ee375f05b42ab57b80d7fcb135 Mon Sep 17 00:00:00 2001 From: Zhihe Li Date: Mon, 4 Dec 2023 11:41:30 -0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20Use=20a=20native=20c?= =?UTF-8?q?lass=20for=20the=20client=20daemon=20handler=20(#2026)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../addon/handlers/client-daemon-handler.js | 67 ++++++++++--------- ui/desktop/app/services/store.js | 8 +-- .../tests/acceptance/authentication-test.js | 4 -- .../tests/acceptance/cluster-url-test.js | 4 -- .../projects/sessions/index-test.js | 4 -- .../projects/sessions/session-test.js | 4 -- .../acceptance/projects/targets/index-test.js | 6 +- .../projects/targets/target-test.js | 4 -- ui/desktop/tests/acceptance/scopes-test.js | 4 -- 9 files changed, 40 insertions(+), 65 deletions(-) diff --git a/addons/api/addon/handlers/client-daemon-handler.js b/addons/api/addon/handlers/client-daemon-handler.js index 51a0ca1b6..0b736ed58 100644 --- a/addons/api/addon/handlers/client-daemon-handler.js +++ b/addons/api/addon/handlers/client-daemon-handler.js @@ -1,6 +1,8 @@ +import { inject as service } from '@ember/service'; +import { getOwner, setOwner } from '@ember/application'; +import ArrayProxy from '@ember/array/proxy'; import { pluralize } from 'ember-inflector'; import { generateMQLExpression } from '../utils/mqlQuery'; -import ArrayProxy from '@ember/array/proxy'; /** * Not all types are yet supported by the client daemon so we'll @@ -13,11 +15,43 @@ const supportedTypes = { session: ['id', 'type', 'status', 'endpoint', 'scope_id', 'target_id'], }; +/** + * Takes an array and the current page and pagesize to calculate the correct + * number of results to return to the caller. + * + * @param array + * @param page + * @param pageSize + * @returns {[*]} + */ +const paginateResults = (array, page, pageSize) => { + const length = array?.length; + if (!array || length === 0) { + return []; + } + if (!page || !pageSize) { + return array; + } + + const offset = (page - 1) * pageSize; + const start = Math.min(length - 1, offset); + const end = Math.min(length, offset + pageSize); + + return array.slice(start, end); +}; + /** * Handler to sit in front of the API request layer * so we can request from the daemon first */ -const ClientDaemonHandler = { +export default class ClientDaemonHandler { + @service session; + @service ipc; + + constructor(context) { + setOwner(this, getOwner(context)); + } + async request(context, next) { switch (context.request.op) { case 'query': { @@ -92,32 +126,5 @@ const ClientDaemonHandler = { default: return next(context.request); } - }, -}; - -/** - * Takes an array and the current page and pagesize to calculate the correct - * number of results to return to the caller. - * - * @param array - * @param page - * @param pageSize - * @returns {[*]} - */ -const paginateResults = (array, page, pageSize) => { - const length = array?.length; - if (!array || length === 0) { - return []; } - if (!page || !pageSize) { - return array; - } - - const offset = (page - 1) * pageSize; - const start = Math.min(length - 1, offset); - const end = Math.min(length, offset + pageSize); - - return array.slice(start, end); -}; - -export default ClientDaemonHandler; +} diff --git a/ui/desktop/app/services/store.js b/ui/desktop/app/services/store.js index 3d59eccb7..e2e120209 100644 --- a/ui/desktop/app/services/store.js +++ b/ui/desktop/app/services/store.js @@ -1,21 +1,17 @@ import Store, { CacheHandler } from '@ember-data/store'; import RequestManager from '@ember-data/request'; import { LegacyNetworkHandler } from '@ember-data/legacy-compat'; -import { inject as service } from '@ember/service'; import ClientDaemonHandler from 'api/handlers/client-daemon-handler'; export default class extends Store { - @service ipc; - @service session; - requestManager = new RequestManager(); constructor(args) { super(args); - ClientDaemonHandler.request = ClientDaemonHandler.request.bind(this); + const clientDaemonHandler = new ClientDaemonHandler(this); - this.requestManager.use([ClientDaemonHandler, LegacyNetworkHandler]); + this.requestManager.use([clientDaemonHandler, LegacyNetworkHandler]); this.requestManager.useCache(CacheHandler); } } diff --git a/ui/desktop/tests/acceptance/authentication-test.js b/ui/desktop/tests/acceptance/authentication-test.js index d49d18f18..b2968f602 100644 --- a/ui/desktop/tests/acceptance/authentication-test.js +++ b/ui/desktop/tests/acceptance/authentication-test.js @@ -22,7 +22,6 @@ import { invalidateSession, } from 'ember-simple-auth/test-support'; import WindowMockIPC from '../helpers/window-mock-ipc'; -import Store from 'api/services/store'; module('Acceptance | authentication', function (hooks) { setupApplicationTest(hooks); @@ -129,9 +128,6 @@ module('Acceptance | authentication', function (hooks) { // Mock the postMessage interface used by IPC. this.owner.register('service:browser/window', WindowMockIPC); setDefaultClusterUrl(this); - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); test('visiting index while unauthenticated redirects to global authenticate method', async function (assert) { diff --git a/ui/desktop/tests/acceptance/cluster-url-test.js b/ui/desktop/tests/acceptance/cluster-url-test.js index dfe9c7747..eede43cb0 100644 --- a/ui/desktop/tests/acceptance/cluster-url-test.js +++ b/ui/desktop/tests/acceptance/cluster-url-test.js @@ -13,7 +13,6 @@ import { invalidateSession } from 'ember-simple-auth/test-support'; import { setupBrowserFakes } from 'ember-browser-services/test-support'; import WindowMockIPC from '../helpers/window-mock-ipc'; import config from '../../config/environment'; -import Store from 'api/services/store'; module('Acceptance | clusterUrl', function (hooks) { setupApplicationTest(hooks); @@ -101,9 +100,6 @@ module('Acceptance | clusterUrl', function (hooks) { urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`; urls.projects = `${urls.scopes.global}/projects`; urls.targets = `${urls.projects}/targets`; - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); hooks.afterEach(function () { diff --git a/ui/desktop/tests/acceptance/projects/sessions/index-test.js b/ui/desktop/tests/acceptance/projects/sessions/index-test.js index 1e6f3f0e5..a071d20a8 100644 --- a/ui/desktop/tests/acceptance/projects/sessions/index-test.js +++ b/ui/desktop/tests/acceptance/projects/sessions/index-test.js @@ -22,7 +22,6 @@ import { STATUS_SESSION_CANCELING, STATUS_SESSION_TERMINATED, } from 'api/models/session'; -import Store from 'api/services/store'; module('Acceptance | projects | sessions', function (hooks) { setupApplicationTest(hooks); @@ -123,9 +122,6 @@ module('Acceptance | projects | sessions', function (hooks) { const ipcService = this.owner.lookup('service:ipc'); stubs.ipcService = sinon.stub(ipcService, 'invoke'); - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); hooks.afterEach(function () { diff --git a/ui/desktop/tests/acceptance/projects/sessions/session-test.js b/ui/desktop/tests/acceptance/projects/sessions/session-test.js index e7f9c0e8d..e69858e0c 100644 --- a/ui/desktop/tests/acceptance/projects/sessions/session-test.js +++ b/ui/desktop/tests/acceptance/projects/sessions/session-test.js @@ -15,7 +15,6 @@ import { authenticateSession } from 'ember-simple-auth/test-support'; import sinon from 'sinon'; import WindowMockIPC from '../../../helpers/window-mock-ipc'; import { STATUS_SESSION_ACTIVE } from 'api/models/session'; -import Store from 'api/services/store'; module('Acceptance | projects | sessions | session', function (hooks) { setupApplicationTest(hooks); @@ -122,9 +121,6 @@ module('Acceptance | projects | sessions | session', function (hooks) { const ipcService = this.owner.lookup('service:ipc'); stubs.ipcService = sinon.stub(ipcService, 'invoke'); - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); hooks.afterEach(function () { diff --git a/ui/desktop/tests/acceptance/projects/targets/index-test.js b/ui/desktop/tests/acceptance/projects/targets/index-test.js index 0c8383abb..62dd7db73 100644 --- a/ui/desktop/tests/acceptance/projects/targets/index-test.js +++ b/ui/desktop/tests/acceptance/projects/targets/index-test.js @@ -21,9 +21,8 @@ import { invalidateSession, currentSession, } from 'ember-simple-auth/test-support'; -import Store from 'api/services/store'; -module('Acceptance | projects | targets', function (hooks) { +module('Acceptance | projects | targets | index', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); @@ -140,9 +139,6 @@ module('Acceptance | projects | targets', function (hooks) { const ipcService = this.owner.lookup('service:ipc'); stubs.ipcService = sinon.stub(ipcService, 'invoke'); - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); hooks.afterEach(function () { diff --git a/ui/desktop/tests/acceptance/projects/targets/target-test.js b/ui/desktop/tests/acceptance/projects/targets/target-test.js index 4592b9e53..e77bf9b66 100644 --- a/ui/desktop/tests/acceptance/projects/targets/target-test.js +++ b/ui/desktop/tests/acceptance/projects/targets/target-test.js @@ -10,7 +10,6 @@ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import sinon from 'sinon'; import { authenticateSession } from 'ember-simple-auth/test-support'; import WindowMockIPC from '../../../helpers/window-mock-ipc'; -import Store from 'api/services/store'; module('Acceptance | projects | targets | target', function (hooks) { setupApplicationTest(hooks); @@ -132,9 +131,6 @@ module('Acceptance | projects | targets | target', function (hooks) { const ipcService = this.owner.lookup('service:ipc'); stubs.ipcService = sinon.stub(ipcService, 'invoke'); - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); hooks.afterEach(function () { diff --git a/ui/desktop/tests/acceptance/scopes-test.js b/ui/desktop/tests/acceptance/scopes-test.js index 099b44085..6cf788a89 100644 --- a/ui/desktop/tests/acceptance/scopes-test.js +++ b/ui/desktop/tests/acceptance/scopes-test.js @@ -16,7 +16,6 @@ import { invalidateSession, } from 'ember-simple-auth/test-support'; import WindowMockIPC from '../helpers/window-mock-ipc'; -import Store from 'api/services/store'; module('Acceptance | scopes', function (hooks) { setupApplicationTest(hooks); @@ -137,9 +136,6 @@ module('Acceptance | scopes', function (hooks) { const ipcService = this.owner.lookup('service:ipc'); stubs.ipcService = sinon.stub(ipcService, 'invoke'); - - // Use the original store so we don't try and hit the client daemon - this.owner.register('service:store', Store); }); hooks.afterEach(function () {