mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-03-29 04:43:22 -04:00
feat(plugins): Implement /plugins/stats endpoint for per-plugin row counts with optional foreignKey filtering
This commit is contained in:
@@ -43,6 +43,7 @@ from .sync_endpoint import handle_sync_post, handle_sync_get # noqa: E402 [flak
|
||||
from .logs_endpoint import clean_log # noqa: E402 [flake8 lint suppression]
|
||||
from .health_endpoint import get_health_status # noqa: E402 [flake8 lint suppression]
|
||||
from .languages_endpoint import get_languages # noqa: E402 [flake8 lint suppression]
|
||||
from models.plugin_object_instance import PluginObjectInstance # noqa: E402 [flake8 lint suppression]
|
||||
from models.user_events_queue_instance import UserEventsQueueInstance # noqa: E402 [flake8 lint suppression]
|
||||
|
||||
from models.event_instance import EventInstance # noqa: E402 [flake8 lint suppression]
|
||||
@@ -97,6 +98,7 @@ from .openapi.schemas import ( # noqa: E402 [flake8 lint suppression]
|
||||
AddToQueueRequest, GetSettingResponse,
|
||||
RecentEventsRequest, SetDeviceAliasRequest,
|
||||
LanguagesResponse,
|
||||
PluginStatsResponse,
|
||||
)
|
||||
|
||||
from .sse_endpoint import ( # noqa: E402 [flake8 lint suppression]
|
||||
@@ -2002,6 +2004,33 @@ def list_languages(payload=None):
|
||||
}), 500
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Plugin Stats endpoint
|
||||
# --------------------------
|
||||
@app.route("/plugins/stats", methods=["GET"])
|
||||
@validate_request(
|
||||
operation_id="get_plugin_stats",
|
||||
summary="Get Plugin Row Counts",
|
||||
description="Return per-plugin row counts across Objects, Events, and History tables. Optionally filter by foreignKey (MAC).",
|
||||
response_model=PluginStatsResponse,
|
||||
tags=["plugins"],
|
||||
auth_callable=is_authorized,
|
||||
query_params=[{
|
||||
"name": "foreignKey",
|
||||
"in": "query",
|
||||
"required": False,
|
||||
"description": "Filter counts to rows matching this foreignKey (typically a MAC address)",
|
||||
"schema": {"type": "string"}
|
||||
}]
|
||||
)
|
||||
def api_plugin_stats(payload=None):
|
||||
"""Get per-plugin row counts, optionally filtered by foreignKey."""
|
||||
foreign_key = request.args.get("foreignKey", None)
|
||||
handler = PluginObjectInstance()
|
||||
data = handler.getStats(foreign_key)
|
||||
return jsonify({"success": True, "data": data})
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Background Server Start
|
||||
# --------------------------
|
||||
|
||||
@@ -1084,3 +1084,32 @@ class GraphQLRequest(BaseModel):
|
||||
"""Request payload for GraphQL queries."""
|
||||
query: str = Field(..., description="GraphQL query string", json_schema_extra={"examples": ["{ devices { devMac devName } }"]})
|
||||
variables: Optional[Dict[str, Any]] = Field(None, description="Variables for the GraphQL query")
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# PLUGIN SCHEMAS
|
||||
# =============================================================================
|
||||
class PluginStatsEntry(BaseModel):
|
||||
"""Per-plugin row count for one table."""
|
||||
tableName: str = Field(..., description="Table category: objects, events, or history")
|
||||
plugin: str = Field(..., description="Plugin unique prefix")
|
||||
cnt: int = Field(..., ge=0, description="Row count")
|
||||
|
||||
|
||||
class PluginStatsResponse(BaseResponse):
|
||||
"""Response for GET /plugins/stats — per-plugin row counts."""
|
||||
model_config = ConfigDict(
|
||||
extra="allow",
|
||||
json_schema_extra={
|
||||
"examples": [{
|
||||
"success": True,
|
||||
"data": [
|
||||
{"tableName": "objects", "plugin": "ARPSCAN", "cnt": 42},
|
||||
{"tableName": "events", "plugin": "ARPSCAN", "cnt": 5},
|
||||
{"tableName": "history", "plugin": "ARPSCAN", "cnt": 100}
|
||||
]
|
||||
}]
|
||||
}
|
||||
)
|
||||
|
||||
data: List[PluginStatsEntry] = Field(default_factory=list, description="Per-plugin row counts")
|
||||
|
||||
Reference in New Issue
Block a user