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,9 +1,9 @@
import angular from 'angular';
import _ from 'lodash';
import moment from 'moment';
import { ElasticResponse } from './elastic_response';
import { IndexPattern } from './index_pattern';
import { ElasticQueryBuilder } from './query_builder';
import { toUtc } from '@grafana/ui/src/utils/moment_wrapper';
export class ElasticDatasource {
basicAuth: string;
@@ -176,7 +176,7 @@ export class ElasticDatasource {
const event = {
annotation: annotation,
time: moment.utc(time).valueOf(),
time: toUtc(time).valueOf(),
text: getFieldFromSource(source, textField),
tags: getFieldFromSource(source, tagsField),
};

View File

@@ -1,4 +1,4 @@
import moment from 'moment';
import { toUtc, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
const intervalMap = {
Hourly: { startOf: 'hour', amount: 'hours' },
@@ -13,7 +13,7 @@ export class IndexPattern {
getIndexForToday() {
if (this.interval) {
return moment.utc().format(this.pattern);
return toUtc().format(this.pattern);
} else {
return this.pattern;
}
@@ -25,10 +25,10 @@ export class IndexPattern {
}
const intervalInfo = intervalMap[this.interval];
const start = moment(from)
const start = dateTime(from)
.utc()
.startOf(intervalInfo.startOf);
const endEpoch = moment(to)
const endEpoch = dateTime(to)
.utc()
.startOf(intervalInfo.startOf)
.valueOf();

View File

@@ -1,8 +1,8 @@
import angular from 'angular';
import * as dateMath from '@grafana/ui/src/utils/datemath';
import _ from 'lodash';
import moment from 'moment';
import { ElasticDatasource } from '../datasource';
import { toUtc, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
describe('ElasticDatasource', function(this: any) {
const backendSrv = {
@@ -66,7 +66,7 @@ describe('ElasticDatasource', function(this: any) {
ctx.ds.testDatasource();
const today = moment.utc().format('YYYY.MM.DD');
const today = toUtc().format('YYYY.MM.DD');
expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping');
});
});
@@ -105,8 +105,8 @@ describe('ElasticDatasource', function(this: any) {
query = {
range: {
from: moment.utc([2015, 4, 30, 10]),
to: moment.utc([2015, 5, 1, 10]),
from: toUtc([2015, 4, 30, 10]),
to: toUtc([2015, 5, 1, 10]),
},
targets: [
{
@@ -159,8 +159,8 @@ describe('ElasticDatasource', function(this: any) {
ctx.ds.query({
range: {
from: moment([2015, 4, 30, 10]),
to: moment([2015, 5, 1, 10]),
from: dateTime([2015, 4, 30, 10]),
to: dateTime([2015, 5, 1, 10]),
},
targets: [
{
@@ -441,8 +441,8 @@ describe('ElasticDatasource', function(this: any) {
ctx.ds.query({
range: {
from: moment([2015, 4, 30, 10]),
to: moment([2015, 5, 1, 10]),
from: dateTime([2015, 4, 30, 10]),
to: dateTime([2015, 5, 1, 10]),
},
targets: [
{

View File

@@ -1,13 +1,13 @@
///<amd-dependency path="test/specs/helpers" name="helpers" />
import moment from 'moment';
import { IndexPattern } from '../index_pattern';
import { toUtc } from '@grafana/ui/src/utils/moment_wrapper';
describe('IndexPattern', () => {
describe('when getting index for today', () => {
test('should return correct index name', () => {
const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
const expected = 'asd-' + moment.utc().format('YYYY.MM.DD');
const expected = 'asd-' + toUtc().format('YYYY.MM.DD');
expect(pattern.getIndexForToday()).toBe(expected);
});