Skip to content

Commit

Permalink
Media status fields update (#61)
Browse files Browse the repository at this point in the history
* task_list and media array flattened to coincide with GQL

* add task_id back

* task fix
  • Loading branch information
aaronshiel authored Apr 28, 2022
1 parent 56e6370 commit 679415c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 122 deletions.
154 changes: 70 additions & 84 deletions mentor_upload_api/src/mentor_upload_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Media:

@dataclass
class TaskInfo:
task_name: str
task_id: str
status: str

Expand All @@ -51,9 +52,12 @@ class TaskInfo:
class UploadTaskRequest:
mentor: str
question: str
task_list: List[TaskInfo]
trim_upload_task: TaskInfo
transcode_web_task: TaskInfo
transcode_mobile_task: TaskInfo
transcribe_task: TaskInfo
transcript: str = None
media: List[Media] = None
original_media: Media = None


def thumbnail_update_gql(req: MentorThumbnailUpdateRequest) -> GQLQueryBody:
Expand All @@ -69,11 +73,14 @@ def thumbnail_update_gql(req: MentorThumbnailUpdateRequest) -> GQLQueryBody:

def upload_task_req_gql(req: UploadTaskRequest) -> GQLQueryBody:
status = {}
status["taskList"] = req.task_list
status["transcodeWebTask"] = req.transcode_web_task
status["transcodeMobileTask"] = req.transcode_mobile_task
status["trimUploadTask"] = req.trim_upload_task
status["transcribeTask"] = req.transcribe_task
if req.transcript:
status["transcript"] = req.transcript
if req.media:
status["media"] = req.media
if req.originalMedia:
status["originalMedia"] = req.original_media

return {
"query": """mutation UploadStatus($mentorId: ID!, $questionId: ID!, $status: UploadTaskInputType!) {
Expand All @@ -89,47 +96,6 @@ def upload_task_req_gql(req: UploadTaskRequest) -> GQLQueryBody:
}


@dataclass
class UpdateTaskStatusRequest:
mentor: str
question: str
task_id: str
new_status: str
transcript: str = None
media: Media = None


def upload_task_status_req_gql(req: UpdateTaskStatusRequest) -> GQLQueryBody:
variables = {}
variables["mentorId"] = req.mentor
variables["questionId"] = req.question
variables["taskId"] = req.task_id
variables["newStatus"] = req.new_status
if req.transcript:
variables["transcript"] = req.transcript
if req.media:
variables["media"] = req.media
return {
"query": """mutation UpdateUploadTaskStatus($mentorId: ID!, $questionId: ID!, $taskId: String!, $newStatus: String!, $transcript: String, $media: [AnswerMediaInputType]) {
api {
uploadTaskStatusUpdate(mentorId: $mentorId, questionId: $questionId, taskId: $taskId, newStatus: $newStatus, transcript: $transcript, media: $media)
}
}""",
"variables": variables,
}


def upload_task_status_update(req: UpdateTaskStatusRequest) -> None:
headers = {"mentor-graphql-req": "true", "Authorization": f"bearer {get_api_key()}"}
body = upload_task_status_req_gql(req)
log.debug(body)
res = requests.post(get_graphql_endpoint(), json=body, headers=headers)
res.raise_for_status()
tdjson = res.json()
if "errors" in tdjson:
raise Exception(json.dumps(tdjson.get("errors")))


def upload_task_update(req: UploadTaskRequest) -> None:
headers = {"mentor-graphql-req": "true", "Authorization": f"bearer {get_api_key()}"}
body = upload_task_req_gql(req)
Expand Down Expand Up @@ -162,9 +128,7 @@ def fetch_upload_task_gql(req: FetchUploadTaskReq) -> GQLQueryBody:
return {
"query": """query UploadTask($mentorId: ID!, $questionId: ID!) {
uploadTask(mentorId: $mentorId, questionId: $questionId){
taskList{
task_name
}
transcript
}
}""",
"variables": {"mentorId": req.mentor, "questionId": req.question},
Expand All @@ -180,12 +144,8 @@ def fetch_upload_task_gql(req: FetchUploadTaskReq) -> GQLQueryBody:
"uploadTask": {
"type": ["object", "null"],
"properties": {
"taskList": {
"type": "array",
"item": {
"type": "object",
"properties": {"task_name": {"type": "string"}},
},
"transcript": {
"type": "string",
}
},
}
Expand Down Expand Up @@ -215,7 +175,6 @@ class AnswerUpdateRequest:
mentor: str
question: str
transcript: str
media: List[Media]
has_edited_transcript: bool = None


Expand All @@ -226,20 +185,20 @@ def upload_answer_and_task_req_gql(
variables["mentorId"] = answer_req.mentor
variables["questionId"] = answer_req.question

variables["answer"] = {
"media": answer_req.media,
}
variables["answer"] = {}
if answer_req.transcript:
variables["answer"]["transcript"] = (answer_req.transcript,)
variables["answer"]["transcript"] = answer_req.transcript
if answer_req.has_edited_transcript is not None:
variables["answer"]["hasEditedTranscript"] = answer_req.has_edited_transcript

variables["status"] = {"taskList": task_req.task_list}
variables["status"] = {
"transcodeWebTask": task_req.transcode_web_task,
"transcodeMobileTask": task_req.transcode_mobile_task,
"transcribeTask": task_req.transcribe_task,
"trimUploadTask": task_req.trim_upload_task,
}
if task_req.transcript:
variables["status"]["transcript"] = task_req.transcript

if task_req.media:
variables["status"]["media"] = task_req.media
return {
"query": """mutation UpdateUploadAnswerAndTaskStatus($mentorId: ID!, $questionId: ID!, $answer: UploadAnswerType!, $status: UploadTaskInputType!) {
api {
Expand All @@ -252,7 +211,7 @@ def upload_answer_and_task_req_gql(


def upload_answer_and_task_update(
answer_req: AnswerUpdateRequest, task_req: UpdateTaskStatusRequest
answer_req: AnswerUpdateRequest, task_req: UploadTaskRequest
) -> None:
headers = {"mentor-graphql-req": "true", "Authorization": f"bearer {get_api_key()}"}
body = upload_answer_and_task_req_gql(answer_req, task_req)
Expand All @@ -268,11 +227,21 @@ def fetch_answer_transcript_and_media_gql(mentor: str, question: str) -> GQLQuer
"query": """query Answer($mentor: ID!, $question: ID!) {
answer(mentor: $mentor, question: $question){
transcript
media {
type
tag
url
}
webMedia {
type
tag
url
}
mobileMedia {
type
tag
url
}
vttMedia {
type
tag
url
}
}
}""",
"variables": {"mentor": mentor, "question": question},
Expand All @@ -289,16 +258,28 @@ def fetch_answer_transcript_and_media_gql(mentor: str, question: str) -> GQLQuer
"type": "object",
"properties": {
"transcript": {"type": "string"},
"media": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"type": "string"},
"tag": {"type": "string"},
"url": {"type": "string"},
},
"required": ["type", "tag", "url"],
"webMedia": {
"type": "object",
"properties": {
"type": {"type": "string"},
"tag": {"type": "string"},
"url": {"type": "string"},
},
},
"mobileMedia": {
"type": "object",
"properties": {
"type": {"type": "string"},
"tag": {"type": "string"},
"url": {"type": "string"},
},
},
"vttMedia": {
"type": "object",
"properties": {
"type": {"type": "string"},
"tag": {"type": "string"},
"url": {"type": "string"},
},
},
},
Expand All @@ -318,10 +299,15 @@ def fetch_answer_transcript_and_media(mentor: str, question: str):
json_res = exec_graphql_with_json_validation(
gql_query, fetch_answer_transcript_media_json_schema, headers=headers
)
return (
json_res["data"]["answer"]["transcript"],
json_res["data"]["answer"]["media"],
)
answer_data = json_res["data"]["answer"]
media = []
if answer_data["webMedia"] is not None:
media.append(answer_data["webMedia"])
if answer_data["mobileMedia"] is not None:
media.append(answer_data["mobileMedia"])
if answer_data["vttMedia"] is not None:
media.append(answer_data["vttMedia"])
return (answer_data["transcript"], media)


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,40 +127,36 @@ def submit_job(req):


def create_task_list(trim, has_edited_transcript):
task_list = []
if trim:
task_list.append(
{
"task_name": "trim_upload",
"task_id": str(uuid.uuid4()),
"status": "DONE",
}
)

task_list.append(
transcode_web_task = {
"task_name": "transcoding-web",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
transcode_mobile_task = {
"task_name": "transcoding-mobile",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
transcribe_task = (
{
"task_name": "transcoding-web",
"task_name": "transcribing",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
if not has_edited_transcript
else None
)
task_list.append(
trim_upload_task = (
{
"task_name": "transcoding-mobile",
"task_name": "trim-upload",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
if trim
else None
)
if not has_edited_transcript:
task_list.append(
{
"task_name": "transcribing",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
)

return task_list
return transcode_web_task, transcode_mobile_task, transcribe_task, trim_upload_task


def upload_to_s3(file_path, s3_path):
Expand Down Expand Up @@ -254,45 +250,55 @@ def upload(body):
s3_path = f"videos/{mentor}/{question}"
upload_to_s3(file_path, s3_path)

task_list = create_task_list(trim, has_edited_transcript)
(
transcode_web_task,
transcode_mobile_task,
transcribe_task,
trim_upload_task,
) = create_task_list(trim, has_edited_transcript)

req = {
"request": {
"mentor": mentor,
"question": question,
"video": f"{s3_path}/original.mp4",
"task_list": task_list,
"transcodeWebTask": transcode_web_task,
"transcodeMobileTask": transcode_mobile_task,
"trimUploadTask": trim_upload_task,
"transcribeTask": transcribe_task,
}
}

original_video_url = get_original_video_url(mentor, question)
# we risk here overriding values, perhaps processing was already done, so status is DONE
# but this will overwrite and revert them back to QUEUED. Can we just append?
upload_answer_and_task_update(
AnswerUpdateRequest(
mentor=mentor,
question=question,
transcript="",
media=[{"type": "video", "tag": "original", "url": original_video_url}],
),
AnswerUpdateRequest(mentor=mentor, question=question, transcript=""),
UploadTaskRequest(
mentor=mentor,
question=question,
task_list=task_list,
transcode_web_task=transcode_web_task,
transcode_mobile_task=transcode_mobile_task,
trim_upload_task=trim_upload_task,
transcribe_task=transcribe_task,
transcript="",
media=[{"type": "video", "tag": "original", "url": original_video_url}],
original_media={
"type": "video",
"tag": "original",
"url": original_video_url,
},
),
)
submit_job(req)

return jsonify(
{
"data": {
"taskList": task_list,
# this seems incorrect, passing multiple ids
"statusUrl": _to_status_url(
request.url_root, [t["task_id"] for t in task_list]
),
"transcodeWebTask": transcode_web_task,
"transcodeMobileTask": transcode_mobile_task,
"transcribeTask": transcribe_task,
"trimUploadTask": trim_upload_task,
"statusUrl": _to_status_url(request.url_root, str(uuid.uuid4())),
}
}
)
Expand Down
Loading

0 comments on commit 679415c

Please sign in to comment.