2024-04-29 01:20:43 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statsSum struct {
|
2024-05-22 09:25:49 +00:00
|
|
|
fields []string
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *statsSum) String() string {
|
2024-05-22 09:25:49 +00:00
|
|
|
return "sum(" + statsFuncFieldsToString(ss.fields) + ")"
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 02:11:10 +00:00
|
|
|
func (ss *statsSum) updateNeededFields(neededFields fieldsSet) {
|
2024-05-22 09:25:49 +00:00
|
|
|
updateNeededFieldsForStatsFunc(neededFields, ss.fields)
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 01:23:41 +00:00
|
|
|
func (ss *statsSum) newStatsProcessor() (statsProcessor, int) {
|
2024-04-29 01:20:43 +00:00
|
|
|
ssp := &statsSumProcessor{
|
2024-05-01 08:31:46 +00:00
|
|
|
ss: ss,
|
|
|
|
sum: nan,
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
return ssp, int(unsafe.Sizeof(*ssp))
|
|
|
|
}
|
|
|
|
|
|
|
|
type statsSumProcessor struct {
|
|
|
|
ss *statsSum
|
|
|
|
|
|
|
|
sum float64
|
|
|
|
}
|
|
|
|
|
2024-04-30 21:03:34 +00:00
|
|
|
func (ssp *statsSumProcessor) updateStatsForAllRows(br *blockResult) int {
|
2024-05-22 09:25:49 +00:00
|
|
|
fields := ssp.ss.fields
|
|
|
|
if len(fields) == 0 {
|
2024-04-29 01:20:43 +00:00
|
|
|
// Sum all the columns
|
2024-04-30 21:03:34 +00:00
|
|
|
for _, c := range br.getColumns() {
|
2024-05-15 20:19:21 +00:00
|
|
|
ssp.updateStateForColumn(br, c)
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
2024-05-03 12:03:17 +00:00
|
|
|
} else {
|
|
|
|
// Sum the requested columns
|
2024-05-22 09:25:49 +00:00
|
|
|
for _, field := range fields {
|
2024-05-03 12:03:17 +00:00
|
|
|
c := br.getColumnByName(field)
|
2024-05-15 20:19:21 +00:00
|
|
|
ssp.updateStateForColumn(br, c)
|
2024-05-01 08:31:46 +00:00
|
|
|
}
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-04-30 21:03:34 +00:00
|
|
|
func (ssp *statsSumProcessor) updateStatsForRow(br *blockResult, rowIdx int) int {
|
2024-05-22 09:25:49 +00:00
|
|
|
fields := ssp.ss.fields
|
|
|
|
if len(fields) == 0 {
|
2024-04-29 01:20:43 +00:00
|
|
|
// Sum all the fields for the given row
|
2024-04-30 21:03:34 +00:00
|
|
|
for _, c := range br.getColumns() {
|
2024-05-15 20:19:21 +00:00
|
|
|
f, ok := c.getFloatValueAtRow(br, rowIdx)
|
2024-05-15 11:07:15 +00:00
|
|
|
if ok {
|
2024-05-15 20:19:21 +00:00
|
|
|
ssp.updateState(f)
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 12:03:17 +00:00
|
|
|
} else {
|
|
|
|
// Sum only the given fields for the given row
|
2024-05-22 09:25:49 +00:00
|
|
|
for _, field := range fields {
|
2024-05-03 12:03:17 +00:00
|
|
|
c := br.getColumnByName(field)
|
2024-05-15 20:19:21 +00:00
|
|
|
f, ok := c.getFloatValueAtRow(br, rowIdx)
|
2024-05-15 11:07:15 +00:00
|
|
|
if ok {
|
2024-05-15 20:19:21 +00:00
|
|
|
ssp.updateState(f)
|
2024-05-01 08:31:46 +00:00
|
|
|
}
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-05-15 20:19:21 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 01:23:41 +00:00
|
|
|
func (ssp *statsSumProcessor) mergeState(sfp statsProcessor) {
|
2024-04-29 01:20:43 +00:00
|
|
|
src := sfp.(*statsSumProcessor)
|
2024-05-21 13:15:16 +00:00
|
|
|
if !math.IsNaN(src.sum) {
|
|
|
|
ssp.updateState(src.sum)
|
|
|
|
}
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 23:19:22 +00:00
|
|
|
func (ssp *statsSumProcessor) finalizeStats() string {
|
|
|
|
return strconv.FormatFloat(ssp.sum, 'f', -1, 64)
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseStatsSum(lex *lexer) (*statsSum, error) {
|
2024-05-22 09:25:49 +00:00
|
|
|
fields, err := parseStatsFuncFields(lex, "sum")
|
2024-04-29 01:20:43 +00:00
|
|
|
if err != nil {
|
2024-05-03 09:15:09 +00:00
|
|
|
return nil, err
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
ss := &statsSum{
|
2024-05-22 09:25:49 +00:00
|
|
|
fields: fields,
|
2024-04-29 01:20:43 +00:00
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|