-
Notifications
You must be signed in to change notification settings - Fork 22
refactor: better processing of summary chart data #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LuccaBitfly
wants to merge
1
commit into
staging
Choose a base branch
from
BEDS-94/summary-chart-refactor
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -849,24 +849,19 @@ func (d *DataAccessService) GetValidatorDashboardSummaryChart(ctx context.Contex | |
|
|
||
| var queryResults []*t.VDBValidatorSummaryChartRow | ||
|
|
||
| containsGroups := false | ||
| requestedGroupsMap := make(map[int64]bool) | ||
| for _, groupId := range groupIds { | ||
| requestedGroupsMap[groupId] = true | ||
| if !containsGroups && groupId >= 0 { | ||
| containsGroups = true | ||
| } | ||
| } | ||
|
|
||
| // need default or all groups for anon dashboards and shared dashboards without group sharing | ||
| // TODO could move this to API layer & generalize for all methods | ||
| if (dashboardId.Validators != nil && !requestedGroupsMap[t.AllGroups] && !requestedGroupsMap[t.DefaultGroupId]) || | ||
| (dashboardId.AggregateGroups && !requestedGroupsMap[t.AllGroups] && !requestedGroupsMap[t.DefaultGroupId]) { | ||
| if (dashboardId.Validators != nil || dashboardId.AggregateGroups) && !requestedGroupsMap[t.AllGroups] && !requestedGroupsMap[t.DefaultGroupId] { | ||
| return ret, nil | ||
| } | ||
|
|
||
| totalLineRequested := requestedGroupsMap[t.AllGroups] || dashboardId.AggregateGroups | ||
| averageNetworkLineRequested := requestedGroupsMap[t.NetworkAverage] | ||
| hasTotalSeries := requestedGroupsMap[t.AllGroups] || dashboardId.AggregateGroups | ||
| hasNetworkAvgSeries := requestedGroupsMap[t.NetworkAverage] | ||
|
|
||
| var dividendColumn, divisorColumn string | ||
| switch efficiency { | ||
|
|
@@ -916,8 +911,8 @@ func (d *DataAccessService) GetValidatorDashboardSummaryChart(ctx context.Contex | |
| Where( | ||
| goqu.I("dashboard_id").Eq(dashboardId.Id), | ||
| goqu.Or( | ||
| goqu.V(hasTotalSeries), | ||
| goqu.I("group_id").In(groupIds), | ||
| goqu.V(totalLineRequested), | ||
| ), | ||
| ), | ||
| ). | ||
|
|
@@ -940,39 +935,48 @@ func (d *DataAccessService) GetValidatorDashboardSummaryChart(ctx context.Contex | |
| return nil, fmt.Errorf("error retrieving data from table %s: %w", dataTable, err) | ||
| } | ||
|
|
||
| // convert the returned data to the expected return type (not pretty) | ||
| tsMap := make(map[time.Time]bool) | ||
| data := make(map[time.Time]map[int64]*float64) | ||
| groupMap := make(map[int64]bool) | ||
| // map from `timestamp:groupId` to row pointer | ||
| type key struct { | ||
| Timestamp uint64 | ||
| GroupId int64 | ||
| } | ||
| rowsMap := make(map[key]*t.VDBValidatorSummaryChartRow) | ||
|
Comment on lines
+939
to
+943
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: https://go.dev/blog/maps#key-types recommends to use key types instead of multidimensional maps. |
||
|
|
||
| totalEfficiencyMap := make(map[time.Time]*t.VDBValidatorSummaryChartRow) | ||
| for _, row := range queryResults { | ||
| tsMap[row.Timestamp] = true | ||
| if data[row.Timestamp] == nil { | ||
| data[row.Timestamp] = make(map[int64]*float64) | ||
| } | ||
| // fill rowsMap with the results | ||
| totalSeriesId := int64(t.AllGroups) | ||
| if dashboardId.AggregateGroups { | ||
| totalSeriesId = t.DefaultGroupId | ||
| } | ||
|
|
||
| timestamps := map[uint64]struct{}{} // set of timestamps | ||
| series := map[int64]struct{}{} // set of series ids | ||
| for _, row := range queryResults { | ||
| ts := uint64(row.Timestamp.Unix()) | ||
| timestamps[ts] = struct{}{} | ||
| if !dashboardId.AggregateGroups && requestedGroupsMap[row.GroupId] { | ||
| eff := calcEfficiencyNulled(row.EfficiencyDividend, row.EfficiencyDivisor) | ||
| if eff == nil { | ||
| continue | ||
| } | ||
| data[row.Timestamp][row.GroupId] = eff | ||
| groupMap[row.GroupId] = true | ||
| series[row.GroupId] = struct{}{} | ||
| rowsMap[key{ts, row.GroupId}] = row | ||
| } | ||
|
|
||
| if totalLineRequested { | ||
| if totalEfficiencyMap[row.Timestamp] == nil { | ||
| totalEfficiencyMap[row.Timestamp] = &t.VDBValidatorSummaryChartRow{ | ||
| Timestamp: row.Timestamp, | ||
| } | ||
| } | ||
| totalEfficiencyMap[row.Timestamp].EfficiencyDividend = totalEfficiencyMap[row.Timestamp].EfficiencyDividend.Add(row.EfficiencyDividend) | ||
| totalEfficiencyMap[row.Timestamp].EfficiencyDivisor = totalEfficiencyMap[row.Timestamp].EfficiencyDivisor.Add(row.EfficiencyDivisor) | ||
| if !hasTotalSeries { | ||
| continue | ||
| } | ||
| series[totalSeriesId] = struct{}{} | ||
| // add up to total row for the timestamp | ||
| totalKey := key{ts, totalSeriesId} | ||
| if rowsMap[totalKey] == nil { | ||
| rowsMap[totalKey] = &t.VDBValidatorSummaryChartRow{} | ||
| } | ||
| rowsMap[totalKey].EfficiencyDividend = rowsMap[totalKey].EfficiencyDividend.Add(row.EfficiencyDividend) | ||
| rowsMap[totalKey].EfficiencyDivisor = rowsMap[totalKey].EfficiencyDivisor.Add(row.EfficiencyDivisor) | ||
| } | ||
|
|
||
| data := make(map[key]*float64) // map from `timestamp:groupId` to efficiency value | ||
| // calculate the efficiency for each group and timestamp | ||
| for key, value := range rowsMap { | ||
| data[key] = calcEfficiencyNulled(value.EfficiencyDividend, value.EfficiencyDivisor) | ||
| } | ||
|
|
||
| if averageNetworkLineRequested { | ||
| if hasNetworkAvgSeries { | ||
| // Get the average network efficiency | ||
| efficiency, err := d.services.GetCurrentEfficiencyInfo() | ||
| if err != nil { | ||
|
|
@@ -983,63 +987,24 @@ func (d *DataAccessService) GetValidatorDashboardSummaryChart(ctx context.Contex | |
| averageNetworkEfficiency = efficiency.AttestationEfficiency[enums.Last24h].Float64 * 100 | ||
| } | ||
|
|
||
| for ts := range tsMap { | ||
| data[ts][int64(t.NetworkAverage)] = &averageNetworkEfficiency | ||
| for ts := range timestamps { | ||
| data[key{ts, t.NetworkAverage}] = &averageNetworkEfficiency | ||
| } | ||
| groupMap[t.NetworkAverage] = true | ||
| series[t.NetworkAverage] = struct{}{} | ||
| } | ||
|
|
||
| if totalLineRequested { | ||
| totalLineGroupId := int64(t.AllGroups) | ||
| if dashboardId.AggregateGroups { | ||
| totalLineGroupId = t.DefaultGroupId | ||
| ret.Categories = slices.Sorted(maps.Keys(timestamps)) | ||
| for _, seriesId := range slices.Sorted(maps.Keys(series)) { | ||
| seriesData := make([]*float64, len(ret.Categories)) | ||
| for i, ts := range ret.Categories { | ||
| seriesData[i] = data[key{ts, seriesId}] | ||
| } | ||
| for _, row := range totalEfficiencyMap { | ||
| data[row.Timestamp][totalLineGroupId] = calcEfficiencyNulled(row.EfficiencyDividend, row.EfficiencyDivisor) | ||
| } | ||
| groupMap[totalLineGroupId] = true | ||
| } | ||
|
|
||
| tsArray := make([]time.Time, 0, len(tsMap)) | ||
| for ts := range tsMap { | ||
| tsArray = append(tsArray, ts) | ||
| } | ||
| sort.Slice(tsArray, func(i, j int) bool { | ||
| return tsArray[i].Before(tsArray[j]) | ||
| }) | ||
|
|
||
| groupsArray := slices.Collect(maps.Keys(groupMap)) | ||
| slices.Sort(groupsArray) | ||
|
|
||
| ret.Categories = make([]uint64, 0, len(tsArray)) | ||
| for _, ts := range tsArray { | ||
| ret.Categories = append(ret.Categories, uint64(ts.Unix())) | ||
| } | ||
| ret.Series = make([]t.ChartSeries[int, *float64], 0, len(groupsArray)) | ||
|
|
||
| seriesMap := make(map[int64]*t.ChartSeries[int, *float64]) | ||
| for _, group := range groupsArray { | ||
| series := t.ChartSeries[int, *float64]{ | ||
| Id: int(group), | ||
| Data: make([]*float64, 0, len(tsMap)), | ||
| } | ||
| seriesMap[group] = &series | ||
| } | ||
|
|
||
| for _, ts := range tsArray { | ||
| for _, group := range groupsArray { | ||
| seriesMap[group].Data = append(seriesMap[group].Data, data[ts][group]) | ||
| } | ||
| } | ||
|
|
||
| for _, series := range seriesMap { | ||
| ret.Series = append(ret.Series, *series) | ||
| ret.Series = append(ret.Series, t.ChartSeries[int, *float64]{ | ||
| Id: int(seriesId), | ||
| Data: seriesData, | ||
| }) | ||
| } | ||
|
|
||
| sort.Slice(ret.Series, func(i, j int) bool { | ||
| return ret.Series[i].Id < ret.Series[j].Id | ||
| }) | ||
|
|
||
| return ret, nil | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note:
containsGroupswas unused, not sure what this was needed for