Commit 069680f0 authored by I.K Seneviratne's avatar I.K Seneviratne

Committing the implementations of APscheduler background task.

parent aa31a5c7
...@@ -11,11 +11,16 @@ each method will return an HttpResponse that allows its data to be rendered into ...@@ -11,11 +11,16 @@ each method will return an HttpResponse that allows its data to be rendered into
arbitrary media types. arbitrary media types.
""" """
import json
from random import Random from random import Random
from apscheduler.jobstores.mongodb import MongoDBJobStore
from MonitorLecturerApp.models import LectureRecordedVideo, LecturerVideoMetaData from MonitorLecturerApp.models import LectureRecordedVideo, LecturerVideoMetaData
from MonitorLecturerApp.serializers import LectureRecordedVideoSerializer, LecturerVideoMetaDataSerializer from MonitorLecturerApp.serializers import LectureRecordedVideoSerializer, LecturerVideoMetaDataSerializer
from rest_framework.views import * from rest_framework.views import *
from integrated_slpes.wsgi import application
from .logic import activity_recognition as ar from .logic import activity_recognition as ar
from . import emotion_detector as ed, automation_process as ap from . import emotion_detector as ed, automation_process as ap
from .logic import id_generator as ig from .logic import id_generator as ig
...@@ -23,10 +28,15 @@ from .logic import pdf_file_generator as pdf ...@@ -23,10 +28,15 @@ from .logic import pdf_file_generator as pdf
from .logic import head_gaze_estimation as hge from .logic import head_gaze_estimation as hge
from .logic import video_extraction as ve from .logic import video_extraction as ve
from . logic import student_behavior_process as sbp from . logic import student_behavior_process as sbp
from .logic.scheduler_tasks import task_scheduler
from .serializers import * from .serializers import *
from braces.views import CsrfExemptMixin from braces.views import CsrfExemptMixin
from django.core.handlers.wsgi import WSGIRequest
from django.http.request import HttpRequest
import datetime import datetime
import os
class LectureViewSet(APIView): class LectureViewSet(APIView):
...@@ -1539,19 +1549,36 @@ class CheckStudentBehaviorAvailability(APIView): ...@@ -1539,19 +1549,36 @@ class CheckStudentBehaviorAvailability(APIView):
def get(self, request): def get(self, request):
video_name = request.query_params.get('video_name') video_name = request.query_params.get('video_name')
# print('video name: ', video_name)
# isActivityExist = LectureActivityFrameGroupings.objects.filter(
# lecture_activity_id__lecture_video_id__video_name=video_name).exists() # retrieve the 'MongoDbJobStore' instance
# jobs = MongoDBJobStore().get_all_jobs()
# isEmotionExist = LectureEmotionFrameGroupings.objects.filter(
# lecture_emotion_id__lecture_video_id__video_name=video_name).exists() print('jobs: ', jobs)
#
# isGazeExist = LectureGazeFrameGroupings.objects.filter( # initialize the variables
# lecture_gaze_id__lecture_video_id__video_name=video_name).exists() isActivityExist = False
isEmotionExist = False
isGazeExist = False
# if there are scheduled jobs
if len(jobs) > 0:
# retrieve the activity frame groupings
isActivityExist = LectureActivityFrameGroupings.objects.filter(lecture_activity_id__lecture_video_id__video_name=video_name).exists()
# retrieve the emotion frame groupings
isEmotionExist = LectureEmotionFrameGroupings.objects.filter(lecture_emotion_id__lecture_video_id__video_name=video_name).exists()
# retrieve the gaze frame groupings
isGazeExist = LectureGazeFrameGroupings.objects.filter(lecture_gaze_id__lecture_video_id__video_name=video_name).exists()
isActivityExist = bool(Random().randint(0,2)) else:
isEmotionExist = bool(Random().randint(0,2))
isGazeExist = bool(Random().randint(0,2)) isActivityExist = True
isEmotionExist = True
isGazeExist = True
# isActivityExist = bool(Random().randint(0,2))
# isEmotionExist = bool(Random().randint(0,2))
# isGazeExist = bool(Random().randint(0,2))
return Response({ return Response({
"isActivityExist": isActivityExist, "isActivityExist": isActivityExist,
...@@ -1657,13 +1684,28 @@ class AutomationProcess(APIView): ...@@ -1657,13 +1684,28 @@ class AutomationProcess(APIView):
}) })
def post(self, request): def post(self, request):
lecturer = request.data['lecturer']
subject = request.data['subject']
subject_code = request.data['subject_code']
video_length = request.data['video_length']
processed = ap.automation_process(lecturer=lecturer, subject=subject, subject_code=subject_code, video_length=video_length) processed = False
return Response({ try :
"is_processed": processed
}) lecturer = request.data['lecturer']
\ No newline at end of file subject = request.data['subject']
subject_code = request.data['subject_code']
video_length = request.data['video_length']
# processed = ap.automation_process(lecturer=lecturer, subject=subject, subject_code=subject_code, video_length=video_length)
# run the scheduler
scheduler = task_scheduler(lecturer=lecturer, subject=subject, subject_code=subject_code, video_length=video_length)
processed = True
return Response({
"is_processed": processed,
})
except Exception as exc:
print('Exception: ', exc)
return Response({
"is_processed": processed,
})
\ No newline at end of file
import requests import requests
import json import json
from background_task import background
from .MongoModels import LectureVideo from .MongoModels import LectureVideo
from . logic import batch_process as bp from . logic import batch_process as bp
...@@ -47,6 +46,7 @@ import datetime ...@@ -47,6 +46,7 @@ import datetime
# return response[0] # return response[0]
# this method will handle the batch processing and video/audio saving pf the system # this method will handle the batch processing and video/audio saving pf the system
from .logic.batch_process import student_behavior_batch_process
from .serializers import LectureVideoSerializer from .serializers import LectureVideoSerializer
...@@ -63,6 +63,7 @@ def automation_process(lecturer, subject, subject_code, video_length="00:20:00") ...@@ -63,6 +63,7 @@ def automation_process(lecturer, subject, subject_code, video_length="00:20:00")
lecturer_video_name = str(current_date) + "_{}_lecturer_video.mp4".format(subject_code) lecturer_video_name = str(current_date) + "_{}_lecturer_video.mp4".format(subject_code)
lecturer_audio_name = str(current_date) + "_{}_lecturer_audio.wav".format(subject_code) lecturer_audio_name = str(current_date) + "_{}_lecturer_audio.wav".format(subject_code)
# this variable will be passed in the individual batch process # this variable will be passed in the individual batch process
student_video_id = 0 student_video_id = 0
...@@ -93,7 +94,7 @@ def automation_process(lecturer, subject, subject_code, video_length="00:20:00") ...@@ -93,7 +94,7 @@ def automation_process(lecturer, subject, subject_code, video_length="00:20:00")
student_video_response = bp.save_student_lecture_video(student_video_content) student_video_response = bp.save_student_lecture_video(student_video_content)
# student_video_response = save_student_lecture_video(student_video_content) # student_video_response = save_student_lecture_video(student_video_content)
print('student video response: ', student_video_response) print('student video response: ', student_video_response)
# student_video_id = student_video_response['id'] student_video_id = student_video_response['id']
# save the lecturer video # save the lecturer video
lecturer_video_response = lbp.save_lecturer_video_details(lecturer_video_content) lecturer_video_response = lbp.save_lecturer_video_details(lecturer_video_content)
...@@ -103,6 +104,13 @@ def automation_process(lecturer, subject, subject_code, video_length="00:20:00") ...@@ -103,6 +104,13 @@ def automation_process(lecturer, subject, subject_code, video_length="00:20:00")
# save the lecturer audio # save the lecturer audio
for i in range(100):
print('outer loop: ', i)
for j in range(10000):
print('inner loop: ', j)
# start the batch processing for lecture summarization component # start the batch processing for lecture summarization component
# lecture_summary_batch_process = lecture_summarization_batch_process(audio_name) # lecture_summary_batch_process = lecture_summarization_batch_process(audio_name)
lecture_summary_batch_process = True lecture_summary_batch_process = True
......
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.mongodb import MongoDBJobStore
import datetime as d
from datetime import datetime
# this method will schedule the automation process task
from FirstApp.automation_process import automation_process
def task_scheduler(lecturer, subject, subject_code, video_length):
jobstores = {
'mongo': MongoDBJobStore(),
}
sched = BackgroundScheduler(jobstores=jobstores)
after_20s = datetime.now() + d.timedelta(seconds=30)
sched.add_job(automation_process, args=[lecturer, subject, subject_code, video_length], trigger='date', run_date=after_20s, id='Automation_1')
sched.start()
job = sched.get_job(job_id='Automation_1')
MongoDBJobStore().add_job(job=job)
return sched
\ No newline at end of file
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
$(document).ready(function () { $(document).ready(function () {
let folder = ''; let folder = '';
{#$('#activity_loader').attr('hidden', false);#} $('#activity_loader').attr('hidden', false);
{#$('#emotion_loader').attr('hidden', false);#} {#$('#emotion_loader').attr('hidden', false);#}
{#$('#gaze_loader').attr('hidden', false);#} {#$('#gaze_loader').attr('hidden', false);#}
...@@ -297,20 +297,30 @@ ...@@ -297,20 +297,30 @@
} }
} }
//this is a test function (delete later)
/* /*
let interval = setInterval(() => { setInterval(() => {
let time = new Date().getTime();
alert('time: ', time);
}, 5000);
*/
//this is a test function (delete later)
//get the due lecture video name
var due_lecture_video_name = "{{ due_lecture_video_name }}";
let interval = setInterval(() => {
{#let url = 'http://127.0.0.1:8000/get-random_number';#} let url = 'http://127.0.0.1:8000/check-availability/?video_name=' + due_lecture_video_name;
let url = 'http://127.0.0.1:8000/check-availability';
fetch(url) fetch(url)
.then((res) => res.json()) .then((res) => res.json())
.then((out) => displayProcess(out)) .then((out) => displayProcess(out))
.catch((err) => alert('error: ' + err)) .catch((err) => alert('error: ' + err))
}, 10000); }, 5000);
//this function will handle the displaying loaders and status in the workflow //this function will handle the displaying loaders and status in the workflow
...@@ -356,7 +366,8 @@ ...@@ -356,7 +366,8 @@
} }
*/
}); });
...@@ -440,11 +451,12 @@ ...@@ -440,11 +451,12 @@
<td class="font-weight-bolder">{{ lecture.start_time }}</td> <td class="font-weight-bolder">{{ lecture.start_time }}</td>
<td class="font-weight-bolder">{{ lecture.end_time }}</td> <td class="font-weight-bolder">{{ lecture.end_time }}</td>
<td> <td>
<button type="button" class="btn btn-success batch_process" <span class="font-italic text-success">Processing</span>
data-video-id="{{ lecture.video_id }}" {# <button type="button" class="btn btn-success batch_process"#}
data-video-name="{{ lecture.video_name }}" {# data-video-id="{{ lecture.video_id }}"#}
id="{{ lecture.subject }}">Process {# data-video-name="{{ lecture.video_name }}"#}
</button> {# id="{{ lecture.subject }}">Process#}
{# </button>#}
{# <span class="font-italic font-weight-bolder text-success">Processing</span>#} {# <span class="font-italic font-weight-bolder text-success">Processing</span>#}
</td> </td>
</tr> </tr>
......
...@@ -33,8 +33,14 @@ from django.contrib.auth.decorators import login_required ...@@ -33,8 +33,14 @@ from django.contrib.auth.decorators import login_required
from . serializers import * from . serializers import *
from . forms import * from . forms import *
import os import os
import datetime as d
from datetime import datetime from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.date import DateTrigger
from apscheduler.jobstores.mongodb import MongoDBJobStore
# Create your views here. # Create your views here.
...@@ -54,6 +60,11 @@ def hello(request): ...@@ -54,6 +60,11 @@ def hello(request):
print('user_type: ', user_type) print('user_type: ', user_type)
print('request type: ', type(request))
# test the scheduler
# test_scheduler()
# retrieve the lecturer's timetable slots # retrieve the lecturer's timetable slots
lecturer_timetable = FacultyTimetable.objects.filter() lecturer_timetable = FacultyTimetable.objects.filter()
...@@ -123,6 +134,7 @@ def hello(request): ...@@ -123,6 +134,7 @@ def hello(request):
return redirect('/401') return redirect('/401')
except Exception as exc: except Exception as exc:
print('exception: ', exc)
return redirect('/500') return redirect('/500')
# this method will handle 404 error page # this method will handle 404 error page
...@@ -270,13 +282,17 @@ def video_result(request): ...@@ -270,13 +282,17 @@ def video_result(request):
# handling the general exceptions # handling the general exceptions
except Exception as exc: except Exception as exc:
print('what is wrong?: ', exc) print('Exception: ', exc)
return redirect('/500') return redirect('/500')
print('due lectures: ', due_lecture_list) print('due lectures: ', due_lecture_list)
due_lecture_video_name = due_lecture_list[0]['video_name'] if len(due_lecture_list) > 0 else "Test.mp4"
# due_lecture_video_name = "Test.mp4"
print('due lecture video name: ', due_lecture_video_name)
return render(request, "FirstApp/video_results.html", return render(request, "FirstApp/video_results.html",
{"lecturer": lecturer, "due_lectures": due_lecture_list}) {"lecturer": lecturer, "due_lectures": due_lecture_list, "due_lecture_video_name": due_lecture_video_name})
# view for emotion page # view for emotion page
...@@ -382,6 +398,7 @@ def activity(request): ...@@ -382,6 +398,7 @@ def activity(request):
# handling the general exception # handling the general exception
except Exception as exc: except Exception as exc:
print('exception: ', exc)
return redirect('/500') return redirect('/500')
return render(request, "FirstApp/activity.html", {"lecturer_subjects": lecturer_subjects, "subjects": subject_list, "lecturer": lecturer}) return render(request, "FirstApp/activity.html", {"lecturer_subjects": lecturer_subjects, "subjects": subject_list, "lecturer": lecturer})
......
...@@ -44,7 +44,6 @@ INSTALLED_APPS = [ ...@@ -44,7 +44,6 @@ INSTALLED_APPS = [
'bootstrap4', 'bootstrap4',
'rest_framework', 'rest_framework',
'os', 'os',
'background_task'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment