Files
mantrae/server/internal/store/db/audit_logs.sql.go
d34dscene 2b3cff317c cleanup
2025-12-03 16:06:44 +01:00

147 lines
3.1 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: audit_logs.sql
package db
import (
"context"
"time"
)
const countAuditLogs = `-- name: CountAuditLogs :one
SELECT
COUNT(*)
FROM
audit_logs
`
func (q *Queries) CountAuditLogs(ctx context.Context) (int64, error) {
row := q.queryRow(ctx, q.countAuditLogsStmt, countAuditLogs)
var count int64
err := row.Scan(&count)
return count, err
}
const createAuditLog = `-- name: CreateAuditLog :exec
INSERT INTO
audit_logs (
profile_id,
user_id,
agent_id,
event,
details,
created_at
)
VALUES
(?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
`
type CreateAuditLogParams struct {
ProfileID *int64 `json:"profileId"`
UserID *string `json:"userId"`
AgentID *string `json:"agentId"`
Event string `json:"event"`
Details *string `json:"details"`
}
func (q *Queries) CreateAuditLog(ctx context.Context, arg *CreateAuditLogParams) error {
_, err := q.exec(ctx, q.createAuditLogStmt, createAuditLog,
arg.ProfileID,
arg.UserID,
arg.AgentID,
arg.Event,
arg.Details,
)
return err
}
const deleteOldAuditLogs = `-- name: DeleteOldAuditLogs :exec
DELETE FROM audit_logs
WHERE
created_at < DATETIME ('now', '-90 days')
`
func (q *Queries) DeleteOldAuditLogs(ctx context.Context) error {
_, err := q.exec(ctx, q.deleteOldAuditLogsStmt, deleteOldAuditLogs)
return err
}
const listAuditLogs = `-- name: ListAuditLogs :many
SELECT
a.id,
a.profile_id,
p.name AS profile_name,
a.user_id,
u.username AS user_name,
a.agent_id,
ag.hostname AS agent_name,
a.event,
a.details,
a.created_at
FROM
audit_logs a
LEFT JOIN profiles p ON a.profile_id = p.id
LEFT JOIN users u ON a.user_id = u.id
LEFT JOIN agents ag ON a.agent_id = ag.id
ORDER BY
a.created_at DESC
LIMIT
COALESCE(CAST(?2 AS INTEGER), -1)
OFFSET
COALESCE(CAST(?1 AS INTEGER), 0)
`
type ListAuditLogsParams struct {
Offset *int64 `json:"offset"`
Limit *int64 `json:"limit"`
}
type ListAuditLogsRow struct {
ID int64 `json:"id"`
ProfileID *int64 `json:"profileId"`
ProfileName *string `json:"profileName"`
UserID *string `json:"userId"`
UserName *string `json:"userName"`
AgentID *string `json:"agentId"`
AgentName *string `json:"agentName"`
Event string `json:"event"`
Details *string `json:"details"`
CreatedAt *time.Time `json:"createdAt"`
}
func (q *Queries) ListAuditLogs(ctx context.Context, arg *ListAuditLogsParams) ([]*ListAuditLogsRow, error) {
rows, err := q.query(ctx, q.listAuditLogsStmt, listAuditLogs, arg.Offset, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*ListAuditLogsRow
for rows.Next() {
var i ListAuditLogsRow
if err := rows.Scan(
&i.ID,
&i.ProfileID,
&i.ProfileName,
&i.UserID,
&i.UserName,
&i.AgentID,
&i.AgentName,
&i.Event,
&i.Details,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}