import { syslogAddEntryCmd as o } from "../syslog"
import { setStatsData as e, enableStats as n, disableStats as t } from "../stats"
const s = { NO_OUTPUT: "", DETAILS: "details", LOAD_ONLY: "load_only" },
r = { batchMs: 1e3, logThresholdPercent: 50, consoleOptions: s.NO_OUTPUT, statsOutput: !0 },
a = ({ withChange: o, noChange: e }) => {
const n = o.counter + o.innerLoop,
t = o.sum / n,
s = o.counter
? `[withChange count=${n} sum=${o.sum} avg=${t} top=${o.topAction.evalTime},${o.topAction.type}]`
: "",
r = e.counter + e.innerLoop,
a = e.sum / r
return `actions=${n + r} ${s} ${e.counter ? `[noChange c=${r} sum=${e.sum} avg=${a}]` : ""}`
},
c = ({ loadPercent: o, withChange: e, noChange: n }) => ({
method: "storeLoopStats",
logType: "LOAD_WARN",
response: `load=${o}% ${a({ withChange: e, noChange: n })}`,
}),
i = ({ loadPercent: o, withChange: e, noChange: n, consoleOptions: t }) => {
switch (t) {
case s.LOAD_ONLY: {
const t = e.counter ? `[withChange sum=${e.sum}` : "",
s = n.counter ? `[noChange sum=${n.sum}]` : ""
console.log(`load=${o}% ${t} ${s}`)
break
}
case s.DETAILS:
console.log(c({ loadPercent: o, withChange: e, noChange: n }).response)
}
}
export const storeLoopStatsMiddleware =
(s = {}) =>
(a) => {
const {
batchMs: p,
logThresholdPercent: u,
consoleOptions: h,
statsOutput: l,
} = { ...r, ...s },
m = { counter: 0, topAction: { evalTime: 0, type: "" }, sum: 0, innerLoop: 0 },
g = { counter: 0, sum: 0, innerLoop: 0 }
let L = !1,
$ = 0,
C = 0,
d = "",
A = 0,
O = 0,
w = !1
return (s) => (r) => {
if (!w) {
if (!n.match(r)) return s(r)
w = !0
}
if (t.match(r))
return ((w = !1), (L = !1), ($ = 0), (C = 0), (d = ""), (A = 0), (O = 0), s(r))
if ((l && e.match(r) && (r.payload.store = { load: d, actions: A, avgAction: O }), L))
return (($ += 1), s(r))
;((L = !0), C || (C = performance.now()))
const T = a.getState(),
f = performance.now(),
y = s(r),
P = performance.now(),
v = P - f
;(T !== a.getState()
? ((m.sum += v),
(m.counter += 1),
(m.innerLoop += $),
m.topAction.evalTime < v && ((m.topAction.evalTime = v), (m.topAction.type = r.type)))
: ((g.sum += v), (g.counter += 1), (g.innerLoop += $)),
(L = !1),
($ = 0))
const S = P - C
if (S > p) {
const e = m.sum + g.sum,
n = Math.ceil((e / S) * 100)
;((d = `${n}%`),
(A = m.counter + m.innerLoop + g.counter + g.innerLoop),
(O = Math.ceil(e / A)),
h && i({ loadPercent: n, withChange: m, noChange: g, consoleOptions: h }),
n > u && a.dispatch(o(c({ loadPercent: n, withChange: m, noChange: g }))),
(C = 0),
(m.counter = 0),
(m.sum = 0),
(m.topAction.type = ""),
(m.topAction.evalTime = 0),
(m.innerLoop = 0),
(g.counter = 0),
(g.sum = 0),
(g.innerLoop = 0))
}
return y
}
}