2020-08-02 00:05:45 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-08-02 00:05:45 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2020-08-02 10:56:36 +00:00
|
|
|
from frappe.utils import flt
|
2020-08-02 00:05:45 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-08-02 00:05:45 +00:00
|
|
|
def execute(filters=None):
|
2020-08-02 10:56:36 +00:00
|
|
|
if not frappe.db.get_single_value("Video Settings", "enable_youtube_tracking") or not filters:
|
|
|
|
return [], []
|
|
|
|
|
2020-08-02 00:05:45 +00:00
|
|
|
columns = get_columns()
|
2020-08-02 10:56:36 +00:00
|
|
|
data = get_data(filters)
|
|
|
|
chart_data, summary = get_chart_summary_data(data)
|
|
|
|
return columns, data, None, chart_data, summary
|
2020-08-02 00:05:45 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-08-02 00:05:45 +00:00
|
|
|
def get_columns():
|
|
|
|
return [
|
|
|
|
{"label": _("Published Date"), "fieldname": "publish_date", "fieldtype": "Date", "width": 100},
|
|
|
|
{"label": _("Title"), "fieldname": "title", "fieldtype": "Data", "width": 200},
|
2020-08-02 10:56:36 +00:00
|
|
|
{"label": _("Duration"), "fieldname": "duration", "fieldtype": "Duration", "width": 100},
|
2020-08-02 00:05:45 +00:00
|
|
|
{"label": _("Views"), "fieldname": "view_count", "fieldtype": "Float", "width": 200},
|
|
|
|
{"label": _("Likes"), "fieldname": "like_count", "fieldtype": "Float", "width": 200},
|
|
|
|
{"label": _("Dislikes"), "fieldname": "dislike_count", "fieldtype": "Float", "width": 100},
|
2020-08-02 10:56:36 +00:00
|
|
|
{"label": _("Comments"), "fieldname": "comment_count", "fieldtype": "Float", "width": 100},
|
2020-08-02 00:05:45 +00:00
|
|
|
]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-08-02 10:56:36 +00:00
|
|
|
def get_data(filters):
|
2020-08-02 00:05:45 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT
|
2020-08-02 10:56:36 +00:00
|
|
|
publish_date, title, provider, duration,
|
2020-08-02 00:05:45 +00:00
|
|
|
view_count, like_count, dislike_count, comment_count
|
|
|
|
FROM `tabVideo`
|
|
|
|
WHERE view_count is not null
|
2020-08-02 10:56:36 +00:00
|
|
|
and publish_date between %(from_date)s and %(to_date)s
|
|
|
|
ORDER BY view_count desc""",
|
|
|
|
filters,
|
|
|
|
as_dict=1,
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-08-02 10:56:36 +00:00
|
|
|
|
|
|
|
def get_chart_summary_data(data):
|
|
|
|
labels, likes, views = [], [], []
|
|
|
|
total_views = 0
|
|
|
|
|
|
|
|
for row in data:
|
|
|
|
labels.append(row.get("title"))
|
|
|
|
likes.append(row.get("like_count"))
|
|
|
|
views.append(row.get("view_count"))
|
|
|
|
total_views += flt(row.get("view_count"))
|
|
|
|
|
|
|
|
chart_data = {
|
|
|
|
"data": {
|
|
|
|
"labels": labels,
|
|
|
|
"datasets": [{"name": "Likes", "values": likes}, {"name": "Views", "values": views}],
|
|
|
|
},
|
|
|
|
"type": "bar",
|
|
|
|
"barOptions": {"stacked": 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
summary = [
|
|
|
|
{
|
|
|
|
"value": total_views,
|
|
|
|
"indicator": "Blue",
|
|
|
|
"label": "Total Views",
|
|
|
|
"datatype": "Float",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
return chart_data, summary
|