Skip to content
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

Create labqueue.py #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions labqueue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#===================================================================================
# Unfinished script for tracking Lab TA attendance.
#===================================================================================
from wsse.client.requests.auth import WSSEAuth
import requests
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from datetime import datetime

from subswap.models import Shift
#===================================================================================

username = "cas-princeton-netid"
API_SECRET = "secret"
wsse_auth = WSSEAuth(username, API_SECRET)
base_url = "https://www.labqueue.io/api/v1/"
#===================================================================================

def get_requests(netid):
"""
Input:
netid: str

Output:
"results":[
{
"pk":61862,
"author_full_name":"Claire + Anna",
"author_netid":"ck2887",
"location":"Lewis 122",
"in_person":true,
"course":"intro-cs-lab_cos-126",
"description":"Error with StingBuilder formatting",
"time_created":"2022-11-18T19:01",
"acceptor_netid":"mmir",
"time_accepted":"2022-11-18T19:05",
"closer_netid":"mmir",
"time_closed":"2022-11-18T19:22"
},
...
]

Gets all the request fom the queue from the current semester.
"""
time = "2022-08-01T00:00"
payload = {"accepted_by": netid, "accepted_after": time}
result = requests.get(base_url + "requests/query",
auth=wsse_auth, params=payload).json()

curr_sem = (9, 12) if 9 <= datetime.today().month <= 12 else (2, 5)
result["results"] = [x for x in result["results"]
if curr_sem[0] <= datetime.strptime(x["time_accepted"], '%Y-%m-%dT%H:%M').month <= curr_sem[1]]

return result


def attendance(netid):
"""
Input:
netid: str
shiftid: int

Output:
{
"COS126, Wednesday, 7pm - 9pm": ["11/13/2022, 7pm - 9pm", "11/18/2022, 9pm - 11pm", ...],
"COS226/217, Sunday, 9pm - 11pm": [*list of dates*]

Gets all the unexcused absenses (no request posted) of the worker from the current semester.
"""


def time_spent_per_request(netid, starttime, endtime):
"""
Inputs:
netid: str
starttime, endtime: DateTime object -> "yyyy-mm-ddThh:mm"

Result input format:
{
"count":6,
"next":null,
"previous":null,
"results":[
{
"pk":61862,
"author_full_name":"Claire + Anna",
"author_netid":"ck2887",
"location":"Lewis 122",
"in_person":true,
"course":"intro-cs-lab_cos-126",
"description":"Error with StingBuilder formatting",
"time_created":"2022-11-18T19:01",
"acceptor_netid":"mmir",
"time_accepted":"2022-11-18T19:05",
"closer_netid":"mmir",
"time_closed":"2022-11-18T19:22"
},
...
]
}

Result output format:
{
"overall": {"count: 10, "average_minutes_per_request": 10}
"intro-cs-lab_cos-126": {"count": 10, "average_minutes_per_request": 10}
"intro-cs-lab_cos-217": {"count": 0, "average_minutes_per_request": 0}
"intro-cs-lab_cos-226": {"count": 0, "average_minutes_per_request": 0}
}
"""
result = get_requests(netid)

attendence = {
"overall": {
"count": result["count"],
"average_minutes_per_request": 0,
}
}

total_duration = 0
for r in result["results"]:
course = r["course"]
if course not in attendance:
time_accepted = datetime.strptime(
r["time_accepted"], '%Y-%m-%dT%H:%M')
time_closed = datetime.strptime(
r["time_closed"], '%Y-%m-%dT%H:%M')
duration = time_closed - time_accepted
duration = divmod(duration.total_seconds(), 60)[0]
total_duration += duration
attendance[course] = {
"count": 1,
"average_minutes_per_request": duration,
}
else:
time_accepted = datetime.strptime(
r["time_accepted"], '%Y-%m-%dT%H:%M')
time_closed = datetime.strptime(
r["time_closed"], '%Y-%m-%dT%H:%M')
duration = time_closed - time_accepted
duration = divmod(duration.total_seconds(), 60)[0]
total_duration += duration
curr_av = attendance[course]["average_minutes_per_request"]
curr_count = attendance[course]["count"]
attendance[course]["average_minutes_per_request"] = (
curr_av*curr_count + duration)/(curr_count + 1)

attendance[course]["count"] += 1

attendance["overall"]["average_minutes_per_request"] = total_duration / \
attendance["overall"]["count"]

return attendance