Activity Log Filtering Limit Parameter (#16000)

* adding changes from ent branch

* adding fmt changes

* adding changelog
This commit is contained in:
akshya96 2022-06-15 15:41:31 -07:00 committed by GitHub
parent 28ada99c48
commit 7e313e29fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

3
changelog/16000.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
core: Limit activity log client count usage by namespaces
```

View File

@ -1505,7 +1505,7 @@ func (a *ActivityLog) DefaultStartTime(endTime time.Time) time.Time {
return monthStart.AddDate(0, -a.defaultReportMonths+1, 0) return monthStart.AddDate(0, -a.defaultReportMonths+1, 0)
} }
func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.Time) (map[string]interface{}, error) { func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.Time, limitNamespaces int) (map[string]interface{}, error) {
queryNS, err := namespace.FromContext(ctx) queryNS, err := namespace.FromContext(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1557,6 +1557,7 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
} else { } else {
displayPath = ns.Path displayPath = ns.Path
} }
byNamespace = append(byNamespace, &ResponseNamespace{ byNamespace = append(byNamespace, &ResponseNamespace{
NamespaceID: nsRecord.NamespaceID, NamespaceID: nsRecord.NamespaceID,
NamespacePath: displayPath, NamespacePath: displayPath,
@ -1577,6 +1578,19 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
sort.Slice(byNamespace, func(i, j int) bool { sort.Slice(byNamespace, func(i, j int) bool {
return byNamespace[i].Counts.Clients > byNamespace[j].Counts.Clients return byNamespace[i].Counts.Clients > byNamespace[j].Counts.Clients
}) })
if limitNamespaces > 0 {
if limitNamespaces > len(byNamespace) {
limitNamespaces = len(byNamespace)
}
byNamespace = byNamespace[:limitNamespaces]
// recalculate total entities and tokens
totalEntities = 0
totalTokens = 0
for _, namespaceData := range byNamespace {
totalEntities += namespaceData.Counts.DistinctEntities
totalTokens += namespaceData.Counts.NonEntityTokens
}
}
responseData["by_namespace"] = byNamespace responseData["by_namespace"] = byNamespace
responseData["total"] = &ResponseCounts{ responseData["total"] = &ResponseCounts{

View File

@ -27,6 +27,11 @@ func (b *SystemBackend) activityQueryPath() *framework.Path {
Type: framework.TypeTime, Type: framework.TypeTime,
Description: "End of query interval", Description: "End of query interval",
}, },
"limit_namespaces": {
Type: framework.TypeInt,
Default: 0,
Description: "Limit query output by namespaces",
},
}, },
HelpSynopsis: strings.TrimSpace(sysHelp["activity-query"][0]), HelpSynopsis: strings.TrimSpace(sysHelp["activity-query"][0]),
HelpDescription: strings.TrimSpace(sysHelp["activity-query"][1]), HelpDescription: strings.TrimSpace(sysHelp["activity-query"][1]),
@ -198,7 +203,12 @@ func (b *SystemBackend) handleClientMetricQuery(ctx context.Context, req *logica
return logical.ErrorResponse(err.Error()), nil return logical.ErrorResponse(err.Error()), nil
} }
results, err := a.handleQuery(ctx, startTime, endTime) var limitNamespaces int
if limitNamespacesRaw, ok := d.GetOk("limit_namespaces"); ok {
limitNamespaces = limitNamespacesRaw.(int)
}
results, err := a.handleQuery(ctx, startTime, endTime, limitNamespaces)
if err != nil { if err != nil {
return nil, err return nil, err
} }