5
5
6
6
import Route from '@ember/routing/route' ;
7
7
import { service } from '@ember/service' ;
8
- import timestamp from 'core/utils/timestamp' ;
9
- import { getUnixTime } from 'date-fns' ;
8
+ import { fromUnixTime } from 'date-fns' ;
10
9
11
10
import type FlagsService from 'vault/services/flags' ;
12
11
import type StoreService from 'vault/services/store' ;
13
12
import type VersionService from 'vault/services/version' ;
14
13
import type { ModelFrom } from 'vault/vault/route' ;
15
14
import type ClientsRoute from '../clients' ;
16
15
import type ClientsCountsController from 'vault/controllers/vault/cluster/clients/counts' ;
17
- import { setStartTimeQuery } from 'core/utils/client-count-utils' ;
18
16
19
17
export interface ClientsCountsRouteParams {
20
18
start_time ?: string | number | undefined ;
@@ -39,38 +37,68 @@ export default class ClientsCountsRoute extends Route {
39
37
return this . flags . fetchActivatedFlags ( ) ;
40
38
}
41
39
42
- async getActivity ( start_time : number | null , end_time : number ) {
40
+ /**
41
+ * This method returns the query param timestamp if it exists. If not, it returns the activity timestamp value instead.
42
+ */
43
+ paramOrResponseTimestamp (
44
+ qpMillisString : string | number | undefined ,
45
+ activityTimeStamp : string | undefined
46
+ ) {
47
+ let timestamp : string | undefined ;
48
+ const millis = Number ( qpMillisString ) ;
49
+ if ( ! isNaN ( millis ) ) {
50
+ timestamp = fromUnixTime ( millis ) . toISOString ( ) ;
51
+ }
52
+ // fallback to activity timestamp only if there was no query param
53
+ if ( ! timestamp && activityTimeStamp ) {
54
+ timestamp = activityTimeStamp ;
55
+ }
56
+ return timestamp ;
57
+ }
58
+
59
+ async getActivity ( params : ClientsCountsRouteParams ) {
43
60
let activity , activityError ;
44
- // if there is no start_time we want the user to manually choose a date
45
- // in that case bypass the query so that the user isn't stuck viewing the activity error
46
- if ( start_time ) {
61
+ // if CE without start time we want to skip the activity call
62
+ // so that the user is forced to choose a date range
63
+ if ( this . version . isEnterprise || params . start_time ) {
64
+ const query = {
65
+ // start and end params are optional -- if not provided, will fallback to API default
66
+ start_time : this . formatTimeQuery ( params ?. start_time ) ,
67
+ end_time : this . formatTimeQuery ( params ?. end_time ) ,
68
+ } ;
47
69
try {
48
- activity = await this . store . queryRecord ( 'clients/activity' , {
49
- start_time : { timestamp : start_time } ,
50
- end_time : { timestamp : end_time } ,
51
- } ) ;
70
+ activity = await this . store . queryRecord ( 'clients/activity' , query ) ;
52
71
} catch ( error ) {
53
72
activityError = error ;
54
73
}
55
74
}
56
- return { activity, activityError } ;
75
+ return {
76
+ activity,
77
+ activityError,
78
+ } ;
79
+ }
80
+
81
+ // Takes the string URL param and formats it as the adapter expects it,
82
+ // if it exists and is valid
83
+ formatTimeQuery ( param : string | number | undefined ) {
84
+ let timeParam : { timestamp : number } | undefined ;
85
+ const millis = Number ( param ) ;
86
+ if ( ! isNaN ( millis ) ) {
87
+ timeParam = { timestamp : millis } ;
88
+ }
89
+ return timeParam ;
57
90
}
58
91
59
92
async model ( params : ClientsCountsRouteParams ) {
60
93
const { config, versionHistory } = this . modelFor ( 'vault.cluster.clients' ) as ModelFrom < ClientsRoute > ;
61
- // only enterprise versions will have a relevant billing start date, if null users must select initial start time
62
- const startTime = setStartTimeQuery ( this . version . isEnterprise , config ) ;
63
-
64
- const startTimestamp = Number ( params . start_time ) || startTime ;
65
- const endTimestamp = Number ( params . end_time ) || getUnixTime ( timestamp . now ( ) ) ;
66
- const { activity, activityError } = await this . getActivity ( startTimestamp , endTimestamp ) ;
67
-
94
+ const { activity, activityError } = await this . getActivity ( params ) ;
68
95
return {
69
96
activity,
70
97
activityError,
71
98
config,
72
- endTimestamp,
73
- startTimestamp,
99
+ // activity.startTime corresponds to first month with data, but we want first month returned or requested
100
+ startTimestamp : this . paramOrResponseTimestamp ( params ?. start_time , activity ?. byMonth [ 0 ] ?. timestamp ) ,
101
+ endTimestamp : this . paramOrResponseTimestamp ( params ?. end_time , activity ?. endTime ) ,
74
102
versionHistory,
75
103
} ;
76
104
}
0 commit comments