43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
// Callers supply hosts already selected through the account/host access scope.
|
|
// Batch reads avoid a separate usage query for every host in an account view.
|
|
func (s *Store) FillProjectBandwidth(ctx context.Context, projects []Project) error {
|
|
byID := make(map[uint64]int, len(projects))
|
|
for i, p := range projects {
|
|
byID[p.ID] = i
|
|
}
|
|
for start := 0; start < len(projects); start += 500 {
|
|
end := min(start+500, len(projects))
|
|
args := make([]any, 0, end-start)
|
|
for _, p := range projects[start:end] {
|
|
args = append(args, p.ID)
|
|
}
|
|
rows, err := s.db.QueryContext(ctx, "SELECT project_id,bandwidth_bytes FROM project_quota_usage WHERE project_id IN ("+strings.TrimSuffix(strings.Repeat("?,", len(args)), ",")+")", args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for rows.Next() {
|
|
var id, used uint64
|
|
if err = rows.Scan(&id, &used); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
if i, ok := byID[id]; ok {
|
|
projects[i].BandwidthBytes = used
|
|
}
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|