From 2b01f5fd83c9c642f21e92c3950e65d40754b582 Mon Sep 17 00:00:00 2001 From: Satyam Date: Wed, 1 Nov 2023 00:22:17 +0530 Subject: [PATCH 1/5] feat: integrate event log api --- app/components/events/logs-page.hbs | 47 +++++---- app/components/events/logs-page.js | 153 +++++++++++++--------------- app/components/live-header.hbs | 30 ++++-- app/components/live-header.js | 3 +- app/constants/live.js | 5 + app/controllers/live.js | 1 - app/models/log.js | 5 + app/templates/live.hbs | 12 ++- app/utils/logsTransformer.js | 14 +++ tests/unit/models/log-test.js | 13 +++ 10 files changed, 167 insertions(+), 116 deletions(-) create mode 100644 app/models/log.js create mode 100644 app/utils/logsTransformer.js create mode 100644 tests/unit/models/log-test.js diff --git a/app/components/events/logs-page.hbs b/app/components/events/logs-page.hbs index 08a0b491..2c0a9456 100644 --- a/app/components/events/logs-page.hbs +++ b/app/components/events/logs-page.hbs @@ -3,24 +3,35 @@ Event logs
- {{!TODO added dummy cards as of now, will integrate api for data }} - {{#each this.EVENT_LOGS_DATA as |data|}} -
- {{data.time}} - {{data.removedPeer}} - was - removed by - {{data.removedBy}} + {{#if this.isLogsLoading}} +
+ Loading...
- {{/each}} + {{else}} + {{#if this.isNoLogsPresent}} +
+ No logs available, for the event! +
+ {{else}} + {{#each this.eventLogsData as |data|}} +
+ [{{data.timestamp}}] + {{data.peerName}} + was + removed by + {{data.removedByUsername}} +
+ {{/each}} + {{/if}} + {{/if}}
\ No newline at end of file diff --git a/app/components/events/logs-page.js b/app/components/events/logs-page.js index 06d564bf..22d5917c 100644 --- a/app/components/events/logs-page.js +++ b/app/components/events/logs-page.js @@ -1,85 +1,76 @@ +import { registerDestructor } from '@ember/destroyable'; +import { inject as service } from '@ember/service'; import Component from '@glimmer/component'; +import { tracked } from '@glimmer/tracking'; +import { removePeerLogsTransformer } from '../../utils/logsTransformer'; +import { + EVENTS_LOGS_POLL_TIME, + EVENTS_LOGS_TYPE, + GET_API_CONFIGS, +} from '../../constants/live'; +import { APPS } from '../../constants/urls'; export default class LogsPageComponent extends Component { - // TODO added dummy data as of now, will integrate api for data - EVENT_LOGS_DATA = [ - { - id: 1, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 2, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 3, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 4, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 5, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 6, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 7, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 8, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 9, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 10, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 11, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 12, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - { - id: 13, - time: '[9:00PM]', - removedPeer: 'xyz', - removedBy: 'Satyam Bajpai', - }, - ]; + @service store; + @service live; + @tracked eventLogsData; + @tracked isLogsLoading; + @tracked isInitialLoading = true; + logsPollingInterval; + LOGS_TYPE_EVENTS_REMOVE_PEER = EVENTS_LOGS_TYPE.EVENTS_REMOVE_PEER; + @tracked isNoLogsPresent; + + constructor(...args) { + super(...args); + + (async () => { + await this.getLogs(); + })(); + + this.logsPollingInterval = setInterval(async () => { + await this.getLogs(); + }, EVENTS_LOGS_POLL_TIME); + + registerDestructor(this, () => { + clearInterval(this.logsPollingInterval); + }); + } + + async getLogs() { + if (this.isInitialLoading) { + this.isLogsLoading = true; + } + + try { + const logsResponse = await fetch( + `${APPS.API_BACKEND}/logs/${this.LOGS_TYPE_EVENTS_REMOVE_PEER}`, + { + ...GET_API_CONFIGS, + credentials: 'include', + } + ); + + const logsFromApi = removePeerLogsTransformer( + (await logsResponse.json())?.logs + ); + + // filtering logs of current active event + this.eventLogsData = logsFromApi.filter((log) => { + return this.live.activeRoomId === log.eventId; + }); + + if (this.eventLogsData.length === 0) { + this.isNoLogsPresent = true; + } else { + this.isNoLogsPresent = false; + } + } catch (err) { + console.error('Something went wrong ', err); + } finally { + if (this.isInitialLoading) { + this.isLogsLoading = false; + this.isInitialLoading = false; + } + } + } } diff --git a/app/components/live-header.hbs b/app/components/live-header.hbs index b218de69..40d8e018 100644 --- a/app/components/live-header.hbs +++ b/app/components/live-header.hbs @@ -13,17 +13,27 @@ {{@activeTab}} -
+
{{#each @tabs as |tab|}} - + {{#if (eq tab.label 'Logs')}} + {{#if this.live.userData.roles.super_user}} + + {{/if}} + {{else}} + + {{/if}} {{/each}}
\ No newline at end of file diff --git a/app/components/live-header.js b/app/components/live-header.js index 095fca01..e359cd19 100644 --- a/app/components/live-header.js +++ b/app/components/live-header.js @@ -1,10 +1,11 @@ import Component from '@glimmer/component'; import { action } from '@ember/object'; import { tracked } from '@glimmer/tracking'; +import { inject as service } from '@ember/service'; export default class LiveHeaderComponent extends Component { + @service live; @tracked isTabOpen = false; - @action toggleTabs() { this.isTabOpen = !this.isTabOpen; } diff --git a/app/constants/live.js b/app/constants/live.js index a11d0474..501cfcc5 100644 --- a/app/constants/live.js +++ b/app/constants/live.js @@ -34,3 +34,8 @@ export const GET_API_CONFIGS = { 'Content-Type': 'application/json', }, }; + +export const EVENTS_LOGS_POLL_TIME = 40000; +export const EVENTS_LOGS_TYPE = { + EVENTS_REMOVE_PEER: 'EVENTS_REMOVE_PEER', +}; diff --git a/app/controllers/live.js b/app/controllers/live.js index eb3bb1e0..55d89c41 100644 --- a/app/controllers/live.js +++ b/app/controllers/live.js @@ -15,7 +15,6 @@ export default class LiveController extends Controller { { id: 1, label: 'Screenshare', active: true }, { id: 2, label: 'Previous Events', active: false }, { id: 3, label: 'Real Dev Squad', active: false }, - // TODO: uncomment this line when logs feature is integrated with API { id: 4, label: 'Logs', active: false }, ]; @tracked activeTab = 'Screenshare'; diff --git a/app/models/log.js b/app/models/log.js new file mode 100644 index 00000000..84c514ed --- /dev/null +++ b/app/models/log.js @@ -0,0 +1,5 @@ +import Model, { attr } from '@ember-data/model'; + +export default class LogModel extends Model { + @attr logs; +} diff --git a/app/templates/live.hbs b/app/templates/live.hbs index 817e9359..38e94fe7 100644 --- a/app/templates/live.hbs +++ b/app/templates/live.hbs @@ -1,11 +1,13 @@ {{page-title 'Live'}}
- + {{#if this.liveService.isJoined}} + + {{/if}} { + const transformedData = logs?.map((log) => { + return { + eventId: log?.body?.event_id, + removedById: log?.meta?.removed_by_id, + removedByUsername: log?.meta?.removed_by_username, + peerId: log?.body?.peer_id, + peerName: log?.body?.peer_name, + reason: log?.body?.reason, + timestamp: new Date(log?.timestamp?._seconds * 1000).toUTCString(), + }; + }); + return transformedData; +}; diff --git a/tests/unit/models/log-test.js b/tests/unit/models/log-test.js new file mode 100644 index 00000000..b108e7bd --- /dev/null +++ b/tests/unit/models/log-test.js @@ -0,0 +1,13 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'website-www/tests/helpers'; + +module('Unit | Model | log', function (hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function (assert) { + let store = this.owner.lookup('service:store'); + let model = store.createRecord('log', {}); + assert.ok(model); + }); +}); From 10d0346aa22e901bee149f9821287c101011c3be Mon Sep 17 00:00:00 2001 From: Satyam Date: Wed, 1 Nov 2023 00:34:33 +0530 Subject: [PATCH 2/5] chore: remove unnecessary files --- app/models/log.js | 5 ----- tests/unit/models/log-test.js | 13 ------------- 2 files changed, 18 deletions(-) delete mode 100644 app/models/log.js delete mode 100644 tests/unit/models/log-test.js diff --git a/app/models/log.js b/app/models/log.js deleted file mode 100644 index 84c514ed..00000000 --- a/app/models/log.js +++ /dev/null @@ -1,5 +0,0 @@ -import Model, { attr } from '@ember-data/model'; - -export default class LogModel extends Model { - @attr logs; -} diff --git a/tests/unit/models/log-test.js b/tests/unit/models/log-test.js deleted file mode 100644 index b108e7bd..00000000 --- a/tests/unit/models/log-test.js +++ /dev/null @@ -1,13 +0,0 @@ -import { module, test } from 'qunit'; -import { setupTest } from 'website-www/tests/helpers'; - -module('Unit | Model | log', function (hooks) { - setupTest(hooks); - - // Replace this with your real tests. - test('it exists', function (assert) { - let store = this.owner.lookup('service:store'); - let model = store.createRecord('log', {}); - assert.ok(model); - }); -}); From e319565dd1f106fb3a18c253fceb02c93af9a488 Mon Sep 17 00:00:00 2001 From: Satyam Date: Wed, 1 Nov 2023 14:50:28 +0530 Subject: [PATCH 3/5] tests: fix failing tests --- app/components/events/logs-page.hbs | 2 +- tests/integration/components/events/logs-page-test.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/components/events/logs-page.hbs b/app/components/events/logs-page.hbs index 2c0a9456..031a6472 100644 --- a/app/components/events/logs-page.hbs +++ b/app/components/events/logs-page.hbs @@ -4,7 +4,7 @@
{{#if this.isLogsLoading}} -
+
Loading...
{{else}} diff --git a/tests/integration/components/events/logs-page-test.js b/tests/integration/components/events/logs-page-test.js index 9d4e5af1..9e757197 100644 --- a/tests/integration/components/events/logs-page-test.js +++ b/tests/integration/components/events/logs-page-test.js @@ -13,9 +13,8 @@ module('Integration | Component | events/logs-page', function (hooks) { assert.dom('[data-test-logs-page-container]').exists(); assert.dom('[data-test-logs-heading]').exists(); assert.dom('[data-test-logs-main]').exists(); - assert.dom('[data-test-log-card]').exists(); - // TODO : will add tests for this when we integrate API to this + // TODO : will add tests for this component // data-test-log-card-time='{{data.id}}' // data-test-log-card-removed-peer='{{data.id}}' // data-test-log-card='{{data.id}}' From 7d205754ec5d5c72dbaf0705dc1ec92100e546a6 Mon Sep 17 00:00:00 2001 From: Satyam Date: Fri, 3 Nov 2023 21:05:28 +0530 Subject: [PATCH 4/5] tests: fix failing tests by some refactor --- app/components/events/logs-page-main.hbs | 39 +++++++++ app/components/events/logs-page.hbs | 43 ++-------- app/styles/app.css | 4 +- ...e.module.css => logs-page-main.module.css} | 0 app/utils/logsTransformer.js | 1 + tests/constants/event-logs-data.js | 42 ++++++++++ .../components/events/logs-page-main-test.js | 82 +++++++++++++++++++ .../components/events/logs-page-test.js | 10 +-- 8 files changed, 173 insertions(+), 48 deletions(-) create mode 100644 app/components/events/logs-page-main.hbs rename app/styles/{logs-page.module.css => logs-page-main.module.css} (100%) create mode 100644 tests/constants/event-logs-data.js create mode 100644 tests/integration/components/events/logs-page-main-test.js diff --git a/app/components/events/logs-page-main.hbs b/app/components/events/logs-page-main.hbs new file mode 100644 index 00000000..392f53f0 --- /dev/null +++ b/app/components/events/logs-page-main.hbs @@ -0,0 +1,39 @@ +
+

+ Event logs +

+
+ {{#if @isLogsLoading}} +
+ Loading... +
+ {{else}} + {{#if @isNoLogsPresent}} +
+ No logs available, for the event! +
+ {{else}} + {{#each @eventLogsData as |log|}} +
+ [{{log.timestamp}}] + {{log.peerName}} + was removed by + {{log.removedByUsername}} +
+ {{/each}} + {{/if}} + {{/if}} +
+
\ No newline at end of file diff --git a/app/components/events/logs-page.hbs b/app/components/events/logs-page.hbs index 031a6472..d25f350f 100644 --- a/app/components/events/logs-page.hbs +++ b/app/components/events/logs-page.hbs @@ -1,37 +1,6 @@ -
-

- Event logs -

-
- {{#if this.isLogsLoading}} -
- Loading... -
- {{else}} - {{#if this.isNoLogsPresent}} -
- No logs available, for the event! -
- {{else}} - {{#each this.eventLogsData as |data|}} -
- [{{data.timestamp}}] - {{data.peerName}} - was - removed by - {{data.removedByUsername}} -
- {{/each}} - {{/if}} - {{/if}} -
-
\ No newline at end of file + \ No newline at end of file diff --git a/app/styles/app.css b/app/styles/app.css index 88e51a38..7a7ce06d 100644 --- a/app/styles/app.css +++ b/app/styles/app.css @@ -27,7 +27,7 @@ @import 'room-code-modal.module.css'; @import 'intro.module.css'; @import 'onboarding-card.module.css'; -@import 'logs-page.module.css'; +@import 'logs-page-main.module.css'; @import 'events.module.css'; * { @@ -61,4 +61,4 @@ button { #toast-container>div { opacity: 1; -} +} \ No newline at end of file diff --git a/app/styles/logs-page.module.css b/app/styles/logs-page-main.module.css similarity index 100% rename from app/styles/logs-page.module.css rename to app/styles/logs-page-main.module.css diff --git a/app/utils/logsTransformer.js b/app/utils/logsTransformer.js index 96ad459f..19bd186c 100644 --- a/app/utils/logsTransformer.js +++ b/app/utils/logsTransformer.js @@ -8,6 +8,7 @@ export const removePeerLogsTransformer = (logs) => { peerName: log?.body?.peer_name, reason: log?.body?.reason, timestamp: new Date(log?.timestamp?._seconds * 1000).toUTCString(), + timestampInSeconds: log?.timestamp?._seconds, }; }); return transformedData; diff --git a/tests/constants/event-logs-data.js b/tests/constants/event-logs-data.js new file mode 100644 index 00000000..593b9db0 --- /dev/null +++ b/tests/constants/event-logs-data.js @@ -0,0 +1,42 @@ +export const EVENTS_LOGS_DATA = [ + { + eventId: 'event-id-1', + removedById: 'removed-by-1', + removedByUsername: 'removed-by-username-1', + peerId: 'peer-id-1', + peerName: 'peer-name-1', + reason: 'reason-1', + timestamp: new Date(1699022859 * 1000).toUTCString(), + timestampInSeconds: 1699022859 + 1000, + }, + { + eventId: 'event-id-2', + removedById: 'removed-by-2', + removedByUsername: 'removed-by-username-2', + peerId: 'peer-id-2', + peerName: 'peer-name-2', + reason: 'reason-2', + timestamp: new Date(1699022859 * 1000 + 2000).toUTCString(), + timestampInSeconds: 1699022859 + 2000, + }, + { + eventId: 'event-id-3', + removedById: 'removed-by-3', + removedByUsername: 'removed-by-username-3', + peerId: 'peer-id-3', + peerName: 'peer-name-3', + reason: 'reason-3', + timestamp: new Date(1699022859 * 1000 + 3000).toUTCString(), + timestampInSeconds: 1699022859 + 3000, + }, + { + eventId: 'event-id-4', + removedById: 'removed-by-4', + removedByUsername: 'removed-by-username-4', + peerId: 'peer-id-4', + peerName: 'peer-name-4', + reason: 'reason-4', + timestamp: new Date(1699022859 * 1000 + 4000).toUTCString(), + timestampInSeconds: 1699022859 + 4000, + }, +]; diff --git a/tests/integration/components/events/logs-page-main-test.js b/tests/integration/components/events/logs-page-main-test.js new file mode 100644 index 00000000..29ab134c --- /dev/null +++ b/tests/integration/components/events/logs-page-main-test.js @@ -0,0 +1,82 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'website-www/tests/helpers'; +import { render } from '@ember/test-helpers'; +import { hbs } from 'ember-cli-htmlbars'; +import { EVENTS_LOGS_DATA } from '../../../constants/event-logs-data'; + +module('Integration | Component | events/logs-page-main', function (hooks) { + setupRenderingTest(hooks); + + test('it should render Events::LogsPageMain', async function (assert) { + await render(hbs` + + `); + + assert.dom('[data-test-logs-page-container]').exists(); + assert.dom('[data-test-logs-heading]').exists(); + assert.dom('[data-test-logs-main]').exists(); + }); + + test('it should show loader if isLoading is true otherwise false', async function (assert) { + await render(hbs` + + `); + + assert.dom('[data-test-logs-loader]').exists(); + + await render(hbs` + + `); + + assert.dom('[data-test-logs-loader]').doesNotExist(); + }); + + test('it should show fallback if isNoLogsPresent is true otherwise false', async function (assert) { + await render(hbs` + + `); + + assert.dom('[data-test-logs-fallback]').exists(); + + await render(hbs` + + `); + + assert.dom('[data-test-logs-fallback]').doesNotExist(); + }); + + test('it should show logs if data is present', async function (assert) { + this.set('logsData', EVENTS_LOGS_DATA); + await render(hbs` + + `); + + EVENTS_LOGS_DATA.forEach((log) => { + assert.dom(`[data-test-log-card="${log.timestampInSeconds}"]`).exists(); + assert + .dom(`[data-test-log-card-time="${log.timestampInSeconds}"]`) + .exists(); + assert + .dom(`[data-test-log-card-text="${log.timestampInSeconds}"]`) + .exists(); + assert + .dom(`[data-test-log-card-removed-peer="${log.timestampInSeconds}"]`) + .exists(); + assert + .dom(`[data-test-log-removed-by="${log.timestampInSeconds}"]`) + .exists(); + }); + }); +}); diff --git a/tests/integration/components/events/logs-page-test.js b/tests/integration/components/events/logs-page-test.js index 9e757197..f2362e71 100644 --- a/tests/integration/components/events/logs-page-test.js +++ b/tests/integration/components/events/logs-page-test.js @@ -10,14 +10,6 @@ module('Integration | Component | events/logs-page', function (hooks) { await render(hbs` `); - assert.dom('[data-test-logs-page-container]').exists(); - assert.dom('[data-test-logs-heading]').exists(); - assert.dom('[data-test-logs-main]').exists(); - - // TODO : will add tests for this component - // data-test-log-card-time='{{data.id}}' - // data-test-log-card-removed-peer='{{data.id}}' - // data-test-log-card='{{data.id}}' - // data-test-log-removed-by='{{data.id}}' + assert.ok(true); }); }); From d7b1595016d796f5ac01773acf6bd7b07c15a559 Mon Sep 17 00:00:00 2001 From: Satyam Date: Fri, 3 Nov 2023 21:12:37 +0530 Subject: [PATCH 5/5] chore: remove unnecessary props --- app/components/events/logs-page.hbs | 1 - 1 file changed, 1 deletion(-) diff --git a/app/components/events/logs-page.hbs b/app/components/events/logs-page.hbs index d25f350f..7d13a24d 100644 --- a/app/components/events/logs-page.hbs +++ b/app/components/events/logs-page.hbs @@ -1,6 +1,5 @@ \ No newline at end of file