From e3b8c28bc61fd6ddc0d1468b4ebee161f5d71994 Mon Sep 17 00:00:00 2001 From: Vladyslav_Prykhodko Date: Tue, 11 Aug 2020 13:43:37 +0300 Subject: [PATCH 1/2] Add sorting object key --- .../app/core/services/script/node-script-test.service.ts | 3 +++ ui-ngx/src/app/core/utils.ts | 7 +++++++ .../modules/home/components/event/event-table-config.ts | 9 ++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ui-ngx/src/app/core/services/script/node-script-test.service.ts b/ui-ngx/src/app/core/services/script/node-script-test.service.ts index d6272d58b7..411f5d2216 100644 --- a/ui-ngx/src/app/core/services/script/node-script-test.service.ts +++ b/ui-ngx/src/app/core/services/script/node-script-test.service.ts @@ -23,6 +23,7 @@ import { NodeScriptTestDialogComponent, NodeScriptTestDialogData } from '@shared/components/dialog/node-script-test-dialog.component'; +import { sortObjectKeys } from '@core/utils'; @Injectable({ providedIn: 'root' @@ -75,6 +76,8 @@ export class NodeScriptTestService { deviceName: 'Test Device', ts: new Date().getTime() + '' }; + } else { + metadata = sortObjectKeys(metadata) as {[key: string]: string}; } if (!msgType) { msgType = 'POST_TELEMETRY_REQUEST'; diff --git a/ui-ngx/src/app/core/utils.ts b/ui-ngx/src/app/core/utils.ts index f8ccb6ae61..fff70435bf 100644 --- a/ui-ngx/src/app/core/utils.ts +++ b/ui-ngx/src/app/core/utils.ts @@ -510,3 +510,10 @@ export function padValue(val: any, dec: number): string { strVal = (n ? '-' : '') + strVal; return strVal; } + +export function sortObjectKeys(obj: object): object{ + return Object.keys(obj).sort().reduce((acc,key)=>{ + acc[key]=obj[key]; + return acc; + },{}); +} diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index 0d99ed3eb7..f5ce96c2e4 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -38,6 +38,7 @@ import { EventContentDialogComponent, EventContentDialogData } from '@home/components/event/event-content-dialog.component'; +import { sortObjectKeys } from '@core/utils'; export class EventTableConfig extends EntityTableConfig { @@ -233,11 +234,17 @@ export class EventTableConfig extends EntityTableConfig { if ($event) { $event.stopPropagation(); } + let sortedContent: string; + try { + sortedContent = JSON.stringify(sortObjectKeys(JSON.parse(content))); + } catch() { + sortedContent = content; + } this.dialog.open(EventContentDialogComponent, { disableClose: true, panelClass: ['tb-dialog', 'tb-fullscreen-dialog'], data: { - content, + constent: sortedContent, title, contentType } From ce649822e2d677a16678605e919f57cc93321ef7 Mon Sep 17 00:00:00 2001 From: Vladyslav_Prykhodko Date: Tue, 11 Aug 2020 17:43:19 +0300 Subject: [PATCH 2/2] Add sort metadata key --- .../services/script/node-script-test.service.ts | 4 ++-- ui-ngx/src/app/core/utils.ts | 8 ++++---- .../home/components/event/event-table-config.ts | 15 +++++++-------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/ui-ngx/src/app/core/services/script/node-script-test.service.ts b/ui-ngx/src/app/core/services/script/node-script-test.service.ts index 411f5d2216..600c290edc 100644 --- a/ui-ngx/src/app/core/services/script/node-script-test.service.ts +++ b/ui-ngx/src/app/core/services/script/node-script-test.service.ts @@ -72,12 +72,12 @@ export class NodeScriptTestService { } if (!metadata) { metadata = { - deviceType: 'default', deviceName: 'Test Device', + deviceType: 'default', ts: new Date().getTime() + '' }; } else { - metadata = sortObjectKeys(metadata) as {[key: string]: string}; + metadata = sortObjectKeys(metadata); } if (!msgType) { msgType = 'POST_TELEMETRY_REQUEST'; diff --git a/ui-ngx/src/app/core/utils.ts b/ui-ngx/src/app/core/utils.ts index fff70435bf..bf18c2dcae 100644 --- a/ui-ngx/src/app/core/utils.ts +++ b/ui-ngx/src/app/core/utils.ts @@ -511,9 +511,9 @@ export function padValue(val: any, dec: number): string { return strVal; } -export function sortObjectKeys(obj: object): object{ - return Object.keys(obj).sort().reduce((acc,key)=>{ - acc[key]=obj[key]; +export function sortObjectKeys(obj: T): T { + return Object.keys(obj).sort().reduce((acc, key) => { + acc[key] = obj[key]; return acc; - },{}); + }, {} as T); } diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index f5ce96c2e4..a0bed03aad 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -210,7 +210,7 @@ export class EventTableConfig extends EntityTableConfig { icon: 'more_horiz', isEnabled: (entity) => entity.body.metadata ? entity.body.metadata.length > 0 : false, onAction: ($event, entity) => this.showContent($event, entity.body.metadata, - 'event.metadata', ContentType.JSON) + 'event.metadata', ContentType.JSON, true) }, '40px'), new EntityActionTableColumn('error', 'event.error', @@ -230,21 +230,20 @@ export class EventTableConfig extends EntityTableConfig { } } - showContent($event: MouseEvent, content: string, title: string, contentType: ContentType = null): void { + showContent($event: MouseEvent, content: string, title: string, contentType: ContentType = null, sortKeys = false): void { if ($event) { $event.stopPropagation(); } - let sortedContent: string; - try { - sortedContent = JSON.stringify(sortObjectKeys(JSON.parse(content))); - } catch() { - sortedContent = content; + if (contentType === ContentType.JSON && sortKeys) { + try { + content = JSON.stringify(sortObjectKeys(JSON.parse(content))); + } catch (e) {} } this.dialog.open(EventContentDialogComponent, { disableClose: true, panelClass: ['tb-dialog', 'tb-fullscreen-dialog'], data: { - constent: sortedContent, + content, title, contentType }