-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ASAP-345]analytics csv export (#4281)
* first iteration of analytics csv export * commit missing file * fix definitions in tests * more tests * lint * more tests and new csv naming * fix errors * add button container styles * fix export button position * allow params when csv export * update button style
- Loading branch information
Showing
11 changed files
with
314 additions
and
64 deletions.
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
92 changes: 92 additions & 0 deletions
92
apps/crn-frontend/src/analytics/leadership/__tests__/export.test.ts
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,92 @@ | ||
import { Stringifier } from 'csv-stringify'; | ||
import { leadershipToCSV, algoliaResultsToStream } from '../export'; | ||
|
||
describe('leadershipToCSV', () => { | ||
it('handles basic data', () => { | ||
const data = { | ||
id: '1', | ||
displayName: 'Team 1', | ||
workingGroupLeadershipRoleCount: 1, | ||
workingGroupPreviousLeadershipRoleCount: 2, | ||
workingGroupMemberCount: 3, | ||
workingGroupPreviousMemberCount: 4, | ||
interestGroupLeadershipRoleCount: 5, | ||
interestGroupPreviousLeadershipRoleCount: 6, | ||
interestGroupMemberCount: 7, | ||
interestGroupPreviousMemberCount: 8, | ||
}; | ||
|
||
expect(leadershipToCSV('working-group')(data)).toEqual({ | ||
team: 'Team 1', | ||
currentlyInALeadershipRole: '1', | ||
previouslyInALeadershipRole: '2', | ||
currentlyAMember: '3', | ||
previouslyAMember: '4', | ||
}); | ||
expect(leadershipToCSV('interest-group')(data)).toEqual({ | ||
team: 'Team 1', | ||
currentlyInALeadershipRole: '5', | ||
previouslyInALeadershipRole: '6', | ||
currentlyAMember: '7', | ||
previouslyAMember: '8', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('algoliaResultsToStream', () => { | ||
const mockCsvStream = { | ||
write: jest.fn(), | ||
end: jest.fn(), | ||
}; | ||
|
||
it('streams results', async () => { | ||
await algoliaResultsToStream( | ||
mockCsvStream as unknown as Stringifier, | ||
() => | ||
Promise.resolve({ | ||
total: 2, | ||
items: [ | ||
{ | ||
id: '1', | ||
displayName: 'Team 1', | ||
workingGroupLeadershipRoleCount: 1, | ||
workingGroupPreviousLeadershipRoleCount: 2, | ||
workingGroupMemberCount: 3, | ||
workingGroupPreviousMemberCount: 4, | ||
interestGroupLeadershipRoleCount: 5, | ||
interestGroupPreviousLeadershipRoleCount: 6, | ||
interestGroupMemberCount: 7, | ||
interestGroupPreviousMemberCount: 8, | ||
}, | ||
{ | ||
id: '2', | ||
displayName: 'Team 2', | ||
workingGroupLeadershipRoleCount: 2, | ||
workingGroupPreviousLeadershipRoleCount: 3, | ||
workingGroupMemberCount: 4, | ||
workingGroupPreviousMemberCount: 5, | ||
interestGroupLeadershipRoleCount: 4, | ||
interestGroupPreviousLeadershipRoleCount: 3, | ||
interestGroupMemberCount: 2, | ||
interestGroupPreviousMemberCount: 1, | ||
}, | ||
], | ||
}), | ||
(a) => a, | ||
); | ||
|
||
expect(mockCsvStream.write).toHaveBeenCalledTimes(2); | ||
|
||
expect(mockCsvStream.end).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('handles undefined response', async () => { | ||
const transformSpy = jest.fn(); | ||
await algoliaResultsToStream( | ||
mockCsvStream as unknown as Stringifier, | ||
() => Promise.resolve(undefined), | ||
transformSpy, | ||
); | ||
expect(transformSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,57 @@ | ||
import { CSVValue, GetListOptions } from '@asap-hub/frontend-utils'; | ||
import { | ||
AnalyticsTeamLeadershipDataObject, | ||
AnalyticsTeamLeadershipResponse, | ||
ListAnalyticsTeamLeadershipResponse, | ||
} from '@asap-hub/model'; | ||
import { Stringifier } from 'csv-stringify/browser/esm'; | ||
|
||
type LeadershipRowCSV = Record<string, CSVValue>; | ||
|
||
export const leadershipToCSV = | ||
(metric: 'working-group' | 'interest-group') => | ||
(data: AnalyticsTeamLeadershipDataObject): LeadershipRowCSV => { | ||
const metricPrefix = | ||
metric === 'working-group' ? 'workingGroup' : 'interestGroup'; | ||
return { | ||
team: data.displayName, | ||
currentlyInALeadershipRole: | ||
data[`${metricPrefix}LeadershipRoleCount`].toString(), | ||
previouslyInALeadershipRole: | ||
data[`${metricPrefix}PreviousLeadershipRoleCount`].toString(), | ||
currentlyAMember: data[`${metricPrefix}MemberCount`].toString(), | ||
previouslyAMember: data[`${metricPrefix}PreviousMemberCount`].toString(), | ||
}; | ||
}; | ||
|
||
export const algoliaResultsToStream = async ( | ||
csvStream: Stringifier, | ||
getResults: ({ | ||
currentPage, | ||
pageSize, | ||
}: Pick<GetListOptions, 'currentPage' | 'pageSize'>) => Readonly< | ||
Promise<ListAnalyticsTeamLeadershipResponse | undefined> | ||
>, | ||
transform: ( | ||
result: AnalyticsTeamLeadershipResponse, | ||
) => Record<string, unknown>, | ||
) => { | ||
let morePages = true; | ||
let currentPage = 0; | ||
while (morePages) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const data = await getResults({ | ||
currentPage, | ||
pageSize: 10, | ||
}); | ||
if (data) { | ||
const nbPages = data.total / 10; | ||
data.items.map(transform).forEach((row) => csvStream.write(row)); | ||
currentPage += 1; | ||
morePages = currentPage <= nbPages; | ||
} else { | ||
morePages = false; | ||
} | ||
} | ||
csvStream.end(); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { ToastContext } from '@asap-hub/react-context'; | ||
import { css } from '@emotion/react'; | ||
import { useContext } from 'react'; | ||
import { Button } from '../atoms'; | ||
import { ExportIcon } from '../icons'; | ||
import { mobileScreen, rem, tabletScreen } from '../pixels'; | ||
|
||
const exportSectionStyles = css({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: rem(15), | ||
|
||
[`@media (max-width: ${tabletScreen.min}px)`]: { | ||
flexDirection: 'column', | ||
alignItems: 'flex-start', | ||
marginTop: rem(24), | ||
width: '100%', | ||
}, | ||
}); | ||
|
||
const exportButtonStyles = css({ | ||
gap: rem(8), | ||
height: '100%', | ||
alignItems: 'center', | ||
padding: rem(9), | ||
paddingRight: rem(15), | ||
[`@media (max-width: ${tabletScreen.min}px)`]: { | ||
width: '100%', | ||
}, | ||
|
||
[`@media (min-width:${tabletScreen.min}px) and (max-width: ${mobileScreen.max}px)`]: | ||
{ | ||
minWidth: 'auto', | ||
}, | ||
}); | ||
|
||
const exportIconStyles = css({ display: 'flex' }); | ||
type ExportButtonProps = { | ||
readonly exportResults?: () => Promise<void>; | ||
}; | ||
|
||
const ExportButton: React.FC<ExportButtonProps> = ({ exportResults }) => { | ||
const toast = useContext(ToastContext); | ||
return exportResults ? ( | ||
<span css={exportSectionStyles}> | ||
<strong>Export as:</strong> | ||
<Button | ||
noMargin | ||
small | ||
onClick={() => | ||
exportResults().catch(() => { | ||
toast('There was an issue exporting to CSV. Please try again.'); | ||
}) | ||
} | ||
overrideStyles={exportButtonStyles} | ||
> | ||
<> | ||
<div css={exportIconStyles}>{ExportIcon}</div> | ||
CSV | ||
</> | ||
</Button> | ||
</span> | ||
) : null; | ||
}; | ||
|
||
export default ExportButton; |
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
Oops, something went wrong.