Skip to content

Commit add77f7

Browse files
committed
feat: impl app run time
1 parent 32b7bf5 commit add77f7

File tree

27 files changed

+961
-383
lines changed

27 files changed

+961
-383
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ require (
3636
github.com/samber/lo v1.49.1
3737
github.com/sony/sonyflake v1.2.0
3838
github.com/spf13/afero v1.2.1
39-
github.com/tuihub/protos v0.5.0
39+
github.com/tuihub/protos v0.5.2
4040
go.opentelemetry.io/otel v1.34.0
4141
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.10.0
4242
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.10.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ
703703
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
704704
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
705705
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
706-
github.com/tuihub/protos v0.5.0 h1:j53mBoPlMd0BSx0G9J3RFe+4aWKzmzEqlQid9tcfDYM=
707-
github.com/tuihub/protos v0.5.0/go.mod h1:NzgjndKsJmA1tPevx2MTFbBLeIVC+ykrW87YpTp/ibA=
706+
github.com/tuihub/protos v0.5.2 h1:9FrBH3iz1OCMgHD7rVbcLaylU3Z7Ml9+Tqx0a/cWQJw=
707+
github.com/tuihub/protos v0.5.2/go.mod h1:NzgjndKsJmA1tPevx2MTFbBLeIVC+ykrW87YpTp/ibA=
708708
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
709709
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
710710
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=

internal/biz/bizgebura/run_time.go

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,97 @@ import (
77
"github.com/tuihub/librarian/internal/biz/bizutils"
88
"github.com/tuihub/librarian/internal/lib/libauth"
99
"github.com/tuihub/librarian/internal/model"
10+
"github.com/tuihub/librarian/internal/model/modelgebura"
1011
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"
1112

1213
"github.com/go-kratos/kratos/v2/errors"
1314
)
1415

15-
func (g *Gebura) AddAppRunTime(
16+
func (g *Gebura) BatchCreateAppRunTime(
1617
ctx context.Context,
17-
instID model.InternalID,
18-
timeRange *model.TimeRange,
18+
runTimes []*modelgebura.AppRunTime,
1919
) *errors.Error {
2020
claims := libauth.FromContextAssertUserType(ctx)
2121
if claims == nil {
2222
return bizutils.NoPermissionError()
2323
}
24-
if timeRange == nil {
25-
return pb.ErrorErrorReasonBadRequest("empty time range")
24+
for _, runTime := range runTimes {
25+
if runTime == nil || runTime.RunTime == nil {
26+
return pb.ErrorErrorReasonBadRequest("empty time range")
27+
}
28+
if runTime.RunTime.Duration <= 0 {
29+
return pb.ErrorErrorReasonBadRequest("invalid time range")
30+
}
2631
}
27-
if timeRange.Duration <= 0 {
28-
return pb.ErrorErrorReasonBadRequest("invalid time range")
32+
ids, err := g.id.BatchNew(len(runTimes))
33+
if err != nil {
34+
return pb.ErrorErrorReasonUnspecified("%s", err)
2935
}
30-
err := g.repo.AddAppRunTime(ctx, claims.UserID, instID, timeRange)
36+
for i, runTime := range runTimes {
37+
runTime.ID = ids[i]
38+
}
39+
err = g.repo.BatchCreateAppRunTime(ctx, claims.UserID, runTimes)
3140
if err != nil {
3241
return pb.ErrorErrorReasonUnspecified("%s", err.Error())
3342
}
3443
return nil
3544
}
3645

37-
func (g *Gebura) SumAppInstRunTime(
46+
func (g *Gebura) SumAppRunTime(
47+
ctx context.Context,
48+
appIDs []model.InternalID,
49+
deviceIDs []model.InternalID,
50+
timeRange *model.TimeRange,
51+
) (*time.Duration, error) {
52+
claims := libauth.FromContextAssertUserType(ctx)
53+
if claims == nil {
54+
return nil, bizutils.NoPermissionError()
55+
}
56+
if timeRange == nil {
57+
return nil, pb.ErrorErrorReasonBadRequest("empty time range")
58+
}
59+
if timeRange.Duration <= 0 {
60+
return nil, pb.ErrorErrorReasonBadRequest("invalid time range")
61+
}
62+
res, err := g.repo.SumAppRunTime(ctx, claims.UserID, appIDs, deviceIDs, timeRange)
63+
if err != nil {
64+
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
65+
}
66+
return &res, nil
67+
}
68+
69+
func (g *Gebura) ListAppRunTimes(
3870
ctx context.Context,
39-
instID model.InternalID,
71+
paging model.Paging,
72+
appIDs []model.InternalID,
73+
deviceIDs []model.InternalID,
4074
timeRange *model.TimeRange,
41-
) (time.Duration, error) {
75+
) ([]*modelgebura.AppRunTime, int, error) {
4276
claims := libauth.FromContextAssertUserType(ctx)
4377
if claims == nil {
44-
return time.Duration(0), bizutils.NoPermissionError()
78+
return nil, 0, bizutils.NoPermissionError()
4579
}
4680
if timeRange == nil {
47-
return time.Duration(0), pb.ErrorErrorReasonBadRequest("empty time range")
81+
return nil, 0, pb.ErrorErrorReasonBadRequest("empty time range")
4882
}
4983
if timeRange.Duration <= 0 {
50-
return time.Duration(0), pb.ErrorErrorReasonBadRequest("invalid time range")
84+
return nil, 0, pb.ErrorErrorReasonBadRequest("invalid time range")
85+
}
86+
res, total, err := g.repo.ListAppRunTimes(ctx, claims.UserID, paging, appIDs, deviceIDs, timeRange)
87+
if err != nil {
88+
return nil, 0, pb.ErrorErrorReasonUnspecified("%s", err.Error())
89+
}
90+
return res, total, nil
91+
}
92+
93+
func (g *Gebura) DeleteAppRunTime(ctx context.Context, id model.InternalID) error {
94+
claims := libauth.FromContextAssertUserType(ctx)
95+
if claims == nil {
96+
return bizutils.NoPermissionError()
5197
}
52-
res, err := g.repo.SumAppRunTime(ctx, claims.UserID, instID, timeRange)
98+
err := g.repo.DeleteAppRunTime(ctx, claims.UserID, id)
5399
if err != nil {
54-
return time.Duration(0), pb.ErrorErrorReasonUnspecified("%s", err.Error())
100+
return pb.ErrorErrorReasonUnspecified("%s", err.Error())
55101
}
56-
return res, nil
102+
return nil
57103
}

internal/data/gebura.go

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -428,39 +428,49 @@ func (g *GeburaRepo) ListApps(
428428
// Strings(ctx)
429429
//}
430430

431-
func (g *GeburaRepo) AddAppRunTime(
431+
func (g *GeburaRepo) BatchCreateAppRunTime(
432432
ctx context.Context,
433433
userID model.InternalID,
434-
instID model.InternalID,
435-
timeRange *model.TimeRange,
434+
runTimes []*modelgebura.AppRunTime,
436435
) error {
437-
return g.data.db.AppRunTime.Create().
438-
SetUserID(userID).
439-
SetAppID(instID).
440-
SetStartTime(timeRange.StartTime).
441-
SetRunDuration(timeRange.Duration).
442-
Exec(ctx)
436+
rt := make([]*ent.AppRunTimeCreate, 0, len(runTimes))
437+
for _, runTime := range runTimes {
438+
rt = append(rt, g.data.db.AppRunTime.Create().
439+
SetID(runTime.ID).
440+
SetUserID(userID).
441+
SetAppID(runTime.AppID).
442+
SetStartTime(runTime.RunTime.StartTime).
443+
SetDuration(runTime.RunTime.Duration),
444+
)
445+
}
446+
return g.data.db.AppRunTime.CreateBulk(rt...).Exec(ctx)
443447
}
444448

445449
func (g *GeburaRepo) SumAppRunTime(
446450
ctx context.Context,
447451
userID model.InternalID,
448-
instID model.InternalID,
452+
appIDs []model.InternalID,
453+
deviceIDs []model.InternalID,
449454
timeRange *model.TimeRange,
450455
) (time.Duration, error) {
451456
var v []struct {
452457
Sum time.Duration
453458
}
454-
err := g.data.db.AppRunTime.Query().Where(
455-
appruntime.UserIDEQ(userID),
456-
appruntime.AppIDEQ(instID),
457-
appruntime.And(
458-
appruntime.StartTimeGTE(timeRange.StartTime),
459-
appruntime.StartTimeLTE(timeRange.StartTime.Add(timeRange.Duration)),
460-
),
461-
).Aggregate(
462-
ent.Sum(appruntime.FieldRunDuration),
463-
).Scan(ctx, &v)
459+
q := g.data.db.AppRunTime.Query().Where(
460+
appruntime.UserIDEQ(userID))
461+
if len(appIDs) > 0 {
462+
q.Where(appruntime.AppIDIn(appIDs...))
463+
}
464+
if len(deviceIDs) > 0 {
465+
q.Where(appruntime.DeviceIDIn(deviceIDs...))
466+
}
467+
err := q.Where(appruntime.And(
468+
appruntime.StartTimeGTE(timeRange.StartTime),
469+
appruntime.StartTimeLTE(timeRange.StartTime.Add(timeRange.Duration)),
470+
)).
471+
Aggregate(
472+
ent.Sum(appruntime.FieldDuration),
473+
).Scan(ctx, &v)
464474
if err != nil {
465475
return time.Duration(0), err
466476
}
@@ -470,3 +480,44 @@ func (g *GeburaRepo) SumAppRunTime(
470480
}
471481
return res, nil
472482
}
483+
484+
func (g *GeburaRepo) ListAppRunTimes(
485+
ctx context.Context,
486+
userID model.InternalID,
487+
paging model.Paging,
488+
appIDs []model.InternalID,
489+
deviceIDs []model.InternalID,
490+
timeRange *model.TimeRange,
491+
) ([]*modelgebura.AppRunTime, int, error) {
492+
q := g.data.db.AppRunTime.Query().Where(
493+
appruntime.UserIDEQ(userID),
494+
)
495+
if len(appIDs) > 0 {
496+
q.Where(appruntime.AppIDIn(appIDs...))
497+
}
498+
if len(deviceIDs) > 0 {
499+
q.Where(appruntime.DeviceIDIn(deviceIDs...))
500+
}
501+
if timeRange != nil {
502+
q.Where(appruntime.And(
503+
appruntime.StartTimeGTE(timeRange.StartTime),
504+
appruntime.StartTimeLTE(timeRange.StartTime.Add(timeRange.Duration)),
505+
))
506+
}
507+
total, err := q.Count(ctx)
508+
if err != nil {
509+
return nil, 0, err
510+
}
511+
res, err := q.
512+
Limit(paging.ToLimit()).
513+
Offset(paging.ToOffset()).
514+
All(ctx)
515+
if err != nil {
516+
return nil, 0, err
517+
}
518+
return converter.ToBizAppRunTimeList(res), total, nil
519+
}
520+
521+
func (g *GeburaRepo) DeleteAppRunTime(ctx context.Context, userID model.InternalID, id model.InternalID) error {
522+
return g.data.db.AppRunTime.DeleteOneID(id).Exec(ctx)
523+
}

internal/data/internal/converter/ent_to_biz.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ type toBizConverter interface { //nolint:unused // used by generator
9595
// goverter:enum:map TypeUnknown AppTypeUnspecified
9696
// goverter:enum:map TypeGame AppTypeGame
9797
ToBizAppType(app.Type) modelgebura.AppType
98+
// goverter:map . RunTime
99+
ToBizAppRunTime(*ent.AppRunTime) *modelgebura.AppRunTime
100+
ToBizAppRunTimeList([]*ent.AppRunTime) []*modelgebura.AppRunTime
98101
ToBizAppBinary(ent.StoreAppBinary) modelgebura.AppBinary
99102

100103
// goverter:map LatestPullAt LatestPullTime

internal/data/internal/converter/generated.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/data/internal/ent/app_create.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)