-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.go
165 lines (133 loc) · 3.63 KB
/
mixer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package datamixer
import (
"errors"
"fmt"
"math"
"sync"
)
// processDataType is a data type for processing
type processDataType struct {
Name string
Resp DataResponse
DataCount int64
Offset int64
Weight int64
}
// Mixer is the mix data struct
type Mixer struct {
GlobalLimit int64
}
// Mix multi source data into one
func (m *Mixer) Mix(datas []SourceData) (dataRes DataResponse, nextOffsetMap map[string]int64, err error) {
pdts, err := m.processData(datas)
if err != nil {
return
}
if len(pdts) == 0 {
return
}
return m.mixResp(pdts)
}
// processData multi source data and return thd data type
func (m *Mixer) processData(datas []SourceData) (pdts []processDataType, err error) {
var wg sync.WaitGroup
for _, sd := range datas {
wg.Add(1)
go func(sd SourceData) {
defer wg.Done()
sDataRes, sErr := sd.GetData(sd.Params, m.GlobalLimit, sd.Offset)
if sErr != nil {
err = errors.New(fmt.Sprintf("%s %s:%s", err.Error(), sd.Name, sErr.Error()))
}
dataLen := int64(len(sDataRes.Data))
if sDataRes.Total > 0 {
pdts = append(pdts, processDataType{
Name: sd.Name,
Resp: sDataRes,
DataCount: dataLen,
Offset: sd.Offset,
Weight: sd.Weight,
})
}
}(sd)
}
wg.Wait()
return
}
func (m *Mixer) mixResp(pdts []processDataType) (retResp DataResponse, nextOffsetMap map[string]int64, err error) {
var (
limit int64
)
totalWeight := m.getTotalWeight(pdts)
nextOffsetMap = make(map[string]int64)
limitMap := m.getRealLimitMap(pdts, totalWeight)
// fmt.Println("limitMap", limitMap)
for _, pdt := range pdts {
limit = limitMap[pdt.Name]
retResp.Data = append(retResp.Data, pdt.Resp.Data[:limit]...)
retResp.Total += pdt.Resp.Total
nextOffsetMap[pdt.Name] = pdt.Offset + limit
}
return
}
func (m *Mixer) getTotalWeight(pdts []processDataType) (totalWeight int64) {
for _, pdt := range pdts {
totalWeight += pdt.Weight
}
return
}
// getRealLimitMap return the real limit map for get data
func (m *Mixer) getRealLimitMap(pdts []processDataType, totalWeight int64) (limitMap map[string]int64) {
var (
weightPercent float64
theoryLimit,
totalTheoryLimit,
realLimit,
needFillCount int64
)
pdtsLen := len(pdts)
leftDataCountMap := make(map[string]int64, pdtsLen)
limitMap = make(map[string]int64, pdtsLen)
for k, pdt := range pdts {
weightPercent = float64(pdt.Weight) / float64(totalWeight)
theoryLimit = int64(round(weightPercent * float64(m.GlobalLimit)))
totalTheoryLimit += theoryLimit
if k == pdtsLen-1 && totalTheoryLimit > m.GlobalLimit {
theoryLimit = m.GlobalLimit - (totalTheoryLimit - theoryLimit)
}
// fmt.Println(pdt.Name, "pdt.Weight", pdt.Weight)
// fmt.Println(pdt.Name, "totalWeight", totalWeight)
// fmt.Println(pdt.Name, "weightPercent", weightPercent)
// fmt.Println(pdt.Name, "pdt.DataCount", pdt.DataCount)
// fmt.Println(pdt.Name, "theoryLimit", theoryLimit)
if pdt.DataCount > theoryLimit {
leftDataCountMap[pdt.Name] = pdt.DataCount - theoryLimit
realLimit = theoryLimit
} else {
needFillCount += theoryLimit - pdt.DataCount
realLimit = pdt.DataCount
}
limitMap[pdt.Name] = realLimit
}
if needFillCount > 0 && len(leftDataCountMap) > 0 {
for name, leftDataCount := range leftDataCountMap {
if needFillCount <= 0 {
break
}
if needFillCount > leftDataCount {
limitMap[name] += leftDataCount
needFillCount = needFillCount - leftDataCount
} else {
limitMap[name] += needFillCount
needFillCount = 0
}
}
}
return
}
func round(input float64) float64 {
if input < 0 {
return math.Ceil(input - 0.5)
}
return math.Floor(input + 0.5)
}