Skip to content

Commit 8dafa2d

Browse files
committed
CE files handle query update
1 parent 56bfaa4 commit 8dafa2d

File tree

2 files changed

+301
-66
lines changed

2 files changed

+301
-66
lines changed

vault/activity_log.go

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,54 +1830,29 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
18301830
pq = storedQuery
18311831
}
18321832

1833-
// Calculate the namespace response breakdowns and totals for entities and tokens from the initial
1834-
// namespace data.
1835-
totalCounts, byNamespaceResponse, err := a.calculateByNamespaceResponseForQuery(ctx, pq.Namespaces)
1836-
if err != nil {
1837-
return nil, err
1838-
}
1839-
1840-
// If we need to add the current month's client counts into the total, compute the namespace
1841-
// breakdown for the current month as well.
18421833
var partialByMonth map[int64]*processMonth
1843-
var partialByNamespace map[string]*processByNamespace
1844-
var byNamespaceResponseCurrent []*ResponseNamespace
1845-
var totalCurrentCounts *ResponseCounts
18461834
if computePartial {
18471835
// Traverse through current month's activitylog data and group clients
18481836
// into months and namespaces
18491837
a.fragmentLock.RLock()
1850-
partialByMonth, partialByNamespace = a.populateNamespaceAndMonthlyBreakdowns()
1838+
partialByMonth, _ = a.populateNamespaceAndMonthlyBreakdowns()
18511839
a.fragmentLock.RUnlock()
18521840

1853-
// Convert the byNamespace breakdowns into structs that are
1854-
// consumable by the /activity endpoint, so as to reuse code between these two
1855-
// endpoints.
1856-
byNamespaceComputation := a.transformALNamespaceBreakdowns(partialByNamespace)
1857-
1858-
// Calculate the namespace response breakdowns and totals for entities
1859-
// and tokens from current month namespace data.
1860-
totalCurrentCounts, byNamespaceResponseCurrent, err = a.calculateByNamespaceResponseForQuery(ctx, byNamespaceComputation)
1841+
// Estimate the current month totals. These record contains is complete with all the
1842+
// current month data, grouped by namespace and mounts
1843+
currentMonth, err := a.computeCurrentMonthForBillingPeriod(ctx, partialByMonth, startTime, endTime)
18611844
if err != nil {
18621845
return nil, err
18631846
}
18641847

1865-
// Create a mapping of namespace id to slice index, so that we can efficiently update our results without
1866-
// having to traverse the entire namespace response slice every time.
1867-
nsrMap := make(map[string]int)
1868-
for i, nr := range byNamespaceResponse {
1869-
nsrMap[nr.NamespaceID] = i
1870-
}
1848+
// Combine the existing months precomputed query with the current month data
1849+
pq.CombineWithCurrentMonth(currentMonth)
1850+
}
18711851

1872-
// Rather than blindly appending, which will create duplicates, check our existing counts against the current
1873-
// month counts, and append or update as necessary. We also want to account for mounts and their counts.
1874-
for _, nrc := range byNamespaceResponseCurrent {
1875-
if ndx, ok := nsrMap[nrc.NamespaceID]; ok {
1876-
byNamespaceResponse[ndx].Add(nrc)
1877-
} else {
1878-
byNamespaceResponse = append(byNamespaceResponse, nrc)
1879-
}
1880-
}
1852+
// Convert the namespace data into a protobuf format that can be returned in the response
1853+
totalCounts, byNamespaceResponse, err := a.calculateByNamespaceResponseForQuery(ctx, pq.Namespaces)
1854+
if err != nil {
1855+
return nil, err
18811856
}
18821857

18831858
// Sort clients within each namespace
@@ -1887,34 +1862,6 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
18871862
totalCounts, byNamespaceResponse = a.limitNamespacesInALResponse(byNamespaceResponse, limitNamespaces)
18881863
}
18891864

1890-
distinctEntitiesResponse := totalCounts.EntityClients
1891-
if computePartial {
1892-
currentMonth, err := a.computeCurrentMonthForBillingPeriod(ctx, partialByMonth, startTime, endTime)
1893-
if err != nil {
1894-
return nil, err
1895-
}
1896-
1897-
// Add the namespace attribution for the current month to the newly computed current month value. Note
1898-
// that transformMonthBreakdowns calculates a superstruct of the required namespace struct due to its
1899-
// primary use-case being for precomputedQueryWorker, but we will reuse this code for brevity and extract
1900-
// the namespaces from it.
1901-
currentMonthNamespaceAttribution := a.transformMonthBreakdowns(partialByMonth)
1902-
1903-
// Ensure that there is only one element in this list -- if not, warn.
1904-
if len(currentMonthNamespaceAttribution) > 1 {
1905-
a.logger.Warn("more than one month worth of namespace and mount attribution calculated for "+
1906-
"current month values", "number of months", len(currentMonthNamespaceAttribution))
1907-
}
1908-
if len(currentMonthNamespaceAttribution) == 0 {
1909-
a.logger.Warn("no month data found, returning query with no namespace attribution for current month")
1910-
} else {
1911-
currentMonth.Namespaces = currentMonthNamespaceAttribution[0].Namespaces
1912-
currentMonth.NewClients.Namespaces = currentMonthNamespaceAttribution[0].NewClients.Namespaces
1913-
}
1914-
pq.Months = append(pq.Months, currentMonth)
1915-
distinctEntitiesResponse += pq.Months[len(pq.Months)-1].NewClients.Counts.EntityClients
1916-
}
1917-
19181865
// Now populate the response based on breakdowns.
19191866
responseData := make(map[string]interface{})
19201867
responseData["start_time"] = pq.StartTime.Format(time.RFC3339)
@@ -1931,8 +1878,6 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
19311878
}
19321879

19331880
responseData["by_namespace"] = byNamespaceResponse
1934-
totalCounts.Add(totalCurrentCounts)
1935-
totalCounts.DistinctEntities = distinctEntitiesResponse
19361881
responseData["total"] = totalCounts
19371882

19381883
// Create and populate the month response structs based on the monthly breakdown.

0 commit comments

Comments
 (0)