-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement events feature #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
316f5af
feat(core): initialize codebase and implement core use cases
ngovinh2k2 552b41b
feat: add license apache 2.0 to source
ngovinh2k2 36669ad
Merge pull request #34 from Space-DF/feat/add-license-apache-2.0-to-s…
ngovinh2k2 ab80f8f
feat: implement events feature
lethanhdat762003 1a0da36
fix: resolve conflicts from merging dev
lethanhdat762003 1856eb3
refactor: move the rules to its own particular component
lethanhdat762003 baa6259
refactor: update default event rules bootstrap and optimize evaluatin…
lethanhdat762003 cdcb9b4
fix: ci issues
lethanhdat762003 cbc6ff8
fix: resolve comments which are removing unnecessary functions, varia…
lethanhdat762003 c8bf5fa
chore: remove unnecessary validations
lethanhdat762003 a05c46a
refactor: use device id from the transformer
lethanhdat762003 a80a779
refactor: removed unnecessary logics. optimized query for evaluating.…
lethanhdat762003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # RAK4630 Event Rules | ||
| brand: "rakwireless" | ||
| model: "rak4630" | ||
| model_id: "" # Will be resolved from device service | ||
| display_name: "RAK4630 Event Rules" | ||
|
|
||
| rules: | ||
| # Battery Low Warning | ||
| - rule_key: "battery" | ||
| operator: "lt" | ||
| operand: "3.3" | ||
| event_type: "device_event" | ||
| event_level: "system" | ||
lethanhdat762003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| description: "Battery voltage is low (< 3.3V)" | ||
| allow_new_event: true | ||
| is_active: true | ||
lethanhdat762003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Battery Critical Warning | ||
| - rule_key: "battery" | ||
| operator: "lt" | ||
| operand: "3.0" | ||
| event_type: "device_event" | ||
| event_level: "system" | ||
| description: "Battery voltage is critically low (< 3.0V)" | ||
| allow_new_event: true | ||
| is_active: true | ||
|
|
||
| # High Temperature Warning | ||
| - rule_key: "temperature" | ||
| operator: "gt" | ||
| operand: "50" | ||
| event_type: "device_event" | ||
| event_level: "system" | ||
| description: "Temperature is high (> 50°C)" | ||
| allow_new_event: true | ||
| is_active: true | ||
|
|
||
| # Low Temperature Warning | ||
| - rule_key: "temperature" | ||
| operator: "lt" | ||
| oper`nd: "-10" | ||
| event_type: "device_event" | ||
| event_level: "system" | ||
| description: "Temperature is very low (< -10°C)" | ||
| allow_new_event: true | ||
| is_active: true | ||
|
|
||
| # High Humidity Warning | ||
| - rule_key: "humidity" | ||
| operator: "gt" | ||
| operand: "90" | ||
| event_type: "device_event" | ||
| event_level: "system" | ||
| description: "Humidity is very high (> 90%)" | ||
| allow_new_event: true | ||
| is_active: true | ||
|
|
||
| # Low Humidity Warning | ||
| - rule_key: "humidity" | ||
| operator: "lt" | ||
| operand: "10" | ||
| event_type: "device_event" | ||
| event_level: "system" | ||
| description: "Humidity is very low (< 10%)" | ||
| allow_new_event: true | ||
| is_active: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| package events | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/Space-DF/telemetry-service/internal/api/common" | ||
| "github.com/Space-DF/telemetry-service/internal/models" | ||
| "github.com/Space-DF/telemetry-service/internal/timescaledb" | ||
| "github.com/labstack/echo/v4" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // getEventsByDevice returns all events for a specific device | ||
| func getEventsByDevice(logger *zap.Logger, tsClient *timescaledb.Client) echo.HandlerFunc { | ||
lethanhdat762003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return func(c echo.Context) error { | ||
| orgToUse := common.ResolveOrgFromRequest(c) | ||
| if orgToUse == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "Could not determine organization from hostname or X-Organization header", | ||
| }) | ||
| } | ||
|
|
||
| deviceID := strings.TrimSpace(c.Param("device_id")) | ||
| if deviceID == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "device_id is required", | ||
| }) | ||
| } | ||
|
|
||
| limit := 100 | ||
| if limitStr := c.QueryParam("limit"); limitStr != "" { | ||
| if l, err := strconv.Atoi(limitStr); err == nil && l > 0 { | ||
| limit = l | ||
| } | ||
| } | ||
|
|
||
| // Parse start_time and end_time query parameters | ||
| var startTime, endTime *int64 | ||
| if startTimeStr := c.QueryParam("start_time"); startTimeStr != "" { | ||
| if ms, err := strconv.ParseInt(startTimeStr, 10, 64); err == nil { | ||
| startTime = &ms | ||
| } | ||
| } | ||
| if endTimeStr := c.QueryParam("end_time"); endTimeStr != "" { | ||
| if ms, err := strconv.ParseInt(endTimeStr, 10, 64); err == nil { | ||
| endTime = &ms | ||
| } | ||
| } | ||
|
|
||
| ctx := timescaledb.ContextWithOrg(c.Request().Context(), orgToUse) | ||
| events, err := tsClient.GetEventsByDevice(ctx, orgToUse, deviceID, limit, startTime, endTime) | ||
| if err != nil { | ||
| logger.Error("failed to get events by device", | ||
| zap.String("device_id", deviceID), | ||
| zap.Error(err)) | ||
| return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
| "error": "failed to get events", | ||
| }) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, map[string]interface{}{ | ||
| "device_id": deviceID, | ||
| "events": events, | ||
| "count": len(events), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // getEventRules returns all event rules | ||
| func getEventRules(logger *zap.Logger, tsClient *timescaledb.Client) echo.HandlerFunc { | ||
| return func(c echo.Context) error { | ||
| orgToUse := common.ResolveOrgFromRequest(c) | ||
| if orgToUse == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "Could not determine organization from hostname or X-Organization header", | ||
| }) | ||
| } | ||
|
|
||
| page := 1 | ||
| if pageStr := c.QueryParam("page"); pageStr != "" { | ||
| if p, err := strconv.Atoi(pageStr); err == nil && p > 0 { | ||
| page = p | ||
| } | ||
| } | ||
|
|
||
| pageSize := 20 | ||
| if sizeStr := c.QueryParam("page_size"); sizeStr != "" { | ||
| if s, err := strconv.Atoi(sizeStr); err == nil && s > 0 && s <= 100 { | ||
| pageSize = s | ||
| } | ||
| } | ||
|
|
||
| deviceID := c.QueryParam("device_id") | ||
|
|
||
| ctx := timescaledb.ContextWithOrg(c.Request().Context(), orgToUse) | ||
| rules, total, err := tsClient.GetEventRules(ctx, deviceID, page, pageSize) | ||
| if err != nil { | ||
| logger.Error("failed to get event rules", | ||
| zap.Error(err)) | ||
| return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
| "error": "failed to get event rules", | ||
| }) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, models.EventRulesListResponse{ | ||
| Rules: rules, | ||
| TotalCount: total, | ||
| Page: page, | ||
| PageSize: pageSize, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // createEventRule creates a new event rule | ||
| func createEventRule(logger *zap.Logger, tsClient *timescaledb.Client) echo.HandlerFunc { | ||
| return func(c echo.Context) error { | ||
| orgToUse := common.ResolveOrgFromRequest(c) | ||
| if orgToUse == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "Could not determine organization from hostname or X-Organization header", | ||
| }) | ||
| } | ||
|
|
||
| var req models.EventRuleRequest | ||
| if err := c.Bind(&req); err != nil { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "invalid request body", | ||
| }) | ||
| } | ||
|
|
||
| ctx := timescaledb.ContextWithOrg(c.Request().Context(), orgToUse) | ||
| rule, err := tsClient.CreateEventRule(ctx, &req) | ||
| if err != nil { | ||
| logger.Error("failed to create event rule", | ||
| zap.Error(err)) | ||
| return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
| "error": "failed to create event rule", | ||
| }) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusCreated, rule) | ||
| } | ||
| } | ||
|
|
||
| // updateEventRule updates an existing event rule | ||
| func updateEventRule(logger *zap.Logger, tsClient *timescaledb.Client) echo.HandlerFunc { | ||
| return func(c echo.Context) error { | ||
| orgToUse := common.ResolveOrgFromRequest(c) | ||
| if orgToUse == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "Could not determine organization from hostname or X-Organization header", | ||
| }) | ||
| } | ||
|
|
||
| ruleID := strings.TrimSpace(c.Param("rule_id")) | ||
| if ruleID == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "rule_id is required", | ||
| }) | ||
| } | ||
|
|
||
| var req models.EventRuleRequest | ||
| if err := c.Bind(&req); err != nil { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "invalid request body", | ||
| }) | ||
| } | ||
|
|
||
| ctx := timescaledb.ContextWithOrg(c.Request().Context(), orgToUse) | ||
| rule, err := tsClient.UpdateEventRule(ctx, ruleID, &req) | ||
| if err != nil { | ||
| logger.Error("failed to update event rule", | ||
| zap.String("rule_id", ruleID), | ||
| zap.Error(err)) | ||
| return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
| "error": "failed to update event rule", | ||
| }) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, rule) | ||
| } | ||
| } | ||
|
|
||
| // deleteEventRule deletes an event rule | ||
| func deleteEventRule(logger *zap.Logger, tsClient *timescaledb.Client) echo.HandlerFunc { | ||
| return func(c echo.Context) error { | ||
| orgToUse := common.ResolveOrgFromRequest(c) | ||
| if orgToUse == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "Could not determine organization from hostname or X-Organization header", | ||
| }) | ||
| } | ||
|
|
||
| ruleID := strings.TrimSpace(c.Param("rule_id")) | ||
| if ruleID == "" { | ||
| return c.JSON(http.StatusBadRequest, map[string]string{ | ||
| "error": "rule_id is required", | ||
| }) | ||
| } | ||
|
|
||
| ctx := timescaledb.ContextWithOrg(c.Request().Context(), orgToUse) | ||
| if err := tsClient.DeleteEventRule(ctx, ruleID); err != nil { | ||
| logger.Error("failed to delete event rule", | ||
| zap.String("rule_id", ruleID), | ||
| zap.Error(err)) | ||
| return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
| "error": "failed to delete event rule", | ||
| }) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, map[string]string{ | ||
| "message": "event rule deleted successfully", | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.