imDrizzle commited on
Commit
8cd8d5d
·
1 Parent(s): cd91aab

UI: Improve force graph collisions and velocity chart window

Browse files
Files changed (1) hide show
  1. src/api/routes/graph.py +5 -7
src/api/routes/graph.py CHANGED
@@ -116,19 +116,18 @@ async def get_graph_stats(current_user: dict = Depends(validate_user_token)):
116
  @router.get("/velocity")
117
  async def get_threat_velocity(current_user: dict = Depends(validate_user_token)):
118
  """
119
- Get attacks per minute for the last 60 minutes, split by API Key.
120
  Queries MongoDB logs.
121
  """
122
  logs = mongo.get_logs_collection()
123
  now = datetime.now(timezone.utc)
124
- sixty_mins_ago = now - timedelta(minutes=60)
125
 
126
  pipeline = [
127
- {"$match": {"user_id": current_user["_id"], "timestamp": {"$gte": sixty_mins_ago}, "safe": False}},
128
  {
129
  "$group": {
130
  "_id": {
131
- "minute": {"$minute": "$timestamp"},
132
  "hour": {"$hour": "$timestamp"},
133
  "day": {"$dayOfMonth": "$timestamp"},
134
  "api_key": "$api_key_id"
@@ -136,13 +135,12 @@ async def get_threat_velocity(current_user: dict = Depends(validate_user_token))
136
  "count": {"$sum": 1}
137
  }
138
  },
139
- {"$sort": {"_id.day": 1, "_id.hour": 1, "_id.minute": 1}}
140
  ]
141
 
142
  velocity_data = []
143
  async for doc in logs.aggregate(pipeline):
144
- # Format a simple HH:MM string for the frontend
145
- time_str = f"{doc['_id']['hour']:02d}:{doc['_id']['minute']:02d}"
146
  velocity_data.append({
147
  "time": time_str,
148
  "api_key": doc["_id"]["api_key"],
 
116
  @router.get("/velocity")
117
  async def get_threat_velocity(current_user: dict = Depends(validate_user_token)):
118
  """
119
+ Get attacks per hour for the last 24 hours, split by API Key.
120
  Queries MongoDB logs.
121
  """
122
  logs = mongo.get_logs_collection()
123
  now = datetime.now(timezone.utc)
124
+ time_window = now - timedelta(hours=24)
125
 
126
  pipeline = [
127
+ {"$match": {"user_id": current_user["_id"], "timestamp": {"$gte": time_window}, "safe": False}},
128
  {
129
  "$group": {
130
  "_id": {
 
131
  "hour": {"$hour": "$timestamp"},
132
  "day": {"$dayOfMonth": "$timestamp"},
133
  "api_key": "$api_key_id"
 
135
  "count": {"$sum": 1}
136
  }
137
  },
138
+ {"$sort": {"_id.day": 1, "_id.hour": 1}}
139
  ]
140
 
141
  velocity_data = []
142
  async for doc in logs.aggregate(pipeline):
143
+ time_str = f"{doc['_id']['hour']:02d}:00"
 
144
  velocity_data.append({
145
  "time": time_str,
146
  "api_key": doc["_id"]["api_key"],