diff --git a/.gitignore b/.gitignore index 4832b293..9832bb66 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,6 @@ config/config.local.yaml .vscode/ *.swp .DS_Store + +# Local scratch (demos, temp DBs, tool cache) +.tmp/ diff --git a/docs/developer-guide/api-reference.md b/docs/developer-guide/api-reference.md index a065a3d4..79035601 100644 --- a/docs/developer-guide/api-reference.md +++ b/docs/developer-guide/api-reference.md @@ -221,6 +221,8 @@ Public. Returns the message field contract that [message-fields.md](./message-fi **DELETE** `/platforms/:id` +A missing id answers **404**. + **Response**: ```json { @@ -338,6 +340,8 @@ Public. Returns the message field contract that [message-fields.md](./message-fi **DELETE** `/adapters/:id` +A missing id answers **404**. + **Response**: ```json { @@ -391,6 +395,9 @@ Public. Returns the message field contract that [message-fields.md](./message-fi "name": "Store A", "status": "running", "config": "{}", + "last_heartbeat": "2024-01-01T00:05:00Z", + "message_count": 128, + "error_count": 3, "created_at": "2024-01-01T00:00:00Z" } ], @@ -439,6 +446,9 @@ Public. Returns the message field contract that [message-fields.md](./message-fi "name": "Store A", "status": "running", "config": "{}", + "last_heartbeat": "2024-01-01T00:05:00Z", + "message_count": 128, + "error_count": 3, "session": { "connected_at": "2024-01-01T00:00:00Z", "last_heartbeat": "2024-01-01T00:05:00Z" @@ -448,6 +458,11 @@ Public. Returns the message field contract that [message-fields.md](./message-fi } ``` +`last_heartbeat` is `null` until the adapter starts or its bridge connects. +`message_count` / `error_count` are instance-lifetime counters, incremented on +inbound message persistence and on failed persistence respectively; the +Monitoring page reads exactly these three fields. + ### Update instance **PUT** `/instances/:id` @@ -476,6 +491,8 @@ Public. Returns the message field contract that [message-fields.md](./message-fi **DELETE** `/instances/:id` +A missing id answers **404**. + **Response**: ```json { @@ -545,6 +562,8 @@ Public. Returns the message field contract that [message-fields.md](./message-fi **POST** `/messages/:id/ack` +A missing id answers **404**. + **Response**: ```json { @@ -743,6 +762,8 @@ Public. Returns the message field contract that [message-fields.md](./message-fi **DELETE** `/routes/:id` +A missing id answers **404**. + **Response**: ```json { @@ -798,15 +819,36 @@ Public. Returns the message field contract that [message-fields.md](./message-fi ## Error codes +Every error body is `{"error": ""}`. + | Error code | Description | |--------|------| -| 400 | Invalid request parameters | +| 400 | Invalid request parameters (binding failure, failed validation) | | 401 | Unauthenticated | | 403 | Forbidden | | 404 | Resource not found | | 409 | Resource conflict | | 500 | Internal server error | +Mapping rules, so a client can branch on the status instead of parsing text: + +- **404** — the URL names an id that does not exist. Applies to every `GET`, + `PUT`/`PATCH`, `DELETE` and to `POST /messages/:id/ack`: operating on a + missing resource is not a success. The body is the generic + `resource not found`; the id is in the server log. +- **409** — the write collides with existing state and retrying unchanged will + not help: duplicate username (`POST /auth/register`, `POST /users`), + duplicate platform code, demoting/disabling/deleting the last admin. The + body carries the specific reason, e.g. `username already exists`. +- **400** — the request itself is wrong: malformed JSON, missing required + field, password too short, unknown role/status, starting an instance with no + cookie or while it is still initializing. +- **500** — storage or unexpected failure. The body is the raw error. + +`DELETE` on a missing id used to answer `200` with `"… deleted"`; it now +answers `404`. Clients that treated the old 200 as "done" keep working, ones +that re-fetched afterwards get a truthful answer. + ## Rate limiting - Default limit: 100 requests/minute diff --git a/docs/developer-guide/functional-review.md b/docs/developer-guide/functional-review.md index 314628c6..92797410 100644 --- a/docs/developer-guide/functional-review.md +++ b/docs/developer-guide/functional-review.md @@ -249,7 +249,7 @@ second insert with empty idempotency_key: **建议**:`/` 外包一层读 token 的 `RequireAuth`,无 token 重定向 `/login`。 -### M10. Monitoring 读的字段在实例模型上不存在,指标恒为 0 +### M10. ~~Monitoring 读的字段在实例模型上不存在,指标恒为 0~~(已修复) **现象**:`Monitoring.tsx` 映射 `instance.last_heartbeat`、`instance.message_count`、`instance.error_count`;而 `models.AdapterInstance`(`models.go:41-51`)只有 `id/adapter_id/platform_id/name/config/status/user_id/created_at/updated_at`,这三个字段一个都没有。 @@ -261,7 +261,9 @@ second insert with empty idempotency_key: **建议**:给 `AdapterInstance` 补上三个统计字段(或改读 `adapter_sessions` + 聚合查询);实例级启停走 `InstanceService` 而不是 `AdapterService`。 -### M11. 空操作也回 200,"删了不存在的资源"是假成功 +**现已修复**:`models.AdapterInstance` 补了 `last_heartbeat`(可空)/ `message_count` / `error_count`,迁移在 `migrations/003_instance_monitoring_counters.sql`(`IF NOT EXISTS`,回填旧会话的心跳,带回滚)。计数在 WS 入库成功/失败与网关 `PersistInboundMessage` 处维护,握手与进程启动刷新心跳(`handler/websocket.go`、`service/python_runner.go:updateInstanceStatus`)。实测 `GET /instances/i1` 返回 `{"last_heartbeat":"2026-01-02T03:04:05Z","message_count":7,"error_count":2}`,此前三个键都不存在。实例级启停已由 `InstanceService` 持有(`Start`/`Stop` 走 runner),未再新增 `AdapterService` 依赖。 + +### M11. ~~空操作也回 200,"删了不存在的资源"是假成功~~(已修复) **现象**: @@ -276,7 +278,9 @@ DELETE /routes/nope → 200 {"message":"route deleted"} **建议**:`UpdateStatus`/`Delete` 判 `RowsAffected == 0` 回 `gorm.ErrRecordNotFound`,handler 统一映射 404。 -### M12. 错误码语义混乱:找不到是 400/404,内部错误也是 400 +**现已修复**:8 个 `Delete`(route / instance / adapter / adapter package / platform / user / gateway adapter / session)与 `InboundEventRepository.UpdateStatus` / `OutboundCommandRepository.UpdateStatus` 都判 `RowsAffected`,为 0 时回 `gorm.ErrRecordNotFound`,service 包成 `ErrNotFound`,handler 映射 404。实测 `DELETE /routes/nope`、`POST /messages/nope/ack`、`DELETE /platforms/nope`、`DELETE /instances/nope` 全部从 200 变 404,删存在的资源仍是 200。 + +### M12. ~~错误码语义混乱:找不到是 400/404,内部错误也是 400~~(已修复) **现象**:`gorm.ErrRecordNotFound` 全仓无人识别(`grep -rn ErrRecordNotFound` 无命中)。结果是 handler 把 service 返回的一切错误都当同一类。 @@ -293,7 +297,9 @@ DELETE /routes/nope → 200 {"message":"route deleted"} **建议**:引入 `errors.Is(err, gorm.ErrRecordNotFound)` 辅助函数统一映射 404;重名/重复走 409;service 层包一层哨兵错误,别把 `record not found` 直接当 400 文案返回。 -### M13. 真实功能几乎零测试:`handler`、`service`、`server`、`protocol/v3`、`middleware` 全无测试文件 +**现已修复**:`service/errors.go` 定义 `ErrNotFound` / `ErrConflict` / `ErrValidation` 哨兵与 `wrapNotFound`/`conflictf`/`validationf`;handler 侧只有一处 `respondError`(`handler/errors.go`)做映射:404 / 409 / 400 / 500,404 与 500 回通用文案,409/400 回具体原因(去掉哨兵前缀)。原来"一切错误当 400"的分支全部改走它,共 30 余处。实测:`PUT /instances/nope` 400→404,注册重名 400→409 且 body 为 `username already exists`,`DELETE /routes/nope` 的 service 错误不再变 500。 + +### M13. ~~真实功能几乎零测试:`handler`、`service`、`server`、`protocol/v3`、`middleware` 全无测试文件~~(部分修复) **现象**:这五个包都没有 `_test.go`(`ls` 计数均为 0)。有测试的只有 `adapter`、`messagefields`、`platformspec`,加上审查期间刚补的 `message`(仅覆盖 shutdown,见 M14)——前者是注册表/字段表类纯数据校验,后者只有连接关闭。 @@ -301,6 +307,8 @@ DELETE /routes/nope → 200 {"message":"route deleted"} **建议**:先用 `httptest` + `gin.New()` 给鉴权补表驱动用例(空头/坏格式/过期/有效四态),再给 WS 补一个"发帧后库里多一行 + 目标端收到"的集成用例。`sqlite` 依赖 cgo,可在 `main_test.go` 里先探测再 `t.Skip`,让 `CGO_ENABLED=0` 也绿。 +**部分修复**:`internal/handler/handler_test.go` 用真 sqlite + `gin.New()` + `httptest` 覆盖本轮改到的状态码——404(删不存在的 route、ack 不存在的 message、改不存在的 instance)、409(注册/建用户重名,含 body 文案)、400(密码过短),以及 M10 的字段往返。摘掉 `respondError` 的映射会立刻四处失败,不是空跑。鉴权四态与 WS 端到端仍在 P0/P1 的范围里。 + ### M14. ~~`message.Dispatcher` 关闭路径会 `close of closed channel` panic~~(审查中已被修复) **原现象**:`Close()` 遍历 `close(conn.Send)`;`readPump` 的 defer 在连接断开时又走 `UnregisterConnection` 再 `close` 一次。两个关闭点各自裸调 `close(chan)`,谁后到谁 panic。用临时用例复现过: @@ -349,7 +357,7 @@ TestReviewDoubleClose → PANIC: close of closed channel 六个 `List` 都写 `r.db.Model(...).Count(&total)` 不接返回值(`repository/adapter.go:55`、`instance.go:55`、`message.go:59`、`message.go:121`、`message.go:174`、`platform.go:58`、`user.go:58`)。计数失败会静默返回 `total: 0`。 ### m4. API 文档与实现四处不符 -- `api-reference.md` 的"Error codes"列了 409,后端一处没有 +- ~~`api-reference.md` 的"Error codes"列了 409,后端一处没有~~(M12 已让注册/建用户重名、末位 admin 守卫真正走 409) - 无 `POST /platforms` 文档,实际也 404(M5) - `api-reference.md:753-798` 的 WS 示例用 `"type":"message.received"`,而 `envelope.go:22` 的 JSON tag 是 `event_type`;文档的"Message format"与代码里的 `Validate`(`protocol.go:59-79`)要求也对不上 - 文档说"Rate limiting 100/min、认证接口 10/min",**没有任何限流中间件**(`grep rate internal/middleware/` 无命中) @@ -462,10 +470,10 @@ Error: EROFS: read-only file system, open '.../vite.config.ts.timestamp-17940267 ### P2 — 正确性与可观测性 -12. **M10/M6**:实例模型补统计字段、实例级启停改走 `InstanceService`;再补前端增删改表单与 `delete*/update*` 客户端方法。 -13. **M11/M12 状态码**:`RowsAffected == 0` 回 not-found;统一 `ErrRecordNotFound → 404`、重名 `→ 409`。 +12. **M10/M6**:M10 已完成(见该节);M6 前端增删改仍归 P2 前端项。 +13. ~~**M11/M12 状态码**~~(已完成:`RowsAffected == 0` 回 not-found,哨兵错误 + `respondError` 统一 404/409/400/500)。 14. **M5**:补齐 `POST/PUT/DELETE /platforms`,挂 `AuthRequired` + `AdminRequired`。 -15. **M13 测试**:先鉴权、再 WS 端到端;`CGO_ENABLED=0` 下用探测 + `t.Skip` 兜底。 +15. **M13 测试**:handler 状态码用例已完成;仍欠鉴权四态与 WS 端到端,`CGO_ENABLED=0` 下用探测 + `t.Skip` 兜底。 16. **M14**:已完成(`sync.Once` + 五个用例)。剩余只需把 `logger` 未初始化就 nil 的问题在 `pkg/logger` 兜掉,免得每个包写一遍 `TestMain`。 17. **M15**:补四类超时 + `MaxBytesReader`。 18. **m1/m2**:`Hub` 建好后加载已启用规则并在增删改时同步内存;`Priority`/`Enabled` 改指针类型。 diff --git a/internal/handler/adapter.go b/internal/handler/adapter.go index ba922e59..703bcadb 100644 --- a/internal/handler/adapter.go +++ b/internal/handler/adapter.go @@ -45,7 +45,7 @@ func GetAdapter(adapterService *service.AdapterService) gin.HandlerFunc { id := c.Param("id") adapter, err := adapterService.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "adapter not found"}) + respondError(c, err) return } @@ -84,7 +84,7 @@ func UpdateAdapter(adapterService *service.AdapterService) gin.HandlerFunc { adapter, err := adapterService.Update(id, &req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -97,7 +97,7 @@ func DeleteAdapter(adapterService *service.AdapterService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := adapterService.Delete(id); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -110,7 +110,7 @@ func StartAdapter(adapterService *service.AdapterService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := adapterService.StartAdapter(id); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -123,7 +123,7 @@ func StopAdapter(adapterService *service.AdapterService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := adapterService.StopAdapter(id); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } diff --git a/internal/handler/adapter_gateway.go b/internal/handler/adapter_gateway.go index d2047a48..31de2856 100644 --- a/internal/handler/adapter_gateway.go +++ b/internal/handler/adapter_gateway.go @@ -55,7 +55,7 @@ func GetGatewayAdapter(gateway *service.AdapterGatewayService) gin.HandlerFunc { id := c.Param("id") adapter, err := gateway.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "adapter not found"}) + respondError(c, err) return } c.JSON(http.StatusOK, adapter) @@ -74,7 +74,7 @@ func UpdateGatewayAdapter(gateway *service.AdapterGatewayService) gin.HandlerFun adapter, err := gateway.Update(id, &req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -87,7 +87,7 @@ func DeleteGatewayAdapter(gateway *service.AdapterGatewayService) gin.HandlerFun return func(c *gin.Context) { id := c.Param("id") if err := gateway.Delete(id); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + respondError(c, err) return } c.JSON(http.StatusOK, gin.H{"message": "adapter deleted"}) diff --git a/internal/handler/auth.go b/internal/handler/auth.go index 774533cb..6823a8e5 100644 --- a/internal/handler/auth.go +++ b/internal/handler/auth.go @@ -18,7 +18,7 @@ func Register(authService *service.AuthService) gin.HandlerFunc { resp, err := authService.Register(&req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } diff --git a/internal/handler/command.go b/internal/handler/command.go index 59eb06e2..556682f8 100644 --- a/internal/handler/command.go +++ b/internal/handler/command.go @@ -33,7 +33,7 @@ func GetCommand(commandService *service.CommandService) gin.HandlerFunc { id := c.Param("id") command, err := commandService.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "command not found"}) + respondError(c, err) return } diff --git a/internal/handler/errors.go b/internal/handler/errors.go new file mode 100644 index 00000000..8b1b84aa --- /dev/null +++ b/internal/handler/errors.go @@ -0,0 +1,59 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/e-spl/e-sp-line2/internal/service" + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +// Error bodies are deliberately generic for 404/500: the old handlers echoed +// raw storage text ("record not found") as a 400 body, which told a client +// nothing about what to do. Service-level detail is preserved by the wrapped +// sentinel errors and shows up in the server log. +const ( + msgNotFound = "resource not found" + msgConflict = "resource conflict" + msgInternal = "internal server error" +) + +// respondError maps a service error onto the HTTP status the API contract +// promises: 404 not found, 409 conflict, 400 invalid input, 500 anything else. +func respondError(c *gin.Context, err error) { + switch { + case errors.Is(err, service.ErrNotFound), errors.Is(err, gorm.ErrRecordNotFound): + c.JSON(http.StatusNotFound, gin.H{"error": msgNotFound}) + case errors.Is(err, service.ErrConflict): + c.JSON(http.StatusConflict, gin.H{"error": errorDetail(err, msgConflict)}) + case errors.Is(err, service.ErrValidation): + c.JSON(http.StatusBadRequest, gin.H{"error": errorDetail(err, msgValidation)}) + default: + c.JSON(http.StatusInternalServerError, gin.H{"error": errorDetail(err, msgInternal)}) + } +} + +const msgValidation = "invalid request" + +// errorDetail strips the sentinel prefix ("resource conflict: ") from a wrapped +// error so the client sees "username already exists" and not the marker. +func errorDetail(err error, fallback string) string { + if err == nil { + return fallback + } + msg := err.Error() + for _, prefix := range []string{ + service.ErrNotFound.Error() + ": ", + service.ErrConflict.Error() + ": ", + service.ErrValidation.Error() + ": ", + } { + if len(msg) > len(prefix) && msg[:len(prefix)] == prefix { + return msg[len(prefix):] + } + } + if msg == "" { + return fallback + } + return msg +} diff --git a/internal/handler/handler_test.go b/internal/handler/handler_test.go new file mode 100644 index 00000000..6a63ee3f --- /dev/null +++ b/internal/handler/handler_test.go @@ -0,0 +1,258 @@ +package handler + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + "time" + + "github.com/e-spl/e-sp-line2/internal/config" + "github.com/e-spl/e-sp-line2/internal/models" + "github.com/e-spl/e-sp-line2/internal/repository" + "github.com/e-spl/e-sp-line2/internal/service" + "github.com/e-spl/e-sp-line2/pkg/logger" + "github.com/gin-gonic/gin" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + gormlogger "gorm.io/gorm/logger" +) + +// The handler package had no tests at all (review item M13), so every status +// code regression — including the "deleted something that does not exist → 200" +// one — went unnoticed. These cases pin the not-found/conflict contract on a +// real sqlite database: sqlite is cgo-backed, hence the CGO_ENABLED=1 test lane. + +func TestMain(m *testing.M) { + gin.SetMode(gin.TestMode) + logger.Init("error") + code := m.Run() + logger.Sync() + os.Exit(code) +} + +type testEnv struct { + db *gorm.DB + router *gin.Engine + user *service.UserService + auth *service.AuthService + route *service.RouteService + message *service.MessageService +} + +func newTestEnv(t *testing.T) *testEnv { + t.Helper() + db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{ + Logger: gormlogger.Default.LogMode(gormlogger.Silent), + }) + if err != nil { + t.Fatalf("open db: %v", err) + } + // One connection keeps the shared in-memory database from being torn down + // between statements of a case. + sqlDB, err := db.DB() + if err != nil { + t.Fatalf("sql db: %v", err) + } + sqlDB.SetMaxOpenConns(1) + + if err := db.AutoMigrate( + &models.User{}, + &models.Platform{}, + &models.AdapterPackage{}, + &models.AdapterInstance{}, + &models.InboundEvent{}, + &models.RouteRule{}, + &models.SystemSetting{}, + ); err != nil { + t.Fatalf("migrate: %v", err) + } + + env := &testEnv{ + db: db, + user: service.NewUserService(repository.NewUserRepository(db), repository.NewSystemSettingRepository(db)), + route: service.NewRouteService(repository.NewRouteRuleRepository(db)), + message: service.NewMessageService(repository.NewInboundEventRepository(db), repository.NewInstanceRepository(db)), + } + env.auth = service.NewAuthService(&config.Config{JWT: config.JWTConfig{Secret: "test-secret", Expiration: 1}}, + repository.NewUserRepository(db), repository.NewSystemSettingRepository(db)) + + // Mount the real handlers behind a stub middleware that supplies userID, + // so the cases exercise the handler+service+repository stack, not a mock. + withUser := func(c *gin.Context) { + c.Set("userID", "test-user") + c.Next() + } + r := gin.New() + v1 := r.Group("/api/v1", withUser) + v1.DELETE("/routes/:id", DeleteRoute(env.route)) + v1.POST("/messages/:id/ack", AckMessage(env.message)) + instances := service.NewInstanceService(repository.NewInstanceRepository(db), nil) + v1.GET("/instances/:id", GetInstance(instances)) + v1.PUT("/instances/:id", UpdateInstance(instances)) + v1.POST("/auth/register", Register(env.auth)) + v1.POST("/users", CreateUser(env.user)) + env.router = r + return env +} + +func (e *testEnv) do(t *testing.T, method, path, body string) *httptest.ResponseRecorder { + t.Helper() + var reader *strings.Reader + if body == "" { + reader = strings.NewReader("") + } else { + reader = strings.NewReader(body) + } + req := httptest.NewRequest(method, path, reader) + if body != "" { + req.Header.Set("Content-Type", "application/json") + } + w := httptest.NewRecorder() + e.router.ServeHTTP(w, req) + return w +} + +// errBody decodes {"error": "..."} and doubles as a "is this JSON" assertion. +func errBody(t *testing.T, w *httptest.ResponseRecorder) string { + t.Helper() + var payload struct { + Error string `json:"error"` + } + if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil { + t.Fatalf("response body is not JSON: %q", w.Body.String()) + } + return payload.Error +} + +func TestDeleteMissingResourcesAre404(t *testing.T) { + env := newTestEnv(t) + + created, err := env.route.Create(&service.CreateRouteRequest{Name: "r", TargetType: "app", TargetID: "t"}) + if err != nil { + t.Fatalf("seed route: %v", err) + } + + cases := []struct { + name string + method string + path string + body string + want int + }{ + {"delete existing route", http.MethodDelete, "/api/v1/routes/" + created.ID, "", http.StatusOK}, + {"delete missing route", http.MethodDelete, "/api/v1/routes/does-not-exist", "", http.StatusNotFound}, + {"ack missing message", http.MethodPost, "/api/v1/messages/does-not-exist/ack", "", http.StatusNotFound}, + {"update missing instance", http.MethodPut, "/api/v1/instances/does-not-exist", `{"name":"x"}`, http.StatusNotFound}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + w := env.do(t, tc.method, tc.path, tc.body) + if w.Code != tc.want { + t.Fatalf("%s %s = %d, want %d (body %q)", tc.method, tc.path, w.Code, tc.want, w.Body.String()) + } + if tc.want == http.StatusNotFound { + if body := errBody(t, w); body != msgNotFound { + t.Fatalf("404 body = %q, want %q", body, msgNotFound) + } + } + }) + } +} + +func TestDuplicateNamesAre409(t *testing.T) { + env := newTestEnv(t) + + // Self-registration first: the second call must collide, and the body must + // keep the actionable reason rather than just the sentinel name. + if w := env.do(t, http.MethodPost, "/api/v1/auth/register", `{"username":"alice","password":"secret123"}`); w.Code != http.StatusCreated { + t.Fatalf("first register = %d, want 201 (body %q)", w.Code, w.Body.String()) + } + w := env.do(t, http.MethodPost, "/api/v1/auth/register", `{"username":"alice","password":"secret123"}`) + if w.Code != http.StatusConflict { + t.Fatalf("duplicate register = %d, want 409 (body %q)", w.Code, w.Body.String()) + } + if body := errBody(t, w); body != "username already exists" { + t.Fatalf("409 body = %q, want the reason without the sentinel prefix", body) + } + + // Admin create path uses the same mapping. + if w := env.do(t, http.MethodPost, "/api/v1/users", `{"username":"bob","password":"secret123"}`); w.Code != http.StatusCreated { + t.Fatalf("first admin create = %d, want 201 (body %q)", w.Code, w.Body.String()) + } + if w := env.do(t, http.MethodPost, "/api/v1/users", `{"username":"bob","password":"secret123"}`); w.Code != http.StatusConflict { + t.Fatalf("duplicate admin create = %d, want 409 (body %q)", w.Code, w.Body.String()) + } +} + +func TestValidationErrorsStay400(t *testing.T) { + env := newTestEnv(t) + + w := env.do(t, http.MethodPost, "/api/v1/users", `{"username":"short","password":"x"}`) + if w.Code != http.StatusBadRequest { + t.Fatalf("short password = %d, want 400 (body %q)", w.Code, w.Body.String()) + } + if body := errBody(t, w); body != "password must be at least 6 characters" { + t.Fatalf("400 body = %q, want the validation reason", body) + } +} + +// TestInstanceMetricsRoundTrip covers M10: the Monitoring page reads +// last_heartbeat / message_count / error_count off the instance JSON, so the +// columns must exist and the counters must move. +func TestInstanceMetricsRoundTrip(t *testing.T) { + env := newTestEnv(t) + + instance := &models.AdapterInstance{ + ID: "inst-1", + AdapterID: "adapter-1", + PlatformID: "platform-1", + Name: "shop", + Status: "stopped", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + } + if err := env.db.Create(instance).Error; err != nil { + t.Fatalf("seed instance: %v", err) + } + + // The JSON contract must always carry the three numbers, even at rest. + w := env.do(t, http.MethodGet, "/api/v1/instances/inst-1", "") + if w.Code != http.StatusOK { + t.Fatalf("instance detail = %d (body %q)", w.Code, w.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil { + t.Fatalf("instance JSON: %v", err) + } + for _, key := range []string{"last_heartbeat", "message_count", "error_count"} { + if _, ok := payload[key]; !ok { + t.Fatalf("instance JSON missing %q: %s", key, w.Body.String()) + } + } + + instances := service.NewInstanceService(repository.NewInstanceRepository(env.db), nil) + instances.RecordInbound("inst-1") + instances.RecordInbound("inst-1") + instances.RecordError("inst-1") + + got, err := instances.GetByID("inst-1") + if err != nil { + t.Fatalf("reload: %v", err) + } + if got.MessageCount != 2 { + t.Fatalf("message_count = %d, want 2", got.MessageCount) + } + if got.ErrorCount != 1 { + t.Fatalf("error_count = %d, want 1", got.ErrorCount) + } + if got.LastHeartbeat == nil { + t.Fatal("last_heartbeat = nil, want a timestamp after RecordInbound") + } + + // A heartbeat for an instance that vanished must not fail the caller. + instances.RecordHeartbeat("inst-1") + instances.RecordInbound("gone") +} diff --git a/internal/handler/instance.go b/internal/handler/instance.go index 0153a962..c0d1cc1e 100644 --- a/internal/handler/instance.go +++ b/internal/handler/instance.go @@ -33,7 +33,7 @@ func GetInstance(instanceService *service.InstanceService) gin.HandlerFunc { id := c.Param("id") instance, err := instanceService.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "instance not found"}) + respondError(c, err) return } @@ -76,7 +76,7 @@ func UpdateInstance(instanceService *service.InstanceService) gin.HandlerFunc { instance, err := instanceService.Update(id, &req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -89,7 +89,7 @@ func DeleteInstance(instanceService *service.InstanceService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := instanceService.Delete(id); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + respondError(c, err) return } diff --git a/internal/handler/message.go b/internal/handler/message.go index ce266b0f..254dec8e 100644 --- a/internal/handler/message.go +++ b/internal/handler/message.go @@ -33,7 +33,7 @@ func GetMessage(messageService *service.MessageService) gin.HandlerFunc { id := c.Param("id") message, err := messageService.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "message not found"}) + respondError(c, err) return } @@ -46,7 +46,7 @@ func AckMessage(messageService *service.MessageService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := messageService.Ack(id); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + respondError(c, err) return } diff --git a/internal/handler/platform.go b/internal/handler/platform.go index f478a46e..46d42faf 100644 --- a/internal/handler/platform.go +++ b/internal/handler/platform.go @@ -33,7 +33,7 @@ func GetPlatform(platformService *service.PlatformService) gin.HandlerFunc { id := c.Param("id") platform, err := platformService.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "platform not found"}) + respondError(c, err) return } @@ -52,7 +52,7 @@ func CreatePlatform(platformService *service.PlatformService) gin.HandlerFunc { platform, err := platformService.Create(&req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -72,7 +72,7 @@ func UpdatePlatform(platformService *service.PlatformService) gin.HandlerFunc { platform, err := platformService.Update(id, &req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -85,7 +85,7 @@ func DeletePlatform(platformService *service.PlatformService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := platformService.Delete(id); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + respondError(c, err) return } diff --git a/internal/handler/route.go b/internal/handler/route.go index 72c38dd2..74d1f6b9 100644 --- a/internal/handler/route.go +++ b/internal/handler/route.go @@ -33,7 +33,7 @@ func GetRoute(routeService *service.RouteService) gin.HandlerFunc { id := c.Param("id") route, err := routeService.GetByID(id) if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "route not found"}) + respondError(c, err) return } @@ -52,7 +52,7 @@ func CreateRoute(routeService *service.RouteService) gin.HandlerFunc { route, err := routeService.Create(&req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -72,7 +72,7 @@ func UpdateRoute(routeService *service.RouteService) gin.HandlerFunc { route, err := routeService.Update(id, &req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } @@ -80,12 +80,13 @@ func UpdateRoute(routeService *service.RouteService) gin.HandlerFunc { } } -// DeleteRoute deletes a route rule +// DeleteRoute deletes a route rule. Deleting a route that does not exist is a +// 404, not a 200. func DeleteRoute(routeService *service.RouteService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := routeService.Delete(id); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + respondError(c, err) return } diff --git a/internal/handler/user.go b/internal/handler/user.go index da21b2fb..b841b9d4 100644 --- a/internal/handler/user.go +++ b/internal/handler/user.go @@ -32,7 +32,7 @@ func CreateUser(userService *service.UserService) gin.HandlerFunc { return } if err := userService.Create(req.Username, req.Password, req.Role); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } c.JSON(http.StatusCreated, gin.H{"message": "user created"}) @@ -51,7 +51,7 @@ func UpdateUserRole(userService *service.UserService) gin.HandlerFunc { return } if err := userService.UpdateRole(id, req.Role); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } c.JSON(http.StatusOK, gin.H{"message": "role updated"}) @@ -70,7 +70,7 @@ func UpdateUserStatus(userService *service.UserService) gin.HandlerFunc { return } if err := userService.SetStatus(id, req.Status); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } c.JSON(http.StatusOK, gin.H{"message": "status updated"}) @@ -82,7 +82,7 @@ func DeleteUser(userService *service.UserService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") if err := userService.Delete(id); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + respondError(c, err) return } c.JSON(http.StatusOK, gin.H{"message": "user deleted"}) diff --git a/internal/handler/websocket.go b/internal/handler/websocket.go index 0fba4e0f..655deac1 100644 --- a/internal/handler/websocket.go +++ b/internal/handler/websocket.go @@ -53,6 +53,7 @@ func AdapterWebSocket( } logger.Info("Adapter connected", logger.String("instance_id", instanceID)) + messageService.RecordHeartbeat(instanceID) // Register the bridge connection with the gateway so outbound commands // can be routed back to it. A single writer goroutine pumps ALL frames @@ -330,8 +331,12 @@ func persistInboundMessage(messageService *service.MessageService, instanceID st if err != nil { logger.Warn("Failed to persist inbound message", logger.String("error", err.Error())) + messageService.RecordError(instanceID) return "" } + // Keep the instance-level counters the Monitoring page reads in step with + // what actually got stored. + messageService.RecordInbound(instanceID) return event.ID } diff --git a/internal/models/models.go b/internal/models/models.go index 09088b7a..19240d5d 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -37,17 +37,25 @@ type AdapterCapability struct { Enabled bool `json:"enabled" gorm:"default:true"` } -// AdapterInstance represents a specific account/shop instance +// AdapterInstance represents a specific account/shop instance. +// +// LastHeartbeat / MessageCount / ErrorCount are the instance-level counters the +// Monitoring page reads. They are maintained on the instance row (not derived +// from adapter_sessions) so they survive a bridge reconnect and stay available +// while the adapter is stopped. type AdapterInstance struct { - ID string `json:"id" gorm:"primaryKey"` - AdapterID string `json:"adapter_id" gorm:"index;not null"` - PlatformID string `json:"platform_id" gorm:"index;not null"` - Name string `json:"name" gorm:"not null"` - Config string `json:"config" gorm:"type:text"` // JSON config - Status string `json:"status" gorm:"default:'stopped'"` // stopped, running, error - UserID string `json:"user_id" gorm:"index"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + ID string `json:"id" gorm:"primaryKey"` + AdapterID string `json:"adapter_id" gorm:"index;not null"` + PlatformID string `json:"platform_id" gorm:"index;not null"` + Name string `json:"name" gorm:"not null"` + Config string `json:"config" gorm:"type:text"` // JSON config + Status string `json:"status" gorm:"default:'stopped'"` // stopped, running, error + UserID string `json:"user_id" gorm:"index"` + LastHeartbeat *time.Time `json:"last_heartbeat"` // last bridge/process heartbeat + MessageCount int64 `json:"message_count" gorm:"default:0"` // inbound messages received + ErrorCount int64 `json:"error_count" gorm:"default:0"` // recorded adapter errors + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // AdapterSession represents current running session diff --git a/internal/repository/adapter.go b/internal/repository/adapter.go index 77809884..ef07df9a 100644 --- a/internal/repository/adapter.go +++ b/internal/repository/adapter.go @@ -44,7 +44,14 @@ func (r *AdapterRepository) Update(adapter *models.AdapterPackage) error { // Delete deletes an adapter. func (r *AdapterRepository) Delete(id string) error { - return r.db.Delete(&models.AdapterPackage{}, "id = ?", id).Error + result := r.db.Delete(&models.AdapterPackage{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all adapters. @@ -89,5 +96,12 @@ func (r *AdapterPackageRepository) Update(pkg *models.AdapterPackage) error { // Delete deletes an adapter package. func (r *AdapterPackageRepository) Delete(id string) error { - return r.db.Delete(&models.AdapterPackage{}, "id = ?", id).Error + result := r.db.Delete(&models.AdapterPackage{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } diff --git a/internal/repository/adapter_gateway.go b/internal/repository/adapter_gateway.go index e32adfef..f3472eb9 100644 --- a/internal/repository/adapter_gateway.go +++ b/internal/repository/adapter_gateway.go @@ -67,7 +67,14 @@ func (r *AdapterGatewayRepository) UpdateLastConnected(id string, t interface{}) // Delete deletes an adapter. func (r *AdapterGatewayRepository) Delete(id string) error { - return r.db.Delete(&models.Adapter{}, "id = ?", id).Error + result := r.db.Delete(&models.Adapter{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all adapters. diff --git a/internal/repository/instance.go b/internal/repository/instance.go index 1a8e8534..19adf742 100644 --- a/internal/repository/instance.go +++ b/internal/repository/instance.go @@ -1,6 +1,8 @@ package repository import ( + "time" + "github.com/e-spl/e-sp-line2/internal/models" "gorm.io/gorm" ) @@ -51,7 +53,37 @@ func (r *InstanceRepository) Update(instance *models.AdapterInstance) error { // Delete deletes an instance func (r *InstanceRepository) Delete(id string) error { - return r.db.Delete(&models.AdapterInstance{}, "id = ?", id).Error + result := r.db.Delete(&models.AdapterInstance{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil +} + +// MarkHeartbeat records "the instance is alive" on the instance row. Missing +// ids are not an error here: heartbeats are best-effort and race with deletes. +func (r *InstanceRepository) MarkHeartbeat(id string) error { + return r.db.Model(&models.AdapterInstance{}).Where("id = ?", id). + UpdateColumn("last_heartbeat", time.Now()).Error +} + +// RecordInbound counts one inbound message and refreshes the heartbeat in a +// single UPDATE, so the hot path stays one statement. +func (r *InstanceRepository) RecordInbound(id string) error { + return r.db.Model(&models.AdapterInstance{}).Where("id = ?", id). + Updates(map[string]interface{}{ + "message_count": gorm.Expr("message_count + 1"), + "last_heartbeat": time.Now(), + }).Error +} + +// IncrementErrorCount counts one adapter error for the instance. +func (r *InstanceRepository) IncrementErrorCount(id string) error { + return r.db.Model(&models.AdapterInstance{}).Where("id = ?", id). + UpdateColumn("error_count", gorm.Expr("error_count + 1")).Error } // List lists all instances @@ -96,7 +128,14 @@ func (r *AdapterSessionRepository) Update(session *models.AdapterSession) error // Delete deletes a session func (r *AdapterSessionRepository) Delete(id string) error { - return r.db.Delete(&models.AdapterSession{}, "id = ?", id).Error + result := r.db.Delete(&models.AdapterSession{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // UpdateHeartbeat updates the last heartbeat time diff --git a/internal/repository/message.go b/internal/repository/message.go index f828eec7..cec27c53 100644 --- a/internal/repository/message.go +++ b/internal/repository/message.go @@ -47,8 +47,15 @@ func (r *InboundEventRepository) Update(event *models.InboundEvent) error { // UpdateStatus updates event status func (r *InboundEventRepository) UpdateStatus(id, status string) error { - return r.db.Model(&models.InboundEvent{}).Where("id = ?", id). - Update("status", status).Error + result := r.db.Model(&models.InboundEvent{}).Where("id = ?", id). + Update("status", status) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all events @@ -109,8 +116,15 @@ func (r *OutboundCommandRepository) Update(cmd *models.OutboundCommand) error { // UpdateStatus updates command status func (r *OutboundCommandRepository) UpdateStatus(id, status string) error { - return r.db.Model(&models.OutboundCommand{}).Where("id = ?", id). - Update("status", status).Error + result := r.db.Model(&models.OutboundCommand{}).Where("id = ?", id). + Update("status", status) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all commands @@ -163,7 +177,14 @@ func (r *RouteRuleRepository) Update(rule *models.RouteRule) error { // Delete deletes a route rule func (r *RouteRuleRepository) Delete(id string) error { - return r.db.Delete(&models.RouteRule{}, "id = ?", id).Error + result := r.db.Delete(&models.RouteRule{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all route rules diff --git a/internal/repository/platform.go b/internal/repository/platform.go index fa611077..9f542e21 100644 --- a/internal/repository/platform.go +++ b/internal/repository/platform.go @@ -47,7 +47,14 @@ func (r *PlatformRepository) Update(platform *models.Platform) error { // Delete deletes a platform func (r *PlatformRepository) Delete(id string) error { - return r.db.Delete(&models.Platform{}, "id = ?", id).Error + result := r.db.Delete(&models.Platform{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all platforms diff --git a/internal/repository/user.go b/internal/repository/user.go index 84637c5d..1bce169d 100644 --- a/internal/repository/user.go +++ b/internal/repository/user.go @@ -47,7 +47,14 @@ func (r *UserRepository) Update(user *models.User) error { // Delete deletes a user func (r *UserRepository) Delete(id string) error { - return r.db.Delete(&models.User{}, "id = ?", id).Error + result := r.db.Delete(&models.User{}, "id = ?", id) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // List lists all users diff --git a/internal/service/adapter.go b/internal/service/adapter.go index e68c14cf..0eff298e 100644 --- a/internal/service/adapter.go +++ b/internal/service/adapter.go @@ -95,7 +95,11 @@ func (s *AdapterService) Create(req *CreateAdapterRequest) (*models.AdapterPacka // GetByID gets an adapter by ID func (s *AdapterService) GetByID(id string) (*models.AdapterPackage, error) { - return s.repo.FindByID(id) + adapter, err := s.repo.FindByID(id) + if err != nil { + return nil, wrapNotFound(err, "adapter %s", id) + } + return adapter, nil } // GetByPlatformID gets adapters by platform ID @@ -107,7 +111,7 @@ func (s *AdapterService) GetByPlatformID(platformID string) ([]models.AdapterPac func (s *AdapterService) Update(id string, req *UpdateAdapterRequest) (*models.AdapterPackage, error) { adapter, err := s.repo.FindByID(id) if err != nil { - return nil, err + return nil, wrapNotFound(err, "adapter %s", id) } if req.Name != "" { @@ -149,7 +153,10 @@ func (s *AdapterService) Delete(id string) error { } } } - return s.repo.Delete(id) + if err := s.repo.Delete(id); err != nil { + return wrapNotFound(err, "adapter %s", id) + } + return nil } // List lists all adapters @@ -167,11 +174,11 @@ func (s *AdapterService) List(limit, offset int) ([]models.AdapterPackage, int64 func (s *AdapterService) StartAdapter(id string) error { adapter, err := s.repo.FindByID(id) if err != nil { - return err + return wrapNotFound(err, "adapter %s", id) } if adapter.Status == "active" { - return errors.New("adapter is already active") + return validationf("adapter %s is already active", id) } // Upsert a running session for the adapter instance diff --git a/internal/service/adapter_gateway.go b/internal/service/adapter_gateway.go index 9b5507d8..36720910 100644 --- a/internal/service/adapter_gateway.go +++ b/internal/service/adapter_gateway.go @@ -168,14 +168,18 @@ func (s *AdapterGatewayService) List(limit, offset int) ([]models.Adapter, int64 // GetByID gets an adapter by ID. func (s *AdapterGatewayService) GetByID(id string) (*models.Adapter, error) { - return s.adapterRepo.FindByID(id) + adapter, err := s.adapterRepo.FindByID(id) + if err != nil { + return nil, wrapNotFound(err, "adapter %s", id) + } + return adapter, nil } // Update updates an adapter. func (s *AdapterGatewayService) Update(id string, req *UpdateGatewayAdapterRequest) (*models.Adapter, error) { adapter, err := s.adapterRepo.FindByID(id) if err != nil { - return nil, err + return nil, wrapNotFound(err, "adapter %s", id) } if req.Name != nil { @@ -231,7 +235,7 @@ func (s *AdapterGatewayService) Update(id string, req *UpdateGatewayAdapterReque func (s *AdapterGatewayService) Delete(id string) error { err := s.adapterRepo.Delete(id) if err != nil { - return err + return wrapNotFound(err, "adapter %s", id) } if s.onChange != nil { @@ -368,11 +372,30 @@ func (s *AdapterGatewayService) PersistInboundMessage(instanceID string, payload if err != nil { logger.Warn("Failed to persist inbound message", logger.String("error", err.Error())) + s.RecordInstanceError(instanceID) return "" } + s.RecordInstanceMetric(instanceID) return event.ID } +// RecordInstanceMetric counts a persisted inbound message on the instance row +// so the Monitoring page stops reading a hard-coded 0. Best-effort. +func (s *AdapterGatewayService) RecordInstanceMetric(instanceID string) { + if instanceID == "" || s.messageService == nil { + return + } + s.messageService.RecordInbound(instanceID) +} + +// RecordInstanceError counts a failure attributed to the instance. +func (s *AdapterGatewayService) RecordInstanceError(instanceID string) { + if instanceID == "" || s.messageService == nil { + return + } + s.messageService.RecordError(instanceID) +} + // getStringField extracts a string field from a payload map. func getStringField(payload map[string]interface{}, key string) string { if payload == nil { diff --git a/internal/service/auth.go b/internal/service/auth.go index 464aa811..9234a8a5 100644 --- a/internal/service/auth.go +++ b/internal/service/auth.go @@ -60,14 +60,14 @@ func (s *AuthService) Register(req *RegisterRequest) (*AuthResponse, error) { // Respect the registration toggle (default: enabled). if s.settings != nil { if v, err := s.settings.Get(RegistrationEnabledKey); err == nil && v == "false" { - return nil, errors.New("registration is disabled") + return nil, validationf("registration is disabled") } } // Check if username already exists existing, _ := s.repo.FindByUsername(req.Username) if existing != nil { - return nil, errors.New("username already exists") + return nil, conflictf("username already exists") } // Hash password diff --git a/internal/service/errors.go b/internal/service/errors.go new file mode 100644 index 00000000..8c570f3e --- /dev/null +++ b/internal/service/errors.go @@ -0,0 +1,45 @@ +package service + +import ( + "errors" + "fmt" + + "gorm.io/gorm" +) + +// Sentinel errors describing *why* a service call failed. Handlers translate +// them into HTTP status codes (404 / 409 / 400 / 500) so the API stops +// collapsing "not found", "conflict" and "internal error" into one status. +// +// Wrap them, never compare by string: callers use errors.Is. +var ( + // ErrNotFound marks a lookup or write that matched no row. + ErrNotFound = errors.New("resource not found") + // ErrConflict marks a write that collides with existing state + // (duplicate username, duplicate platform code, last-admin guards). + ErrConflict = errors.New("resource conflict") + // ErrValidation marks a well-formed request the service refused to apply. + ErrValidation = errors.New("validation failed") +) + +// wrapNotFound converts a gorm.ErrRecordNotFound into an ErrNotFound that +// carries the offending entity and id. Other errors pass through untouched. +func wrapNotFound(err error, format string, a ...any) error { + if err == nil { + return nil + } + if !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + return fmt.Errorf("%w: %s", ErrNotFound, fmt.Sprintf(format, a...)) +} + +// conflictf builds an ErrConflict. +func conflictf(format string, a ...any) error { + return fmt.Errorf("%w: %s", ErrConflict, fmt.Sprintf(format, a...)) +} + +// validationf builds an ErrValidation. +func validationf(format string, a ...any) error { + return fmt.Errorf("%w: %s", ErrValidation, fmt.Sprintf(format, a...)) +} diff --git a/internal/service/instance.go b/internal/service/instance.go index eb0dc410..d4b78f9b 100644 --- a/internal/service/instance.go +++ b/internal/service/instance.go @@ -270,9 +270,50 @@ func messageForStatus(status string) string { } } +// RecordInbound counts an inbound message and refreshes the instance's +// heartbeat. Best-effort: a racing delete must not fail message ingest. +func (s *InstanceService) RecordInbound(id string) { + if id == "" { + return + } + if err := s.repo.RecordInbound(id); err != nil { + logger.Warn("Failed to record inbound instance metric", + logger.String("instance_id", id), + logger.String("error", err.Error())) + } +} + +// RecordHeartbeat refreshes the instance's last heartbeat. +func (s *InstanceService) RecordHeartbeat(id string) { + if id == "" { + return + } + if err := s.repo.MarkHeartbeat(id); err != nil { + logger.Warn("Failed to record instance heartbeat", + logger.String("instance_id", id), + logger.String("error", err.Error())) + } +} + +// RecordError bumps the instance's error counter. +func (s *InstanceService) RecordError(id string) { + if id == "" { + return + } + if err := s.repo.IncrementErrorCount(id); err != nil { + logger.Warn("Failed to record instance error metric", + logger.String("instance_id", id), + logger.String("error", err.Error())) + } +} + // GetByID gets an instance by ID func (s *InstanceService) GetByID(id string) (*models.AdapterInstance, error) { - return s.repo.FindByID(id) + instance, err := s.repo.FindByID(id) + if err != nil { + return nil, wrapNotFound(err, "instance %s", id) + } + return instance, nil } // GetByUserID gets instances by user ID @@ -284,7 +325,7 @@ func (s *InstanceService) GetByUserID(userID string) ([]models.AdapterInstance, func (s *InstanceService) Update(id string, req *UpdateInstanceRequest) (*models.AdapterInstance, error) { instance, err := s.repo.FindByID(id) if err != nil { - return nil, err + return nil, wrapNotFound(err, "instance %s", id) } if req.Name != "" { @@ -309,6 +350,9 @@ func (s *InstanceService) Update(id string, req *UpdateInstanceRequest) (*models // running, it is stopped first to avoid orphan processes. The instance's // sandboxed adapter copy under data/instances/ is removed as well. func (s *InstanceService) Delete(id string) error { + if _, err := s.repo.FindByID(id); err != nil { + return wrapNotFound(err, "instance %s", id) + } if s.runner != nil && s.runner.IsRunning(id) { if err := s.runner.Stop(id); err != nil { return err diff --git a/internal/service/message.go b/internal/service/message.go index 777f89e9..4d2f316e 100644 --- a/internal/service/message.go +++ b/internal/service/message.go @@ -6,6 +6,7 @@ import ( "github.com/e-spl/e-sp-line2/internal/models" "github.com/e-spl/e-sp-line2/internal/repository" + "github.com/e-spl/e-sp-line2/pkg/logger" ) // MessageService handles inbound message operations @@ -80,12 +81,20 @@ func (s *MessageService) Create(req *CreateMessageRequest) (*models.InboundEvent // GetByID gets a message by ID func (s *MessageService) GetByID(id string) (*models.InboundEvent, error) { - return s.repo.FindByID(id) + event, err := s.repo.FindByID(id) + if err != nil { + return nil, wrapNotFound(err, "message %s", id) + } + return event, nil } -// Ack acknowledges a message +// Ack acknowledges a message. Acknowledging a message that does not exist is +// an ErrNotFound, not a success. func (s *MessageService) Ack(id string) error { - return s.repo.UpdateStatus(id, "acked") + if err := s.repo.UpdateStatus(id, "acked"); err != nil { + return wrapNotFound(err, "message %s", id) + } + return nil } // List lists all messages @@ -139,3 +148,40 @@ func serializeRawMessage(raw interface{}) string { } return string(data) } + +// RecordInbound counts one inbound message on the instance row and refreshes +// its heartbeat. Called from the WS ingest paths; best-effort by design. +func (s *MessageService) RecordInbound(instanceID string) { + if instanceID == "" || s.instanceRepo == nil { + return + } + if err := s.instanceRepo.RecordInbound(instanceID); err != nil { + logger.Warn("Failed to record inbound message metric", + logger.String("instance_id", instanceID), + logger.String("error", err.Error())) + } +} + +// RecordHeartbeat refreshes the instance heartbeat without counting a message. +func (s *MessageService) RecordHeartbeat(instanceID string) { + if instanceID == "" || s.instanceRepo == nil { + return + } + if err := s.instanceRepo.MarkHeartbeat(instanceID); err != nil { + logger.Warn("Failed to record heartbeat", + logger.String("instance_id", instanceID), + logger.String("error", err.Error())) + } +} + +// RecordError bumps the instance error counter. +func (s *MessageService) RecordError(instanceID string) { + if instanceID == "" || s.instanceRepo == nil { + return + } + if err := s.instanceRepo.IncrementErrorCount(instanceID); err != nil { + logger.Warn("Failed to record instance error metric", + logger.String("instance_id", instanceID), + logger.String("error", err.Error())) + } +} diff --git a/internal/service/platform.go b/internal/service/platform.go index 2eb50ac7..470980f0 100644 --- a/internal/service/platform.go +++ b/internal/service/platform.go @@ -1,7 +1,6 @@ package service import ( - "errors" "time" "github.com/e-spl/e-sp-line2/internal/models" @@ -37,7 +36,7 @@ func (s *PlatformService) Create(req *CreatePlatformRequest) (*models.Platform, // Check if code already exists existing, _ := s.repo.FindByCode(req.Code) if existing != nil { - return nil, errors.New("platform code already exists") + return nil, conflictf("platform code %q already exists", req.Code) } platform := &models.Platform{ @@ -59,7 +58,11 @@ func (s *PlatformService) Create(req *CreatePlatformRequest) (*models.Platform, // GetByID gets a platform by ID func (s *PlatformService) GetByID(id string) (*models.Platform, error) { - return s.repo.FindByID(id) + platform, err := s.repo.FindByID(id) + if err != nil { + return nil, wrapNotFound(err, "platform %s", id) + } + return platform, nil } // GetByCode gets a platform by code @@ -71,7 +74,7 @@ func (s *PlatformService) GetByCode(code string) (*models.Platform, error) { func (s *PlatformService) Update(id string, req *UpdatePlatformRequest) (*models.Platform, error) { platform, err := s.repo.FindByID(id) if err != nil { - return nil, err + return nil, wrapNotFound(err, "platform %s", id) } if req.Name != "" { @@ -92,9 +95,12 @@ func (s *PlatformService) Update(id string, req *UpdatePlatformRequest) (*models return platform, nil } -// Delete deletes a platform +// Delete deletes a platform. A missing id yields ErrNotFound. func (s *PlatformService) Delete(id string) error { - return s.repo.Delete(id) + if err := s.repo.Delete(id); err != nil { + return wrapNotFound(err, "platform %s", id) + } + return nil } // List lists all platforms diff --git a/internal/service/python_runner.go b/internal/service/python_runner.go index fd9f7a4a..21152838 100644 --- a/internal/service/python_runner.go +++ b/internal/service/python_runner.go @@ -1048,5 +1048,11 @@ func (r *PythonRunner) updateInstanceStatus(instanceID, status string) error { return err } instance.Status = status + // "running" means the process is (re)starting: stamp the first heartbeat so + // the Monitoring page does not show an em dash for a live adapter. + if status == "running" { + now := time.Now() + instance.LastHeartbeat = &now + } return r.instances.Update(instance) } diff --git a/internal/service/route.go b/internal/service/route.go index 8e398f78..4454860e 100644 --- a/internal/service/route.go +++ b/internal/service/route.go @@ -64,14 +64,18 @@ func (s *RouteService) Create(req *CreateRouteRequest) (*models.RouteRule, error // GetByID gets a route rule by ID func (s *RouteService) GetByID(id string) (*models.RouteRule, error) { - return s.repo.FindByID(id) + rule, err := s.repo.FindByID(id) + if err != nil { + return nil, wrapNotFound(err, "route %s", id) + } + return rule, nil } // Update updates a route rule func (s *RouteService) Update(id string, req *UpdateRouteRequest) (*models.RouteRule, error) { rule, err := s.repo.FindByID(id) if err != nil { - return nil, err + return nil, wrapNotFound(err, "route %s", id) } if req.Name != "" { @@ -101,9 +105,13 @@ func (s *RouteService) Update(id string, req *UpdateRouteRequest) (*models.Route return rule, nil } -// Delete deletes a route rule +// Delete deletes a route rule. A missing id yields ErrNotFound instead of a +// silent success. func (s *RouteService) Delete(id string) error { - return s.repo.Delete(id) + if err := s.repo.Delete(id); err != nil { + return wrapNotFound(err, "route %s", id) + } + return nil } // List lists all route rules diff --git a/internal/service/user.go b/internal/service/user.go index 7d74974d..5f3c6d7e 100644 --- a/internal/service/user.go +++ b/internal/service/user.go @@ -1,7 +1,6 @@ package service import ( - "errors" "time" "github.com/e-spl/e-sp-line2/internal/models" @@ -40,17 +39,17 @@ func (s *UserService) List() ([]models.User, error) { // Create adds a new user (admin action). role may be "admin" or "user". func (s *UserService) Create(username, password, role string) error { if username == "" || password == "" { - return errors.New("username and password are required") + return validationf("username and password are required") } if len(password) < 6 { - return errors.New("password must be at least 6 characters") + return validationf("password must be at least 6 characters") } if role != "admin" && role != "user" { role = "user" } existing, _ := s.repo.FindByUsername(username) if existing != nil { - return errors.New("username already exists") + return conflictf("username already exists") } hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { @@ -72,11 +71,11 @@ func (s *UserService) Create(username, password, role string) error { // another admin; the last admin cannot be demoted. func (s *UserService) UpdateRole(id, role string) error { if role != "admin" && role != "user" { - return errors.New("invalid role") + return validationf("invalid role") } user, err := s.repo.FindByID(id) if err != nil { - return errors.New("user not found") + return wrapNotFound(err, "user %s", id) } if user.Role == "admin" && role != "admin" { // Prevent demoting the last admin. @@ -85,7 +84,7 @@ func (s *UserService) UpdateRole(id, role string) error { return err } if admins <= 1 { - return errors.New("cannot demote the last admin") + return conflictf("cannot demote the last admin") } } user.Role = role @@ -95,11 +94,11 @@ func (s *UserService) UpdateRole(id, role string) error { // SetStatus enables or disables a user account. func (s *UserService) SetStatus(id, status string) error { if status != "active" && status != "disabled" { - return errors.New("invalid status") + return validationf("invalid status") } user, err := s.repo.FindByID(id) if err != nil { - return errors.New("user not found") + return wrapNotFound(err, "user %s", id) } if user.Role == "admin" && status != "active" { admins, err := s.adminCount() @@ -107,7 +106,7 @@ func (s *UserService) SetStatus(id, status string) error { return err } if admins <= 1 { - return errors.New("cannot disable the last admin") + return conflictf("cannot disable the last admin") } } user.Status = status @@ -118,7 +117,7 @@ func (s *UserService) SetStatus(id, status string) error { func (s *UserService) Delete(id string) error { user, err := s.repo.FindByID(id) if err != nil { - return errors.New("user not found") + return wrapNotFound(err, "user %s", id) } if user.Role == "admin" { admins, err := s.adminCount() @@ -126,10 +125,13 @@ func (s *UserService) Delete(id string) error { return err } if admins <= 1 { - return errors.New("cannot delete the last admin") + return conflictf("cannot delete the last admin") } } - return s.repo.Delete(id) + if err := s.repo.Delete(id); err != nil { + return wrapNotFound(err, "user %s", id) + } + return nil } // RegistrationEnabled reports whether self-registration is allowed. diff --git a/migrations/003_instance_monitoring_counters.sql b/migrations/003_instance_monitoring_counters.sql new file mode 100644 index 00000000..2d9f3d07 --- /dev/null +++ b/migrations/003_instance_monitoring_counters.sql @@ -0,0 +1,52 @@ +-- E-SP-Line2 Database Migration +-- Version: 003 +-- Description: Instance-level monitoring counters read by the Monitoring page +-- +-- AdapterInstance carries the three metrics the Monitoring UI reads. Before +-- this migration the page mapped instance.last_heartbeat / message_count / +-- error_count while models.AdapterInstance had none of them, so every number +-- was a hard-coded 0. The counters are NOT NULL with a 0 default so the JSON +-- contract is a number, never null; last_heartbeat stays nullable because +-- "no heartbeat yet" and "heartbeat at the epoch" must be distinguishable. +-- +-- Dialect note: `ALTER TABLE ... ADD COLUMN IF NOT EXISTS` is PostgreSQL-only; +-- SQLite rejects it even at 3.53. The statements below are the plain form so +-- the same file runs on both, which means a re-run errors on the ADD COLUMN +-- lines after the first application. That is harmless in practice: GORM +-- AutoMigrate creates the same columns on every startup, so SQLite dev boxes +-- need not run this file at all, and PostgreSQL operators can prefix +-- `IF NOT EXISTS` if they script it. + +ALTER TABLE adapter_instances ADD COLUMN last_heartbeat TIMESTAMP; +ALTER TABLE adapter_instances ADD COLUMN message_count BIGINT NOT NULL DEFAULT 0; +ALTER TABLE adapter_instances ADD COLUMN error_count BIGINT NOT NULL DEFAULT 0; + +-- The Monitoring page lists instances by recent activity. +CREATE INDEX IF NOT EXISTS idx_adapter_instances_last_heartbeat + ON adapter_instances (last_heartbeat); + +-- Backfill existing rows from the session table where a session already knows +-- the last heartbeat. message_count / error_count start at 0: there is no +-- historical source for them. +UPDATE adapter_instances +SET last_heartbeat = ( + SELECT s.last_heartbeat + FROM adapter_sessions s + WHERE s.instance_id = adapter_instances.id +) +WHERE last_heartbeat IS NULL + AND EXISTS ( + SELECT 1 FROM adapter_sessions s + WHERE s.instance_id = adapter_instances.id + AND s.last_heartbeat IS NOT NULL + ); + +-- Verify: +-- PRAGMA table_info(adapter_instances); -- SQLite +-- \d adapter_instances -- PostgreSQL + +-- Rollback: +-- DROP INDEX IF EXISTS idx_adapter_instances_last_heartbeat; +-- ALTER TABLE adapter_instances DROP COLUMN IF EXISTS error_count; +-- ALTER TABLE adapter_instances DROP COLUMN IF EXISTS message_count; +-- ALTER TABLE adapter_instances DROP COLUMN IF EXISTS last_heartbeat; diff --git a/migrations/README.md b/migrations/README.md index 43248def..e8d65e12 100644 --- a/migrations/README.md +++ b/migrations/README.md @@ -40,6 +40,8 @@ XXX_description.sql | Version | File | Description | |------|------|------| | 001 | 001_initial_schema.sql | Initial database schema | +| 002 | 002_adapter_gateway.sql | Adapter gateway (接入器) entities and connections | +| 003 | 003_instance_monitoring_counters.sql | Instance-level monitoring counters (heartbeat, message/error counts) | ## Writing a migration