-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (128 loc) · 4.84 KB
/
main.py
File metadata and controls
155 lines (128 loc) · 4.84 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
145
146
147
148
149
150
151
152
153
154
155
from __future__ import annotations
from contextlib import asynccontextmanager
from datetime import datetime, timezone
from pathlib import Path
from typing import List
import pandas as pd
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from db import get_spark
from models import HealthScore, PackageDetail, PackageSummary, SentimentDetail
for _d in [Path(__file__).resolve().parents[2], *Path(__file__).resolve().parents]:
_env = _d / ".env"
if _env.is_file():
load_dotenv(_env)
break
_scores_df: pd.DataFrame = pd.DataFrame()
_sentiment_df: pd.DataFrame = pd.DataFrame()
@asynccontextmanager
async def lifespan(app: FastAPI):
global _scores_df, _sentiment_df
spark = get_spark()
_scores_df = (
spark.table("ddc_databricks.gold.package_health_scores")
.toPandas()
)
_sentiment_df = (
spark.table("ddc_databricks.gold.package_sentiment")
.select(
"package_name",
"so_question_sentiment_avg",
"so_answer_sentiment_avg",
"readme_sentiment_compound",
"pypi_desc_sentiment_compound",
"overall_sentiment",
)
.toPandas()
)
yield
app = FastAPI(
title="OpenLens API",
version="1.0.0",
lifespan=lifespan,
)
def _row_to_health_score(row: pd.Series) -> HealthScore:
return HealthScore(
package_name=row["package_name"],
github_score=float(row["github_score"]),
pypi_score=float(row["pypi_score"]),
community_score=float(row["community_score"]),
sentiment_score=float(row["sentiment_score"]),
overall_health_score=float(row["overall_health_score"]),
health_tier=str(row["health_tier"]),
scored_at=pd.Timestamp(row["scored_at"]).to_pydatetime(),
)
def _row_to_sentiment(row: pd.Series) -> SentimentDetail:
def _f(val):
return float(val) if pd.notna(val) else None
return SentimentDetail(
package_name=row["package_name"],
so_question_sentiment_avg=_f(row["so_question_sentiment_avg"]),
so_answer_sentiment_avg=_f(row["so_answer_sentiment_avg"]),
readme_sentiment_compound=_f(row["readme_sentiment_compound"]),
pypi_desc_sentiment_compound=_f(row["pypi_desc_sentiment_compound"]),
overall_sentiment=_f(row["overall_sentiment"]),
)
@app.get("/health", tags=["meta"])
def health_check():
"""API liveness check."""
return {
"status": "ok",
"packages_cached": len(_scores_df),
"timestamp": datetime.now(timezone.utc).isoformat(),
}
@app.get("/packages", response_model=List[PackageSummary], tags=["packages"])
def list_packages():
"""All tracked packages sorted by health score."""
if _scores_df.empty:
return []
ranked = _scores_df.sort_values("overall_health_score", ascending=False)
return [
PackageSummary(
package_name=row["package_name"],
overall_health_score=float(row["overall_health_score"]),
health_tier=str(row["health_tier"]),
)
for _, row in ranked.iterrows()
]
@app.get("/packages/{name}", response_model=PackageDetail, tags=["packages"])
def get_package(name: str):
"""Health score + sentiment for one package."""
score_rows = _scores_df[_scores_df["package_name"] == name]
if score_rows.empty:
raise HTTPException(status_code=404, detail=f"Package '{name}' not found.")
sent_rows = _sentiment_df[_sentiment_df["package_name"] == name]
sentiment = (
_row_to_sentiment(sent_rows.iloc[0])
if not sent_rows.empty
else SentimentDetail(package_name=name)
)
return PackageDetail(
scores=_row_to_health_score(score_rows.iloc[0]),
sentiment=sentiment,
)
@app.get("/packages/{name}/scores", response_model=HealthScore, tags=["packages"])
def get_package_scores(name: str):
"""Just the score breakdown for one package."""
rows = _scores_df[_scores_df["package_name"] == name]
if rows.empty:
raise HTTPException(status_code=404, detail=f"Package '{name}' not found.")
return _row_to_health_score(rows.iloc[0])
@app.get("/packages/{name}/sentiment", response_model=SentimentDetail, tags=["packages"])
def get_package_sentiment(name: str):
"""Sentiment breakdown for one package."""
rows = _sentiment_df[_sentiment_df["package_name"] == name]
if rows.empty:
raise HTTPException(status_code=404, detail=f"Package '{name}' not found.")
return _row_to_sentiment(rows.iloc[0])
@app.get("/scores/leaderboard", response_model=List[HealthScore], tags=["scores"])
def leaderboard():
"""All packages ranked by overall health score."""
if _scores_df.empty:
return []
return [
_row_to_health_score(row)
for _, row in _scores_df.sort_values(
"overall_health_score", ascending=False
).iterrows()
]