-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample-dash.component.ts
More file actions
331 lines (297 loc) · 8.49 KB
/
example-dash.component.ts
File metadata and controls
331 lines (297 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// eslint-disable:no-invalid-template-strings max-inline-declarations
import { AsyncPipe, JsonPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component, Inject, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {
Dashboard,
DataSource,
dataSourceMarker,
EditorApi,
JsonPrimitive,
Model,
ModelApi,
ModelProperty,
ModelPropertyEditor,
NUMBER_PROPERTY,
Renderer,
STRING_PROPERTY
} from '@hypertrace/hyperdash';
import {
DashboardComponent,
DashboardModelDirective,
EDITOR_API,
ModelChangedEventService,
ModelEditorComponent,
ModelInject,
MODEL_API,
RendererApi,
RENDERER_API,
ThemePropertyPipe
} from '@hypertrace/hyperdash-angular';
import { remove } from 'lodash-es';
import { EMPTY, interval, Observable, of } from 'rxjs';
// eslint-disable-next-line:no-submodule-imports
import { catchError, map, take } from 'rxjs/operators';
@Component({
selector: 'app-example-dash',
templateUrl: './example-dash.component.html',
styleUrls: ['./example-dash.component.scss'],
imports: [FormsModule, JsonPipe, DashboardComponent, ModelEditorComponent]
})
export class ExampleDashComponent implements OnInit {
public json: { [key: string]: JsonPrimitive } = {
type: 'example-container',
children: [
{
type: 'example-model',
title: 'Variable data source example',
data: {
type: 'example-data-source',
text: '${foo}',
exclamations: '${bar}',
rate: '${baz}'
},
theme: {
type: 'theme',
'background-color': 'whitesmoke',
'text-color': 'navy'
}
},
{
type: 'example-model',
title: 'Second child',
theme: {
type: 'theme',
'background-color': 'ivory'
}
},
{
type: 'example-model',
title: 'Third child',
data: {
type: 'graphql-data-source',
query: '{ spans { ${spanFields} } }'
}
}
],
data: {
type: 'example-data-source',
text: 'default data source'
},
theme: {
type: 'theme',
'background-color': 'aliceblue',
'text-color': 'dimgray'
}
};
public jsonAsString!: string;
public variablePairs: VariablePair[] = [
{ name: 'foo', value: 'variable text' },
{ name: 'bar', value: '3' },
{ name: 'baz', value: '1000' },
{ name: 'spanFields', value: 'id' }
];
public dashboard?: Dashboard;
public serializedDashboard?: object;
public selectedWidget?: object;
public constructor(private readonly modelChangedEvent: ModelChangedEventService) {}
public ngOnInit(): void {
// eslint-disable-next-line:no-null-keyword
this.jsonAsString = JSON.stringify(this.json, null, 4);
}
public updateJson(): void {
this.json = JSON.parse(this.jsonAsString);
// eslint-disable-next-line:no-null-keyword
this.jsonAsString = JSON.stringify(this.json, null, 4);
}
public setDashboard(dashboard: Dashboard): void {
this.dashboard = dashboard;
this.selectedWidget = dashboard.root;
this.setVariables();
this.modelChangedEvent.getObservableForModel(this.dashboard.root).subscribe(() => {
this.serializedDashboard = dashboard.serialize();
});
this.serializedDashboard = dashboard.serialize();
}
public setVariables(): void {
if (this.dashboard) {
this.variablePairs.forEach(pair => this.updatePair(pair));
}
}
public addVariable(): void {
this.variablePairs.push({});
}
public removePair(pairToRemove: VariablePair): void {
remove(this.variablePairs, pair => pair === pairToRemove);
}
public updatePair(pair: VariablePair): void {
if (typeof pair.name !== 'string' || pair.name.length === 0) {
return;
}
try {
this.dashboard!.setVariable(pair.name, JSON.parse(pair.value!));
} catch {
this.dashboard!.setVariable(pair.name, pair.value);
}
// Replace to rerender
this.variablePairs.splice(this.variablePairs.indexOf(pair), 1, { ...pair });
}
public onWidgetSelectionChange(obj: object): void {
this.selectedWidget = obj;
}
public onClick(): void {
// Nothing to do
}
}
@Model({
type: 'example-container'
})
export class ExampleContainer {
@ModelProperty({
key: 'children',
type: 'array'
})
public readonly children: object[] = [];
@ModelInject(MODEL_API)
public api!: ModelApi;
}
@Model({
type: 'example-model'
})
export class ExampleModel {
@ModelProperty({
key: 'title',
type: STRING_PROPERTY.type,
displayName: 'Title'
})
public readonly title!: string;
@ModelInject(MODEL_API)
public api!: ModelApi;
public getData(): Observable<string> {
return this.api.getData<unknown>().pipe(
map(value => {
if (typeof value === 'string') {
return value;
}
// eslint-disable-next-line:no-null-keyword
return JSON.stringify(value, null, 2);
}),
// eslint-disable-next-line:no-null-keyword
catchError(err => of(JSON.stringify(err, null, 2)))
);
}
}
@Model({
type: 'example-data-source'
})
export class ExampleDataSource implements DataSource<string> {
public readonly dataSourceMarker: typeof dataSourceMarker = dataSourceMarker;
@ModelProperty({
key: 'text',
type: STRING_PROPERTY.type
})
public readonly text?: string;
@ModelProperty({
key: 'exclamations',
type: NUMBER_PROPERTY.type,
required: false
})
public readonly exclamations: number = 1;
@ModelProperty({
key: 'rate',
type: NUMBER_PROPERTY.type,
required: false
})
public readonly rateMs: number = 1000;
public getData(): Observable<string> {
if (this.text !== undefined) {
return interval(this.rateMs).pipe(
take(this.exclamations + 1),
map(sequenceNumber => `${this.text}${'!'.repeat(sequenceNumber)}`)
);
}
return EMPTY;
}
}
@Renderer({ modelClass: ExampleModel })
@Component({
selector: 'app-example-renderer',
template: `
<div
style="border: 1px solid black; padding: 4px"
[style.backgroundColor]="'background-color' | themeProp"
[style.color]="'text-color' | themeProp"
>
<h4 [style.border]="'title-border' | themeProp">{{ api.model.title }}</h4>
<div [style.border]="'content-border' | themeProp">
<pre style="overflow-x: auto">{{ dataObservable | async }}</pre>
</div>
</div>
`,
imports: [AsyncPipe, ThemePropertyPipe]
})
export class ExampleRendererComponent implements OnInit {
public dataObservable!: Observable<string>;
public constructor(@Inject(RENDERER_API) public readonly api: RendererApi<ExampleModel>) {}
public ngOnInit(): void {
this.fetchData();
this.api.change$.subscribe(() => this.fetchData());
}
private fetchData(): void {
this.dataObservable = this.api.model.getData();
}
}
@Renderer({ modelClass: ExampleContainer })
@Component({
selector: 'app-example-container',
template: `
<div
style="border: 1px solid black; padding: 4px"
[style.backgroundColor]="'background-color' | themeProp"
[style.color]="'text-color' | themeProp"
>
@for (child of api.model.children; track $index) {
<ng-container [hdaDashboardModel]="child"> </ng-container>
}
</div>
`,
imports: [DashboardModelDirective, ThemePropertyPipe]
})
export class ExampleContainerRendererComponent {
public constructor(@Inject(RENDERER_API) public readonly api: RendererApi<ExampleContainer>) {}
}
@ModelPropertyEditor({
propertyType: STRING_PROPERTY.type
})
@Component({
selector: 'app-string-property-editor',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<h5>{{ label }}</h5>
<input type="text" [(ngModel)]="currentValue" (keyup.enter)="propagateChange()" (focusout)="propagateChange()" />
`,
imports: [FormsModule]
})
export class StringPropertyEditorComponent {
public currentValue?: string;
public readonly label: string;
private lastPropagatedValue?: string;
public constructor(@Inject(EDITOR_API) private readonly api: EditorApi<string | undefined>) {
this.currentValue = api.value;
this.lastPropagatedValue = this.currentValue;
this.label = api.label;
}
public propagateChange(): void {
if (this.currentValue === '') {
this.currentValue = undefined; // Treat empty string as unset
}
if (this.lastPropagatedValue !== this.currentValue) {
this.api.valueChange(this.currentValue);
this.lastPropagatedValue = this.currentValue;
}
}
}
interface VariablePair {
name?: string;
value?: string;
}