Lightweight server monitoring with historical data, docker stats, and alerts.
fix(hub): support Battery numeric array unmarshaling with json/v2 (#2746, #2748)
Aditya raie0fc98862636
diff --git a/internal/entities/system/system.go b/internal/entities/system/system.goindex 002bdc8fc..49caecd68 100644--- a/internal/entities/system/system.go+++ b/internal/entities/system/system.go@@ -103,6 +103,15 @@ func (b Battery) MarshalJSON() ([]byte, error) {return json.Marshal([2]uint16{uint16(b[0]), uint16(b[1])})}+func (b *Battery) UnmarshalJSON(data []byte) error {+ var arr [2]uint8+ if err := json.Unmarshal(data, &arr); err != nil {+ return err+ }+ *b = arr+ return nil+}+type GPUData struct {Name string `json:"n" cbor:"0,keyasint"`Temperature float64 `json:"-"`diff --git a/internal/entities/system/system_test.go b/internal/entities/system/system_test.goindex d8addf71a..6602b1c0a 100644--- a/internal/entities/system/system_test.go+++ b/internal/entities/system/system_test.go@@ -76,9 +76,29 @@ func TestStatsDiskIOTotalAndFansTransport(t *testing.T) {}func TestStatsBatteryNumericArrayUnmarshal(t *testing.T) {- var stats Stats- require.NoError(t, json.Unmarshal([]byte(`{"bat":[50,4]}`), &stats))- assert.Equal(t, Battery{50, 4}, stats.Battery)+ for name, unmarshal := range map[string]func([]byte, any) error{+ "json_v1": json.Unmarshal,+ "json_v2": func(data []byte, val any) error { return jsonv2.Unmarshal(data, val) },+ } {+ t.Run(name, func(t *testing.T) {+ var stats Stats+ require.NoError(t, unmarshal([]byte(`{"bat":[50,4]}`), &stats))+ assert.Equal(t, Battery{50, 4}, stats.Battery)++ var info Info+ require.NoError(t, unmarshal([]byte(`{"bat":[59,5],"jl":true}`), &info))+ assert.True(t, info.SystemdLogs)+ assert.Equal(t, Battery{59, 5}, info.Battery)++ var bat Battery+ require.NoError(t, unmarshal([]byte(`[100,2]`), &bat))+ assert.Equal(t, Battery{100, 2}, bat)++ var nullBat Battery+ require.NoError(t, unmarshal([]byte(`null`), &nullBat))+ assert.Equal(t, Battery{0, 0}, nullBat)+ })+ }}func TestStatsLegacyBatteryPayload(t *testing.T) {diff --git a/internal/hub/api.go b/internal/hub/api.goindex 7feadc7cf..31a28819a 100644--- a/internal/hub/api.go+++ b/internal/hub/api.go@@ -503,7 +503,11 @@ func (h *Hub) getSystemdLogs(e *core.RequestEvent) error {return e.NotFoundError("", err)}var info systementity.Info- if err := systemRecord.UnmarshalJSONField("info", &info); err != nil || !info.SystemdLogs {+ if err := systemRecord.UnmarshalJSONField("info", &info); err != nil {+ e.App.Logger().Error("failed to unmarshal system info for systemd logs", "system", systemID, "err", err)+ return e.JSON(http.StatusOK, map[string]string{"logs": ""})+ }+ if !info.SystemdLogs {return e.JSON(http.StatusOK, map[string]string{"logs": ""})}diff --git a/internal/hub/api_test.go b/internal/hub/api_test.goindex c2b963380..88fdbded3 100644--- a/internal/hub/api_test.go+++ b/internal/hub/api_test.go@@ -707,6 +707,32 @@ func TestApiRoutesAuthentication(t *testing.T) {})},},+ {+ Name: "GET /systemd/logs - agent with battery and capability proceeds to fetch logs",+ Method: http.MethodGet,+ URL: fmt.Sprintf("/api/beszel/systemd/logs?system=%s&service=nginx.service", system.Id),+ Headers: map[string]string{+ "Authorization": userToken,+ },+ ExpectedStatus: 500,+ ExpectedContent: []string{"Something went wrong while processing your request."},+ TestAppFactory: testAppFactory,+ BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {+ beszelTests.CreateRecord(app, "systemd_services", map[string]any{+ "system": system.Id,+ "name": "nginx.service",+ "state": 0,+ "sub": 1,+ })+ sysRecord, err := app.FindRecordById("systems", system.Id)+ require.NoError(t, err)+ sysRecord.Set("info", map[string]any{+ "bat": []int{59, 5},+ "jl": true,+ })+ require.NoError(t, app.Save(sysRecord))+ },+ },// Auth Optional Routes - Should work without authentication{