Skip to content

Commit 1ac2bab

Browse files
authored
Merge pull request #5449 from SDU-eScience/feature-key-metrics
Feature key metrics
2 parents 8871f60 + 002e5e0 commit 1ac2bab

14 files changed

Lines changed: 1124 additions & 260 deletions

File tree

core2/pkg/accounting/accounting.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@ func WalletsBrowse(actor rpc.Actor, request accapi.WalletsBrowseRequest) fndapi.
369369
reference = string(actor.Project.Value)
370370
}
371371
allWallets := internalRetrieveWallets(time.Now(), reference, walletFilter{
372-
RequireActive: false,
373-
IncludeChildren: request.IncludeChildren,
372+
RequireActive: false,
373+
IncludeChildren: request.IncludeChildren,
374+
FilterChildrenByIdleTimeInDays: request.FilterChildrenByIdleTimeInDays,
374375
})
375376

376377
result := fndapi.PageV2[accapi.WalletV2]{}

core2/pkg/accounting/accounting_internal.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ func internalRetrieveWallet(
14321432

14331433
ownerId := w.OwnedBy
14341434
owner := accGlobals.OwnersById[ownerId].WalletOwner()
1435-
apiWallet := lInternalWalletToApi(now, b, w, owner, includeChildren)
1435+
apiWallet := lInternalWalletToApi(now, b, w, owner, includeChildren, util.OptNone[int]())
14361436

14371437
b.Mu.RUnlock()
14381438
accGlobals.Mu.RUnlock()
@@ -1447,6 +1447,7 @@ func lInternalWalletToApi(
14471447
w *internalWallet,
14481448
owner accapi.WalletOwner,
14491449
includeChildren bool,
1450+
filterChildrenByIdleTimeInDays util.Option[int],
14501451
) accapi.WalletV2 {
14511452
groups := w.AllocationsByParent
14521453
apiWallet := accapi.WalletV2{
@@ -1559,7 +1560,19 @@ func lInternalWalletToApi(
15591560
}
15601561

15611562
if includeChildren {
1563+
activeChildren := map[AccWalletId]util.Empty{}
1564+
if filterChildrenByIdleTimeInDays.Present && filterChildrenByIdleTimeInDays.Value > 0 {
1565+
from := now.AddDate(0, 0, -filterChildrenByIdleTimeInDays.Value)
1566+
activeChildren = lUsageActiveChildrenInWindow(from, now, w.Id)
1567+
}
1568+
15621569
for childId, _ := range w.ChildrenUsage {
1570+
if len(activeChildren) > 0 {
1571+
if _, isActive := activeChildren[childId]; isActive {
1572+
continue
1573+
}
1574+
}
1575+
15631576
childWallet := b.WalletsById[childId]
15641577
childOwner := accGlobals.OwnersById[childWallet.OwnedBy]
15651578
g := childWallet.AllocationsByParent[w.Id]
@@ -1598,7 +1611,7 @@ func internalRetrieveWalletByAllocationId(
15981611
if iWallet != nil {
15991612
owner := accGlobals.OwnersById[iWallet.OwnedBy]
16001613
if owner != nil {
1601-
wallet = lInternalWalletToApi(now, bucket, iWallet, owner.WalletOwner(), false)
1614+
wallet = lInternalWalletToApi(now, bucket, iWallet, owner.WalletOwner(), false, util.OptNone[int]())
16021615
wId = walletId
16031616
found = true
16041617
bucket.Mu.RUnlock()
@@ -1620,6 +1633,23 @@ type walletFilter struct {
16201633

16211634
IncludeChildren bool
16221635
RequireActive bool
1636+
1637+
FilterChildrenByIdleTimeInDays util.Option[int]
1638+
}
1639+
1640+
func lUsageActiveChildrenInWindow(from time.Time, until time.Time, parentWallet AccWalletId) map[AccWalletId]util.Empty {
1641+
result := map[AccWalletId]util.Empty{}
1642+
1643+
reports := usageRetrieveHistoricReports(from, until, parentWallet)
1644+
for _, report := range reports {
1645+
for _, item := range report.UsageOverTime.Delta {
1646+
if item.Child.Present && item.Change != 0 {
1647+
result[item.Child.Value] = util.Empty{}
1648+
}
1649+
}
1650+
}
1651+
1652+
return result
16231653
}
16241654

16251655
func internalRetrieveWallets(
@@ -1669,7 +1699,14 @@ func internalRetrieveWallets(
16691699
}
16701700

16711701
if shouldInclude {
1672-
apiWallet := lInternalWalletToApi(now, b, w, owner.WalletOwner(), filter.IncludeChildren)
1702+
apiWallet := lInternalWalletToApi(
1703+
now,
1704+
b,
1705+
w,
1706+
owner.WalletOwner(),
1707+
filter.IncludeChildren,
1708+
filter.FilterChildrenByIdleTimeInDays,
1709+
)
16731710
wallets = append(wallets, apiWallet)
16741711
}
16751712

@@ -1730,7 +1767,7 @@ func lRetrieveAncestorWallets(bucket *internalBucket, now time.Time, root AccWal
17301767
var wallets []accapi.WalletV2
17311768
for _, w := range relevantWallets {
17321769
owner := accGlobals.OwnersById[w.OwnedBy].WalletOwner()
1733-
wallets = append(wallets, lInternalWalletToApi(now, bucket, w, owner, false))
1770+
wallets = append(wallets, lInternalWalletToApi(now, bucket, w, owner, false, util.OptNone[int]()))
17341771
}
17351772

17361773
return wallets

core2/pkg/accounting/accounting_usage_generator_real.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strings"
77
"time"
8+
89
accapi "ucloud.dk/shared/pkg/accounting"
910
fndapi "ucloud.dk/shared/pkg/foundation"
1011
"ucloud.dk/shared/pkg/log"
@@ -89,8 +90,8 @@ func initUsageGenerator() {
8990
}
9091

9192
func usageGenReal(actor rpc.Actor, request accapi.UsageGenConfig) *util.HttpError {
92-
timeStart := util.StartOfDayUTC(time.Now()).AddDate(0, 0, -request.Days)
93-
timeEnd := util.StartOfDayUTC(time.Now()).AddDate(0, 0, request.Days+1)
93+
timeStart := util.StartOfDayUTC(time.Now()).AddDate(0, -6, 0)
94+
timeEnd := util.StartOfDayUTC(time.Now()).AddDate(0, 1, 0)
9495

9596
titleBase := fmt.Sprintf("usegen_%v", time.Now().Format(time.DateTime))
9697

@@ -155,6 +156,9 @@ func usageGenReal(actor rpc.Actor, request accapi.UsageGenConfig) *util.HttpErro
155156
nowTime := tm(now)
156157
startTime := tm(start)
157158
endTime := tm(end)
159+
if !request.Expiration {
160+
endTime = timeEnd
161+
}
158162

159163
// Create sub-project
160164
// ---------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)