Skip to content

Commit

Permalink
add metrics (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Dec 6, 2024
1 parent b671bce commit 1283fa1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion YYeTsFE
7 changes: 5 additions & 2 deletions yyetsweb/databases/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def get_grafana_data(self, date_series) -> str:


class Metrics(Mongo):
def set_metrics(self, metrics_type: str):
def set_metrics(self, metrics_type: str, data: str):
today = time.strftime("%Y-%m-%d", time.localtime())
self.db["metrics"].update_one({"date": today}, {"$inc": {metrics_type: 1}}, upsert=True)
if metrics_type == "viewSubtitle":
self.db["subtitle"].find_one_and_update({"id": data}, {"$inc": {"views": 1}})
else:
self.db["metrics"].update_one({"date": today}, {"$inc": {metrics_type: 1}}, upsert=True)

def get_metrics(self, from_date: str, to_date: str) -> dict:
start_int = [int(i) for i in from_date.split("-")]
Expand Down
19 changes: 17 additions & 2 deletions yyetsweb/databases/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from databases.comment import CommentSearch


class SubtitleDownload(Mongo):
def add_download(self, _id):
self.db["subtitle"].find_one_and_update({"id": _id}, {"$inc": {"downloads": 1}})


class Resource(SearchEngine):
def fansub_search(self, class_name: str, kw: str):
class_ = globals().get(class_name)
Expand Down Expand Up @@ -79,8 +84,18 @@ def search_resource():
data = self.db["yyets"].find(
{
"$or": [
{"data.info.cnname": {"$regex": f".*{keyword}.*", "$options": "i"}},
{"data.info.enname": {"$regex": f".*{keyword}.*", "$options": "i"}},
{
"data.info.cnname": {
"$regex": f".*{keyword}.*",
"$options": "i",
}
},
{
"data.info.enname": {
"$regex": f".*{keyword}.*",
"$options": "i",
}
},
{
"data.info.aliasname": {
"$regex": f".*{keyword}.*",
Expand Down
3 changes: 2 additions & 1 deletion yyetsweb/handlers/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class MetricsHandler(BaseHandler):
def set_metrics(self):
payload = self.json
metrics_type = payload.get("type", self.get_query_argument("type", "unknown"))
_id = payload.get("id")

self.instance.set_metrics(metrics_type)
self.instance.set_metrics(metrics_type, _id)
self.set_status(HTTPStatus.CREATED)
return {}

Expand Down
4 changes: 4 additions & 0 deletions yyetsweb/handlers/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@


class SubtitleDownloadHandler(BaseHandler):
filename = filename

@run_on_executor()
def find_and_download(self):
file = self.json.get("file")
_id = self.json.get("id")
self.set_header("x-filename", Path(file).name)
p = Path(__file__).parent.parent.joinpath("subtitle_data", file)
self.set_header("Content-Type", "application/bin")
try:
data = p.read_bytes()
self.instance.add_download(_id)
return data
except FileNotFoundError:
self.set_status(HTTPStatus.NOT_FOUND)
Expand Down

0 comments on commit 1283fa1

Please sign in to comment.