Chore: Replaces moment with Grafanas DateTime (#16919)

* Wip: Initial commit

* Refactor: Replaces moment.utc(

* Refactor: replaces the last isMoment statements

* Refactor: Removes almost all moment imports

* Refactor: Moves moment_wrapper to grafana/ui

* Refactor: Renames momentWrapper

* Refactor: Removes one more moment import

* Refactor: Removes unitOfTime import

* Fix: Fixes Prettier error

* Refactor: Renames DateTimeType to DateTime

* Refactor: Renames isDateTimeType to isDateTime

* Refactor: Renames dateTime to dateTime

* Feature: Bans moment imports and types
This commit is contained in:
Hugo Häggmark
2019-05-08 13:51:44 +02:00
committed by GitHub
parent e7a9afe983
commit ceb9f0855b
86 changed files with 583 additions and 484 deletions

View File

@@ -1,3 +1,4 @@
/* tslint:disable:import-blacklist */
import angular from 'angular';
import moment from 'moment';
import _ from 'lodash';

View File

@@ -1,6 +1,6 @@
import moment from 'moment';
import { TimeSrv } from './TimeSrv';
import { ContextSrvStub } from 'test/specs/helpers';
import { isDateTime, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
describe('timeSrv', () => {
const rootScope = {
@@ -43,8 +43,8 @@ describe('timeSrv', () => {
it('should return parsed when parse is true', () => {
timeSrv.setTime({ from: 'now', to: 'now-1h' });
const time = timeSrv.timeRange();
expect(moment.isMoment(time.from)).toBe(true);
expect(moment.isMoment(time.to)).toBe(true);
expect(isDateTime(time.from)).toBe(true);
expect(isDateTime(time.to)).toBe(true);
});
});
@@ -164,8 +164,8 @@ describe('timeSrv', () => {
it('should restore refresh after relative time range is set', () => {
_dashboard.refresh = '10s';
timeSrv.setTime({
from: moment([2011, 1, 1]),
to: moment([2015, 1, 1]),
from: dateTime([2011, 1, 1]),
to: dateTime([2015, 1, 1]),
});
expect(_dashboard.refresh).toBe(false);
timeSrv.setTime({ from: '2011-01-01', to: 'now' });

View File

@@ -1,5 +1,4 @@
// Libraries
import moment from 'moment';
import _ from 'lodash';
// Utils
@@ -12,6 +11,7 @@ import { TimeRange, RawTimeRange } from '@grafana/ui';
import { ITimeoutService, ILocationService } from 'angular';
import { ContextSrv } from 'app/core/services/context_srv';
import { DashboardModel } from '../state/DashboardModel';
import { toUtc, dateTime, isDateTime } from '@grafana/ui/src/utils/moment_wrapper';
export class TimeSrv {
time: any;
@@ -65,10 +65,10 @@ export class TimeSrv {
private parseTime() {
// when absolute time is saved in json it is turned to a string
if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
this.time.from = moment(this.time.from).utc();
this.time.from = dateTime(this.time.from).utc();
}
if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
this.time.to = moment(this.time.to).utc();
this.time.to = dateTime(this.time.to).utc();
}
}
@@ -77,15 +77,15 @@ export class TimeSrv {
return value;
}
if (value.length === 8) {
return moment.utc(value, 'YYYYMMDD');
return toUtc(value, 'YYYYMMDD');
}
if (value.length === 15) {
return moment.utc(value, 'YYYYMMDDTHHmmss');
return toUtc(value, 'YYYYMMDDTHHmmss');
}
if (!isNaN(value)) {
const epoch = parseInt(value, 10);
return moment.utc(epoch);
return toUtc(epoch);
}
return null;
@@ -184,7 +184,7 @@ export class TimeSrv {
_.extend(this.time, time);
// disable refresh if zoom in or zoom out
if (moment.isMoment(time.to)) {
if (isDateTime(time.to)) {
this.oldRefresh = this.dashboard.refresh || this.oldRefresh;
this.setAutoRefresh(false);
} else if (this.oldRefresh && this.oldRefresh !== this.dashboard.refresh) {
@@ -207,10 +207,10 @@ export class TimeSrv {
timeRangeForUrl() {
const range = this.timeRange().raw;
if (moment.isMoment(range.from)) {
if (isDateTime(range.from)) {
range.from = range.from.valueOf().toString();
}
if (moment.isMoment(range.to)) {
if (isDateTime(range.to)) {
range.to = range.to.valueOf().toString();
}
@@ -220,8 +220,8 @@ export class TimeSrv {
timeRange(): TimeRange {
// make copies if they are moment (do not want to return out internal moment, because they are mutable!)
const raw = {
from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from,
to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to,
from: isDateTime(this.time.from) ? dateTime(this.time.from) : this.time.from,
to: isDateTime(this.time.to) ? dateTime(this.time.to) : this.time.to,
};
const timezone = this.dashboard && this.dashboard.getTimezone();
@@ -242,7 +242,7 @@ export class TimeSrv {
const to = center + (timespan * factor) / 2;
const from = center - (timespan * factor) / 2;
this.setTime({ from: moment.utc(from), to: moment.utc(to) });
this.setTime({ from: toUtc(from), to: toUtc(to) });
}
}