Skip to content

Commit

Permalink
[UXE-2304] refactor: isolate beholder date proto (#935)
Browse files Browse the repository at this point in the history
* refactor: export minute/hour constants

* refactor: move date prototype to metrics module

* refactor: simplify convert-date file in helpers

* test: add tests to convert-date in helper

* test: adjust tests in metrics module helper

* refactor: remove unused import methods in filters metrics/events

* chore: import date proto in main file

* refactor: move personal tokens date methosd to convert-date in helpers

* refactor: inject convertDateToLocalTimezone in personal tokens

* refactor: improve date proto file

* refactor: inject convertDateToLocalTimezone in personal tokens

* chore: import date proto in metricsView file

* refactor: remove unused import

* test: create make-domains-url test

* test: create search-domains test

* test: update make-domains-url test

* test: create edge-dns test

* test: create make edge-functions test

* test: create edge-dns test

* test: create edge-functions test

* feat: move module import to App.vue
  • Loading branch information
william-tome authored Mar 26, 2024
1 parent 7d920a8 commit d17e05d
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 181 deletions.
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { useAccountStore } from '@/stores/account'
import { storeToRefs } from 'pinia'
import { themeSelect } from '@/helpers'
import '@modules/real-time-metrics/helpers/convert-date'
import '@/helpers/store-handler'
/** @type {import('@/plugins/analytics/AnalyticsTrackerAdapter').AnalyticsTrackerAdapter} */
const tracker = inject('tracker')
Expand Down
99 changes: 8 additions & 91 deletions src/helpers/convert-date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const HOURS_TO_MSEC = 3_600_000
const MINUTE_IN_MILLISECONDS = 60_000
const HOUR_IN_MILLISECONDS = 3_600_000

/**
* Converts a given value to a date string in a specific format.
Expand All @@ -19,22 +20,6 @@ const convertValueToDate = (value) => {
return date.toLocaleString('en-US', options)
}

/**
* Remove the specified amount of hours from the given date
*
* @param {number} pOffset - The number of hours to remove
* @param {Date} pDate - The date from which to remove the hours
* @returns {Date} The new date after removing the specified hours
*/
function removeSelectedAmountOfHours(pOffset, pDate) {
const offset = Number(pOffset)
const offsetTimestamp = offset * HOURS_TO_MSEC
const calculatedTimestamp = pDate.getTime() - offsetTimestamp
const calculatedDate = new Date(calculatedTimestamp)

return calculatedDate
}

/**
* Formats a given date to ISO format without milliseconds.
*
Expand Down Expand Up @@ -68,88 +53,20 @@ const convertOffsetToDecimal = (offset) => {
return `${offsetSign}${hours}.${decimalMinutes}`
}

export {
removeSelectedAmountOfHours,
formatToEndOfDayIsoDate,
convertOffsetToDecimal,
convertValueToDate
}

/**
* Set the current date to UTC0 and remove Timezone tag
*
* @returns {string} Date without timezone
*/
Date.prototype.removeZone = function () {
const dateWithUserTimezone = this.toISOString()
const dateWithoutZone = dateWithUserTimezone.replace(/(\..+)/, '')
return dateWithoutZone
}

/**
* Convert current date to the UTC informed
*
* @param {number} userUTC - The UTC offset to convert the date to
* @returns {Date} The new date converted to the specified UTC
*/
Date.prototype.toUTC = function (userUTC = 0) {
const tz = Number(userUTC)
const tzTimeStamp = (tz / 100) * HOURS_TO_MSEC
const dateWithUserTimezone = new Date(this.getTime() + tzTimeStamp)
const dateWithoutZone = dateWithUserTimezone.toBeholderFormat()
return new Date(dateWithoutZone)
}

/**
* Reset the current date according to the specified UTC offset
*
* @param {number} userUTC - The UTC offset to reset the date to
* @returns {Date} The new date reset to the specified UTC
*/
Date.prototype.resetUTC = function (userUTC = 0) {
const regexpChangeUTC = /(.+)([+|-]\d+)(.+)/g
const injectUserUTC = this.toString().replace(regexpChangeUTC, `$1${userUTC}$3`)

return new Date(injectUserUTC)
}

/**
* Format date as the Beholder standard
*
* @returns {string} Date in Beholder format
*/
Date.prototype.toBeholderFormat = function () {
return this.toISOString().replace(/(\..+)/, '')
}

/**
* Convert the date from the local format to the Beholder format
*
* @returns {string} Date in Beholder format
*/
Date.prototype.fromLocaletoBeholderFormat = function () {
const toLocale = this.toLocaleString('en-GB')
const parts = toLocale.split(/\/|, /)
const day = parts[0]
const month = parts[1]
const year = parts[2]
const time = parts[3]

return `${year}-${month}-${day}T${time}`
}

/**
* Converts the instance of Date to the local timezone based on the provided UTC offset.
*
* @param {string} utcOffset - The UTC offset for the desired timezone.
* @returns {string} The date converted to the local timezone in ISO format.
*/
Date.prototype.convertDateToLocalTimezone = function (utcOffset) {
const convertDateToLocalTimezone = (date, utcOffset) => {
const userOffset = convertOffsetToDecimal(utcOffset)
const timeZoneOffsetMinutesToMilli = this.getTimezoneOffset() * 60000
const toUTC = this.getTime() + timeZoneOffsetMinutesToMilli
const offsetHoursToMilli = userOffset * 3600000
const timeZoneOffsetMinutesToMilli = date.getTimezoneOffset() * MINUTE_IN_MILLISECONDS
const toUTC = date.getTime() + timeZoneOffsetMinutesToMilli
const offsetHoursToMilli = userOffset * HOUR_IN_MILLISECONDS

const userRealDate = new Date(toUTC + offsetHoursToMilli)
return formatToEndOfDayIsoDate(userRealDate)
}

export { convertValueToDate, convertDateToLocalTimezone }
5 changes: 4 additions & 1 deletion src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import InviteSession from './invite-session'
import { metricsPlaygroundOpener } from './metrics-playground-opener'
import { parseCamelToSnake, parseSnakeToCamel } from './parse-api-body'
import { themeSelect } from './theme-select'
import { convertValueToDate, convertDateToLocalTimezone } from './convert-date'

export {
InviteSession,
Expand All @@ -37,5 +38,7 @@ export {
openSearchResult,
parseCamelToSnake,
parseSnakeToCamel,
themeSelect
themeSelect,
convertValueToDate,
convertDateToLocalTimezone
}
3 changes: 2 additions & 1 deletion src/modules/real-time-metrics/constants/time-intervals.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const RESAMPLING_INTERVALS = {

const TIME_INTERVALS = {
DATE_TIME_FILTER_INTERVALS,
RESAMPLING_INTERVALS
RESAMPLING_INTERVALS,
HOUR_IN_MILLISECONDS
}

export default TIME_INTERVALS
76 changes: 76 additions & 0 deletions src/modules/real-time-metrics/helpers/convert-date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { TIME_INTERVALS } from '@modules/real-time-metrics/constants'

/**
* Remove the specified amount of hours from the given date
*
* @param {number} pOffset - The number of hours to remove
* @returns {Date} The new date after removing the specified hours
*/
Date.prototype.removeSelectedAmountOfHours = function (pOffset) {
const offset = Number(pOffset)
const offsetTimestamp = offset * TIME_INTERVALS.HOUR_IN_MILLISECONDS
const calculatedTimestamp = this.getTime() - offsetTimestamp
const calculatedDate = new Date(calculatedTimestamp)

return calculatedDate
}

/**
* Set the current date to UTC0 and remove Timezone tag
*
* @returns {string} Date without timezone
*/
Date.prototype.removeZone = function () {
const dateWithUserTimezone = this.toISOString()
const dateWithoutZone = dateWithUserTimezone.replace(/(\..+)/, '')
return dateWithoutZone
}

/**
* Convert current date to the UTC informed
*
* @param {number} userUTC - The UTC offset to convert the date to
* @returns {Date} The new date converted to the specified UTC
*/
Date.prototype.toUTC = function (userUTC = 0) {
const tz = Number(userUTC)
const tzTimeStamp = (tz / 100) * TIME_INTERVALS.HOUR_IN_MILLISECONDS
const dateWithUserTimezone = new Date(this.getTime() + tzTimeStamp)
const dateWithoutZone = dateWithUserTimezone.toBeholderFormat()
return new Date(dateWithoutZone)
}

/**
* Reset the current date according to the specified UTC offset
*
* @param {number} userUTC - The UTC offset to reset the date to
* @returns {Date} The new date reset to the specified UTC
*/
Date.prototype.resetUTC = function (userUTC = 0) {
const regexpChangeUTC = /(.+)([+|-]\d+)(.+)/g
const injectUserUTC = this.toString().replace(regexpChangeUTC, `$1${userUTC}$3`)

return new Date(injectUserUTC)
}

/**
* Format date as the Beholder standard
*
* @returns {string} Date in Beholder format
*/
Date.prototype.toBeholderFormat = function () {
return this.toISOString().replace(/(\..+)/, '')
}

/**
* Convert the date from the local format to the Beholder format
*
* @returns {string} Date in Beholder format
*/
Date.prototype.fromLocaletoBeholderFormat = function () {
const toLocale = this.toLocaleString('en-GB')
const parts = toLocale.split(/\/|, /)
const [day, month, year, time] = parts

return `${year}-${month}-${day}T${time}`
}
3 changes: 2 additions & 1 deletion src/router/routes/personal-tokens-routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const personalTokensRoutes = {
component: () => import('@views/PersonalTokens/CreateView.vue'),
props: {
createPersonalTokenService: PersonalTokensService.createPersonalToken,
clipboardWrite: Helpers.clipboardWrite
clipboardWrite: Helpers.clipboardWrite,
convertDateToLocalTimezone: Helpers.convertDateToLocalTimezone
},
meta: {
breadCrumbs: [
Expand Down
82 changes: 12 additions & 70 deletions src/tests/helpers/convert-date.test.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,21 @@
import { describe, expect, it } from 'vitest'
import '@/helpers/convert-date'
import {
removeSelectedAmountOfHours,
formatToEndOfDayIsoDate,
convertOffsetToDecimal
} from '@/helpers/convert-date'
import { describe, it, expect } from 'vitest'
import { convertValueToDate, convertDateToLocalTimezone } from '@/helpers/convert-date'

describe('convertDate', () => {
describe('removeSelectedAmountOfHours', () => {
it('should remove the specified amount of hours from a date', () => {
const date = new Date('2022-01-01T05:00:00Z')
const hoursToRemove = 2
const expectedDate = new Date('2022-01-01T03:00:00Z')
it('should convert a given value to a date string in a specific format', () => {
const value = '2022-01-01T00:00:00'
const expectedDate = 'January 1, 2022 at 12:00 AM'

expect(removeSelectedAmountOfHours(hoursToRemove, date)).toEqual(expectedDate)
})
expect(convertValueToDate(value)).toBe(expectedDate)
})

describe('formatToEndOfDayIsoDate', () => {
it('should format a date to ISO format without milliseconds', () => {
const date = new Date('2022-01-01T23:59:59.999Z')
const expectedFormattedDate = '2022-01-01T23:59:59'
it('should convert a date to the local timezone', () => {
const utcOffset = '+0300'
const dateInUtcFormat = new Date('2022-01-01T00:00:00Z')

expect(formatToEndOfDayIsoDate(date)).toBe(expectedFormattedDate)
})
})

describe('convertOffsetToDecimal', () => {
it('should convert a UTC offset to decimal format', () => {
const offset = '+0300'
const expectedDecimal = '+03.00'

expect(convertOffsetToDecimal(offset)).toBe(expectedDecimal)
})
})

describe('Date.prototype methods', () => {
it('should convert date to UTC based on user offset', () => {
const date = new Date('2022-01-01T00:00:00Z')
const expectedDate = new Date('2022-01-01T00:00:00.000Z')

expect(date.toUTC(0)).toEqual(expectedDate)
})

it('should reset date to specified UTC offset', () => {
const date = new Date('2022-01-01T00:00:00Z')
const userUTC = '+0200'
const expectedDate = new Date('2021-12-31T22:00:00Z')

expect(date.resetUTC(userUTC)).toEqual(expectedDate)
})

it('should format date to Beholder format', () => {
const date = new Date('2022-01-01T00:00:00.000Z')
const expectedFormattedDate = '2022-01-01T00:00:00'

expect(date.toBeholderFormat()).toBe(expectedFormattedDate)
})

it('should convert local date to Beholder format', () => {
const date = new Date('2022-01-01T00:00:00')
date.toLocaleString = () => '01/01/2022, 00:00:00'
const expectedFormattedDate = '2022-01-01T00:00:00'

expect(date.fromLocaletoBeholderFormat()).toBe(expectedFormattedDate)
})

it('should convert a date to the local timezone', () => {
const utcOffset = '+0200'
const dateInUtcFormat = new Date('2022-01-01T00:00:00Z')

const convertedDate = dateInUtcFormat.convertDateToLocalTimezone(utcOffset)
const expectedDate = '2022-01-01T23:59:59'
const convertedDate = convertDateToLocalTimezone(dateInUtcFormat, utcOffset)
const expectedDate = '2022-01-01T23:59:59'

expect(convertedDate).toBe(expectedDate)
})
expect(convertedDate).toBe(expectedDate)
})
})
Loading

0 comments on commit d17e05d

Please sign in to comment.