refactor(go): make realtime totals opt in

This commit is contained in:
lingniu
2026-07-03 07:50:14 +08:00
parent eced1873cd
commit b3ea2bd91f
3 changed files with 101 additions and 20 deletions

View File

@@ -16,11 +16,12 @@ type mysqlQueryer interface {
}
type RealtimeTableQuery struct {
Protocol string
VIN string
Plate string
Limit int
Offset int
Protocol string
VIN string
Plate string
IncludeTotal bool
Limit int
Offset int
}
type SnapshotRow struct {
@@ -191,16 +192,22 @@ func (h *SnapshotQueryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
writeError(w, http.StatusBadRequest, err.Error())
return
}
total, err := h.repository.Count(r.Context(), query)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
var total int64
if query.IncludeTotal {
total, err = h.repository.Count(r.Context(), query)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
}
rows, err := h.repository.Query(r.Context(), query)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
if !query.IncludeTotal {
total = int64(len(rows))
}
writePage(w, rows, total, query)
}
@@ -229,16 +236,22 @@ func (h *LocationQueryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
writeError(w, http.StatusBadRequest, err.Error())
return
}
total, err := h.repository.Count(r.Context(), query)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
var total int64
if query.IncludeTotal {
total, err = h.repository.Count(r.Context(), query)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
}
rows, err := h.repository.Query(r.Context(), query)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
if !query.IncludeTotal {
total = int64(len(rows))
}
writePage(w, rows, total, query)
}
@@ -253,11 +266,12 @@ func parseRealtimeTableQuery(r *http.Request) (RealtimeTableQuery, error) {
return RealtimeTableQuery{}, err
}
return normalizeRealtimeTableQuery(RealtimeTableQuery{
Protocol: values.Get("protocol"),
VIN: values.Get("vin"),
Plate: values.Get("plate"),
Limit: limit,
Offset: offset,
Protocol: values.Get("protocol"),
VIN: values.Get("vin"),
Plate: values.Get("plate"),
IncludeTotal: strings.EqualFold(strings.TrimSpace(values.Get("includeTotal")), "true"),
Limit: limit,
Offset: offset,
}), nil
}