rc2
This commit is contained in:
@@ -29,7 +29,7 @@ type InstantPeriodsWriter struct {
|
||||
time2period func(uint32) time.Time
|
||||
currentPeriod time.Time
|
||||
lastTimestamp uint32
|
||||
endTimestamp uint32 // время показания на конец периода
|
||||
endTimestamp uint32
|
||||
min float64
|
||||
max float64
|
||||
total float64
|
||||
@@ -43,7 +43,6 @@ func NewInstantPeriodsWriter(opt InstantPeriodsWriterOptions) (*InstantPeriodsWr
|
||||
if opt.FirstHourOfDay < 0 || opt.FirstHourOfDay > 23 {
|
||||
return nil, fmt.Errorf("wrong FirstHourOfDay option: %d", opt.FirstHourOfDay)
|
||||
}
|
||||
// Считаю q, чтобы заранее выделить массив для упаковки периодов
|
||||
var q int
|
||||
if (opt.AggregateFuncs & diploma.AggregateMin) == diploma.AggregateMin {
|
||||
q++
|
||||
@@ -59,8 +58,6 @@ func NewInstantPeriodsWriter(opt InstantPeriodsWriterOptions) (*InstantPeriodsWr
|
||||
return nil, errors.New("AggregateFuncs option is required")
|
||||
}
|
||||
|
||||
// 12 - это period, since, until
|
||||
// 8 - это размер float64
|
||||
s := &InstantPeriodsWriter{
|
||||
aggregateFuncs: opt.AggregateFuncs,
|
||||
arr: make([]byte, 12+q*8),
|
||||
@@ -121,20 +118,14 @@ func (s *InstantPeriodsWriter) feed(timestamp uint32, value float64, isBuffer bo
|
||||
if s.entries > 0 {
|
||||
period := s.time2period(timestamp)
|
||||
if period != s.currentPeriod {
|
||||
// закрываю период
|
||||
// готовый период
|
||||
s.packPeriod(timestamp)
|
||||
if isBuffer {
|
||||
s.responder.BufferRecord(s.arr)
|
||||
} else {
|
||||
s.responder.AppendRecord(s.arr)
|
||||
}
|
||||
// затем
|
||||
s.decrementPeriod()
|
||||
//fmt.Println(" period: ", period.Format("2006-01-02 15:04:05"))
|
||||
//fmt.Println("current period: ", s.currentPeriod.Format("2006-01-02 15:04:05"))
|
||||
for period.Before(s.currentPeriod) {
|
||||
// вставляю пустышку
|
||||
s.packBlankPeriod()
|
||||
if isBuffer {
|
||||
s.responder.BufferRecord(s.arr)
|
||||
@@ -142,9 +133,6 @@ func (s *InstantPeriodsWriter) feed(timestamp uint32, value float64, isBuffer bo
|
||||
s.responder.AppendRecord(s.arr)
|
||||
}
|
||||
s.decrementPeriod()
|
||||
//fmt.Println(" period: ", period.Format("2006-01-02 15:04:05"))
|
||||
//fmt.Println("current period: ", s.currentPeriod.Format("2006-01-02 15:04:05"))
|
||||
//return
|
||||
}
|
||||
s.endTimestamp = timestamp
|
||||
s.min = value
|
||||
@@ -157,7 +145,6 @@ func (s *InstantPeriodsWriter) feed(timestamp uint32, value float64, isBuffer bo
|
||||
} else if value > s.max {
|
||||
s.max = value
|
||||
}
|
||||
// для подсчета AVG
|
||||
s.total += value
|
||||
s.entries++
|
||||
}
|
||||
@@ -176,7 +163,6 @@ func (s *InstantPeriodsWriter) decrementPeriod() {
|
||||
switch s.groupBy {
|
||||
case diploma.GroupByHour:
|
||||
s.currentPeriod = s.currentPeriod.Add(-1 * time.Hour)
|
||||
//fmt.Println("decrement")
|
||||
case diploma.GroupByDay:
|
||||
s.currentPeriod = s.currentPeriod.AddDate(0, 0, -1)
|
||||
case diploma.GroupByMonth:
|
||||
@@ -185,11 +171,6 @@ func (s *InstantPeriodsWriter) decrementPeriod() {
|
||||
}
|
||||
|
||||
func (s *InstantPeriodsWriter) packBlankPeriod() {
|
||||
//period := s.currentPeriod.Format("2006-01-02 15:04:05")
|
||||
//since := "0"
|
||||
//until := "0"
|
||||
//fmt.Printf("%s: %s - %s, %.0f - %.0f\n", period, since, until, 0.0, 0.0)
|
||||
// until - это endTimestamp всегда
|
||||
bin.PutUint32(s.arr[0:], uint32(s.currentPeriod.Unix()))
|
||||
for i := 4; i < len(s.arr); i++ {
|
||||
s.arr[i] = 0
|
||||
@@ -223,17 +204,10 @@ func (s *InstantPeriodsWriter) packPeriod(timestamp uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Идея с разбивкой на периоды:
|
||||
Для каждого периода нахожу одно последнее значение.
|
||||
Начало периода - это конец предыдущего. Если предыдущий не строго предыдущий,
|
||||
а с пропусками - на место пропусков вставляю пустышки.
|
||||
Плюс такого решения - я всегда показываю реальное значение на конец периода.
|
||||
*/
|
||||
|
||||
type CumulativePeriodsWriter struct {
|
||||
arr []byte
|
||||
responder *ChunkedResponder
|
||||
since uint32
|
||||
firstHourOfDay int
|
||||
currentPeriod time.Time
|
||||
groupBy diploma.GroupBy
|
||||
@@ -247,6 +221,7 @@ type CumulativePeriodsWriter struct {
|
||||
type CumulativePeriodsWriterOptions struct {
|
||||
Dst io.Writer
|
||||
GroupBy diploma.GroupBy
|
||||
Since uint32
|
||||
FirstHourOfDay int
|
||||
}
|
||||
|
||||
@@ -254,7 +229,6 @@ func NewCumulativePeriodsWriter(opt CumulativePeriodsWriterOptions) (*Cumulative
|
||||
if opt.Dst == nil {
|
||||
return nil, errors.New("Dst option is required")
|
||||
}
|
||||
// Считаю q, чтобы заранее выделить массив для упаковки периодов
|
||||
if opt.FirstHourOfDay < 0 || opt.FirstHourOfDay > 23 {
|
||||
return nil, fmt.Errorf("wrong firstHourOfDay option: %d", opt.FirstHourOfDay)
|
||||
}
|
||||
@@ -262,6 +236,7 @@ func NewCumulativePeriodsWriter(opt CumulativePeriodsWriterOptions) (*Cumulative
|
||||
s := &CumulativePeriodsWriter{
|
||||
arr: make([]byte, 28),
|
||||
responder: NewChunkedResponder(opt.Dst),
|
||||
since: opt.Since,
|
||||
firstHourOfDay: opt.FirstHourOfDay,
|
||||
groupBy: opt.GroupBy,
|
||||
}
|
||||
@@ -322,17 +297,13 @@ func (s *CumulativePeriodsWriter) feed(timestamp uint32, value float64, isBuffer
|
||||
if s.endTimestamp > 0 {
|
||||
period := s.time2period(timestamp)
|
||||
if period != s.currentPeriod {
|
||||
// закрываю период
|
||||
s.packPeriod(timestamp, value)
|
||||
if isBuffer {
|
||||
s.responder.BufferRecord(s.arr)
|
||||
} else {
|
||||
s.responder.AppendRecord(s.arr)
|
||||
}
|
||||
// затем
|
||||
s.decrementPeriod()
|
||||
//fmt.Println(" period: ", period.Format("2006-01-02 15:04:05"))
|
||||
//fmt.Println("current period: ", s.currentPeriod.Format("2006-01-02 15:04:05"))
|
||||
for period.Before(s.currentPeriod) {
|
||||
// вставляю пустышку
|
||||
s.packBlankPeriod()
|
||||
@@ -342,9 +313,6 @@ func (s *CumulativePeriodsWriter) feed(timestamp uint32, value float64, isBuffer
|
||||
s.responder.AppendRecord(s.arr)
|
||||
}
|
||||
s.decrementPeriod()
|
||||
//fmt.Println(" period: ", period.Format("2006-01-02 15:04:05"))
|
||||
//fmt.Println("current period: ", s.currentPeriod.Format("2006-01-02 15:04:05"))
|
||||
//return
|
||||
}
|
||||
s.endTimestamp = timestamp
|
||||
s.endValue = value
|
||||
@@ -362,7 +330,6 @@ func (s *CumulativePeriodsWriter) decrementPeriod() {
|
||||
switch s.groupBy {
|
||||
case diploma.GroupByHour:
|
||||
s.currentPeriod = s.currentPeriod.Add(-1 * time.Hour)
|
||||
//fmt.Println("decrement")
|
||||
case diploma.GroupByDay:
|
||||
s.currentPeriod = s.currentPeriod.AddDate(0, 0, -1)
|
||||
case diploma.GroupByMonth:
|
||||
@@ -371,11 +338,6 @@ func (s *CumulativePeriodsWriter) decrementPeriod() {
|
||||
}
|
||||
|
||||
func (s *CumulativePeriodsWriter) packBlankPeriod() {
|
||||
//period := s.currentPeriod.Format("2006-01-02 15:04:05")
|
||||
//since := "0"
|
||||
//until := "0"
|
||||
//fmt.Printf("%s: %s - %s, %.0f - %.0f\n", period, since, until, 0.0, 0.0)
|
||||
// until - это endTimestamp всегда
|
||||
bin.PutUint32(s.arr[0:], uint32(s.currentPeriod.Unix()))
|
||||
for i := 4; i < len(s.arr); i++ {
|
||||
s.arr[i] = 0
|
||||
@@ -383,11 +345,6 @@ func (s *CumulativePeriodsWriter) packBlankPeriod() {
|
||||
}
|
||||
|
||||
func (s *CumulativePeriodsWriter) packPeriod(start uint32, startValue float64) {
|
||||
//period := s.currentPeriod.Format("2006-01-02 15:04:05")
|
||||
//since := time.Unix(int64(start), 0).Format("2006-01-02 15:04:05")
|
||||
//until := time.Unix(int64(s.endTimestamp), 0).Format("2006-01-02 15:04:05")
|
||||
//fmt.Printf("%s: %s - %s, %.0f - %.0f\n", period, since, until, startValue, s.endValue)
|
||||
// until - это endTimestamp всегда
|
||||
bin.PutUint32(s.arr[0:], uint32(s.currentPeriod.Unix()))
|
||||
bin.PutUint32(s.arr[4:], start)
|
||||
bin.PutUint32(s.arr[8:], s.endTimestamp)
|
||||
@@ -397,12 +354,14 @@ func (s *CumulativePeriodsWriter) packPeriod(start uint32, startValue float64) {
|
||||
|
||||
func (s *CumulativePeriodsWriter) Close() error {
|
||||
if s.endTimestamp > 0 {
|
||||
if s.lastTimestamp != s.endTimestamp {
|
||||
s.packPeriod(s.lastTimestamp, s.lastValue)
|
||||
} else {
|
||||
s.packPeriod(s.endTimestamp, s.endValue)
|
||||
if s.endTimestamp >= s.since {
|
||||
if s.lastTimestamp != s.endTimestamp {
|
||||
s.packPeriod(s.lastTimestamp, s.lastValue)
|
||||
} else {
|
||||
s.packPeriod(s.endTimestamp, s.endValue)
|
||||
}
|
||||
s.responder.AppendRecord(s.arr)
|
||||
}
|
||||
s.responder.AppendRecord(s.arr)
|
||||
}
|
||||
return s.responder.Flush()
|
||||
}
|
||||
|
||||
@@ -51,7 +51,6 @@ type InstantMeasureWriter struct {
|
||||
}
|
||||
|
||||
func NewInstantMeasureWriter(dst io.Writer, since uint32) *InstantMeasureWriter {
|
||||
// 12 - это timestamp, value
|
||||
return &InstantMeasureWriter{
|
||||
arr: make([]byte, 12),
|
||||
responder: NewChunkedResponder(dst),
|
||||
@@ -101,7 +100,6 @@ type CumulativeMeasureWriter struct {
|
||||
}
|
||||
|
||||
func NewCumulativeMeasureWriter(dst io.Writer, since uint32) *CumulativeMeasureWriter {
|
||||
// 20 - это timestamp, value, total
|
||||
return &CumulativeMeasureWriter{
|
||||
arr: make([]byte, 20),
|
||||
responder: NewChunkedResponder(dst),
|
||||
@@ -138,10 +136,8 @@ func (s *CumulativeMeasureWriter) pack(total float64) {
|
||||
|
||||
func (s *CumulativeMeasureWriter) Close() error {
|
||||
if s.endTimestamp >= s.since {
|
||||
// endTimestamp внутри заданного периода. Других показаний нет,
|
||||
// поэтому время добавляю, но накопленную сумму ставлю 0.
|
||||
s.pack(0)
|
||||
// Если < since - ничего делать не нужно, ибо накопленная сумма уже добавлена
|
||||
s.responder.BufferRecord(s.arr)
|
||||
}
|
||||
return s.responder.Flush()
|
||||
}
|
||||
|
||||
@@ -70,36 +70,20 @@ func (s *ChunkedResponder) Flush() error {
|
||||
if _, err := s.dst.Write(endMsg); err != nil {
|
||||
return err
|
||||
}
|
||||
//fmt.Printf("sent endMsg %d\n", endMsg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ChunkedResponder) sendBuffered() (err error) {
|
||||
msg := s.buf.Bytes()
|
||||
bin.PutUint32(msg[1:], uint32(s.recordsQty))
|
||||
//fmt.Printf("put uint16: %d\n", msg[:3])
|
||||
|
||||
//fmt.Printf("send %d records\n", s.recordsQty)
|
||||
|
||||
//fmt.Printf("send buffered: %d, qty: %d\n", msg, s.recordsQty)
|
||||
|
||||
n, err := s.dst.Write(msg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if n != len(msg) {
|
||||
return fmt.Errorf("incomplete write %d bytes instead of %d", n, len(msg))
|
||||
}
|
||||
|
||||
s.buf.Reset()
|
||||
return
|
||||
}
|
||||
|
||||
// Для Aggregation пишем функцию определения периода и пуляем фактические периоды
|
||||
//
|
||||
|
||||
// By default net/http.Server uses 4KB buffers, which are flushed to client with chunked responses.
|
||||
// These buffers may result in visible overhead for responses exceeding a few megabytes.
|
||||
// So allocate 64Kb buffers.
|
||||
// bw: bufio.NewWriterSize(w, 64*1024),
|
||||
|
||||
Reference in New Issue
Block a user