-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
avoid day long gaps in sample data #20897
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
715ddce
avoid day long gaps in sample data
nreese 3fb7ab2
avoid using toISOString to avoid an timezone problems
nreese 4a1de8d
unskip sample test now that problem is fixed
nreese d8b531a
use much better cj algorithm for translating time
nreese 117585b
Merge branch 'master' of https://github.com/elastic/kibana into issue…
nreese bca4726
cjcenizal review updates
nreese 04d721f
update funtion name in install.js
nreese ac02a3c
push source reference date back a week
nreese edf5060
Merge branch 'master' of https://github.com/elastic/kibana into issue…
nreese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
73 changes: 0 additions & 73 deletions
73
src/server/sample_data/routes/lib/adjust_timestamp.test.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
const MILLISECONDS_IN_DAY = 86400000; | ||
|
||
function iso8601ToDateIgnoringTime(iso8601) { | ||
const split = iso8601.split('-'); | ||
if (split.length < 3) { | ||
throw new Error('Unexpected timestamp format, expecting YYYY-MM-DDTHH:mm:ss'); | ||
} | ||
const year = parseInt(split[0]); | ||
const month = parseInt(split[1]) - 1; // javascript months are zero-based indexed | ||
const date = parseInt(split[2]); | ||
return new Date(year, month, date); | ||
} | ||
|
||
export function dateToIso8601IgnoringTime(date) { | ||
// not using "Date.toISOString" because only using Date methods that deal with local time | ||
const year = date.getFullYear(); | ||
const month = date.getMonth() + 1; | ||
const monthString = month < 10 ? `0${month}` : `${month}`; | ||
const dateString = date.getDate() < 10 ? `0${date.getDate()}` : `${date.getDate()}`; | ||
return `${year}-${monthString}-${dateString}`; | ||
} | ||
|
||
// Translate source timestamp by targetReference timestamp, | ||
// perserving the distance between source and sourceReference | ||
export function translateTimeRelativeToDifference(source, sourceReference, targetReference) { | ||
const sourceDate = iso8601ToDateIgnoringTime(source); | ||
const sourceReferenceDate = iso8601ToDateIgnoringTime(sourceReference); | ||
const targetReferenceDate = iso8601ToDateIgnoringTime(targetReference); | ||
|
||
const timeDelta = sourceDate.getTime() - sourceReferenceDate.getTime(); | ||
const translatedDate = (new Date(targetReferenceDate.getTime() + timeDelta)); | ||
|
||
return `${dateToIso8601IgnoringTime(translatedDate)}T${source.substring(11)}`; | ||
} | ||
|
||
// Translate source timestamp by targetReference timestamp, | ||
// perserving the week distance between source and sourceReference and day of week of the source timestamp | ||
export function translateTimeRelativeToWeek(source, sourceReference, targetReference) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also added some comments here and changed some names to help me reason about this. Might be useful to others too. export function translateTimeRelativeToWeek(source, sourceReference, targetReference) {
const sourceReferenceDate = iso8601ToDateIgnoringTime(sourceReference);
const targetReferenceDate = iso8601ToDateIgnoringTime(targetReference);
// If these dates were in the same week, how many days apart would they be?
const dayOfWeekDelta = sourceReferenceDate.getDay() - targetReferenceDate.getDay();
// If we pretend that the targetReference is actually the same day of the week as the
// sourceReference, then we can translate the source to the target while preserving their
// days of the week.
const normalizationDelta = dayOfWeekDelta * MILLISECONDS_IN_DAY;
const normalizedTargetReference =
dateToISO8601IgnoringTime(new Date(targetReferenceDate.getTime() + normalizationDelta));
return translateTimeRelativeToDifference(
source,
sourceReference,
normalizedTargetReference);
} |
||
const sourceReferenceDate = iso8601ToDateIgnoringTime(sourceReference); | ||
const targetReferenceDate = iso8601ToDateIgnoringTime(targetReference); | ||
|
||
// If these dates were in the same week, how many days apart would they be? | ||
const dayOfWeekDelta = sourceReferenceDate.getDay() - targetReferenceDate.getDay(); | ||
|
||
// If we pretend that the targetReference is actually the same day of the week as the | ||
// sourceReference, then we can translate the source to the target while preserving their | ||
// days of the week. | ||
const normalizationDelta = dayOfWeekDelta * MILLISECONDS_IN_DAY; | ||
const normalizedTargetReference = | ||
dateToIso8601IgnoringTime(new Date(targetReferenceDate.getTime() + normalizationDelta)); | ||
|
||
return translateTimeRelativeToDifference( | ||
source, | ||
sourceReference, | ||
normalizedTargetReference); | ||
} |
107 changes: 107 additions & 0 deletions
107
src/server/sample_data/routes/lib/translate_timestamp.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
|
||
import { translateTimeRelativeToWeek } from './translate_timestamp'; | ||
|
||
describe('translateTimeRelativeToWeek', () => { | ||
const sourceReference = '2018-01-02T00:00:00'; //Tuesday | ||
const targetReference = '2018-04-25T18:24:58.650'; // Wednesday | ||
|
||
describe('2 weeks before', () => { | ||
test('should properly adjust timestamp when day is before targetReference day of week', () => { | ||
const source = '2017-12-18T23:50:00'; // Monday, -2 week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-09T23:50:00'); // Monday 2 week before targetReference week | ||
}); | ||
|
||
test('should properly adjust timestamp when day is same as targetReference day of week', () => { | ||
const source = '2017-12-20T23:50:00'; // Wednesday, -2 week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-11T23:50:00'); // Wednesday 2 week before targetReference week | ||
}); | ||
|
||
test('should properly adjust timestamp when day is after targetReference day of week', () => { | ||
const source = '2017-12-22T16:16:50'; // Friday, -2 week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-13T16:16:50'); // Friday 2 week before targetReference week | ||
}); | ||
}); | ||
|
||
describe('week before', () => { | ||
test('should properly adjust timestamp when day is before targetReference day of week', () => { | ||
const source = '2017-12-25T23:50:00'; // Monday, -1 week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-16T23:50:00'); // Monday 1 week before targetReference week | ||
}); | ||
|
||
test('should properly adjust timestamp when day is same as targetReference day of week', () => { | ||
const source = '2017-12-27T23:50:00'; // Wednesday, -1 week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-18T23:50:00'); // Wednesday 1 week before targetReference week | ||
}); | ||
|
||
test('should properly adjust timestamp when day is after targetReference day of week', () => { | ||
const source = '2017-12-29T16:16:50'; // Friday, -1 week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-20T16:16:50'); // Friday 1 week before targetReference week | ||
}); | ||
}); | ||
|
||
describe('same week', () => { | ||
test('should properly adjust timestamp when day is before targetReference day of week', () => { | ||
const source = '2018-01-01T23:50:00'; // Monday, same week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-23T23:50:00'); // Monday same week as targetReference | ||
}); | ||
|
||
test('should properly adjust timestamp when day is same as targetReference day of week', () => { | ||
const source = '2018-01-03T23:50:00'; // Wednesday, same week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-25T23:50:00'); // Wednesday same week as targetReference | ||
}); | ||
|
||
test('should properly adjust timestamp when day is after targetReference day of week', () => { | ||
const source = '2018-01-05T16:16:50'; // Friday, same week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-27T16:16:50'); // Friday same week as targetReference | ||
}); | ||
}); | ||
|
||
describe('week after', () => { | ||
test('should properly adjust timestamp when day is before targetReference day of week', () => { | ||
const source = '2018-01-08T23:50:00'; // Monday, 1 week after relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-04-30T23:50:00'); // Monday 1 week after targetReference week | ||
}); | ||
|
||
test('should properly adjust timestamp when day is same as targetReference day of week', () => { | ||
const source = '2018-01-10T23:50:00'; // Wednesday, same week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-05-02T23:50:00'); // Wednesday 1 week after targetReference week | ||
}); | ||
|
||
test('should properly adjust timestamp when day is after targetReference day of week', () => { | ||
const source = '2018-01-12T16:16:50'; // Friday, same week relative to sourceReference | ||
const timestamp = translateTimeRelativeToWeek(source, sourceReference, targetReference); | ||
expect(timestamp).toBe('2018-05-04T16:16:50'); // Friday 1 week after targetReference week | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed a variable here to help me reason about this code in the correct terms... it's a small change but maybe it will help others too?