Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

METRON-1107 add support for handling epoch dates in seconds or milliseconds #692

Closed

Conversation

ottobackwards
Copy link
Contributor

This PR adds the capability for the Stellar Date functions which take Long times to handle both seconds and milliseconds, so they just handle epoch time.

Testing

run stellar shell and exercise the Stellar date functions using epochconverter.com.

screen shot 2017-08-13 at 00 32 06

Use both the epoch seconds and millisecond values

In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:

For all changes:

  • Is there a JIRA ticket associated with this PR? If not one needs to be created at Metron Jira.
  • Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
  • Has your PR been rebased against the latest commit within the target branch (typically master)?

For code changes:

  • Have you included steps to reproduce the behavior or problem that is being changed or addressed?

  • Have you included steps or a guide to how the change may be verified and tested manually?

  • Have you ensured that the full suite of tests and checks have been executed in the root metron folder via:

    mvn -q clean integration-test install && build_utils/verify_licenses.sh 
    
  • Have you written or updated unit tests and or integration tests to verify your changes?

  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?

  • Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via site-book/target/site/index.html:

    cd site-book
    mvn site
    

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
It is also recommended that travis-ci is set up for your personal repository such that your branches are built there before submitting a pull request.

Copy link
Member

@JonZeolla JonZeolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, aside from the 10 digit assumption and some minor typos.

*
*
* </p>
* @param canidate The Long value to concider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/concider/consider/ for the whole file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:%s/x/y/g
will do the whole file :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't assuming vi, I was assuming more of a sed-style formatting, plus you only need the g for your canidate typo because it occurs twice on one line. =)

*/
public static Long ensureEpochMillis(Long canidate) {
int length = (int)Math.floor(Math.log10(canidate) + 1);
return length == 10 ? canidate * 1000 : canidate;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should handle inputs of length 1-9 as well.

* Ensures returns the passed value as milliseconds from EPOCH if the value is in seconds.
* This is done by looking at the number of digits.
* If there are 10, then the value is concidered to be in seconds and will by
* muliplited by 1000.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/muliplited/multiplied/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

* @param canidate The Long value to concider
* @return A Long value
*/
public static Long ensureEpochMillis(Long canidate) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/canidate/candidate/g for this whole file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Assert.assertTrue(meh.longValue() == EpochUtils.ensureEpochMillis(meh).longValue());
}


Copy link
Member

@JonZeolla JonZeolla Aug 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding some tests for < 10 digit epochs.

Object result = run("DAY_OF_YEAR(epoch_seconds)");
assertEquals(238, result);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding tests for an epoch time at or before Sunday, September 9, 2001 1:46:39 AM.

@JonZeolla
Copy link
Member

JonZeolla commented Aug 13, 2017

Also, I did notice a couple of things when poking around that probably fit best into some new JIRAs that I at least wanted to mention here:

  • It's somewhat unintuitive that we count starting at zero for MONTH(), unlike WEEK_OF_MONTH, WEEK_OF_YEAR, DAY_OF_MONTH, and DAY_OF_WEEK.
  • We don't comply with ISO 8601 regarding the first day of the week (We use Sunday, ISO says Monday).
[Stellar]>>> MONTH()
7
[Stellar]>>> MONTH(1502664380)
7
[Stellar]>>> WEEK_OF_MONTH(1502664380)
3
[Stellar]>>> DAY_OF_MONTH(1502664380)
13
[Stellar]>>> WEEK_OF_YEAR(1502664380)
33
[Stellar]>>> DAY_OF_WEEK(1502664380)
1

@ottobackwards
Copy link
Contributor Author

This is due to our time stuff being java Calendar based. We should have a new jira to convert to the new java.time concepts.


Comments from Reviewable

@ottobackwards
Copy link
Contributor Author

metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/utils/EpochUtils.java, line 41 at r1 (raw file):

Previously, JonZeolla (JonZeolla) wrote…

We should handle inputs of length 1-9 as well.

So, the 10/13 rule is pretty standard. If we do this, then we know we are doing seconds->millis for current timestamps.

for < 10, we don't know if it is millis or timestamps, at least that is my thinking. adding a 0 up to 13 for those numbers is equally as bad as just leaving them no?


Comments from Reviewable

@ottobackwards
Copy link
Contributor Author

metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/DateFunctionsTest.java, line 248 at r1 (raw file):

Previously, JonZeolla (JonZeolla) wrote…

Suggest adding tests for an epoch time at or before Sunday, September 9, 2001 1:46:39 AM.

can I ask why?


Comments from Reviewable

@ottobackwards
Copy link
Contributor Author

metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/utils/EpochUtils.java, line 36 at r1 (raw file):

Previously, JonZeolla (JonZeolla) wrote…

I wasn't assuming vi, I was assuming more of a sed-style formatting, plus you only need the g for your canidate typo because it occurs twice on one line. =)

one upper


Comments from Reviewable

@JonZeolla
Copy link
Member

can I ask why?

Because that is 9x 9s in epoch (999999999, assuming seconds).

for < 10, we don't know if it is millis or timestamps, at least that is my thinking. adding a 0 up to 13 for those numbers is equally as bad as just leaving them no?

Well, that's partially true. We know that for 1-3 digits it must be seconds, and for 11-13 digits it must be millis. Perhaps we provide an optional field that specifies millis vs seconds with a default to whatever is more common (seconds) and be done with it? Do we have any core assumptions of using milli elsewhere? Of course this will need an update to a lot of tests to either change to seconds or specify milli.

As far as attempting to appropriately zero-pad it (which I think is what you mentioned but I could be wrong), I'm probably against that because a small mistake could drastically affect the timestamp. To be explicit - 1234567890 (2009) could become 00001234567890 (1970) or 123456789 (1973) could become 0000123456789 (1970). More realistically, this probably isn't a horrible approach either.

@ottobackwards
Copy link
Contributor Author

I'm going to close this. We need more discussion.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants