From 0caab096bd0f33ea37339b0acfd89ea1b0b28b8a Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 30 Jan 2021 00:03:43 +0800 Subject: [PATCH] feat: add functions --- statistics/main.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/statistics/main.go b/statistics/main.go index 197fe0b..5244e2c 100644 --- a/statistics/main.go +++ b/statistics/main.go @@ -13,7 +13,7 @@ type ChannelItem struct { type DurationChannelItems struct { durationTag string - channelItemMap map[string]ChannelItem + channelItemMap map[string]*ChannelItem } var durationChannelItemsMap = map[string]DurationChannelItems{} @@ -26,6 +26,24 @@ type CountItem struct { 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(ChannelItem, 0) @@ -38,6 +56,11 @@ func collectCountItem() { for { countItem := <-countItemChan // TODO ... + fmt.Printf("%v\n", countItem) } } + +func makeDurationTag(timestamp uint64) string { + return fmt.Sprintf("%d", uint64(timestamp/1000)) +}