This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
/
ktb-evaluation-info.component.ts
153 lines (139 loc) · 5.5 KB
/
ktb-evaluation-info.component.ts
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
import { ChangeDetectorRef, Component, Input, NgZone, OnDestroy, TemplateRef, ViewChild } from '@angular/core';
import { DtOverlay, DtOverlayConfig, DtOverlayRef } from '@dynatrace/barista-components/overlay';
import { Trace } from '../../_models/trace';
import { ResultTypes } from '../../../../shared/models/result-types';
import { EvaluationResult } from '../../../../shared/interfaces/evaluation-result';
import { DataService } from '../../_services/data.service';
import { Subject, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DateUtil } from '../../_utils/date.utils';
export interface EventData {
project: string;
stage: string;
service: string;
}
interface EvaluationInfo {
trace: Trace | undefined;
showHistory: boolean;
data: EventData;
}
@Component({
selector: 'ktb-evaluation-info',
templateUrl: './ktb-evaluation-info.component.html',
styleUrls: ['./ktb-evaluation-info.component.scss'],
})
export class KtbEvaluationInfoComponent implements OnDestroy {
private readonly unsubscribe$ = new Subject<void>();
private _evaluationResult?: EvaluationResult;
private eventData?: EventData;
private _evaluationHistory?: Trace[];
public readonly evaluationHistoryCount = 5;
public TraceClass = Trace;
public isError = false;
public isWarning = false;
public isSuccess = false;
public showHistory = false;
public overlayConfig: DtOverlayConfig = {
pinnable: true,
};
public evaluationsLoaded = false;
private overlayRef?: DtOverlayRef<unknown>;
private updateOverlayPositionSubscription = Subscription.EMPTY;
@ViewChild('overlay', { static: true, read: TemplateRef }) overlayTemplate?: TemplateRef<unknown>;
@Input() public overlayDisabled?: boolean;
@Input() public fill?: boolean;
@Input() public evaluation?: Trace;
@Input()
get evaluationResult(): EvaluationResult | undefined {
return this._evaluationResult;
}
set evaluationResult(result: EvaluationResult | undefined) {
this._evaluationResult = result;
if (this.evaluationResult) {
this.isError = this.evaluationResult.result === ResultTypes.FAILED;
this.isWarning = this.evaluationResult.result === ResultTypes.WARNING;
this.isSuccess = this.evaluationResult.result === ResultTypes.PASSED;
}
}
@Input()
public set evaluationInfo(evaluation: EvaluationInfo | undefined) {
const idBefore = this.evaluation?.id;
this.evaluation = evaluation?.trace;
this.evaluationsLoaded = !!evaluation?.trace?.data.evaluationHistory?.length;
this.showHistory = evaluation?.showHistory ?? false;
this.eventData = evaluation?.data;
if (idBefore !== evaluation?.trace?.id || (!evaluation?.trace && evaluation?.data)) {
this.fetchEvaluationHistory();
}
}
get evaluationHistory(): Trace[] {
return (
this._evaluationHistory ||
this.evaluation?.data?.evaluationHistory
?.filter((evaluation) => evaluation.id !== this.evaluation?.id)
.slice(0, this.evaluationHistoryCount) ||
[]
);
}
constructor(
private dataService: DataService,
private ngZone: NgZone,
private _dtOverlay: DtOverlay,
private changeDetectorRef_: ChangeDetectorRef
) {}
private fetchEvaluationHistory(): void {
const evaluation = this.evaluation;
let _eventData = this.eventData;
if (this.evaluation && this.evaluation.data.project && this.evaluation.data.stage && this.evaluation.data.service) {
_eventData = {
project: this.evaluation.data.project,
service: this.evaluation.data.service,
stage: this.evaluation.data.stage,
};
}
if (this.showHistory && _eventData) {
// currently the event endpoint does not support skipping entries
// the other endpoint we have does not support excluding invalidated evaluations
// we can't use fromTime here if we have a limit. 10 new evaluations and limit to 5 would not pull the new ones
this.dataService
.getEvaluationResults(_eventData, this.evaluationHistoryCount + (this.evaluation ? 1 : 0), false)
.subscribe((traces: Trace[]) => {
traces.sort((a, b) => DateUtil.compareTraceTimesDesc(a, b));
this.evaluationsLoaded = true;
// we don't have an evaluation trace if the sequence is currently running or it just doesn't have an evaluation task
if (evaluation) {
this._evaluationHistory = undefined;
evaluation.data.evaluationHistory = traces;
} else {
this._evaluationHistory = traces;
}
this.changeDetectorRef_.markForCheck();
});
}
}
public showEvaluationOverlay(event: MouseEvent, data?: unknown): void {
if (!this.overlayRef && !this.overlayDisabled && this.overlayTemplate) {
this.overlayRef = this._dtOverlay.create(event, this.overlayTemplate, { ...this.overlayConfig, data });
this.updateEvaluationOverlayPosition();
}
}
public updateEvaluationOverlayPosition(): void {
this.updateOverlayPositionSubscription = this.ngZone.onMicrotaskEmpty
.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => {
this.overlayRef?.updatePosition();
// if the content of the overlay changed after initialization the position stayed the same
});
}
public hideEvaluationOverlay(): void {
if (this.overlayRef) {
this._dtOverlay.dismiss();
this.updateOverlayPositionSubscription.unsubscribe();
this.overlayRef = undefined;
}
}
public ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
}