2024-05-12 14:33:29 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statsSum struct {
|
2024-05-22 19:01:20 +00:00
|
|
|
fields []string
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *statsSum) String() string {
|
2024-05-22 19:01:20 +00:00
|
|
|
return "sum(" + statsFuncFieldsToString(ss.fields) + ")"
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
func (ss *statsSum) updateNeededFields(neededFields fieldsSet) {
|
2024-05-22 19:01:20 +00:00
|
|
|
updateNeededFieldsForStatsFunc(neededFields, ss.fields)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
2024-12-22 01:09:36 +00:00
|
|
|
func (ss *statsSum) newStatsProcessor(a *chunkedAllocator) statsProcessor {
|
|
|
|
ssp := a.newStatsSumProcessor()
|
|
|
|
ssp.sum = nan
|
|
|
|
return ssp
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type statsSumProcessor struct {
|
|
|
|
sum float64
|
|
|
|
}
|
|
|
|
|
2025-01-13 03:48:19 +00:00
|
|
|
func (ssp *statsSumProcessor) updateStatsForAllRows(sf statsFunc, br *blockResult) int {
|
|
|
|
ss := sf.(*statsSum)
|
|
|
|
fields := ss.fields
|
2024-05-22 19:01:20 +00:00
|
|
|
if len(fields) == 0 {
|
2024-05-12 14:33:29 +00:00
|
|
|
// Sum all the columns
|
|
|
|
for _, c := range br.getColumns() {
|
2024-05-20 02:08:30 +00:00
|
|
|
ssp.updateStateForColumn(br, c)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Sum the requested columns
|
2024-05-22 19:01:20 +00:00
|
|
|
for _, field := range fields {
|
2024-05-12 14:33:29 +00:00
|
|
|
c := br.getColumnByName(field)
|
2024-05-20 02:08:30 +00:00
|
|
|
ssp.updateStateForColumn(br, c)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2025-01-13 03:48:19 +00:00
|
|
|
func (ssp *statsSumProcessor) updateStatsForRow(sf statsFunc, br *blockResult, rowIdx int) int {
|
|
|
|
ss := sf.(*statsSum)
|
|
|
|
fields := ss.fields
|
2024-05-22 19:01:20 +00:00
|
|
|
if len(fields) == 0 {
|
2024-05-12 14:33:29 +00:00
|
|
|
// Sum all the fields for the given row
|
|
|
|
for _, c := range br.getColumns() {
|
2024-05-20 02:08:30 +00:00
|
|
|
f, ok := c.getFloatValueAtRow(br, rowIdx)
|
|
|
|
if ok {
|
|
|
|
ssp.updateState(f)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Sum only the given fields for the given row
|
2024-05-22 19:01:20 +00:00
|
|
|
for _, field := range fields {
|
2024-05-12 14:33:29 +00:00
|
|
|
c := br.getColumnByName(field)
|
2024-05-20 02:08:30 +00:00
|
|
|
f, ok := c.getFloatValueAtRow(br, rowIdx)
|
|
|
|
if ok {
|
|
|
|
ssp.updateState(f)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
func (ssp *statsSumProcessor) updateStateForColumn(br *blockResult, c *blockResultColumn) {
|
|
|
|
f, count := c.sumValues(br)
|
|
|
|
if count > 0 {
|
|
|
|
ssp.updateState(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssp *statsSumProcessor) updateState(f float64) {
|
|
|
|
if math.IsNaN(ssp.sum) {
|
|
|
|
ssp.sum = f
|
|
|
|
} else {
|
|
|
|
ssp.sum += f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-14 14:30:03 +00:00
|
|
|
func (ssp *statsSumProcessor) mergeState(_ *chunkedAllocator, _ statsFunc, sfp statsProcessor) {
|
2024-05-12 14:33:29 +00:00
|
|
|
src := sfp.(*statsSumProcessor)
|
2024-05-22 19:01:20 +00:00
|
|
|
if !math.IsNaN(src.sum) {
|
|
|
|
ssp.updateState(src.sum)
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
2025-01-13 03:48:19 +00:00
|
|
|
func (ssp *statsSumProcessor) finalizeStats(_ statsFunc, dst []byte, _ <-chan struct{}) []byte {
|
2024-12-17 14:16:03 +00:00
|
|
|
return strconv.AppendFloat(dst, ssp.sum, 'f', -1, 64)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseStatsSum(lex *lexer) (*statsSum, error) {
|
2024-05-22 19:01:20 +00:00
|
|
|
fields, err := parseStatsFuncFields(lex, "sum")
|
2024-05-12 14:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ss := &statsSum{
|
2024-05-22 19:01:20 +00:00
|
|
|
fields: fields,
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|