Spaces:
Running
Running
ChenyuRabbitLove
commited on
Commit
โข
83e7365
1
Parent(s):
d4ccbc6
feat: add load and save player data process
Browse files- app.py +4 -0
- utils/utils.py +52 -1
app.py
CHANGED
@@ -10,6 +10,7 @@ from utils.utils import (
|
|
10 |
get_player_adventure_logs_html,
|
11 |
get_player_achievements,
|
12 |
get_current_story,
|
|
|
13 |
)
|
14 |
|
15 |
seafoam = Seafoam()
|
@@ -96,6 +97,7 @@ with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
|
|
96 |
player_info_query_btn = gr.Button(
|
97 |
"Query", elem_id="trigger_button", visible=False
|
98 |
)
|
|
|
99 |
|
100 |
# actions when player login
|
101 |
player_info_query_btn.click(get_player_info, player_backend_id, player_info).then(
|
@@ -112,5 +114,7 @@ with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
|
|
112 |
player_info_query_btn.click(get_current_story, None, adventure)
|
113 |
)
|
114 |
|
|
|
|
|
115 |
if __name__ == "__main__":
|
116 |
demo.launch()
|
|
|
10 |
get_player_adventure_logs_html,
|
11 |
get_player_achievements,
|
12 |
get_current_story,
|
13 |
+
save_latest_player_data,
|
14 |
)
|
15 |
|
16 |
seafoam = Seafoam()
|
|
|
97 |
player_info_query_btn = gr.Button(
|
98 |
"Query", elem_id="trigger_button", visible=False
|
99 |
)
|
100 |
+
pull_newest_player_info_btn = gr.Button("", visible=False)
|
101 |
|
102 |
# actions when player login
|
103 |
player_info_query_btn.click(get_player_info, player_backend_id, player_info).then(
|
|
|
114 |
player_info_query_btn.click(get_current_story, None, adventure)
|
115 |
)
|
116 |
|
117 |
+
pull_newest_player_info_btn.click(save_latest_player_data, None, None)
|
118 |
+
|
119 |
if __name__ == "__main__":
|
120 |
demo.launch()
|
utils/utils.py
CHANGED
@@ -1,12 +1,28 @@
|
|
|
|
1 |
import json
|
2 |
-
|
|
|
3 |
from typing import List
|
|
|
4 |
|
5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
6 |
|
7 |
MEDIA_PATH = "medias/"
|
8 |
MEDIA_FILE_TYPE = ".png"
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def get_content(file_name: str) -> str:
|
12 |
with open(file_name, "r", encoding="utf-8") as file:
|
@@ -127,3 +143,38 @@ def get_current_story():
|
|
127 |
interactive=False,
|
128 |
info="็่ฒ่ฒ่ๅ
ๆๅฎ่ญท่
็ๆ
็จๅฐๆผ 2023/12/04 ้ๅง๏ผๆฌ่ซๆๅพ
๏ผ",
|
129 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import json
|
3 |
+
import logging
|
4 |
+
from datetime import datetime, timedelta, date
|
5 |
from typing import List
|
6 |
+
from dataclasses import asdict
|
7 |
|
8 |
import gradio as gr
|
9 |
+
from google.oauth2.service_account import Credentials
|
10 |
+
from google.cloud import bigquery
|
11 |
+
|
12 |
+
from mes_player_model import Player
|
13 |
|
14 |
MEDIA_PATH = "medias/"
|
15 |
MEDIA_FILE_TYPE = ".png"
|
16 |
|
17 |
+
SCOPES = ["https://www.googleapis.com/auth/bigquery"]
|
18 |
+
SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
|
19 |
+
service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
|
20 |
+
|
21 |
+
creds = Credentials.from_service_account_info(
|
22 |
+
service_account_info_dict, scopes=SCOPES
|
23 |
+
)
|
24 |
+
client = bigquery.Client(credentials=creds, project=service_account_info_dict['project_id'])
|
25 |
+
|
26 |
|
27 |
def get_content(file_name: str) -> str:
|
28 |
with open(file_name, "r", encoding="utf-8") as file:
|
|
|
143 |
interactive=False,
|
144 |
info="็่ฒ่ฒ่ๅ
ๆๅฎ่ญท่
็ๆ
็จๅฐๆผ 2023/12/04 ้ๅง๏ผๆฌ่ซๆๅพ
๏ผ",
|
145 |
)
|
146 |
+
|
147 |
+
|
148 |
+
def query_bq_table(client, sql):
|
149 |
+
try:
|
150 |
+
query_job = client.query(sql)
|
151 |
+
query_job.result()
|
152 |
+
return query_job.to_dataframe()
|
153 |
+
except Exception as e:
|
154 |
+
logging.error(f"Query Failed: {e}")
|
155 |
+
raise
|
156 |
+
|
157 |
+
def load_player_statuses(client, date_str):
|
158 |
+
table_name = f"mes_report_{date_str}"
|
159 |
+
sql = f"SELECT * FROM `data_mart.{table_name}`"
|
160 |
+
return {
|
161 |
+
row["player_backend_user_id"]: Player.from_dict(row)
|
162 |
+
for _, row in query_bq_table(client, sql).iterrows()
|
163 |
+
}
|
164 |
+
|
165 |
+
def get_date_strs(delta_days=0):
|
166 |
+
target_date = datetime.now().date() - timedelta(days=delta_days)
|
167 |
+
return target_date.strftime("%Y%m%d")
|
168 |
+
|
169 |
+
def save_latest_player_data():
|
170 |
+
latest_player_data = load_player_statuses(client, '20231113')
|
171 |
+
latest_player_data_as_dict = {key: asdict(value) for key, value in latest_player_data.items()}
|
172 |
+
|
173 |
+
def date_serializer(obj):
|
174 |
+
if isinstance(obj, date):
|
175 |
+
return obj.isoformat()
|
176 |
+
raise TypeError("Type not serializable")
|
177 |
+
|
178 |
+
with open('latest_player_data.json', 'w') as fp:
|
179 |
+
print("Saving latest player data...")
|
180 |
+
json.dump(latest_player_data_as_dict, fp, default=date_serializer)
|