refactor(go): make daily metric totals opt in
This commit is contained in:
@@ -17,12 +17,13 @@ type Queryer interface {
|
||||
}
|
||||
|
||||
type MetricQuery struct {
|
||||
VIN string
|
||||
Protocol string
|
||||
DateFrom string
|
||||
DateTo string
|
||||
Limit int
|
||||
Offset int
|
||||
VIN string
|
||||
Protocol string
|
||||
IncludeTotal bool
|
||||
DateFrom string
|
||||
DateTo string
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
type MetricRow struct {
|
||||
@@ -184,16 +185,22 @@ func (h *MetricHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
writeMetricError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
total, err := h.repository.Count(r.Context(), query)
|
||||
if err != nil {
|
||||
writeMetricError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
var total int64
|
||||
if query.IncludeTotal {
|
||||
total, err = h.repository.Count(r.Context(), query)
|
||||
if err != nil {
|
||||
writeMetricError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
rows, err := h.repository.Query(r.Context(), query)
|
||||
if err != nil {
|
||||
writeMetricError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if !query.IncludeTotal {
|
||||
total = int64(len(rows))
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"items": rows,
|
||||
@@ -214,12 +221,13 @@ func parseMetricQuery(r *http.Request) (MetricQuery, error) {
|
||||
return MetricQuery{}, err
|
||||
}
|
||||
query := MetricQuery{
|
||||
VIN: values.Get("vin"),
|
||||
Protocol: values.Get("protocol"),
|
||||
DateFrom: values.Get("dateFrom"),
|
||||
DateTo: values.Get("dateTo"),
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
VIN: values.Get("vin"),
|
||||
Protocol: values.Get("protocol"),
|
||||
IncludeTotal: strings.EqualFold(strings.TrimSpace(values.Get("includeTotal")), "true"),
|
||||
DateFrom: values.Get("dateFrom"),
|
||||
DateTo: values.Get("dateTo"),
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
if !validDate(query.DateFrom) || !validDate(query.DateTo) {
|
||||
return MetricQuery{}, errors.New("dateFrom/dateTo must use YYYY-MM-DD")
|
||||
|
||||
Reference in New Issue
Block a user