feat: 上报时包含版本信息

This commit is contained in:
Rock Chin
2023-01-05 15:50:45 +08:00
parent 584cacba6c
commit 948b0f4df9
2 changed files with 26 additions and 4 deletions

View File

@@ -5,8 +5,7 @@ import logging
import requests import requests
import pkg.utils.context import pkg.utils.context
import pkg.utils.updater
version = "0.1.0"
class DataGatherer: class DataGatherer:
@@ -21,15 +20,21 @@ class DataGatherer:
} }
}为值的字典""" }为值的字典"""
version_str = "0.1.0"
def __init__(self): def __init__(self):
self.load_from_db() self.load_from_db()
try:
self.version_str = pkg.utils.updater.get_commit_id_and_time_and_msg()
except:
pass
def report_to_server(self, subservice_name: str, count: int): def report_to_server(self, subservice_name: str, count: int):
try: try:
config = pkg.utils.context.get_config() config = pkg.utils.context.get_config()
if hasattr(config, "report_usage") and not config.report_usage: if hasattr(config, "report_usage") and not config.report_usage:
return return
res = requests.get("http://rockchin.top:18989/usage?service_name=qchatgpt.{}&version={}&count={}".format(subservice_name, version, count)) res = requests.get("http://rockchin.top:18989/usage?service_name=qchatgpt.{}&version={}&count={}".format(subservice_name, self.version_str, count))
if res.status_code != 200 or res.text != "ok": if res.status_code != 200 or res.text != "ok":
logging.warning("report to server failed, status_code: {}, text: {}".format(res.status_code, res.text)) logging.warning("report to server failed, status_code: {}, text: {}".format(res.status_code, res.text))
except: except:

View File

@@ -1,5 +1,4 @@
import datetime import datetime
import time
def update_all(): def update_all():
@@ -41,3 +40,21 @@ def get_current_version_info() -> str:
break break
return version_str return version_str
def get_commit_id_and_time_and_msg() -> str:
"""获取当前提交id和时间和提交信息"""
try:
import dulwich
except ModuleNotFoundError:
raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77")
from dulwich import porcelain
repo = porcelain.open_repo('.')
for entry in repo.get_walker():
tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600))
dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz)
return str(entry.commit.id)[2:9] + " " + dt.strftime('%Y-%m-%d %H:%M:%S') + " [" + str(entry.commit.message, encoding="utf-8").strip()+"]"