feat: adjustments for Demo Team (team_id = 42)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import io
|
||||
import itertools
|
||||
import random
|
||||
import base64
|
||||
from typing import Annotated
|
||||
from fastapi import APIRouter, HTTPException, Security, status
|
||||
@@ -12,6 +14,7 @@ import numpy as np
|
||||
import matplotlib
|
||||
|
||||
from cutt.security import TeamScopedRequest, verify_team_scope
|
||||
from cutt.demo import demo_players
|
||||
|
||||
matplotlib.use("agg")
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -55,14 +58,65 @@ def sociogram_json():
|
||||
|
||||
|
||||
def graph_json(
|
||||
request: Annotated[
|
||||
TeamScopedRequest, Security(verify_team_scope, scopes=["analysis"])
|
||||
],
|
||||
request: Annotated[TeamScopedRequest, Security(verify_team_scope)],
|
||||
networkx_graph: bool = False,
|
||||
):
|
||||
nodes = []
|
||||
edges = []
|
||||
player_map = {}
|
||||
if request.team_id == 42:
|
||||
players = [request.user] + demo_players
|
||||
random.seed(42)
|
||||
for p in players:
|
||||
nodes.append({"id": p.display_name, "label": p.display_name})
|
||||
for p, other in itertools.permutations(players, 2):
|
||||
value = random.random()
|
||||
if value > 0.5:
|
||||
edges.append(
|
||||
{
|
||||
"id": f"{p.display_name}->{other.display_name}",
|
||||
"source": p.display_name,
|
||||
"target": other.display_name,
|
||||
"size": max(value, 0.3),
|
||||
"data": {
|
||||
"relation": 2,
|
||||
"origSize": max(value, 0.3),
|
||||
"origFill": "#bed4ff",
|
||||
},
|
||||
}
|
||||
)
|
||||
elif value < 0.1:
|
||||
edges.append(
|
||||
{
|
||||
"id": f"{p.display_name}-x>{other.display_name}",
|
||||
"source": p.display_name,
|
||||
"target": other.display_name,
|
||||
"size": 0.3,
|
||||
"data": {"relation": 0, "origSize": 0.3, "origFill": "#ff7c7c"},
|
||||
"fill": "#ff7c7c",
|
||||
}
|
||||
)
|
||||
G = nx.DiGraph()
|
||||
G.add_nodes_from([n["id"] for n in nodes])
|
||||
G.add_weighted_edges_from(
|
||||
[
|
||||
(
|
||||
e["source"],
|
||||
e["target"],
|
||||
e["size"] if e["data"]["relation"] == 2 else -e["size"],
|
||||
)
|
||||
for e in edges
|
||||
]
|
||||
)
|
||||
in_degrees = G.in_degree(weight="weight")
|
||||
nodes = [
|
||||
dict(node, **{"data": {"inDegree": in_degrees[node["id"]]}})
|
||||
for node in nodes
|
||||
]
|
||||
if networkx_graph:
|
||||
return G
|
||||
return JSONResponse({"nodes": nodes, "edges": edges})
|
||||
|
||||
with Session(engine) as session:
|
||||
players = session.exec(
|
||||
select(P)
|
||||
@@ -240,9 +294,7 @@ async def render_sociogram(params: Params):
|
||||
|
||||
|
||||
def mvp(
|
||||
request: Annotated[
|
||||
TeamScopedRequest, Security(verify_team_scope, scopes=["analysis"])
|
||||
],
|
||||
request: Annotated[TeamScopedRequest, Security(verify_team_scope)],
|
||||
):
|
||||
ranks = dict()
|
||||
with Session(engine) as session:
|
||||
|
||||
Reference in New Issue
Block a user