|
import os |
|
import gradio as gr |
|
import pandas as pd |
|
from datetime import datetime |
|
import time |
|
from azure_count_ip_data import count_files_per_ip |
|
from log_reader import RemoteLogReader |
|
import threading |
|
|
|
|
|
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
|
STATS_FILE = os.path.join(DATA_DIR, 'battle_stats.csv') |
|
LAST_UPDATE_FILE = os.path.join(DATA_DIR, 'last_update.txt') |
|
|
|
|
|
os.makedirs(DATA_DIR, exist_ok=True) |
|
|
|
def save_stats(df, current_time): |
|
"""Save statistics and last update time to files""" |
|
df.to_csv(STATS_FILE, index=False) |
|
with open(LAST_UPDATE_FILE, 'w') as f: |
|
f.write(current_time) |
|
|
|
def load_stats(): |
|
"""Load statistics and last update time from files""" |
|
try: |
|
df = pd.read_csv(STATS_FILE) |
|
with open(LAST_UPDATE_FILE, 'r') as f: |
|
last_update = f.read().strip() |
|
return df, last_update |
|
except (FileNotFoundError, pd.errors.EmptyDataError): |
|
return pd.DataFrame(columns=['Annotator', 'Battle Count']), "" |
|
|
|
def update_stats(): |
|
"""Get the latest battle statistics""" |
|
try: |
|
|
|
reader = RemoteLogReader() |
|
|
|
|
|
ip_counts = count_files_per_ip(reader) |
|
|
|
|
|
df = pd.DataFrame(list(ip_counts.items()), columns=['Annotator', 'Battle Count']) |
|
df = df.sort_values('Battle Count', ascending=False) |
|
|
|
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
|
|
|
|
save_stats(df, current_time) |
|
|
|
return df, current_time |
|
except Exception as e: |
|
print(f"Error updating stats: {e}") |
|
return pd.DataFrame(columns=['Annotator', 'Battle Count']), "" |
|
|
|
def auto_update(state): |
|
"""Background task to update stats every hour""" |
|
while state['running']: |
|
state['stats'], state['last_update'] = update_stats() |
|
time.sleep(3600) |
|
|
|
def create_ui(): |
|
state = {'running': True} |
|
|
|
|
|
state['stats'], state['last_update'] = load_stats() |
|
|
|
|
|
if state['stats'].empty: |
|
state['stats'], state['last_update'] = update_stats() |
|
|
|
|
|
update_thread = threading.Thread(target=auto_update, args=(state,)) |
|
update_thread.daemon = True |
|
update_thread.start() |
|
|
|
def get_current_stats(): |
|
return state['stats'] |
|
|
|
def get_last_update(): |
|
return state['last_update'] |
|
|
|
def manual_refresh(): |
|
state['stats'], state['last_update'] = update_stats() |
|
return state['stats'], state['last_update'] |
|
|
|
with gr.Blocks(title="Battle Count Statistics") as app: |
|
gr.Markdown("# Battle Count Statistics") |
|
gr.Markdown("Displays the count of valid battles per IP address. Updates automatically every hour.") |
|
|
|
with gr.Row(): |
|
last_update = gr.Textbox( |
|
value=get_last_update, |
|
label="Last Updated", |
|
interactive=False |
|
) |
|
|
|
with gr.Row(): |
|
output = gr.DataFrame( |
|
value=get_current_stats, |
|
interactive=False, |
|
wrap=True, |
|
) |
|
|
|
|
|
|
|
|
|
return app |
|
|
|
if __name__ == "__main__": |
|
app = create_ui() |
|
app.launch() |