From 441b3085fab7f0c83e8eb2df388661d6e18c0120 Mon Sep 17 00:00:00 2001 From: Furkan Sahin Date: Mon, 1 Aug 2022 18:13:53 -0500 Subject: WiP parsing of office hours --- src/components/FileUploads.svelte | 24 +++++++++++++++-- src/logic/EditorActions.ts | 16 ++++++++--- src/models/PeerTeacher.ts | 2 ++ src/util/parser.ts | 57 ++++++++++++++++++++++++++++++++++----- 4 files changed, 88 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/components/FileUploads.svelte b/src/components/FileUploads.svelte index e5df56f..5f072ea 100644 --- a/src/components/FileUploads.svelte +++ b/src/components/FileUploads.svelte @@ -7,8 +7,9 @@ import { parseDatabaseFile, parseLabScheduleFile, + parseOfficeHoursFile, parsePTFile, - readQuestionairre, + readQuestionnaire, } from "../logic/EditorActions"; import { labStore, ptStore } from "../stores"; @@ -16,6 +17,7 @@ let labSchedule: FileList | null; let dbFile: FileList | null; let questionairreFile: FileList | null; + let officehoursFiles: FileList | null; let snackbar: Snackbar; let snackbarText; @@ -85,7 +87,13 @@ $: { if (questionairreFile?.length) { - readQuestionairre(questionairreFile[0]); + readQuestionnaire(questionairreFile[0]); + } + } + + $: { + if (officehoursFiles?.length) { + parseOfficeHoursFile(officehoursFiles[0]); } } @@ -193,6 +201,18 @@ bind:files={questionairreFile} /> + + + diff --git a/src/logic/EditorActions.ts b/src/logic/EditorActions.ts index 7c90c5b..8f3c105 100644 --- a/src/logic/EditorActions.ts +++ b/src/logic/EditorActions.ts @@ -1,4 +1,4 @@ -import { parseDatabase, parseLabSchedule, parsePTSchedule, parseQuestionairreCSV } from "../util/parser"; +import { parseDatabase, parseLabSchedule, parsePTSchedule, parseQuestionnaireCSV, parseOfficeHours } from "../util/parser"; export async function parsePTFile(file: File) { try { @@ -10,10 +10,20 @@ export async function parsePTFile(file: File) { } } -export async function readQuestionairre(file: File) { +export async function readQuestionnaire(file: File) { try { const text = await file.text(); - return parseQuestionairreCSV(text); + return parseQuestionnaireCSV(text); + } catch (error) { + console.error(file.name, error); + throw error; + } +} + +export async function parseOfficeHoursFile(file: File) { + try { + const text = await file.text(); + return parseOfficeHours(text); } catch (error) { console.error(file.name, error); throw error; diff --git a/src/models/PeerTeacher.ts b/src/models/PeerTeacher.ts index 9069069..7fee179 100644 --- a/src/models/PeerTeacher.ts +++ b/src/models/PeerTeacher.ts @@ -101,6 +101,8 @@ export default class PeerTeacher { return total_hours; } + // The public URL for displaying an an image on Google Drive has to be in this format + // This function simply replaces everthing before '=' with '' get drive_pic(): string { if (this.prof_pic_url == null || this.prof_pic_url == undefined) return "" return `https://drive.google.com/uc?export=view&id=${this.prof_pic_url.replace(/^[^=]*=/, '')}`; diff --git a/src/util/parser.ts b/src/util/parser.ts index 891099d..fa4fb6d 100644 --- a/src/util/parser.ts +++ b/src/util/parser.ts @@ -182,7 +182,15 @@ export function parseDatabaseLocalStorage(database_string: string) { ptStore.set(result.peerTeachers) } -export function parseQuestionairreCSV(csv: string) { +/** + * @param csv csv representation of PT Quentionairre + * The questionairre collects all sorts of information + * from Peer Teachers. The `attributes` variable below + * houses all of the attributes we want to collect. + * It looks for those attributes in the header row of the + * questionnairre to begin populating PT data. + */ +export function parseQuestionnaireCSV(csv: string) { const attributes = [ "Timestamp", "UIN", @@ -200,11 +208,7 @@ export function parseQuestionairreCSV(csv: string) { "Profile picture for website (image)", "Schedule (text file)" ] - const t = csv.split("\n") - const sheet = t.map((val) => { - const reg = new RegExp(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))") - return val.split(reg) - }) + const sheet = csv2sheet(csv); const m = mapAttributeToIndex(attributes, sheet[0]); @@ -221,6 +225,7 @@ export function parseQuestionairreCSV(csv: string) { pt.ethnicity = row[m["Racial Background"]]; pt.graduation = row[m["When are you graduating? (Day does not matter)"]] const c_teach = row[m["Classes you CAN peer teach for"]].split(",").map((val) => parseInt(val)); + // TODO parsing of this information is wrong, Cannot ParseInt("CSCE 110") pt.can_teach = new Set(c_teach) const p_teach = row[m["Classes you PREFER to peer teach for"]].split(",").map((val) => parseInt(val)); pt.pref_teach = new Set(p_teach); @@ -233,6 +238,46 @@ export function parseQuestionairreCSV(csv: string) { }) } +/** + * + * @param csv a string in csv format + * @returns 2d array representation of CSV file + */ +function csv2sheet(csv: string): string[][] { + const t = csv.split("\n"); + return t.map((val) => { + const reg = new RegExp(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); + return val.split(reg); + }); +} + +/** + * @param csv csv representation of a Strawpoll output + * Specific function, should be updated to parse whatever office hour format is being used + * The current format is a Strawpoll output in csv format + * It should be a messy function, but it has to be + */ +export function parseOfficeHours(csv: string) { + const sheet = csv2sheet(csv); + const begin = sheet.findIndex((row) => row[0].trim().toLowerCase() == "name"); + const end = sheet.findIndex((row) => row[0].trim() == "Total ✓ Votes"); + const pts = get(ptStore); + for (let i = begin + 1; i < end; i++) { + for (let j = 1; j < sheet[0]?.length ?? 0; j++) { + if (sheet[i][j] == "1") { + const pt_uin = parseInt(sheet[i][0]); + console.log("Found", sheet[begin][j], "for PT", pts.get(pt_uin)); + const datestr = sheet[begin][j]; + const regex = /\(([^)]*)\)/; // find time between parantheses eg ($1) find $1 + const match = datestr.match(regex) + const date_time_form = datestr.split(" ")[0] + `T${match[1].split(" ")[0]}`; + const date = new Date(date_time_form); + console.log(pts.get(pt_uin).name, date, date.getDate()) + } + } + } +} + /** * @param {Array} attributes Strings of attributes to look for * @param {Array} title_row Title row of sheet (usually first row: data[0]) -- cgit v1.2.3