generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/moevm/nosql2h24-factory
- Loading branch information
Showing
78 changed files
with
153,376 additions
and
525,656 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,56 @@ | ||
package influx_parser | ||
|
||
import ( | ||
"context" | ||
"vvnbd/internal/pkg/domain/authentication" | ||
"vvnbd/internal/pkg/domain/equipment" | ||
"vvnbd/internal/pkg/middleware" | ||
|
||
influx "github.com/influxdata/influxdb-client-go/v2/api/query" | ||
"github.com/labstack/echo" | ||
) | ||
|
||
type equipmentRepo interface { | ||
GetEquipment(ctx context.Context, key string) (equipment.Equipment, error) | ||
} | ||
|
||
type influxRepo interface { | ||
AnyQuery(ctx context.Context, query string, args ...interface{}) ([]*influx.FluxRecord, error) | ||
} | ||
|
||
type Handler struct { | ||
equipmentRepo equipmentRepo | ||
influxRepo influxRepo | ||
} | ||
|
||
func New( | ||
equipmentRepo equipmentRepo, | ||
influxRepo influxRepo, | ||
) *Handler { | ||
return &Handler{ | ||
equipmentRepo: equipmentRepo, | ||
influxRepo: influxRepo, | ||
} | ||
} | ||
|
||
func (h *Handler) RouteHandler(e *echo.Echo) { | ||
group := e.Group("/influx_parser") | ||
|
||
group.POST("/live-charts", | ||
middleware.Authenticate( | ||
authentication.NewRoleSet(authentication.ROLE_ADMIN), | ||
h.LiveCharts, | ||
)) | ||
|
||
group.POST("/get_working_percentage", | ||
middleware.Authenticate( | ||
authentication.NewRoleSet(authentication.ROLE_ADMIN), | ||
h.WorkingPercentage, | ||
)) | ||
|
||
group.POST("/work_percent", | ||
middleware.Authenticate( | ||
authentication.NewRoleSet(authentication.ROLE_ADMIN), | ||
h.WorkPercent, | ||
)) | ||
} |
91 changes: 91 additions & 0 deletions
91
backend/internal/pkg/handlers/influx_parser/live_charts.go
This file contains 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,91 @@ | ||
package influx_parser | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/labstack/echo" | ||
) | ||
|
||
type PointData struct { | ||
Time time.Time `json:"time"` | ||
Value interface{} `json:"value"` | ||
} | ||
|
||
type liveChartsRequest struct { | ||
StartTime string `json:"start_time"` | ||
EndTime string `json:"end_time"` | ||
Interval string `json:"interval"` | ||
AggFunc string `json:"aggregate_function"` | ||
EquipmentsParameters map[string]map[string][]string `json:"equipments_parameters"` | ||
} | ||
|
||
func (h *Handler) LiveCharts(c echo.Context) error { | ||
var request liveChartsRequest | ||
err := c.Bind(&request) | ||
if err != nil { | ||
fmt.Println(err) | ||
return c.NoContent(http.StatusBadRequest) | ||
} | ||
|
||
result := make(map[string]map[string]map[string][]interface{}) | ||
|
||
for equipKey, parameters := range request.EquipmentsParameters { | ||
equipment, err := h.equipmentRepo.GetEquipment(c.Request().Context(), equipKey) | ||
if err != nil { | ||
return c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to get equipment data for %s", equipKey)) | ||
} | ||
|
||
fmt.Println(equipment) | ||
|
||
result[equipKey] = make(map[string]map[string][]interface{}) | ||
|
||
for param, subParams := range parameters { | ||
if paramData, ok := equipment.Parameters[param]; ok { | ||
result[equipKey][param] = make(map[string][]interface{}) | ||
|
||
for _, subParam := range subParams { | ||
subParamData, ok := paramData.Subparameters[subParam] | ||
if !ok { | ||
log.Printf("Subparameter %s not found for equipment %s", subParam, equipKey) | ||
continue | ||
} | ||
topic := subParamData.Topic | ||
|
||
points, err := h.influxRepo.AnyQuery( | ||
c.Request().Context(), | ||
`|> range(start: %s, stop: %s) | ||
|> filter(fn: (r) => r.topic == "%s") | ||
|> filter(fn: (r) => r._field == "value") | ||
|> aggregateWindow(every: %s, fn: %s, createEmpty: false) | ||
`, | ||
request.StartTime, | ||
request.EndTime, | ||
topic, | ||
request.Interval, | ||
request.AggFunc, | ||
) | ||
if err != nil { | ||
return c.String(http.StatusInternalServerError, fmt.Sprintf("err while do query %s", err)) | ||
} | ||
|
||
var resultPoints []interface{} | ||
for _, point := range points { | ||
resultPoints = append(resultPoints, PointData{ | ||
Time: point.Time(), | ||
Value: point.Value(), | ||
}) | ||
} | ||
|
||
result[equipKey][param][subParam] = resultPoints | ||
} | ||
} else { | ||
log.Printf("Parameter %s not found for equipment %s", param, equipKey) | ||
} | ||
} | ||
} | ||
|
||
return c.JSON(http.StatusOK, result) | ||
} |
Oops, something went wrong.