Skip to content

Commit 925cf72

Browse files
authored
Merge pull request #283 from iteratec/feature/resultSelectionDefaultValues
Feature: Set start values for the result selection in its components
2 parents 928203f + 58e16b3 commit 925cf72

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

frontend/src/app/modules/result-selection/components/time-frame/time-frame.component.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,19 @@ export class TimeFrameComponent implements OnInit {
5757
}
5858

5959
ngOnInit() {
60-
this.selectedDates = [this.resultSelectionStore.resultSelectionCommand.from, this.resultSelectionStore.resultSelectionCommand.to];
61-
this.selectTimeFrame();
60+
if (this.resultSelectionStore.validQuery) {
61+
this.selectedDates = [this.resultSelectionStore.resultSelectionCommand.from, this.resultSelectionStore.resultSelectionCommand.to];
62+
this.selectTimeFrame();
63+
64+
} else {
65+
let defaultFrom = new Date();
66+
let defaultTo = new Date();
67+
defaultFrom.setDate(defaultTo.getDate() - 3);
68+
this.selectedDates = [defaultFrom, defaultTo];
69+
this.timeFrameInSeconds = TIME_FRAME_IN_SECONDS.THREE_DAYS;
70+
71+
this.resultSelectionStore.setResultSelectionCommandTimeFrame(this.selectedDates);
72+
}
6273

6374
if (this.showAggregation) {
6475
this.resultSelectionStore.setRemainingResultSelectionInterval(this.aggregationIntervalInSeconds);
@@ -101,8 +112,8 @@ export class TimeFrameComponent implements OnInit {
101112

102113
comparativeTo.setSeconds(comparativeTo.getSeconds() - 1);
103114

104-
let calculatedTimeFrameInSecondes = this.calculateTimeFrameInSeconds(from, to);
105-
comparativeFrom.setSeconds(comparativeTo.getSeconds() - calculatedTimeFrameInSecondes);
115+
let calculatedTimeFrameInSeconds = this.calculateTimeFrameInSeconds(from, to);
116+
comparativeFrom.setSeconds(comparativeTo.getSeconds() - calculatedTimeFrameInSeconds);
106117

107118
this.selectedComparativeDates = [comparativeFrom, comparativeTo];
108119
}
@@ -112,26 +123,26 @@ export class TimeFrameComponent implements OnInit {
112123
let to = new Date(this.selectedDates[1]);
113124

114125
// Remove seconds and millisecond
115-
from.setSeconds(0,0);
116-
to.setSeconds(0,0);
126+
from.setSeconds(0, 0);
127+
to.setSeconds(0, 0);
117128

118-
let calculatedTimeFrameInSecondes = this.calculateTimeFrameInSeconds(from, to);
119-
if (this.isValidTimeFrameUntilNow(calculatedTimeFrameInSecondes)) {
120-
this.timeFrameInSeconds = calculatedTimeFrameInSecondes;
129+
let calculatedTimeFrameInSeconds = this.calculateTimeFrameInSeconds(from, to);
130+
if (this.isValidTimeFrameUntilNow(calculatedTimeFrameInSeconds)) {
131+
this.timeFrameInSeconds = calculatedTimeFrameInSeconds;
121132
} else {
122133
this.timeFrameInSeconds = TIME_FRAME_IN_SECONDS.MANUAL_SELECTION;
123134
}
124135
}
125136

126137
private calculateTimeFrameInSeconds(from: Date, to: Date): number {
127138
let timeZoneOffsetInSeconds = (to.getTimezoneOffset() - from.getTimezoneOffset()) * 60;
128-
return to.getTime()/1000 - from.getTime()/1000 - timeZoneOffsetInSeconds;
139+
return to.getTime() / 1000 - from.getTime() / 1000 - timeZoneOffsetInSeconds;
129140
}
130141

131-
private isValidTimeFrameUntilNow(calculatedTimeFrameInSecondes: number): boolean {
142+
private isValidTimeFrameUntilNow(calculatedTimeFrameInSeconds: number): boolean {
132143
let to = new Date(this.selectedDates[1]);
133144
to.setHours(23, 59, 0, 0);
134-
return to >= this.max && this.selectableTimeFramesInSeconds.find(value => value == calculatedTimeFrameInSecondes) != undefined
145+
return to >= this.max && this.selectableTimeFramesInSeconds.find(value => value == calculatedTimeFrameInSeconds) != undefined
135146
}
136147

137148
updateFromDate(calendar: CalendarType): void {

frontend/src/app/modules/result-selection/services/result-selection.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ export class ResultSelectionService {
1111
}
1212

1313
fetchResultSelectionData<T>(resultSelectionCommand: ResultSelectionCommand, url: URL): Observable<T> {
14-
const params = this.createParamsFromResultSelectionCommand(resultSelectionCommand);
15-
return this.http.get<T>(url, {params: params}).pipe(
16-
this.handleError()
17-
)
14+
if (resultSelectionCommand.from && resultSelectionCommand.to) {
15+
const params = this.createParamsFromResultSelectionCommand(resultSelectionCommand);
16+
return this.http.get<T>(url, {params: params}).pipe(
17+
this.handleError()
18+
)
19+
}
20+
return new Observable<T>();
1821
}
1922

2023
private createParamsFromResultSelectionCommand(resultSelectionCommand: ResultSelectionCommand) {

frontend/src/app/modules/result-selection/services/result-selection.store.ts

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import {ActivatedRoute, Params, Router} from "@angular/router";
1919

2020
@Injectable()
2121
export class ResultSelectionStore {
22-
from: Date;
23-
to: Date;
2422
_resultSelectionCommand$: BehaviorSubject<ResultSelectionCommand>;
2523
_remainingResultSelection$: BehaviorSubject<RemainingResultSelection>;
2624

@@ -68,10 +66,15 @@ export class ResultSelectionStore {
6866
selectedTimeFrameInterval: 'interval'
6967
};
7068

71-
constructor(private resultSelectionService: ResultSelectionService, route: ActivatedRoute, private router: Router) {
72-
this._resultSelectionCommand$ = new BehaviorSubject<ResultSelectionCommand>({});
69+
constructor(private resultSelectionService: ResultSelectionService, private route: ActivatedRoute, private router: Router) {
70+
this._resultSelectionCommand$ = new BehaviorSubject<ResultSelectionCommand>({caller: Caller.EventResult});
7371
this._remainingResultSelection$ = new BehaviorSubject<RemainingResultSelection>({});
74-
route.queryParams.subscribe((params: Params) => {
72+
73+
this.readQueryParams();
74+
}
75+
76+
readQueryParams(): void {
77+
this.route.queryParams.subscribe((params: Params) => {
7578
if (params) {
7679
params = this.renameParamKeys(this.oldToNewChartKeyMap, params);
7780
this.validQuery = this.checkQuery(params);
@@ -101,22 +104,9 @@ export class ResultSelectionStore {
101104
this._remainingResultSelection$.next(remainingResultSelection);
102105
}
103106
});
104-
105-
if (!this.validQuery) {
106-
let defaultFrom = new Date();
107-
let defaultTo = new Date();
108-
defaultFrom.setDate(defaultTo.getDate() - 3);
109-
110-
const resultSelectionCommand: ResultSelectionCommand = {
111-
from: defaultFrom,
112-
to: defaultTo,
113-
caller: Caller.EventResult
114-
};
115-
this._resultSelectionCommand$.next(resultSelectionCommand);
116-
}
117107
}
118108

119-
writeQueryParams(additionalParams?: Params) {
109+
writeQueryParams(additionalParams?: Params): void {
120110
this.router.navigate([], {
121111
queryParams: {
122112
from: this.resultSelectionCommand.from.toISOString(),
@@ -157,27 +147,6 @@ export class ResultSelectionStore {
157147
});
158148
}
159149

160-
private checkQuery(params: Params): boolean {
161-
const dates: Date[] = [new Date(params.from), new Date(params.to)];
162-
const datesValid: boolean = dates.every(this.isValidDate);
163-
const jobGroupIdsValid: boolean = !!params.jobGroupIds;
164-
165-
return datesValid && jobGroupIdsValid;
166-
}
167-
168-
private isValidDate(date: Date) {
169-
return date instanceof Date && !isNaN(date.getTime())
170-
}
171-
172-
private renameParamKeys = (keysMap, object) =>
173-
Object.keys(object).reduce(
174-
(acc, key) => ({
175-
...acc,
176-
...{[keysMap[key] || key]: object[key]}
177-
}),
178-
{}
179-
);
180-
181150
setResultSelectionCommandTimeFrame(timeFrame: Date[]): void {
182151
this.setResultSelectionCommand({...this.resultSelectionCommand, from: timeFrame[0], to: timeFrame[1]});
183152
}
@@ -216,6 +185,27 @@ export class ResultSelectionStore {
216185
return this._remainingResultSelection$.getValue();
217186
}
218187

188+
private checkQuery(params: Params): boolean {
189+
const dates: Date[] = [new Date(params.from), new Date(params.to)];
190+
const datesValid: boolean = dates.every(this.isValidDate);
191+
const jobGroupIdsValid: boolean = !!params.jobGroupIds;
192+
193+
return datesValid && jobGroupIdsValid;
194+
}
195+
196+
private isValidDate(date: Date) {
197+
return date instanceof Date && !isNaN(date.getTime())
198+
}
199+
200+
private renameParamKeys = (keysMap, object) =>
201+
Object.keys(object).reduce(
202+
(acc, key) => ({
203+
...acc,
204+
...{[keysMap[key] || key]: object[key]}
205+
}),
206+
{}
207+
);
208+
219209
private setRemainingResultSelection(newState: RemainingResultSelection): void {
220210
this.dataAvailable$.next(true);
221211
this._remainingResultSelection$.next(newState);

0 commit comments

Comments
 (0)