Skip to content

Commit

Permalink
Merge pull request #71 from ibi-group/handle-new-cdp-filenames
Browse files Browse the repository at this point in the history
fix: support hourly CDP uploads
  • Loading branch information
miles-grant-ibigroup authored Jul 21, 2022
2 parents 1b8ff2a + d48823c commit daca59d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ jobs:

- name: Download middleware
run: "git clone https://github.com/ibi-group/otp-middleware.git"
- name: "[TEMP] checkout docker branch"
run: cd otp-middleware && git checkout docker
- name: Load e2e middleware config
run: echo $E2E_OTP_MIDDLEWARE_DOCKER_CONFIG_BASE64 | base64 -d > otp-middleware/configurations/default/env.docker.yml
env:
Expand Down
15 changes: 7 additions & 8 deletions __tests__/e2e/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'expect-puppeteer'

import { server } from '../../jest-puppeteer.config'
import { dateFormatterOptions } from '../../util/constants'
import { waitForDownload } from '../util/waitForDownload'

jest.setTimeout(50000)
Expand Down Expand Up @@ -185,13 +184,13 @@ describe('end-to-end tests', () => {
await expect(page).toMatch('Raw Request Data Download', { timeout: 6000 })
})

const dateFormatter = new Intl.DateTimeFormat('en-US', dateFormatterOptions)
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
const todaysUploadString = dateFormatter.format(yesterday)
// Ideally this would be a file that the middleware uploads at e2e test launch,
// however the CDP refactor opt-middleware#170 changed the behavior so that this
// no longer happens. Therefore, we use a string known to exist
const uploadString = 'May 20, 2022'

it('should see the file that the middleware uploaded', async () => {
await expect(page).toMatch(todaysUploadString)
it('should see a file that a previous middleware uploaded', async () => {
await expect(page).toMatch(uploadString)
})
it('should be able to download a CDP zip file', async () => {
const cdpsession = await page.target().createCDPSession()
Expand All @@ -200,7 +199,7 @@ describe('end-to-end tests', () => {
downloadPath: '/tmp'
})

await expect(page).toClick('div', { text: todaysUploadString })
await expect(page).toClick('div', { text: uploadString })

await waitForDownload('anon-trip-data')
await expect(page).toMatch('You last downloaded', { timeout: 6000 })
Expand Down
5 changes: 5 additions & 0 deletions types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export type CDPFile = {
}

export type ConvertedMD = { markdown: string }

export type FileNameParserResponse = {
dateFormatterOptions: Intl.DateTimeFormatOptions
timestamp: number
} | null
7 changes: 6 additions & 1 deletion util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ export const USER_TYPES: { label: string; url: string; value: USER_TYPE }[] = [
{ label: 'OTP Users', url: OTP_USER_URL, value: 'otp' }
]

export const dateFormatterOptions: Intl.DateTimeFormatOptions = {
export const dateFormatterOptionsForDate: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'long',
timeZone: 'UTC',
year: 'numeric'
}

export const dateFormatterOptionsForTime: Intl.DateTimeFormatOptions = {
dateStyle: 'long',
timeStyle: 'short'
}
50 changes: 45 additions & 5 deletions util/ui.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Children, isValidElement, cloneElement } from 'react'

import { dateFormatterOptions, USER_TYPES } from '../util/constants'
import { FileNameParserResponse } from '../types/response'
import {
dateFormatterOptionsForDate,
dateFormatterOptionsForTime,
USER_TYPES
} from '../util/constants'

import type { USER_TYPE } from './constants'

Expand Down Expand Up @@ -89,14 +94,49 @@ export const getActiveUserTypes = (): {
/**
* Converts a CDP file name to a human-readable date. A bit fickle and
* not universally browser supported. Attempts to fail as gracefully as possible
*
* Able to handle all the date/time formats that are generated by the CDP.
*/
export const getDateFromCDPFileName = (filename: string): string => {
const date = filename.split('-anon-trip-data')?.[0].split('/')?.[1]
if (!date) return filename

const parsedDate = Date.parse(date)
if (!parsedDate) return filename
const parsedDate =
getDateOnlyFromCDPFileName(date) || getDateAndTimeFromCDPFileName(date)
if (!parsedDate || !parsedDate.timestamp) return filename

const dateFormatter = new Intl.DateTimeFormat('en-US', dateFormatterOptions)
return dateFormatter.format(new Date(parsedDate))
const dateFormatter = new Intl.DateTimeFormat(
'en-US',
parsedDate.dateFormatterOptions
)
return dateFormatter.format(new Date(parsedDate.timestamp))
}
/**
* Some CDP file names are only a date (yyyy-mm-dd). These are easy to parse.
*/
const getDateOnlyFromCDPFileName = (date: string): FileNameParserResponse => {
const timestamp = Date.parse(date)
if (!timestamp) return null

return { dateFormatterOptions: dateFormatterOptionsForDate, timestamp }
}

/**
* Some CDP zip file names include the hour in the very non-standard format yyyy-mm-dd-hh. This method
* attempts to process this file name
*/
const getDateAndTimeFromCDPFileName = (
date: string
): FileNameParserResponse => {
const datePortion = date.split('-')
if (datePortion?.find((dp) => Number.isNaN(parseInt(dp)))) return null

// The last number after the last dash is the hour
const timePortion = datePortion.pop()
if (!timePortion) return null

// :00 helps the parser know we are talking about a time
const timestamp = Date.parse(`${datePortion.join('-')} ${timePortion}:00`)

return { dateFormatterOptions: dateFormatterOptionsForTime, timestamp }
}

1 comment on commit daca59d

@vercel
Copy link

@vercel vercel bot commented on daca59d Jul 21, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

otp-admin-ui – ./

otp-admin-ui-ibi-group.vercel.app
otp-admin-ui.vercel.app
otp-admin-ui-git-dev-ibi-group.vercel.app

Please sign in to comment.