Split netquery package files and update bandwidth chart handler

This commit is contained in:
Patrick Pacher 2023-10-05 10:28:18 +02:00 committed by Daniel
parent bc285b593d
commit 1484591aea
3 changed files with 49 additions and 18 deletions

View file

@ -19,6 +19,7 @@ type BandwidthChartHandler struct {
} }
type BandwidthChartRequest struct { type BandwidthChartRequest struct {
AllProfiles bool `json:"allProfiles"`
Profiles []string `json:"profiles"` Profiles []string `json:"profiles"`
Connections []string `json:"connections"` Connections []string `json:"connections"`
} }
@ -107,18 +108,21 @@ func (req *BandwidthChartRequest) generateSQL(ctx context.Context, schema *orm.T
whereClause := "" whereClause := ""
params := make(map[string]any) params := make(map[string]any)
if len(req.Profiles) > 0 { if (len(req.Profiles) > 0) || (req.AllProfiles == true) {
groupBy = []string{"profile", "round(time/10, 0)*10"} groupBy = []string{"profile", "round(time/10, 0)*10"}
selects = append(selects, "profile") selects = append(selects, "profile")
clauses := make([]string, len(req.Profiles))
for idx, p := range req.Profiles { if !req.AllProfiles {
key := fmt.Sprintf(":p%d", idx) clauses := make([]string, len(req.Profiles))
clauses[idx] = "profile = " + key
params[key] = p for idx, p := range req.Profiles {
key := fmt.Sprintf(":p%d", idx)
clauses[idx] = "profile = " + key
params[key] = p
}
whereClause = "WHERE " + strings.Join(clauses, " OR ")
} }
whereClause = "WHERE " + strings.Join(clauses, " OR ")
} else if len(req.Connections) > 0 { } else if len(req.Connections) > 0 {
groupBy = []string{"conn_id", "round(time/10, 0)*10"} groupBy = []string{"conn_id", "round(time/10, 0)*10"}
selects = append(selects, "conn_id") selects = append(selects, "conn_id")

View file

@ -71,12 +71,18 @@ type (
Distinct bool `json:"distinct"` Distinct bool `json:"distinct"`
} }
FieldSelect struct {
Field string `json:"field"`
As string `json:"as"`
}
Select struct { Select struct {
Field string `json:"field"` Field string `json:"field"`
Count *Count `json:"$count,omitempty"` FieldSelect *FieldSelect `json:"$field"`
Sum *Sum `json:"$sum,omitempty"` Count *Count `json:"$count,omitempty"`
Min *Min `json:"$min,omitempty"` Sum *Sum `json:"$sum,omitempty"`
Distinct *string `json:"$distinct,omitempty"` Min *Min `json:"$min,omitempty"`
Distinct *string `json:"$distinct,omitempty"`
} }
Selects []Select Selects []Select
@ -538,11 +544,12 @@ func (sel *Select) UnmarshalJSON(blob []byte) error {
// directly // directly
if blob[0] == '{' { if blob[0] == '{' {
var res struct { var res struct {
Field string `json:"field"` Field string `json:"field"`
Count *Count `json:"$count"` Count *Count `json:"$count"`
Sum *Sum `json:"$sum"` Sum *Sum `json:"$sum"`
Min *Min `json:"$min"` Min *Min `json:"$min"`
Distinct *string `json:"$distinct"` Distinct *string `json:"$distinct"`
FieldSelect *FieldSelect `json:"$field"`
} }
if err := json.Unmarshal(blob, &res); err != nil { if err := json.Unmarshal(blob, &res); err != nil {
@ -551,6 +558,7 @@ func (sel *Select) UnmarshalJSON(blob []byte) error {
sel.Count = res.Count sel.Count = res.Count
sel.Field = res.Field sel.Field = res.Field
sel.FieldSelect = res.FieldSelect
sel.Distinct = res.Distinct sel.Distinct = res.Distinct
sel.Sum = res.Sum sel.Sum = res.Sum
sel.Min = res.Min sel.Min = res.Min
@ -570,6 +578,11 @@ func (sel *Select) UnmarshalJSON(blob []byte) error {
return fmt.Errorf("invalid characters in $min.as, value must match [a-zA-Z]+") return fmt.Errorf("invalid characters in $min.as, value must match [a-zA-Z]+")
} }
} }
if sel.FieldSelect != nil && sel.FieldSelect.As != "" {
if !charOnlyRegexp.MatchString(sel.FieldSelect.As) {
return fmt.Errorf("invalid characters in $field.as, value must match [a-zA-Z]+")
}
}
return nil return nil
} }

View file

@ -126,6 +126,8 @@ func (req *QueryRequestPayload) prepareSelectedFields(ctx context.Context, schem
} else { } else {
field = "*" field = "*"
} }
case s.FieldSelect != nil:
field = s.FieldSelect.Field
default: default:
field = s.Field field = s.Field
} }
@ -141,6 +143,18 @@ func (req *QueryRequestPayload) prepareSelectedFields(ctx context.Context, schem
} }
switch { switch {
case s.FieldSelect != nil:
as := s.FieldSelect.As
if as == "" {
as = s.FieldSelect.Field
}
req.selectedFields = append(
req.selectedFields,
fmt.Sprintf("%s AS %s", s.FieldSelect.Field, as),
)
req.whitelistedFields = append(req.whitelistedFields, as)
case s.Count != nil: case s.Count != nil:
as := s.Count.As as := s.Count.As
if as == "" { if as == "" {