Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e5724f0611 |
+28
-8
@@ -1059,12 +1059,6 @@ const docTemplate = `{
|
|||||||
"description": "Time period (e.g. 1h, 24h, 7d)",
|
"description": "Time period (e.g. 1h, 24h, 7d)",
|
||||||
"name": "period",
|
"name": "period",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"description": "Filter by agent ID",
|
|
||||||
"name": "agent_id",
|
|
||||||
"in": "query"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
@@ -1690,7 +1684,7 @@ const docTemplate = `{
|
|||||||
"201": {
|
"201": {
|
||||||
"description": "Created",
|
"description": "Created",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/internal_handlers.JobResult"
|
"$ref": "#/definitions/internal_handlers.RunScriptOut"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"400": {
|
||||||
@@ -2124,7 +2118,7 @@ const docTemplate = `{
|
|||||||
"201": {
|
"201": {
|
||||||
"description": "Created",
|
"description": "Created",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/internal_handlers.AddJobOut"
|
"$ref": "#/definitions/internal_handlers.RunScriptOut"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2910,6 +2904,32 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"internal_handlers.RunScriptOut": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"command": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"stderr": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"stdin": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"stdout": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"internal_handlers.RunStoredScriptIn": {
|
"internal_handlers.RunStoredScriptIn": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
|
|||||||
@@ -1048,12 +1048,6 @@
|
|||||||
"description": "Time period (e.g. 1h, 24h, 7d)",
|
"description": "Time period (e.g. 1h, 24h, 7d)",
|
||||||
"name": "period",
|
"name": "period",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"description": "Filter by agent ID",
|
|
||||||
"name": "agent_id",
|
|
||||||
"in": "query"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
@@ -1679,7 +1673,7 @@
|
|||||||
"201": {
|
"201": {
|
||||||
"description": "Created",
|
"description": "Created",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/internal_handlers.JobResult"
|
"$ref": "#/definitions/internal_handlers.RunScriptOut"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"400": {
|
||||||
@@ -2113,7 +2107,7 @@
|
|||||||
"201": {
|
"201": {
|
||||||
"description": "Created",
|
"description": "Created",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/internal_handlers.AddJobOut"
|
"$ref": "#/definitions/internal_handlers.RunScriptOut"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2899,6 +2893,32 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"internal_handlers.RunScriptOut": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"command": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"stderr": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"stdin": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"stdout": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"internal_handlers.RunStoredScriptIn": {
|
"internal_handlers.RunStoredScriptIn": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
|
|||||||
@@ -504,6 +504,23 @@ definitions:
|
|||||||
- interpreter_id
|
- interpreter_id
|
||||||
- script_text
|
- script_text
|
||||||
type: object
|
type: object
|
||||||
|
internal_handlers.RunScriptOut:
|
||||||
|
properties:
|
||||||
|
command:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
type: array
|
||||||
|
id:
|
||||||
|
type: integer
|
||||||
|
status:
|
||||||
|
type: integer
|
||||||
|
stderr:
|
||||||
|
type: string
|
||||||
|
stdin:
|
||||||
|
type: string
|
||||||
|
stdout:
|
||||||
|
type: string
|
||||||
|
type: object
|
||||||
internal_handlers.RunStoredScriptIn:
|
internal_handlers.RunStoredScriptIn:
|
||||||
properties:
|
properties:
|
||||||
stdin:
|
stdin:
|
||||||
@@ -1229,10 +1246,6 @@ paths:
|
|||||||
in: query
|
in: query
|
||||||
name: period
|
name: period
|
||||||
type: string
|
type: string
|
||||||
- description: Filter by agent ID
|
|
||||||
in: query
|
|
||||||
name: agent_id
|
|
||||||
type: string
|
|
||||||
produces:
|
produces:
|
||||||
- application/json
|
- application/json
|
||||||
responses:
|
responses:
|
||||||
@@ -1595,7 +1608,7 @@ paths:
|
|||||||
"201":
|
"201":
|
||||||
description: Created
|
description: Created
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/internal_handlers.JobResult'
|
$ref: '#/definitions/internal_handlers.RunScriptOut'
|
||||||
"400":
|
"400":
|
||||||
description: Bad Request
|
description: Bad Request
|
||||||
schema:
|
schema:
|
||||||
@@ -1869,7 +1882,7 @@ paths:
|
|||||||
"201":
|
"201":
|
||||||
description: Created
|
description: Created
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/internal_handlers.AddJobOut'
|
$ref: '#/definitions/internal_handlers.RunScriptOut'
|
||||||
security:
|
security:
|
||||||
- Bearer: []
|
- Bearer: []
|
||||||
summary: Run a script on an agent
|
summary: Run a script on an agent
|
||||||
|
|||||||
@@ -230,7 +230,6 @@ type JobMetricsOut struct {
|
|||||||
// @Tags jobs
|
// @Tags jobs
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param period query string false "Time period (e.g. 1h, 24h, 7d)" default(24h)
|
// @Param period query string false "Time period (e.g. 1h, 24h, 7d)" default(24h)
|
||||||
// @Param agent_id query string false "Filter by agent ID"
|
|
||||||
// @Success 200 {object} JobMetricsOut
|
// @Success 200 {object} JobMetricsOut
|
||||||
// @Failure 400 {object} map[string]string
|
// @Failure 400 {object} map[string]string
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
@@ -243,9 +242,8 @@ func (h *JobsHandlers) GetJobMetrics(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
agentID := c.Query("agent_id")
|
|
||||||
since := time.Now().Add(-period)
|
since := time.Now().Add(-period)
|
||||||
metrics, err := h.jobRepo.GetJobMetrics(c.Request.Context(), since, agentID)
|
metrics, err := h.jobRepo.GetJobMetrics(c.Request.Context(), since)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ func (sh *ScriptHandlersGroup) DeleteScript(c *gin.Context) {
|
|||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param id path int true "Script ID"
|
// @Param id path int true "Script ID"
|
||||||
// @Param body body RunStoredScriptIn true "Agent token and optional stdin"
|
// @Param body body RunStoredScriptIn true "Agent token and optional stdin"
|
||||||
// @Success 201 {object} JobResult
|
// @Success 201 {object} RunScriptOut
|
||||||
// @Failure 400 {object} map[string]string
|
// @Failure 400 {object} map[string]string
|
||||||
// @Failure 404 {object} map[string]string
|
// @Failure 404 {object} map[string]string
|
||||||
// @Failure 500 {object} map[string]string
|
// @Failure 500 {object} map[string]string
|
||||||
|
|||||||
@@ -113,22 +113,18 @@ type JobMetrics struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetJobMetrics returns job success metrics for jobs updated since the given time.
|
// GetJobMetrics returns job success metrics for jobs updated since the given time.
|
||||||
// If agentID is non-empty, results are filtered to that agent only.
|
// A successful job has status == 0, failed has status != 0, pending has status == 0 with empty stdout/stderr.
|
||||||
func (r *JobRepository) GetJobMetrics(ctx context.Context, since time.Time, agentID string) (JobMetrics, error) {
|
func (r *JobRepository) GetJobMetrics(ctx context.Context, since time.Time) (JobMetrics, error) {
|
||||||
var m JobMetrics
|
var m JobMetrics
|
||||||
query := `SELECT
|
err := r.DB.QueryRowContext(ctx,
|
||||||
|
`SELECT
|
||||||
COUNT(*),
|
COUNT(*),
|
||||||
SUM(CASE WHEN status = 0 AND (stdout != '' OR stderr != '') THEN 1 ELSE 0 END),
|
SUM(CASE WHEN status = 0 AND (stdout != '' OR stderr != '') THEN 1 ELSE 0 END),
|
||||||
SUM(CASE WHEN status != 0 THEN 1 ELSE 0 END),
|
SUM(CASE WHEN status != 0 THEN 1 ELSE 0 END),
|
||||||
SUM(CASE WHEN status = 0 AND stdout = '' AND stderr = '' THEN 1 ELSE 0 END)
|
SUM(CASE WHEN status = 0 AND stdout = '' AND stderr = '' THEN 1 ELSE 0 END)
|
||||||
FROM jobs WHERE updated_at >= ?`
|
FROM jobs WHERE updated_at >= ?`,
|
||||||
args := []any{since}
|
since,
|
||||||
if agentID != "" {
|
).Scan(&m.Total, &m.Success, &m.Failed, &m.Pending)
|
||||||
query += " AND agent_id = ?"
|
|
||||||
args = append(args, agentID)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := r.DB.QueryRowContext(ctx, query, args...).Scan(&m.Total, &m.Success, &m.Failed, &m.Pending)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return JobMetrics{}, err
|
return JobMetrics{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user