Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lenanaidyonova committed Dec 15, 2024
2 parents 5cf0f69 + 24abb68 commit fa3e86f
Show file tree
Hide file tree
Showing 78 changed files with 153,376 additions and 525,656 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# mongodb
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example
DATABASE_HOST=mongo
DATABASE_HOST=db
DATABASE_NAME=nosql
USER_COLLECTION=users
LOGO_COLLECTION=logos
Expand All @@ -18,6 +18,6 @@ DOCKER_INFLUXDB_INIT_ORG=docs
DOCKER_INFLUXDB_INIT_BUCKET=test_bucket

# auth
ACCESS_TOKEN_LIFETIME=2m
ACCESS_TOKEN_LIFETIME=2h
REFRESH_TOKEN_LIFETIME=1h
SECRET_KEY=secret_key
4 changes: 2 additions & 2 deletions .github/workflows/5_prototype_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ jobs:
- name: Build docker-compose
run: |
docker-compose build --no-cache
docker compose build --no-cache
- name: Run docker-compose
run: |
docker-compose up -d
docker compose up -d
sleep 30
- name: Check containers are alive
Expand Down
40 changes: 36 additions & 4 deletions backend/cmd/main/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (
"vvnbd/internal/pkg/db"
authhandlers "vvnbd/internal/pkg/handlers/authentication"
equipmenthandler "vvnbd/internal/pkg/handlers/equipment"
"vvnbd/internal/pkg/handlers/influx_parser"
settingshandler "vvnbd/internal/pkg/handlers/settings"
"vvnbd/internal/pkg/handlers/snapshot"
warninghandler "vvnbd/internal/pkg/handlers/warning"
authrepo "vvnbd/internal/pkg/repositories/authentication"
"vvnbd/internal/pkg/repositories/equipment"
influx_repo "vvnbd/internal/pkg/repositories/influx"
"vvnbd/internal/pkg/repositories/logo"
"vvnbd/internal/pkg/repositories/mongo"
staffrepo "vvnbd/internal/pkg/repositories/staff"
warningrepo "vvnbd/internal/pkg/repositories/warning"
authservice "vvnbd/internal/pkg/service/authentication"
Expand Down Expand Up @@ -120,9 +124,37 @@ func RunApp(ctx context.Context, e *echo.Echo) error {
warningHandler := warninghandler.NewHandler(ctx, warningService)
warningHandler.RouteHandler(e)

// influxClient, err := db.NewInfluxClient(ctx)
// if err != nil {
// return fmt.Errorf("unable to create influx client Err: %w", err)
// }
influxClient, err := db.NewInfluxClient(ctx)
if err != nil {
return fmt.Errorf("unable to create influx client Err: %w", err)
}

influxOrg, err := config.GetValue(config.InfluxOrg)
if err != nil {
return fmt.Errorf("cannot get influx org from config. Err: %w", err)
}

influxBucket, err := config.GetValue(config.InfluxBucket)
if err != nil {
return fmt.Errorf("cannot get influx bucket from config. Err: %w", err)
}

influxRepo := influx_repo.NewRepository(
influxClient,
influxOrg,
influxBucket,
)

influxHandler := influx_parser.New(
equipmentRepo,
influxRepo,
)
influxHandler.RouteHandler(e)

mongoRepo := mongo.NewRepository(ctx, mongoClient, dbName)

snapshotHandler := snapshot.New(mongoRepo, influxRepo)
snapshotHandler.RouteHandler(e)

return nil
}
4 changes: 3 additions & 1 deletion backend/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const (
WarningCollection = "WARNING_COLLECTION"

// influx configs
InfluxToken = "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"
InfluxToken = "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"
InfluxOrg = "DOCKER_INFLUXDB_INIT_ORG"
InfluxBucket = "DOCKER_INFLUXDB_INIT_BUCKET"

// auth configs
AccessTokenLifetime = "ACCESS_TOKEN_LIFETIME"
Expand Down
11 changes: 11 additions & 0 deletions backend/internal/pkg/domain/warning/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ type Statistics struct {
StatNumber string `bson:"_id"`
Value float64 `bson:"value"`
}

type EquipmentStatistics struct {
TotalCount int `bson:"total_count"`
MaxExcess float64 `bson:"max_excess"`
MinExcess float64 `bson:"min_excess"`
AvgExcess float64 `bson:"avg_excess"`
MaxDuration float64 `bson:"max_duration"`
MinDuration float64 `bson:"min_duration"`
AvgDuration float64 `bson:"avg_duration"`
TotalDuration float64 `bson:"total_duration"`
}
62 changes: 43 additions & 19 deletions backend/internal/pkg/domain/warning/dto.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package warning

import "time"

type GetWarningsRequest struct {
PageNum int `json:"page" query:"page"`
PageLen int `json:"per_page" query:"per_page"`
ExcessPercent float64 `json:"excess_percent" query:"excess_percent"`
Equipment string `json:"equipment_key" query:"equipment_key"`
StartDate time.Time `json:"start_date" query:"start_date"`
EndDate time.Time `json:"end_date" query:"end_date"`
IsOrderASC bool `json:"order_ascending" query:"order_ascending"`
IsWithDescription bool `json:"with_description" query:"with_description"`
Viewed bool `json:"viewed" query:"viewed"`
PageNum int `json:"page" query:"page"`
PageLen int `json:"per_page" query:"per_page"`
ExcessPercent float64 `json:"excess_percent" query:"excess_percent"`
Equipment string `json:"equipment_key" query:"equipment_key"`
StartDate string `json:"start_date" query:"start_date"`
EndDate string `json:"end_date" query:"end_date"`
IsOrderASC bool `json:"order_ascending" query:"order_ascending"`
IsWithDescription bool `json:"with_description" query:"with_description"`
Viewed bool `json:"viewed" query:"viewed"`
}

type GetWarningsResponse struct {
Expand All @@ -30,15 +28,41 @@ type SetDescriptionsRequest struct {
type SetViewedRequest map[string]bool

type GetStatisticsRequest struct {
StartDate time.Time `json:"start_date"`
EndDate time.Time `json:"end_date"`
Equipment *string `json:"equipment"`
GroupBy string `json:"group_by"`
Metric string `json:"metric"`
ExcessPercent float64 `json:"excess_percent"`
StartDate string `json:"start_date" query:"start_date"`
EndDate string `json:"end_date" query:"end_date"`
Equipment string `json:"equipment" query:"equipment"`
GroupBy string `json:"group_by" query:"group_by"`
Metric string `json:"metric" query:"metric"`
ExcessPercent float64 `json:"excess_percent" query:"excess_percent"`
}

type GetStatisticsResponse struct {
Values []string `bson:"x"`
Points []float64 `bson:"y"`
Values string `json:"x"`
Points float64 `json:"y"`
}

type GetEquipmentStatisticsRequest struct {
StartDate string `json:"start_date" query:"start_date"`
EndDate string `json:"end_date" query:"end_date"`
Equipment string `json:"equipment" query:"equipment"`
ExcessPercent float64 `json:"excess_percent" query:"excess_percent"`
}

type GetEquipmentStatisticsResponse struct {
TotalCount int `json:"total_count" bson:"total_count"`
ExcessPercent ExcessPercentAll `json:"excess_percent" bson:"excess_percent"`
Duration DurationAll `json:"duration" bson:"duration"`
}

type ExcessPercentAll struct {
Max float64 `json:"max" bson:"max"`
Min float64 `json:"min" bson:"min"`
Avg float64 `json:"avg" bson:"avg"`
}

type DurationAll struct {
Max float64 `json:"max" bson:"max"`
Min float64 `json:"min" bson:"min"`
Avg float64 `json:"avg" bson:"avg"`
Total float64 `json:"total" bson:"total"`
}
56 changes: 56 additions & 0 deletions backend/internal/pkg/handlers/influx_parser/handler.go
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 backend/internal/pkg/handlers/influx_parser/live_charts.go
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)
}
Loading

0 comments on commit fa3e86f

Please sign in to comment.