Skip to content

Commit

Permalink
fix(gebura): tested app inst run time
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Mar 14, 2024
1 parent 699263b commit fed04fa
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 167 deletions.
14 changes: 10 additions & 4 deletions app/sephirah/internal/biz/bizgebura/run_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func (g *Gebura) AddAppInstRunTime(
ctx context.Context,
packageID model.InternalID,
instID model.InternalID,
timeRange *model.TimeRange,
) *errors.Error {
claims := libauth.FromContextAssertUserType(ctx)
Expand All @@ -24,7 +24,10 @@ func (g *Gebura) AddAppInstRunTime(
if timeRange == nil {
return pb.ErrorErrorReasonBadRequest("empty time range")
}
err := g.repo.AddAppInstRunTime(ctx, claims.UserID, packageID, timeRange)
if timeRange.Duration <= 0 {
return pb.ErrorErrorReasonBadRequest("invalid time range")
}
err := g.repo.AddAppInstRunTime(ctx, claims.UserID, instID, timeRange)
if err != nil {
return pb.ErrorErrorReasonUnspecified("%s", err.Error())
}
Expand All @@ -33,7 +36,7 @@ func (g *Gebura) AddAppInstRunTime(

func (g *Gebura) SumAppInstRunTime(
ctx context.Context,
packageID model.InternalID,
instID model.InternalID,
timeRange *model.TimeRange,
) (time.Duration, error) {
claims := libauth.FromContextAssertUserType(ctx)
Expand All @@ -43,7 +46,10 @@ func (g *Gebura) SumAppInstRunTime(
if timeRange == nil {
return time.Duration(0), pb.ErrorErrorReasonBadRequest("empty time range")
}
res, err := g.repo.SumAppInstRunTime(ctx, claims.UserID, packageID, timeRange)
if timeRange.Duration <= 0 {
return time.Duration(0), pb.ErrorErrorReasonBadRequest("invalid time range")
}
res, err := g.repo.SumAppInstRunTime(ctx, claims.UserID, instID, timeRange)
if err != nil {
return time.Duration(0), pb.ErrorErrorReasonUnspecified("%s", err.Error())
}
Expand Down
33 changes: 23 additions & 10 deletions app/sephirah/internal/data/gebura.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,29 +504,42 @@ func (g geburaRepo) ListAppInsts(
func (g geburaRepo) AddAppInstRunTime(
ctx context.Context,
userID model.InternalID,
packageID model.InternalID,
instID model.InternalID,
timeRange *model.TimeRange,
) error {
return g.data.db.AppInstRunTime.Create().
SetUserID(userID).
SetAppID(packageID).
SetAppInstID(instID).
SetStartTime(timeRange.StartTime).
SetRunDuration(timeRange.Duration).Exec(ctx)
SetRunDuration(timeRange.Duration).
Exec(ctx)
}

func (g geburaRepo) SumAppInstRunTime(
ctx context.Context,
userID model.InternalID,
packageID model.InternalID,
instID model.InternalID,
timeRange *model.TimeRange,
) (time.Duration, error) {
res, err := g.data.db.AppInstRunTime.Query().Where(
var v []struct {
Sum time.Duration
}
err := g.data.db.AppInstRunTime.Query().Where(
appinstruntime.UserIDEQ(userID),
appinstruntime.AppIDEQ(packageID),
appinstruntime.StartTimeGTE(timeRange.StartTime),
appinstruntime.StartTimeLTE(timeRange.StartTime.Add(timeRange.Duration)),
appinstruntime.AppInstIDEQ(instID),
appinstruntime.And(
appinstruntime.StartTimeGTE(timeRange.StartTime),
appinstruntime.StartTimeLTE(timeRange.StartTime.Add(timeRange.Duration)),
),
).Aggregate(
ent.Sum(appinstruntime.FieldRunDuration),
).Only(ctx)
return res.RunDuration, err
).Scan(ctx, &v)
if err != nil {
return time.Duration(0), err
}
var res time.Duration
for _, rt := range v {
res += rt.Sum
}
return res, nil
}
16 changes: 8 additions & 8 deletions app/sephirah/internal/data/internal/ent/appinstruntime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions app/sephirah/internal/data/internal/ent/appinstruntime/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fed04fa

Please sign in to comment.