Skip to content

Commit

Permalink
fix: entity update/delete message format
Browse files Browse the repository at this point in the history
  • Loading branch information
minoic committed Jan 11, 2024
1 parent eb36154 commit 1be8dd1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion modules/backend/app/mobile/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ from(bucket: "backend")
wg.Add(1)
go func() {
defer wg.Done()
series, err := storage.QuickRCSearch[[]bool](ctx, "EntityLiveSeries"+id.ID, func() ([]bool, error) {
series, err := storage.QuickRCSearch[[]bool](ctx, "ENTITY_LIVE_SERIES_"+id.ID, func() ([]bool, error) {
var ret []bool
var query *api.QueryTableResult
if id.Scheme != nil {
Expand Down
51 changes: 45 additions & 6 deletions modules/backend/app/mobile/entity/entity_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/bellis-daemon/bellis/common/cryptoo"
"github.com/bellis-daemon/bellis/common/models"
"github.com/bellis-daemon/bellis/common/storage"
"github.com/bellis-daemon/bellis/modules/backend/assertion"
"github.com/bellis-daemon/bellis/modules/backend/producer"
"github.com/minoic/glgf"
"github.com/spf13/cast"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)

var errEntityOwnership = errors.New("No permission to this entity! ")
var errEntityOwnership = errors.New("no permission to this entity! ")

func checkEntityOwnershipById(ctx context.Context, user *models.User, entityID string) assertion.AssertionFunc {
return func() error {
ok, err := storage.QuickRCSearch(ctx, "EntityOwnership"+user.ID.Hex()+entityID, func() (bool, error) {
ok, err := storage.QuickRCSearch(ctx, "ENTITY_OWNERSHIP_"+user.ID.Hex()+entityID, func() (bool, error) {
eid, err := primitive.ObjectIDFromHex(entityID)
if err != nil {
return false, err
Expand Down Expand Up @@ -77,8 +79,45 @@ func loadPublicOptions(src *Entity, dst *models.Application) {
}
}

func getEntityAvalibility(ctx context.Context, entityID string, duration string) float64 {
a, err := storage.QuickRCSearch(ctx, "AVALIBILITY_"+entityID, func() (float64, error) {
result, err := storage.QueryInfluxDB.Query(ctx, fmt.Sprintf(`
total = from(bucket: "backend")
|> range(start: %s)
|> filter(fn: (r) => r["id"] == "%s")
|> filter(fn: (r) => r["_field"] == "c_live")
total
|> count()
|> yield(name: "total")
total
|> filter(fn: (r) => r["_value"] == true)
|> count()
|> yield(name: "live")
`, duration, entityID))
if err != nil {
return 0, err
}
var total, live float64 = 0.0, 1.0
for result.Next() {
if result.Record().Result() == "total" {
total = cast.ToFloat64(result.Record().Value())
} else if result.Record().Result() == "live" {
live = cast.ToFloat64(result.Record().Value())
}
}
return live / total, nil
})
if err != nil {
glgf.Error(err)
return 0
}
return *a
}

func getEntityUptime(ctx context.Context, entityID string) string {
s, err := storage.QuickRCSearch(ctx, "Uptime"+entityID, func() (string, error) {
s, err := storage.QuickRCSearch(ctx, "UPTIME_"+entityID, func() (string, error) {
id, err := primitive.ObjectIDFromHex(entityID)
if err != nil {
return cryptoo.FormatDuration(0), err
Expand All @@ -92,14 +131,14 @@ func getEntityUptime(ctx context.Context, entityID string) string {
if errF != nil {
return cryptoo.FormatDuration(0), fmt.Errorf("cant find entity by id %s: %w", entityID, errF)
}
return cryptoo.FormatDuration(time.Now().Sub(entity.CreatedAt)), nil
return cryptoo.FormatDuration(time.Since(entity.CreatedAt)), nil
}
return cryptoo.FormatDuration(0), fmt.Errorf("cant find offline log by EntityID %s: %w", entityID, err)
}
if offlineLog.OnlineTime.IsZero() {
return cryptoo.FormatDuration(0), nil
}
return cryptoo.FormatDuration(time.Now().Sub(offlineLog.OnlineTime)), nil
return cryptoo.FormatDuration(time.Since(offlineLog.OnlineTime)), nil
})
if err != nil {
glgf.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion modules/backend/app/web/services/sentry_singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func SentrySingletonAuth() gin.HandlerFunc {
return
}

idHex, err := storage.QuickRCSearch[string](ctx, "SENTRY_SINGLETON_TOKEN"+token, func() (string, error) {
idHex, err := storage.QuickRCSearch[string](ctx, "SENTRY_SINGLETON_TOKEN_"+token, func() (string, error) {
var user models.User
err := storage.CUser.FindOne(ctx, bson.M{"CustomSentries": token}).Decode(&user)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions modules/backend/producer/entity_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NoticeEntityUpdate(ctx context.Context, id string, entity *models.Applicati
MaxLen: 256,
Approx: true,
Values: map[string]interface{}{
"id": id,
"EntityID": id,
"Entity": s,
},
}).Err()
Expand All @@ -31,7 +31,7 @@ func NoticeEntityDelete(ctx context.Context, id string) error {
MaxLen: 256,
Approx: true,
Values: map[string]interface{}{
"id": id,
"EntityID": id,
},
}).Err()
}

0 comments on commit 1be8dd1

Please sign in to comment.