2020-10-05 21:34:52 +00:00
|
|
|
/**
|
|
|
|
* @module PricingMetricsDates
|
|
|
|
* PricingMetricsDates components are used on the Pricing Metrics page to handle queries related to pricing metrics.
|
2021-02-08 20:13:00 +00:00
|
|
|
* This component assumes that query parameters (as in, from route params) are being passed in with the format MM-yyyy,
|
|
|
|
* while the inputs expect a format of MM/yyyy.
|
2020-10-05 21:34:52 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <PricingMetricsDates @resultStart="2020-03-01T00:00:00Z" @resultEnd="2020-08-31T23:59:59Z" @queryStart="03-2020" @queryEnd="08-2020" />
|
|
|
|
* ```
|
|
|
|
* @param {object} resultStart - resultStart is the start date of the metrics returned. Should be a valid date string that the built-in Date() fn can parse
|
|
|
|
* @param {object} resultEnd - resultEnd is the end date of the metrics returned. Should be a valid date string that the built-in Date() fn can parse
|
2021-02-08 20:13:00 +00:00
|
|
|
* @param {string} [queryStart] - queryStart is the route param (formatted MM-yyyy) that the result will be measured against for showing discrepancy warning
|
|
|
|
* @param {string} [queryEnd] - queryEnd is the route param (formatted MM-yyyy) that the result will be measured against for showing discrepancy warning
|
2020-10-05 21:34:52 +00:00
|
|
|
* @param {number} [defaultSpan=12] - setting for default time between start and end input dates
|
|
|
|
* @param {number} [retentionMonths=24] - setting for the retention months, which informs valid dates to query by
|
|
|
|
*/
|
2021-04-26 16:23:57 +00:00
|
|
|
import { set, computed } from '@ember/object';
|
2020-10-21 16:35:36 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-10-05 21:34:52 +00:00
|
|
|
import Component from '@ember/component';
|
2021-09-16 22:28:03 +00:00
|
|
|
import { subMonths, startOfToday, format, endOfMonth, startOfMonth, isBefore } from 'date-fns';
|
2020-10-05 21:34:52 +00:00
|
|
|
import layout from '../templates/components/pricing-metrics-dates';
|
|
|
|
import { parseDateString } from 'vault/helpers/parse-date-string';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
2020-10-21 16:35:36 +00:00
|
|
|
router: service(),
|
|
|
|
|
2020-10-05 21:34:52 +00:00
|
|
|
queryStart: null,
|
|
|
|
queryEnd: null,
|
|
|
|
resultStart: null,
|
|
|
|
resultEnd: null,
|
|
|
|
|
|
|
|
start: null,
|
|
|
|
end: null,
|
|
|
|
|
|
|
|
defaultSpan: 12,
|
|
|
|
retentionMonths: 24,
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
startDate: computed('start', function () {
|
2020-10-05 21:34:52 +00:00
|
|
|
if (!this.start) return null;
|
|
|
|
let date;
|
|
|
|
try {
|
|
|
|
date = parseDateString(this.start, '/');
|
|
|
|
if (date) return date;
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}),
|
2021-12-17 03:44:29 +00:00
|
|
|
endDate: computed('end', function () {
|
2020-10-05 21:34:52 +00:00
|
|
|
if (!this.end) return null;
|
|
|
|
let date;
|
|
|
|
try {
|
|
|
|
date = parseDateString(this.end, '/');
|
|
|
|
if (date) return endOfMonth(date);
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
error: computed('end', 'endDate', 'retentionMonths', 'start', 'startDate', function () {
|
2020-10-05 21:34:52 +00:00
|
|
|
if (!this.startDate) {
|
2021-02-08 20:13:00 +00:00
|
|
|
return 'Start date is invalid. Please use format MM/yyyy';
|
2020-10-05 21:34:52 +00:00
|
|
|
}
|
|
|
|
if (!this.endDate) {
|
2021-02-08 20:13:00 +00:00
|
|
|
return 'End date is invalid. Please use format MM/yyyy';
|
2020-10-05 21:34:52 +00:00
|
|
|
}
|
2020-10-09 15:14:55 +00:00
|
|
|
if (isBefore(this.endDate, this.startDate)) {
|
2020-10-05 21:34:52 +00:00
|
|
|
return 'Start date is after end date';
|
|
|
|
}
|
2020-10-09 15:14:55 +00:00
|
|
|
const lastMonthAvailable = endOfMonth(subMonths(startOfToday(), 1));
|
|
|
|
if (isBefore(lastMonthAvailable, this.endDate)) {
|
|
|
|
return `Data is not available until the end of the month`;
|
|
|
|
}
|
|
|
|
const earliestRetained = startOfMonth(subMonths(lastMonthAvailable, this.retentionMonths));
|
|
|
|
if (isBefore(this.startDate, earliestRetained)) {
|
2021-02-08 20:13:00 +00:00
|
|
|
return `No data retained before ${format(earliestRetained, 'MM/yyyy')} due to your settings`;
|
2020-10-09 15:14:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:34:52 +00:00
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let initialEnd;
|
|
|
|
let initialStart;
|
|
|
|
|
|
|
|
initialEnd = subMonths(startOfToday(), 1);
|
|
|
|
if (this.queryEnd) {
|
|
|
|
initialEnd = parseDateString(this.queryEnd, '-');
|
|
|
|
} else {
|
|
|
|
// if query isn't passed in, set it so that showResultsWarning works
|
2021-02-08 20:13:00 +00:00
|
|
|
this.queryEnd = format(initialEnd, 'MM-yyyy');
|
2020-10-05 21:34:52 +00:00
|
|
|
}
|
|
|
|
initialStart = subMonths(initialEnd, this.defaultSpan);
|
|
|
|
if (this.queryStart) {
|
|
|
|
initialStart = parseDateString(this.queryStart, '-');
|
|
|
|
} else {
|
|
|
|
// if query isn't passed in, set it so that showResultsWarning works
|
2021-02-08 20:13:00 +00:00
|
|
|
this.queryStart = format(initialStart, 'MM-yyyy');
|
2020-10-05 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:23:57 +00:00
|
|
|
set(this, 'start', format(initialStart, 'MM/yyyy'));
|
|
|
|
set(this, 'end', format(initialEnd, 'MM/yyyy'));
|
2020-10-05 21:34:52 +00:00
|
|
|
},
|
2020-10-21 16:35:36 +00:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
handleQuery() {
|
2021-02-08 20:13:00 +00:00
|
|
|
const start = format(this.startDate, 'MM-yyyy');
|
|
|
|
const end = format(this.endDate, 'MM-yyyy');
|
2021-09-16 22:28:03 +00:00
|
|
|
this.router.transitionTo('vault.cluster.clients', {
|
2020-10-21 16:35:36 +00:00
|
|
|
queryParams: {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2020-10-05 21:34:52 +00:00
|
|
|
});
|