File size: 7,002 Bytes
5ac9254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import json
from datetime import datetime, timezone
from typing import Dict, Any
from nc_py_api import Nextcloud
import arena_config
from leaderboard import load_leaderboard, get_human_readable_name, get_model_size

def get_internal_stats() -> Dict[str, Any]:
    leaderboard = load_leaderboard()
    
    total_battles = sum(
        model_data['wins'] + model_data['losses']
        for model_data in leaderboard.values()
    )
    
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    active_models = len(leaderboard)
    
    most_battles = max(
        (model_data['wins'] + model_data['losses'], model)
        for model, model_data in leaderboard.items()
    )
    
    highest_win_rate = max(
        (model_data['wins'] / (model_data['wins'] + model_data['losses']) if (model_data['wins'] + model_data['losses']) > 0 else 0, model)
        for model, model_data in leaderboard.items()
    )
    
    most_diverse_opponent = max(
        (len(model_data['opponents']), model)
        for model, model_data in leaderboard.items()
    )
    
    stats = {
        "timestamp": timestamp,
        "total_battles": total_battles,
        "active_models": active_models,
        "most_battles": {
            "model": get_human_readable_name(most_battles[1]),
            "battles": most_battles[0]
        },
        "highest_win_rate": {
            "model": get_human_readable_name(highest_win_rate[1]),
            "win_rate": f"{highest_win_rate[0]:.2%}"
        },
        "most_diverse_opponent": {
            "model": get_human_readable_name(most_diverse_opponent[1]),
            "unique_opponents": most_diverse_opponent[0]
        }
    }
    
    return stats

def save_internal_stats(stats: Dict[str, Any]) -> bool:
    nc = Nextcloud(
        nextcloud_url=arena_config.NEXTCLOUD_URL,
        nc_auth_user=arena_config.NEXTCLOUD_USERNAME,
        nc_auth_pass=arena_config.NEXTCLOUD_PASSWORD
    )
    
    try:
        json_data = json.dumps(stats, indent=2)
        nc.files.upload(arena_config.NEXTCLOUD_INTERNAL_STATS_PATH, json_data.encode('utf-8'))
        return True
    except Exception as e:
        print(f"Error saving internal stats to Nextcloud: {str(e)}")
        return False

def save_local_stats(stats: Dict[str, Any], filename: str = "internal_stats.json") -> bool:
    try:
        with open(filename, 'w') as f:
            json.dump(stats, f, indent=2)
        return True
    except Exception as e:
        print(f"Error saving internal stats to local file: {str(e)}")
        return False

def get_fun_stats() -> Dict[str, Any]:
    leaderboard = load_leaderboard()
    
    total_battles = sum(
        model_data['wins'] + model_data['losses']
        for model_data in leaderboard.values()
    )
    
    timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
    
    active_models = len(leaderboard)
    
    most_battles = max(
        (model_data['wins'] + model_data['losses'], model)
        for model, model_data in leaderboard.items()
    )
    
    highest_win_rate = max(
        (model_data['wins'] / (model_data['wins'] + model_data['losses']) if (model_data['wins'] + model_data['losses']) > 0 else 0, model)
        for model, model_data in leaderboard.items()
    )
    
    most_diverse_opponent = max(
        (len(model_data['opponents']), model)
        for model, model_data in leaderboard.items()
    )
    
    # Existing fun stats
    underdog_champion = min(
        ((get_model_size(model), model_data['wins'] / (model_data['wins'] + model_data['losses'])) if (model_data['wins'] + model_data['losses']) > 0 else (get_model_size(model), 0), model)
        for model, model_data in leaderboard.items()
    )
    
    most_consistent = min(
        (abs(model_data['wins'] - model_data['losses']), model)
        for model, model_data in leaderboard.items()
        if (model_data['wins'] + model_data['losses']) > 10  # Minimum battles threshold
    )
    
    biggest_rivalry = max(
        (results['wins'] + results['losses'], (model, opponent))
        for model, data in leaderboard.items()
        for opponent, results in data['opponents'].items()
    )
    
    # New fun stats
    david_vs_goliath = max(
        ((get_model_size(opponent) - get_model_size(model), model_data['opponents'][opponent]['wins']), (model, opponent))
        for model, model_data in leaderboard.items()
        for opponent in model_data['opponents']
        if get_model_size(opponent) > get_model_size(model) and model_data['opponents'][opponent]['wins'] > 0
    )
    
    comeback_king = max(
        (model_data['wins'] - model_data['losses'], model)
        for model, model_data in leaderboard.items()
        if model_data['losses'] > model_data['wins']
    )
    
    pyrrhic_victor = min(
        (model_data['wins'] / (model_data['wins'] + model_data['losses']) if (model_data['wins'] + model_data['losses']) > 0 else float('inf'), model)
        for model, model_data in leaderboard.items()
        if model_data['wins'] > model_data['losses'] and (model_data['wins'] + model_data['losses']) > 10
    )
    
    stats = {
        "timestamp": timestamp,
        "total_battles": total_battles,
        "active_models": active_models,
        "most_battles": {
            "model": get_human_readable_name(most_battles[1]),
            "battles": most_battles[0]
        },
        "highest_win_rate": {
            "model": get_human_readable_name(highest_win_rate[1]),
            "win_rate": f"{highest_win_rate[0]:.2%}"
        },
        "most_diverse_opponent": {
            "model": get_human_readable_name(most_diverse_opponent[1]),
            "unique_opponents": most_diverse_opponent[0]
        },
        "underdog_champion": {
            "model": get_human_readable_name(underdog_champion[1]),
            "size": f"{underdog_champion[0][0]}B",
            "win_rate": f"{underdog_champion[0][1]:.2%}"
        },
        "most_consistent": {
            "model": get_human_readable_name(most_consistent[1]),
            "win_loss_difference": most_consistent[0]
        },
        "biggest_rivalry": {
            "model1": get_human_readable_name(biggest_rivalry[1][0]),
            "model2": get_human_readable_name(biggest_rivalry[1][1]),
            "total_battles": biggest_rivalry[0]
        },
        "david_vs_goliath": {
            "david": get_human_readable_name(david_vs_goliath[1][0]),
            "goliath": get_human_readable_name(david_vs_goliath[1][1]),
            "size_difference": f"{david_vs_goliath[0][0]:.1f}B",
            "wins": david_vs_goliath[0][1]
        },
        "comeback_king": {
            "model": get_human_readable_name(comeback_king[1]),
            "comeback_margin": comeback_king[0]
        },
        "pyrrhic_victor": {
            "model": get_human_readable_name(pyrrhic_victor[1]),
            "win_rate": f"{pyrrhic_victor[0]:.2%}"
        }
    }
    
    return stats

if __name__ == "__main__":
    stats = get_internal_stats()