-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·144 lines (107 loc) · 4.36 KB
/
app.py
File metadata and controls
executable file
·144 lines (107 loc) · 4.36 KB
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
from collections import OrderedDict
from typing import Any, Dict, Optional
from urllib.parse import quote
from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from db import RepoDB
app = FastAPI(title="Git Leaderboard", version="1.0", openapi_url=None, docs_url=None, redoc_url=None)
DB_PATH = "repos.db"
LEADERBOARD_PAGE_SIZE = 100
IS_PROD = True
db = RepoDB(DB_PATH)
class LRUCache:
def __init__(self, max_size: int):
self.max_size = max_size
self.cache: OrderedDict[str, Any] = OrderedDict()
def get(self, key: str) -> Optional[Any]:
if key in self.cache:
self.cache.move_to_end(key)
return self.cache[key]
return None
def set(self, key: str, value: Any):
self.cache[key] = value
if len(self.cache) > self.max_size:
self.cache.popitem(last=False)
response_cache = LRUCache(max_size=10_000)
@app.on_event("shutdown")
def _shutdown() -> None:
db.close()
if not IS_PROD:
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def home() -> FileResponse:
return FileResponse("index.html")
@app.get("/api/leaderboard")
def leaderboard(
metric: str = Query("stars", description="stars|forks|watchers|diskUsage|trending24h|trending3d|trending7d|trending30d"),
page: int = Query(1, ge=1),
q: Optional[str] = Query(None, description="Search in name/description"),
in_description: bool = Query(True),
language: Optional[str] = None,
topic: Optional[str] = None,
) -> Dict[str, Any]:
cache_key = f"lb:{metric}:{page}:{q}:{in_description}:{language}:{topic}"
if cached := response_cache.get(cache_key):
return cached
try:
total = db.count_leaderboard(q=q, in_description=in_description, language=language, topic=topic)
items = db.leaderboard(
metric=metric,
page=page,
q=q,
in_description=in_description,
language=language,
topic=topic,
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
result = {
"page": int(page),
"total": total,
"totalPages": (total + int(LEADERBOARD_PAGE_SIZE) - 1) // int(LEADERBOARD_PAGE_SIZE) if total > 0 else 1,
"items": items,
}
response_cache.set(cache_key, result)
return result
@app.get("/api/repo")
def repo_latest(name: str = Query(..., description="owner/repo")) -> Dict[str, Any]:
cache_key = f"repo:{name}"
if cached := response_cache.get(cache_key):
return cached
if not (result := db.get_repo_latest(name)):
raise HTTPException(status_code=404, detail="Repo not found")
response_cache.set(cache_key, result)
return result
@app.get("/api/repo/history")
def repo_history(
name: str = Query(..., description="owner/repo"),
) -> Dict[str, Any]:
cache_key = f"hist:{name}"
if cached := response_cache.get(cache_key):
return cached
segs = db.history_segments(name_with_owner=name, limit=2920) # 2 years
result = {"nameWithOwner": name, "segments": segs}
response_cache.set(cache_key, result)
return result
@app.get("/api/rank")
def shields_rank(name: str = Query(..., description="owner/repo")) -> Dict[str, Any]:
cache_key = f"rank:{name}"
if cached := response_cache.get(cache_key):
return cached
if not (rank := db.get_global_rank(name)):
result = {"schemaVersion": 1, "label": "rank", "message": "repo not found", "color": "inactive"}
response_cache.set(cache_key, result)
return result
color = "brightgreen" if rank <= 100 else "orange" if rank <= 1000 else "blue"
result = {"schemaVersion": 1, "label": "global rank", "message": f"#{rank}", "color": color, "cacheSeconds": 3600}
response_cache.set(cache_key, result)
return result
@app.get("/{owner}/{repo}")
def repo_short_url(owner: str, repo: str) -> RedirectResponse:
name = f"{owner}/{repo}"
if not (rank := db.get_global_rank(name)):
raise HTTPException(status_code=404, detail="Repo not found")
page = ((rank - 1) // LEADERBOARD_PAGE_SIZE) + 1
qs = f"page={page}&metric=stars&view=table&highlight={quote(name, safe='')}&open={quote(name, safe='')}"
return RedirectResponse(url=f"/?{qs}", status_code=302)