feat: add functions

This commit is contained in:
2021-01-30 00:03:43 +08:00
parent c10ff6a81e
commit 0caab096bd

View File

@@ -13,7 +13,7 @@ type ChannelItem struct {
type DurationChannelItems struct { type DurationChannelItems struct {
durationTag string durationTag string
channelItemMap map[string]ChannelItem channelItemMap map[string]*ChannelItem
} }
var durationChannelItemsMap = map[string]DurationChannelItems{} var durationChannelItemsMap = map[string]DurationChannelItems{}
@@ -26,6 +26,24 @@ type CountItem struct {
downBytes uint64 downBytes uint64
} }
func (i *CountItem) merge(i2 *CountItem) (*CountItem, error) {
if i.tag != i2.tag {
return nil, fmt.Errorf("Tag mis match %s vs %s", i.tag, i2.tag)
}
t1 := uint64(i.timestamp/1000) * 1000
t2 := uint64(i2.timestamp/1000) * 1000
if t1 != t2 {
return nil, fmt.Errorf("Timestamp mis match %d vs %d", t1, t2)
}
i3 := &CountItem{
tag: i.tag,
timestamp: t1,
upBytes: i.upBytes + i2.upBytes,
downBytes: i.downBytes + i2.downBytes,
}
return i3, nil
}
var countItemChan = make(chan CountItem) var countItemChan = make(chan CountItem)
// var countItemChan = make(ChannelItem, 0) // var countItemChan = make(ChannelItem, 0)
@@ -38,6 +56,11 @@ func collectCountItem() {
for { for {
countItem := <-countItemChan countItem := <-countItemChan
// TODO ... // TODO ...
fmt.Printf("%v\n", countItem) fmt.Printf("%v\n", countItem)
} }
} }
func makeDurationTag(timestamp uint64) string {
return fmt.Sprintf("%d", uint64(timestamp/1000))
}