fix: Commonified function and updated scheduler events

Co-authored-by: marination <maricadsouza221197@gmail.com>
This commit is contained in:
Gavin D'souza 2020-09-10 13:57:44 +05:30
parent 0a0e258d8b
commit 71ac399369
2 changed files with 19 additions and 26 deletions

View File

@ -284,7 +284,7 @@ auto_cancel_exempted_doctypes= [
scheduler_events = { scheduler_events = {
"cron": { "cron": {
"0/30 * * * *": [ "0/30 * * * *": [
"erpnext.utilities.doctype.video.video.update_youtube_data_half_hourly", "erpnext.utilities.doctype.video.video.update_youtube_data",
] ]
}, },
"all": [ "all": [
@ -302,7 +302,6 @@ scheduler_events = {
"erpnext.projects.doctype.project.project.collect_project_status", "erpnext.projects.doctype.project.project.collect_project_status",
"erpnext.hr.doctype.shift_type.shift_type.process_auto_attendance_for_all_shifts", "erpnext.hr.doctype.shift_type.shift_type.process_auto_attendance_for_all_shifts",
"erpnext.support.doctype.issue.issue.set_service_level_agreement_variance", "erpnext.support.doctype.issue.issue.set_service_level_agreement_variance",
"erpnext.utilities.doctype.video.video.update_youtube_data"
], ],
"daily": [ "daily": [
"erpnext.stock.reorder_item.reorder_item", "erpnext.stock.reorder_item.reorder_item",

View File

@ -50,45 +50,37 @@ class Video(Document):
title = "Failed to Update YouTube Statistics for Video: {0}".format(self.name) title = "Failed to Update YouTube Statistics for Video: {0}".format(self.name)
frappe.log_error(title + "\n\n" + frappe.get_traceback(), title=title) frappe.log_error(title + "\n\n" + frappe.get_traceback(), title=title)
def is_tracking_enabled(): def is_tracking_enabled():
return frappe.db.get_single_value("Video Settings", "enable_youtube_tracking") return frappe.db.get_single_value("Video Settings", "enable_youtube_tracking")
def get_frequency(value):
if not value:
return None
# Return frequency in hours def get_frequency(value):
# Return numeric value from frequency field, return 1 as fallback default value: 1 hour
if value != "Daily": if value != "Daily":
return frappe.utils.cint(value[:2].strip()) return frappe.utils.cint(value[:2].strip())
else: elif value:
# 24 hours for Daily
return 24 return 24
return 1
def update_youtube_data_half_hourly():
# Called every 30 mins via hooks
frequency = get_frequency(frappe.db.get_single_value("Video Settings", "frequency"))
if not is_tracking_enabled() or not frequency:
return
if frequency == 30:
batch_update_youtube_data()
def update_youtube_data(): def update_youtube_data():
# Called every hour via hooks # Called every 30 minutes via hooks
frequency = get_frequency(frappe.db.get_single_value("Video Settings", "frequency")) enable_youtube_tracking, frequency = frappe.db.get_value("Video Settings", "Video Settings", ["enable_youtube_tracking", "frequency"])
# if frequency is 30 mins dont proceed, as its handled in another method if not enable_youtube_tracking:
if not is_tracking_enabled() or not frequency or frequency == 30:
return return
frequency = get_frequency(frequency)
time = datetime.now() time = datetime.now()
timezone = pytz.timezone(frappe.utils.get_time_zone()) timezone = pytz.timezone(frappe.utils.get_time_zone())
site_time = time.astimezone(timezone) site_time = time.astimezone(timezone)
if site_time.hour % frequency == 0: if frequency == 30:
batch_update_youtube_data() batch_update_youtube_data()
elif site_time.hour % frequency == 0:
batch_update_youtube_data()
def get_formatted_ids(video_list): def get_formatted_ids(video_list):
# format ids to comma separated string for bulk request # format ids to comma separated string for bulk request
@ -98,6 +90,7 @@ def get_formatted_ids(video_list):
return ','.join(ids) return ','.join(ids)
@frappe.whitelist() @frappe.whitelist()
def get_id_from_url(url): def get_id_from_url(url):
""" """
@ -128,7 +121,8 @@ def batch_update_youtube_data():
'like_count' : video_stats.get('likeCount'), 'like_count' : video_stats.get('likeCount'),
'view_count' : video_stats.get('viewCount'), 'view_count' : video_stats.get('viewCount'),
'dislike_count' : video_stats.get('dislikeCount'), 'dislike_count' : video_stats.get('dislikeCount'),
'comment_count' : video_stats.get('commentCount') 'comment_count' : video_stats.get('commentCount'),
'video_id': video_id
} }
frappe.db.sql(""" frappe.db.sql("""
@ -138,7 +132,7 @@ def batch_update_youtube_data():
view_count = %(view_count)s, view_count = %(view_count)s,
dislike_count = %(dislike_count)s, dislike_count = %(dislike_count)s,
comment_count = %(comment_count)s comment_count = %(comment_count)s
WHERE youtube_video_id = '{0}'""".format(video_id), stats) WHERE youtube_video_id = %(video_id)s""", stats)
video_list = frappe.get_all("Video", fields=["youtube_video_id"]) video_list = frappe.get_all("Video", fields=["youtube_video_id"])
if len(video_list) > 50: if len(video_list) > 50:
@ -150,4 +144,4 @@ def batch_update_youtube_data():
start += 50 start += 50
end += 50 end += 50
else: else:
prepare_and_set_data(video_list) prepare_and_set_data(video_list)