all: imp code; fix time conversion
This commit is contained in:
parent
34310cffd7
commit
4743c81038
|
@ -125,10 +125,10 @@ func (l *queryLog) handleGetQueryLogConfig(w http.ResponseWriter, r *http.Reques
|
||||||
ignored := l.conf.Ignored.Values()
|
ignored := l.conf.Ignored.Values()
|
||||||
slices.Sort(ignored)
|
slices.Sort(ignored)
|
||||||
_ = aghhttp.WriteJSONResponse(w, r, getConfigResp{
|
_ = aghhttp.WriteJSONResponse(w, r, getConfigResp{
|
||||||
Enabled: aghalg.BoolToNullBool(l.conf.Enabled),
|
|
||||||
Interval: float64(l.conf.RotationIvl.Milliseconds()),
|
|
||||||
AnonymizeClientIP: aghalg.BoolToNullBool(l.conf.AnonymizeClientIP),
|
|
||||||
Ignored: ignored,
|
Ignored: ignored,
|
||||||
|
Interval: float64(l.conf.RotationIvl.Milliseconds()),
|
||||||
|
Enabled: aghalg.BoolToNullBool(l.conf.Enabled),
|
||||||
|
AnonymizeClientIP: aghalg.BoolToNullBool(l.conf.AnonymizeClientIP),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@ func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ivl := time.Duration(newConf.Interval) * timeutil.Day
|
ivl := time.Duration(float64(timeutil.Day) * newConf.Interval)
|
||||||
|
|
||||||
hasIvl := !math.IsNaN(newConf.Interval)
|
hasIvl := !math.IsNaN(newConf.Interval)
|
||||||
if hasIvl && !checkInterval(ivl) {
|
if hasIvl && !checkInterval(ivl) {
|
||||||
|
@ -204,7 +204,6 @@ func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request)
|
||||||
// queries.
|
// queries.
|
||||||
func (l *queryLog) handlePutQueryLogConfig(w http.ResponseWriter, r *http.Request) {
|
func (l *queryLog) handlePutQueryLogConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
newConf := &getConfigResp{}
|
newConf := &getConfigResp{}
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(newConf)
|
err := json.NewDecoder(r.Body).Decode(newConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
||||||
|
@ -212,8 +211,14 @@ func (l *queryLog) handlePutQueryLogConfig(w http.ResponseWriter, r *http.Reques
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ivl := time.Duration(float64(time.Millisecond) * newConf.Interval)
|
set, err := aghnet.NewDomainNameSet(newConf.Ignored)
|
||||||
|
if err != nil {
|
||||||
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "ignored: %s", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ivl := time.Duration(newConf.Interval) * time.Millisecond
|
||||||
err = validateIvl(ivl)
|
err = validateIvl(ivl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "unsupported interval: %s", err)
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "unsupported interval: %s", err)
|
||||||
|
@ -221,6 +226,18 @@ func (l *queryLog) handlePutQueryLogConfig(w http.ResponseWriter, r *http.Reques
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if newConf.Enabled == aghalg.NBNull {
|
||||||
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if newConf.AnonymizeClientIP == aghalg.NBNull {
|
||||||
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "anonymize_client_ip is null")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
defer l.conf.ConfigModified()
|
defer l.conf.ConfigModified()
|
||||||
|
|
||||||
l.lock.Lock()
|
l.lock.Lock()
|
||||||
|
@ -229,21 +246,10 @@ func (l *queryLog) handlePutQueryLogConfig(w http.ResponseWriter, r *http.Reques
|
||||||
// Copy data, modify it, then activate. Other threads (readers) don't need
|
// Copy data, modify it, then activate. Other threads (readers) don't need
|
||||||
// to use this lock.
|
// to use this lock.
|
||||||
conf := *l.conf
|
conf := *l.conf
|
||||||
if newConf.Enabled == aghalg.NBNull {
|
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null")
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conf.Enabled = newConf.Enabled == aghalg.NBTrue
|
|
||||||
|
|
||||||
|
conf.Ignored = set
|
||||||
conf.RotationIvl = ivl
|
conf.RotationIvl = ivl
|
||||||
|
conf.Enabled = newConf.Enabled == aghalg.NBTrue
|
||||||
if newConf.AnonymizeClientIP == aghalg.NBNull {
|
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "anonymize_client_ip is null")
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conf.AnonymizeClientIP = newConf.AnonymizeClientIP == aghalg.NBTrue
|
conf.AnonymizeClientIP = newConf.AnonymizeClientIP == aghalg.NBTrue
|
||||||
if conf.AnonymizeClientIP {
|
if conf.AnonymizeClientIP {
|
||||||
|
@ -252,17 +258,6 @@ func (l *queryLog) handlePutQueryLogConfig(w http.ResponseWriter, r *http.Reques
|
||||||
l.anonymizer.Store(nil)
|
l.anonymizer.Store(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(newConf.Ignored) > 0 {
|
|
||||||
set, serr := aghnet.NewDomainNameSet(newConf.Ignored)
|
|
||||||
if serr != nil {
|
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "ignored: %s", serr)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conf.Ignored = set
|
|
||||||
}
|
|
||||||
|
|
||||||
l.conf = &conf
|
l.conf = &conf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,8 +87,7 @@ func (s *StatsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
days := uint32(s.limit.Hours() / 24)
|
days := uint32(s.limit / timeutil.Day)
|
||||||
|
|
||||||
ok := checkInterval(days)
|
ok := checkInterval(days)
|
||||||
if !ok || (s.enabled && days == 0) {
|
if !ok || (s.enabled && days == 0) {
|
||||||
// NOTE: If interval is custom we set it to 90 days for compatibility
|
// NOTE: If interval is custom we set it to 90 days for compatibility
|
||||||
|
@ -113,9 +112,9 @@ func (s *StatsCtx) handleGetStatsConfig(w http.ResponseWriter, r *http.Request)
|
||||||
slices.Sort(ignored)
|
slices.Sort(ignored)
|
||||||
|
|
||||||
resp := getConfigResp{
|
resp := getConfigResp{
|
||||||
Enabled: aghalg.BoolToNullBool(s.enabled),
|
|
||||||
Interval: float64(s.limit.Milliseconds()),
|
|
||||||
Ignored: ignored,
|
Ignored: ignored,
|
||||||
|
Interval: float64(s.limit.Milliseconds()),
|
||||||
|
Enabled: aghalg.BoolToNullBool(s.enabled),
|
||||||
}
|
}
|
||||||
_ = aghhttp.WriteJSONResponse(w, r, resp)
|
_ = aghhttp.WriteJSONResponse(w, r, resp)
|
||||||
}
|
}
|
||||||
|
@ -159,8 +158,14 @@ func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ivl := time.Duration(float64(time.Millisecond) * reqData.Interval)
|
set, err := aghnet.NewDomainNameSet(reqData.Ignored)
|
||||||
|
if err != nil {
|
||||||
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "ignored: %s", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ivl := time.Duration(reqData.Interval) * time.Millisecond
|
||||||
err = validateIvl(ivl)
|
err = validateIvl(ivl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "unsupported interval: %s", err)
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "unsupported interval: %s", err)
|
||||||
|
@ -168,31 +173,20 @@ func (s *StatsCtx) handlePutStatsConfig(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer s.configModified()
|
|
||||||
|
|
||||||
if reqData.Enabled == aghalg.NBNull {
|
if reqData.Enabled == aghalg.NBNull {
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null")
|
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "enabled is null")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer s.configModified()
|
||||||
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
s.enabled = reqData.Enabled == aghalg.NBTrue
|
s.ignored = set
|
||||||
|
|
||||||
s.limit = ivl
|
s.limit = ivl
|
||||||
|
s.enabled = reqData.Enabled == aghalg.NBTrue
|
||||||
if len(reqData.Ignored) > 0 {
|
|
||||||
set, serr := aghnet.NewDomainNameSet(reqData.Ignored)
|
|
||||||
if serr != nil {
|
|
||||||
aghhttp.Error(r, w, http.StatusUnprocessableEntity, "ignored: %s", serr)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ignored = set
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleStatsReset handles requests to the POST /control/stats_reset endpoint.
|
// handleStatsReset handles requests to the POST /control/stats_reset endpoint.
|
||||||
|
|
|
@ -41,7 +41,7 @@ func TestHandleStatsConfig(t *testing.T) {
|
||||||
body: getConfigResp{
|
body: getConfigResp{
|
||||||
Enabled: aghalg.NBTrue,
|
Enabled: aghalg.NBTrue,
|
||||||
Interval: float64(minIvl.Milliseconds()),
|
Interval: float64(minIvl.Milliseconds()),
|
||||||
Ignored: nil,
|
Ignored: []string{},
|
||||||
},
|
},
|
||||||
wantCode: http.StatusOK,
|
wantCode: http.StatusOK,
|
||||||
wantErr: "",
|
wantErr: "",
|
||||||
|
|
|
@ -119,8 +119,7 @@ type StatsCtx struct {
|
||||||
// enabled tells if the statistics are enabled.
|
// enabled tells if the statistics are enabled.
|
||||||
enabled bool
|
enabled bool
|
||||||
|
|
||||||
// limit is an upper limit for collecting statistics into the
|
// limit is an upper limit for collecting statistics.
|
||||||
// current unit.
|
|
||||||
limit time.Duration
|
limit time.Duration
|
||||||
|
|
||||||
// ignored is the list of host names, which should not be counted.
|
// ignored is the list of host names, which should not be counted.
|
||||||
|
@ -460,7 +459,7 @@ func (s *StatsCtx) setLimitLocked(limit time.Duration) {
|
||||||
if limit != 0 {
|
if limit != 0 {
|
||||||
s.enabled = true
|
s.enabled = true
|
||||||
s.limit = limit
|
s.limit = limit
|
||||||
log.Debug("stats: set limit: %d days", int(limit.Hours()/24))
|
log.Debug("stats: set limit: %d days", int(limit/timeutil.Day))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -550,7 +549,6 @@ func (s *StatsCtx) loadUnits(limit uint32) (units []*unitDB, firstID uint32) {
|
||||||
// Per-hour units.
|
// Per-hour units.
|
||||||
units = make([]*unitDB, 0, limit)
|
units = make([]*unitDB, 0, limit)
|
||||||
firstID = curID - limit + 1
|
firstID = curID - limit + 1
|
||||||
|
|
||||||
for i := firstID; i != curID; i++ {
|
for i := firstID; i != curID; i++ {
|
||||||
u := loadUnitFromDB(tx, i)
|
u := loadUnitFromDB(tx, i)
|
||||||
if u == nil {
|
if u == nil {
|
||||||
|
|
Loading…
Reference in New Issue