Compare commits

...

6 Commits

8 changed files with 118 additions and 25 deletions

View File

@ -24,10 +24,10 @@ P = Player
def sociogram_json(): def sociogram_json():
nodes = [] nodes = []
necessary_nodes = set() necessary_nodes = set()
links = [] edges = []
with Session(engine) as session: with Session(engine) as session:
for p in session.exec(select(P)).fetchall(): for p in session.exec(select(P)).fetchall():
nodes.append({"id": p.name, "appearance": 1}) nodes.append({"id": p.name, "label": p.name})
subquery = ( subquery = (
select(C.user, func.max(C.time).label("latest")) select(C.user, func.max(C.time).label("latest"))
.where(C.time > datetime(2025, 2, 1, 10)) .where(C.time > datetime(2025, 2, 1, 10))
@ -44,9 +44,49 @@ def sociogram_json():
# G.add_edge(c.user, p) # G.add_edge(c.user, p)
# p_id = session.exec(select(P.id).where(P.name == p)).one() # p_id = session.exec(select(P.id).where(P.name == p)).one()
necessary_nodes.add(p) necessary_nodes.add(p)
links.append({"source": c.user, "target": p}) edges.append({"from": c.user, "to": p, "relation": "likes"})
for p in c.hate:
edges.append({"from": c.user, "to": p, "relation": "dislikes"})
# nodes = [n for n in nodes if n["name"] in necessary_nodes] # nodes = [n for n in nodes if n["name"] in necessary_nodes]
return JSONResponse({"nodes": nodes, "links": links}) return JSONResponse({"nodes": nodes, "edges": edges})
def graph_json():
nodes = []
edges = []
with Session(engine) as session:
for p in session.exec(select(P)).fetchall():
nodes.append({"id": p.name, "label": p.name})
subquery = (
select(C.user, func.max(C.time).label("latest"))
.where(C.time > datetime(2025, 2, 1, 10))
.group_by(C.user)
.subquery()
)
statement2 = select(C).join(
subquery, (C.user == subquery.c.user) & (C.time == subquery.c.latest)
)
for c in session.exec(statement2):
for p in c.love:
edges.append(
{
"id": f"{c.user}->{p}",
"source": c.user,
"target": p,
"relation": "likes",
}
)
continue
for p in c.hate:
edges.append(
{
id: f"{c.user}-x>{p}",
"source": c.user,
"target": p,
"relation": "dislikes",
}
)
return JSONResponse({"nodes": nodes, "edges": edges})
def sociogram_data(show: int | None = 2): def sociogram_data(show: int | None = 2):
@ -144,6 +184,7 @@ async def render_sociogram(params: Params):
analysis_router.add_api_route("/json", endpoint=sociogram_json, methods=["GET"]) analysis_router.add_api_route("/json", endpoint=sociogram_json, methods=["GET"])
analysis_router.add_api_route("/graph_json", endpoint=graph_json, methods=["GET"])
analysis_router.add_api_route("/image", endpoint=render_sociogram, methods=["POST"]) analysis_router.add_api_route("/image", endpoint=render_sociogram, methods=["POST"])
if __name__ == "__main__": if __name__ == "__main__":

2
db.py
View File

@ -12,7 +12,7 @@ from sqlmodel import (
with open("db.secrets", "r") as f: with open("db.secrets", "r") as f:
db_secrets = f.readline().strip() db_secrets = f.readline().strip()
engine = create_engine(db_secrets) engine = create_engine(db_secrets, connect_args={"connect_timeout": 8})
del db_secrets del db_secrets

View File

@ -10,15 +10,14 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"d3": "^7.9.0",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
"react-sortablejs": "^6.1.4", "react-sortablejs": "^6.1.4",
"reagraph": "^4.21.2",
"sortablejs": "^1.15.6" "sortablejs": "^1.15.6"
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.17.0", "@eslint/js": "^9.17.0",
"@types/d3": "^7.4.3",
"@types/react": "^18.3.18", "@types/react": "^18.3.18",
"@types/react-dom": "^18.3.5", "@types/react-dom": "^18.3.5",
"@types/sortablejs": "^1.15.8", "@types/sortablejs": "^1.15.8",

View File

@ -9,6 +9,7 @@ from db import engine, User
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
from passlib.context import CryptContext from passlib.context import CryptContext
from sqlalchemy.exc import OperationalError
class Config(BaseSettings): class Config(BaseSettings):
@ -47,10 +48,13 @@ def get_password_hash(password):
def get_user(username: str | None): def get_user(username: str | None):
if username: if username:
try:
with Session(engine) as session: with Session(engine) as session:
return session.exec( return session.exec(
select(User).where(User.username == username) select(User).where(User.username == username)
).one_or_none() ).one_or_none()
except OperationalError:
return
def authenticate_user(username: str, password: str): def authenticate_user(username: str, password: str):

View File

@ -5,19 +5,19 @@ import Header from "./Header";
import Rankings from "./Rankings"; import Rankings from "./Rankings";
import { BrowserRouter, Routes, Route } from "react-router"; import { BrowserRouter, Routes, Route } from "react-router";
import { SessionProvider } from "./Session"; import { SessionProvider } from "./Session";
import { GraphComponent } from "./Network";
function App() { function App() {
//const [data, setData] = useState({ nodes: [], links: [] } as SociogramData);
//async function loadData() {
// await fetch(`${baseUrl}api/analysis/json`, { method: "GET" }).then(resp => resp.json() as unknown as SociogramData).then(json => { setData(json) })
//}
//useEffect(() => { loadData() }, [])
//
return ( return (
<BrowserRouter> <BrowserRouter>
<Header /> <Header />
<Routes> <Routes>
<Route index element={<Rankings />} /> <Route index element={<Rankings />} />
<Route path="/network" element={
<SessionProvider>
<GraphComponent />
</SessionProvider>
} />
<Route path="/analysis" element={ <Route path="/analysis" element={
<SessionProvider> <SessionProvider>
<Analysis /> <Analysis />

View File

@ -5,7 +5,7 @@ export default function Footer() {
<div className="navbar"> <div className="navbar">
<Link to="/" ><span>Form</span></Link> <Link to="/" ><span>Form</span></Link>
<span>|</span> <span>|</span>
<Link to="/analysis" ><span>Trainer Analysis</span></Link> <Link to="/network" ><span>Trainer Analysis</span></Link>
</div> </div>
<p className="grey extra-margin"> <p className="grey extra-margin">
something not working? something not working?

46
src/Network.tsx Normal file
View File

@ -0,0 +1,46 @@
import { useEffect, useRef, useState } from "react";
import { apiAuth } from "./api";
import { GraphCanvas, GraphCanvasRef, GraphEdge, GraphNode, useSelection } from "reagraph";
interface NetworkData {
nodes: GraphNode[],
edges: GraphEdge[],
}
export const GraphComponent = () => {
const [data, setData] = useState({ nodes: [], edges: [] } as NetworkData);
const [loading, setLoading] = useState(true);
async function loadData() {
setLoading(true);
await apiAuth("analysis/graph_json", null)
.then(json => json as Promise<NetworkData>).then(json => { setData(json) })
setLoading(false);
}
useEffect(() => { loadData() }, [])
const graphRef = useRef<GraphCanvasRef | null>(null);
const { selections, actives, onNodeClick, onCanvasClick } = useSelection({
ref: graphRef,
nodes: data.nodes,
edges: data.edges,
pathSelectionType: 'out'
});
return (
<GraphCanvas
draggable
ref={graphRef}
nodes={data.nodes}
edges={data.edges}
selections={selections}
actives={actives}
onCanvasClick={onCanvasClick}
onNodeClick={onNodeClick}
/>
);
}

View File

@ -20,13 +20,16 @@ export default async function api(path: string, data: any): Promise<any> {
export async function apiAuth(path: string, data: any, method: string = "GET"): Promise<any> { export async function apiAuth(path: string, data: any, method: string = "GET"): Promise<any> {
const req = new Request(`${baseUrl}api/${path}`, { const req = new Request(`${baseUrl}api/${path}`,
method: method, headers: { {
method: method,
headers: {
"Authorization": `Bearer ${token()} `, "Authorization": `Bearer ${token()} `,
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(data), ...(data && { body: JSON.stringify(data) })
}); }
);
let resp: Response; let resp: Response;
try { try {
resp = await fetch(req); resp = await fetch(req);