Skip to content

Commit baaae23

Browse files
author
DanielSteger
committed
[IT-2812] Set time frame default value in time frame component
1 parent 0669ed2 commit baaae23

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
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/models/result-selection-command.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface ResultSelectionCommandDTO {
2-
from: Date;
3-
to: Date;
2+
from?: Date;
3+
to?: Date;
44
caller?: Caller;
55
jobGroupIds?: number[];
66
pageIds?: number[];
@@ -11,8 +11,8 @@ export interface ResultSelectionCommandDTO {
1111
}
1212

1313
export class ResultSelectionCommand implements ResultSelectionCommandDTO {
14-
from: Date;
15-
to: Date;
14+
from?: Date;
15+
to?: Date;
1616
caller?: Caller;
1717
jobGroupIds?: number[];
1818
pageIds?: number[];

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

Lines changed: 32 additions & 25 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,8 +66,17 @@ export class ResultSelectionStore {
6866
selectedTimeFrameInterval: 'interval'
6967
};
7068

71-
constructor(private resultSelectionService: ResultSelectionService, route: ActivatedRoute, private router: Router) {
72-
route.queryParams.subscribe((params: Params) => {
69+
constructor(private resultSelectionService: ResultSelectionService, private route: ActivatedRoute, private router: Router) {
70+
this.getQueryParams();
71+
72+
if (!this.validQuery) {
73+
this._resultSelectionCommand$ = new BehaviorSubject({caller: Caller.EventResult});
74+
this._remainingResultSelection$ = new BehaviorSubject({});
75+
}
76+
}
77+
78+
getQueryParams(): void {
79+
this.route.queryParams.subscribe((params: Params) => {
7380
if (params) {
7481
params = this.renameParamKeys(this.oldToNewChartKeyMap, params);
7582
this.validQuery = this.checkQuery(params);
@@ -155,27 +162,6 @@ export class ResultSelectionStore {
155162
});
156163
}
157164

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

203+
private checkQuery(params: Params): boolean {
204+
const dates: Date[] = [new Date(params.from), new Date(params.to)];
205+
const datesValid: boolean = dates.every(this.isValidDate);
206+
const jobGroupIdsValid: boolean = !!params.jobGroupIds;
207+
208+
return datesValid && jobGroupIdsValid;
209+
}
210+
211+
private isValidDate(date: Date) {
212+
return date instanceof Date && !isNaN(date.getTime())
213+
}
214+
215+
private renameParamKeys = (keysMap, object) =>
216+
Object.keys(object).reduce(
217+
(acc, key) => ({
218+
...acc,
219+
...{[keysMap[key] || key]: object[key]}
220+
}),
221+
{}
222+
);
223+
217224
private setRemainingResultSelection(newState: RemainingResultSelection): void {
218225
this.dataAvailable$.next(true);
219226
this._remainingResultSelection$.next(newState);

0 commit comments

Comments
 (0)