-
Notifications
You must be signed in to change notification settings - Fork 6
Database summary statistics export #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eozden-wq
wants to merge
12
commits into
main
Choose a base branch
from
feature/anon-csv-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+316
−1
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6701e7a
Wrote class to augment user age to age groups. Started working on the…
eozden-wq 1e646d8
Added DSU Privacy policy and photo/media consent to AnonymousCsvTrans…
eozden-wq 887097e
Wrote augment to find user CV and last time CV was updated
eozden-wq ce5d11b
Wrote augmentor to augment user flags in DB to json arrays stored as …
eozden-wq bb2fe8c
Added dietary requirements and discipline of study to the csv transfo…
eozden-wq 3a92289
Added route and export handler
eozden-wq e40342f
Refactored age augment transformer
eozden-wq dfb88d4
Selected just updatedAt date in DB query to get userCV data
eozden-wq 71f179b
Refactored user flag augmenting transform. Fixed API route. Added a f…
eozden-wq cb2216c
Tested and checked whether data is exported correctly. Made changes t…
eozden-wq 3902edc
Added URL for anonymous application export to the root of the data-ex…
eozden-wq 08213c3
Fixed small mistake made while rebasing, had to add CV augmenting tra…
eozden-wq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
server/src/routes/applications/data-export/anonymous-csv-transform.ts
This file contains hidden or 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,46 @@ | ||
import type { DisciplineOfStudy } from "@durhack/durhack-common/input/discipline-of-study" | ||
import type { DietaryRequirement } from "@durhack/durhack-common/types/application" | ||
import type { UserInfo } from "@/database" | ||
import type { IdAugments } from "@/routes/applications/data-export/anonymous-id-augmenting-transform" | ||
import type { AttendanceAugments } from "@/routes/applications/data-export/attendance-augmenting-transform" | ||
import type { ConsentAugments } from "@/routes/applications/data-export/consent-augmenting-transform" | ||
import { PickAttributesToCsvTransform } from "@/routes/applications/data-export/pick-attributes-to-csv-transform" | ||
import type { AgeAugments } from "@/routes/applications/data-export/user-age-augmenting-transform" | ||
import type { UserCvAugments } from "@/routes/applications/data-export/user-cv-augmenting-transform" | ||
import type { FlagAugments } from "@/routes/applications/data-export/user-flag-augmenting-transform" | ||
|
||
type Source = UserInfo & | ||
IdAugments & | ||
AgeAugments & | ||
AttendanceAugments & | ||
ConsentAugments<"media" | "dsuPrivacy"> & | ||
UserCvAugments & | ||
FlagAugments<"discipline-of-study", DisciplineOfStudy> & | ||
FlagAugments<"dietary-requirement", DietaryRequirement> | ||
|
||
export class AnonymousCsvTransform extends PickAttributesToCsvTransform<Source> { | ||
constructor() { | ||
super({ | ||
attributes: [ | ||
{ name: "id", label: "anon_id" }, | ||
{ name: "countryOfResidence", label: "country_of_residence" }, | ||
{ name: "university", label: "university" }, | ||
{ name: "levelOfStudy", label: "level_of_study" }, | ||
{ name: "tShirtSize", label: "t_shirt_size" }, | ||
{ name: "gender", label: "gender" }, | ||
{ name: "ethnicity", label: "ethnicity" }, | ||
{ name: "hackathonExperience", label: "hackathon_experience" }, | ||
{ name: "dietary-requirement", label: "dietary_requirements" }, | ||
{ name: "discipline-of-study", label: "discipline_of_study" }, | ||
{ name: "ageGroup", label: "age_group" }, | ||
{ name: "dsuPrivacy", label: "dsu_privacy" }, | ||
{ name: "media", label: "photo_media_consent" }, | ||
{ name: "is_cv_uploaded", label: "did_upload_cv" }, | ||
{ name: "cv_update_time", label: "cv_last_updated_at" }, | ||
{ name: "applicationSubmittedAt", label: "application_submitted_at" }, | ||
{ name: "applicationStatus", label: "application_status" }, | ||
{ name: "isCheckedIn", label: "attendance" }, | ||
], | ||
}) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
server/src/routes/applications/data-export/anonymous-id-augmenting-transform.ts
This file contains hidden or 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,40 @@ | ||
import stream from "node:stream" | ||
import { isString } from "@/lib/type-guards" | ||
|
||
export type IdAugments = { id: number } | ||
|
||
type IdAugmentedUserInfo = IdAugments & { userId: string } | ||
|
||
export class AnonymousIdAugmentingTransform extends stream.Transform { | ||
counter: number | ||
|
||
constructor() { | ||
super({ | ||
writableObjectMode: true, | ||
readableObjectMode: true, | ||
}) | ||
|
||
this.counter = 0 | ||
} | ||
|
||
augmentUserInfo(userInfo: { userId: string; id?: number }): IdAugmentedUserInfo { | ||
userInfo.id = this.counter++ | ||
return userInfo as IdAugmentedUserInfo | ||
} | ||
|
||
async augmentChunk(chunk: { userId: string }[]): Promise<IdAugmentedUserInfo[]> { | ||
return await Promise.all(chunk.map((userInfo) => this.augmentUserInfo(userInfo))) | ||
} | ||
|
||
_transform(chunk: { userId: string }[], _encoding: never, callback: stream.TransformCallback): void { | ||
this.augmentChunk(chunk) | ||
.then((filteredChunk) => { | ||
callback(null, filteredChunk satisfies IdAugmentedUserInfo[]) | ||
}) | ||
.catch((error: unknown) => { | ||
if (error instanceof Error) return callback(error) | ||
if (isString(error)) return callback(new Error(error)) | ||
return callback(new Error(`Something really strange happened. Error object: ${error}`)) | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
68 changes: 68 additions & 0 deletions
68
server/src/routes/applications/data-export/user-age-augmenting-transform.ts
This file contains hidden or 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,68 @@ | ||
import assert from "node:assert/strict" | ||
import stream from "node:stream" | ||
import type { UserInfo } from "@/database" | ||
import { isString } from "@/lib/type-guards" | ||
|
||
type AgeGroup = "<18" | "18-21" | "22-25" | "26-29" | "30-39" | "40-59" | "60+" | ||
|
||
export type AgeAugments = { | ||
ageGroup: AgeGroup | ||
} | ||
|
||
type AgeAugmentedUserInfo = { userId: string } & AgeAugments | ||
|
||
export class UserAgeAugmentingTransform extends stream.Transform { | ||
constructor() { | ||
super({ | ||
writableObjectMode: true, | ||
readableObjectMode: true, | ||
}) | ||
} | ||
|
||
resolveAgeGroup(age: number): AgeGroup { | ||
if (age < 18) { | ||
return "<18" | ||
} | ||
if (age <= 21) { | ||
return "18-21" | ||
} | ||
if (age <= 25) { | ||
return "22-25" | ||
} | ||
if (age <= 29) { | ||
return "26-29" | ||
} | ||
if (age <= 39) { | ||
return "30-39" | ||
} | ||
if (age <= 59) { | ||
return "40-59" | ||
} | ||
return "60+" | ||
} | ||
|
||
augmentUserInfo(userInfo: UserInfo): AgeAugmentedUserInfo { | ||
assert(userInfo.applicationSubmittedAt) | ||
assert(userInfo.age) | ||
|
||
const ageGroup: AgeGroup = this.resolveAgeGroup(userInfo.age) | ||
|
||
return { ...userInfo, ageGroup } | ||
} | ||
|
||
async augmentChunk(chunk: UserInfo[]): Promise<AgeAugmentedUserInfo[]> { | ||
return await Promise.all(chunk.map((userInfo) => this.augmentUserInfo(userInfo))) | ||
} | ||
|
||
_transform(chunk: UserInfo[], _encoding: never, callback: stream.TransformCallback): void { | ||
this.augmentChunk(chunk) | ||
.then((filteredChunk) => { | ||
callback(null, filteredChunk satisfies AgeAugmentedUserInfo[]) | ||
}) | ||
.catch((error: unknown) => { | ||
if (error instanceof Error) return callback(error) | ||
if (isString(error)) return callback(new Error(error)) | ||
return callback(new Error(`Something really strange happened. Error object: ${error}`)) | ||
}) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
server/src/routes/applications/data-export/user-cv-augmenting-transform.ts
This file contains hidden or 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,46 @@ | ||
import stream from "node:stream" | ||
import { prisma, type UserInfo } from "@/database" | ||
|
||
import { isString } from "@/lib/type-guards" | ||
|
||
export type UserCvAugments = { | ||
is_cv_uploaded: boolean | ||
cv_update_time: Date | null | ||
} | ||
|
||
type CvAugmentedUserInfo = { userId: string } & UserCvAugments | ||
|
||
export class UserCvAugmentingTransform extends stream.Transform { | ||
constructor() { | ||
super({ writableObjectMode: true, readableObjectMode: true }) | ||
} | ||
|
||
async augmentUserInfo(userInfo: UserInfo): Promise<CvAugmentedUserInfo> { | ||
const foundCV = await prisma.userCV.findUnique({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not use |
||
where: { | ||
userId: userInfo.userId, | ||
}, | ||
select: { | ||
updatedAt: true, | ||
}, | ||
}) | ||
|
||
return { ...userInfo, is_cv_uploaded: !!foundCV, cv_update_time: foundCV ? foundCV.updatedAt : null } | ||
} | ||
|
||
async augmentChunk(chunk: UserInfo[]): Promise<CvAugmentedUserInfo[]> { | ||
return await Promise.all(chunk.map((userInfo) => this.augmentUserInfo(userInfo))) | ||
} | ||
|
||
_transform(chunk: UserInfo[], _encoding: never, callback: stream.TransformCallback): void { | ||
this.augmentChunk(chunk) | ||
.then((filteredChunk) => { | ||
callback(null, filteredChunk satisfies CvAugmentedUserInfo[]) | ||
}) | ||
.catch((error: unknown) => { | ||
if (error instanceof Error) return callback(error) | ||
if (isString(error)) return callback(new Error(error)) | ||
return callback(new Error(`Something really strange happened. Error object: ${error}`)) | ||
}) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
server/src/routes/applications/data-export/user-flag-augmenting-transform.ts
This file contains hidden or 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,58 @@ | ||
import stream from "node:stream" | ||
|
||
import { prisma } from "@/database" | ||
import { isString } from "@/lib/type-guards" | ||
|
||
export type FlagAugments<Namespace extends string, Member extends string> = { [K in Namespace]: Member[] } | ||
|
||
type FlagAugmentedUserInfo<Namespace extends string, Member extends string> = FlagAugments<Namespace, Member> & { | ||
userId: string | ||
} | ||
|
||
export class UserFlagAugmentingTransform<Namespace extends string, Member extends string> extends stream.Transform { | ||
namespace: Namespace | ||
|
||
constructor(namespace: Namespace) { | ||
super({ | ||
writableObjectMode: true, | ||
readableObjectMode: true, | ||
}) | ||
|
||
this.namespace = namespace | ||
} | ||
|
||
async augmentUserInfo(userInfo: { userId: string }): Promise<FlagAugmentedUserInfo<Namespace, Member>> { | ||
const flags = await prisma.userFlag.findMany({ | ||
where: { userId: userInfo.userId, flagName: { startsWith: `${this.namespace}:` } }, | ||
}) | ||
|
||
const members: Member[] = [] | ||
|
||
for (const flag of flags) { | ||
const flagName = flag.flagName.substring(this.namespace.length + 1) as Member | ||
members.push(flagName) | ||
} | ||
|
||
const result = { [this.namespace as Namespace]: members } satisfies FlagAugments<string, Member> as FlagAugments< | ||
Namespace, | ||
Member | ||
> | ||
return { ...userInfo, ...result } | ||
} | ||
|
||
async augmentChunk(chunk: { userId: string }[]): Promise<FlagAugmentedUserInfo<Namespace, Member>[]> { | ||
return await Promise.all(chunk.map((userInfo) => this.augmentUserInfo(userInfo))) | ||
} | ||
|
||
_transform(chunk: { userId: string }[], _encoding: never, callback: stream.TransformCallback): void { | ||
this.augmentChunk(chunk) | ||
.then((filteredChunk) => { | ||
callback(null, filteredChunk satisfies FlagAugmentedUserInfo<Namespace, Member>[]) | ||
}) | ||
.catch((error: unknown) => { | ||
if (error instanceof Error) return callback(error) | ||
if (isString(error)) return callback(new Error(error)) | ||
return callback(new Error(`Something really strange happened. Error object: ${error}`)) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan of this name, don't feel it fully explains what it does