Skip to content

Commit

Permalink
[Angular] Signals to handle threads and metrics (#25070)
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert authored Feb 3, 2024
1 parent ba385ea commit 25cc5e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,52 @@

<h3 <%= jhiPrefix %>Translate="metrics.jvm.title">__jhiTransformTranslate__('metrics.jvm.title')</h3>

@if (metrics && !updatingMetrics()) {
@if (metrics() && !updatingMetrics()) {
<div class="row">
<<%= jhiPrefixDashed %>-jvm-memory class="col-md-4" [updating]="updatingMetrics()" [jvmMemoryMetrics]="metrics.jvm"></<%= jhiPrefixDashed %>-jvm-memory>
<<%= jhiPrefixDashed %>-jvm-memory class="col-md-4" [updating]="updatingMetrics()" [jvmMemoryMetrics]="metrics()?.jvm"></<%= jhiPrefixDashed %>-jvm-memory>

<<%= jhiPrefixDashed %>-jvm-threads class="col-md-4" [threads]="threads"></<%= jhiPrefixDashed %>-jvm-threads>
<<%= jhiPrefixDashed %>-jvm-threads class="col-md-4" [threads]="threads()"></<%= jhiPrefixDashed %>-jvm-threads>

<<%= jhiPrefixDashed %>-metrics-system class="col-md-4" [updating]="updatingMetrics()" [systemMetrics]="metrics.processMetrics"></<%= jhiPrefixDashed %>-metrics-system>
<<%= jhiPrefixDashed %>-metrics-system class="col-md-4" [updating]="updatingMetrics()" [systemMetrics]="metrics()?.processMetrics"></<%= jhiPrefixDashed %>-metrics-system>
</div>
}

@if (metrics && metricsKeyExists('garbageCollector')) {
@if (metrics() && metricsKeyExists('garbageCollector')) {
<<%= jhiPrefixDashed %>-metrics-garbagecollector
[updating]="updatingMetrics()"
[garbageCollectorMetrics]="metrics.garbageCollector"
[garbageCollectorMetrics]="metrics()?.garbageCollector"
></<%= jhiPrefixDashed %>-metrics-garbagecollector>
}

@if (updatingMetrics()) {
<div class="well well-lg" <%= jhiPrefix %>Translate="metrics.updating">__jhiTransformTranslate__('metrics.updating')</div>
}

@if (metrics && metricsKeyExists('http.server.requests')) {
@if (metrics() && metricsKeyExists('http.server.requests')) {
<<%= jhiPrefixDashed %>-metrics-request
[updating]="updatingMetrics()"
[requestMetrics]="metrics['http.server.requests']"
[requestMetrics]="metrics()?.['http.server.requests']"
></<%= jhiPrefixDashed %>-metrics-request>
}

@if (metrics && metricsKeyExists('services')) {
@if (metrics() && metricsKeyExists('services')) {
<<%= jhiPrefixDashed %>-metrics-endpoints-requests
[updating]="updatingMetrics()"
[endpointsRequestsMetrics]="metrics.services"
[endpointsRequestsMetrics]="metrics()?.services"
></<%= jhiPrefixDashed %>-metrics-endpoints-requests>
}

@if (metrics && metricsKeyExists('cache')) {
@if (metrics() && metricsKeyExists('cache')) {
<<%= jhiPrefixDashed %>-metrics-cache
[updating]="updatingMetrics()"
[cacheMetrics]="metrics.cache"
[cacheMetrics]="metrics()?.cache"
></<%= jhiPrefixDashed %>-metrics-cache>
}

@if (metrics && metricsKeyExistsAndObjectNotEmpty('databases')) {
@if (metrics() && metricsKeyExistsAndObjectNotEmpty('databases')) {
<<%= jhiPrefixDashed %>-metrics-datasource
[updating]="updatingMetrics()"
[datasourceMetrics]="metrics.databases"
[datasourceMetrics]="metrics()?.databases"
></<%= jhiPrefixDashed %>-metrics-datasource>
}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('MetricsComponent', () => {

// THEN
expect(service.getMetrics).toHaveBeenCalled();
expect(comp.metrics).toEqual(metrics);
expect(comp.threads).toEqual(threadDump.threads);
expect(comp.metrics()).toEqual(metrics);
expect(comp.threads()).toEqual(threadDump.threads);
expect(comp.updatingMetrics()).toBeFalsy();
expect(changeDetector.constructor.prototype.markForCheck).toHaveBeenCalled();
});
Expand All @@ -84,7 +84,7 @@ describe('MetricsComponent', () => {
describe('metricsKeyExists', () => {
it('should check that metrics key exists', () => {
// GIVEN
comp.metrics = {
comp.metrics.set({
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
Expand All @@ -95,7 +95,7 @@ describe('MetricsComponent', () => {
collectionTime: 0,
},
},
} as unknown as Metrics;
} as unknown as Metrics);

// WHEN
const garbageCollectorKeyExists = comp.metricsKeyExists('garbageCollector');
Expand All @@ -106,7 +106,7 @@ describe('MetricsComponent', () => {

it('should check that metrics key does not exist', () => {
// GIVEN
comp.metrics = {
comp.metrics.set({
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
Expand All @@ -117,7 +117,7 @@ describe('MetricsComponent', () => {
collectionTime: 0,
},
},
} as unknown as Metrics;
} as unknown as Metrics);

// WHEN
const databasesCollectorKeyExists = comp.metricsKeyExists('databases');
Expand All @@ -130,7 +130,7 @@ describe('MetricsComponent', () => {
describe('metricsKeyExistsAndObjectNotEmpty', () => {
it('should check that metrics key exists and is not empty', () => {
// GIVEN
comp.metrics = {
comp.metrics.set({
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
Expand All @@ -141,7 +141,7 @@ describe('MetricsComponent', () => {
collectionTime: 0,
},
},
} as unknown as Metrics;
} as unknown as Metrics);

// WHEN
const garbageCollectorKeyExistsAndNotEmpty = comp.metricsKeyExistsAndObjectNotEmpty('garbageCollector');
Expand All @@ -152,9 +152,9 @@ describe('MetricsComponent', () => {

it('should check that metrics key is empty', () => {
// GIVEN
comp.metrics = {
comp.metrics.set({
garbageCollector: {},
} as Metrics;
} as Metrics);

// WHEN
const garbageCollectorKeyEmpty = comp.metricsKeyExistsAndObjectNotEmpty('garbageCollector');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ import { MetricsSystemComponent } from './blocks/metrics-system/metrics-system.c
],
})
export default class MetricsComponent implements OnInit {
metrics?: Metrics;
threads?: Thread[];
metrics = signal<Metrics | undefined>(undefined);
threads = signal<Thread[] | undefined>(undefined);
updatingMetrics = signal(true);

private metricsService = inject(MetricsService);
Expand All @@ -65,18 +65,18 @@ export default class MetricsComponent implements OnInit {
refresh(): void {
this.updatingMetrics.set(true);
combineLatest([this.metricsService.getMetrics(), this.metricsService.threadDump()]).subscribe(([metrics, threadDump]) => {
this.metrics = metrics;
this.threads = threadDump.threads;
this.metrics.set(metrics);
this.threads.set(threadDump.threads);
this.updatingMetrics.set(false);
this.changeDetector.markForCheck();
});
}

metricsKeyExists(key: keyof Metrics): boolean {
return Boolean(this.metrics?.[key]);
return Boolean(this.metrics()?.[key]);
}

metricsKeyExistsAndObjectNotEmpty(key: keyof Metrics): boolean {
return Boolean(this.metrics?.[key] && JSON.stringify(this.metrics[key]) !== '{}');
return Boolean(this.metrics()?.[key] && JSON.stringify(this.metrics()?.[key]) !== '{}');
}
}

0 comments on commit 25cc5e9

Please sign in to comment.