add metrics (#174)

This commit is contained in:
Benny
2024-12-06 17:27:19 +00:00
parent b671bcef6a
commit 1283fa1e42
5 changed files with 29 additions and 6 deletions

Submodule YYeTsFE updated: 3c57d80ec7...af2104915f

View File

@@ -16,9 +16,12 @@ class GrafanaQuery(Mongo):
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("-")]

View File

@@ -14,6 +14,11 @@ from databases.base import Mongo, Redis, SearchEngine
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)
@@ -79,8 +84,18 @@ class Resource(SearchEngine):
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}.*",

View File

@@ -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 {}

View File

@@ -13,14 +13,18 @@ filename = Path(__file__).name.split(".")[0]
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)