newrelic / nr1-community

An open-source library of useful components for building on New Relic One's programmability platform.
https://developer.newrelic.com
Apache License 2.0
11 stars 12 forks source link

feat(durationToAbsoluteRange): Utility function for converting duration to begin_time/end_time #32

Open devfreddy opened 4 years ago

devfreddy commented 4 years ago

Summary

A given duration does not indicate exact time only an interval. To say "last 30 min" means nothing without knowing when you said "last 30 min" from.

Desired Behaviour

Users will want an ability to "freeze time" by specifying something akin to "last x time from when I made this selection". This utility function will allow us to convert that to an absolute timeRange.

Possible Solution

export const durationToAbsoluteRange = (timeRange) => {
  if (!timeRange) {
    return durationToAbsoluteRange({
      duration: 30 * 60 * 1000
    });
  } else if (timeRange.begin_time && timeRange.end_time) {
    return timeRange;
  } else if (timeRange.duration) {
    const now = Math.floor(new Date().getTime() / 1000);
    return {
      begin_time: now - (timeRange.duration / 1000),
      end_time: now
    };
  }
};

Additional context

Unable to find usage of this in any of the current public apps but it seems useful depending on how the TimePicker evolves and is presented in NR1.