/
statistics.go
292 lines (269 loc) · 7.98 KB
/
statistics.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package service
import (
"bytes"
"context"
"fmt"
"strconv"
"time"
"go-common/app/job/main/up-rating/model"
"go-common/library/log"
xtime "go-common/library/time"
"golang.org/x/sync/errgroup"
)
const (
// TotalType total type
TotalType int = iota
// CreativeType creative type
CreativeType
// InfluenceType influence type
InfluenceType
// CreditType Credit type
CreditType
)
var (
_offset int64 = 10
_sumTotal int64
_creativeTotal int64
_influenceTotal int64
_creditTotal int64
)
func initSection(min, max, section int64, ctype int, tagID int64, date xtime.Time) *model.RatingStatis {
return &model.RatingStatis{
Section: section,
Tips: fmt.Sprintf("\"%d-%d\"", min, max),
TagID: tagID,
CDate: date,
CType: ctype,
}
}
func initSections(totalScore, tagID int64, ctype int, date xtime.Time) (statis []*model.RatingStatis) {
var (
idx int64
)
statis = make([]*model.RatingStatis, totalScore/_offset)
for idx*_offset < totalScore {
statis[idx] = initSection(idx*_offset, (idx+1)*_offset, idx, ctype, tagID, date)
idx++
}
return
}
// RunStatistics run up rating statistics
func (s *Service) RunStatistics(c context.Context, date time.Time) (err error) {
err = s.initTotalScore(c)
if err != nil {
log.Error("s.initTotalScore error(%v)", err)
return
}
err = s.delStatistics(c, date)
if err != nil {
log.Error("s.delStatistics error(%v)", err)
return
}
err = s.statistics(c, date)
if err != nil {
log.Error("s.scoreStatistics error(%v)", err)
}
return
}
func (s *Service) initTotalScore(c context.Context) (err error) {
params, err := s.getAllParamter(c)
if err != nil {
log.Error("s.getAllParamter error(%v)", err)
return
}
_sumTotal = params.WCSR + params.HR + params.WISR
_creativeTotal = params.WCSR
_influenceTotal = params.WISR
_creditTotal = params.HR
return
}
func (s *Service) delStatistics(c context.Context, date time.Time) (err error) {
err = s.delRatingCom(c, "up_rating_statistics", date)
if err != nil {
return
}
err = s.delRatingCom(c, "up_rating_top", date)
return
}
// delRatingCom del com
func (s *Service) delRatingCom(c context.Context, table string, date time.Time) (err error) {
for {
var rows int64
rows, err = s.dao.DelRatingCom(c, table, date, _limit)
if err != nil {
return
}
if rows == 0 {
break
}
}
return
}
func (s *Service) statistics(c context.Context, date time.Time) (err error) {
var (
readGroup errgroup.Group
sourceCh = make(chan []*model.Rating, _limit)
statisCh = make(chan []*model.Rating, _limit)
topCh = make(chan []*model.Rating, _limit)
sections map[int]map[int64][]*model.RatingStatis
topRating map[int]map[int64]*RatingHeap
)
baseInfo, err := s.BaseTotal(c, date)
if err != nil {
log.Error("s.BaseTotal error(%v)", err)
return
}
// get rating info
readGroup.Go(func() (err error) {
err = s.RatingInfos(c, date, sourceCh)
if err != nil {
log.Error("s.RatingInfos error(%v)", err)
}
return
})
// dispatch
readGroup.Go(func() (err error) {
defer func() {
close(topCh)
close(statisCh)
}()
for rating := range sourceCh {
statisCh <- rating
topCh <- rating
}
return
})
// top
readGroup.Go(func() (err error) {
topRating, err = s.ratingTop(c, date, topCh)
if err != nil {
log.Error("s.RatingTop error(%v)", err)
}
return
})
// statis
readGroup.Go(func() (err error) {
sections, err = s.scoreStatistics(c, date, statisCh, baseInfo)
if err != nil {
log.Error("s.scoreStatistics error(%v)", err)
}
return
})
if err = readGroup.Wait(); err != nil {
log.Error("run readGroup.Wait error(%v)", err)
return
}
// persistent
var writeGroup errgroup.Group
//up_rating_statistics
writeGroup.Go(func() (err error) {
err = s.insertSections(c, sections)
if err != nil {
log.Error("s.insertSections error(%v)", err)
}
return
})
// up_rating_top
writeGroup.Go(func() (err error) {
_, err = s.insertTopRating(c, date, topRating, baseInfo)
if err != nil {
log.Error("s.insertSections error(%v)", err)
}
return
})
if err = writeGroup.Wait(); err != nil {
log.Error("run writeGroup.Wait error(%v)", err)
}
return
}
func (s *Service) scoreStatistics(c context.Context, date time.Time, source chan []*model.Rating, baseInfo map[int64]*model.BaseInfo) (sections map[int]map[int64][]*model.RatingStatis, err error) {
sections = make(map[int]map[int64][]*model.RatingStatis) // map[ctype][tagID][]*model.RatingStatis
sections[TotalType] = make(map[int64][]*model.RatingStatis)
sections[CreativeType] = make(map[int64][]*model.RatingStatis)
sections[InfluenceType] = make(map[int64][]*model.RatingStatis)
sections[CreditType] = make(map[int64][]*model.RatingStatis)
for rating := range source {
for _, r := range rating {
statisScoreCtype(TotalType, r.CreativityScore+r.InfluenceScore+r.CreditScore, _sumTotal, sections, date, r, baseInfo[r.MID])
statisScoreCtype(CreativeType, r.CreativityScore, _creativeTotal, sections, date, r, baseInfo[r.MID])
statisScoreCtype(InfluenceType, r.InfluenceScore, _influenceTotal, sections, date, r, baseInfo[r.MID])
statisScoreCtype(CreditType, r.CreditScore, _creditTotal, sections, date, r, baseInfo[r.MID])
}
}
return
}
func statisScoreCtype(ctype int, score, totalScore int64, sections map[int]map[int64][]*model.RatingStatis, date time.Time, rate *model.Rating, base *model.BaseInfo) {
if _, ok := sections[ctype][rate.TagID]; !ok {
sections[ctype][rate.TagID] = initSections(totalScore, rate.TagID, ctype, xtime.Time(date.Unix()))
}
idx := score / _offset
if idx >= int64(len(sections[ctype][rate.TagID])) {
idx = int64(len(sections[ctype][rate.TagID]) - 1)
}
sections[ctype][rate.TagID][idx].Ups++
sections[ctype][rate.TagID][idx].TotalScore += rate.CreativityScore + rate.InfluenceScore + rate.CreditScore
sections[ctype][rate.TagID][idx].CreativityScore += rate.CreativityScore
sections[ctype][rate.TagID][idx].InfluenceScore += rate.InfluenceScore
sections[ctype][rate.TagID][idx].CreditScore += rate.CreditScore
if base != nil {
sections[ctype][rate.TagID][idx].Fans += base.TotalFans
sections[ctype][rate.TagID][idx].Avs += base.TotalAvs
sections[ctype][rate.TagID][idx].Coin += base.TotalCoin
sections[ctype][rate.TagID][idx].Play += base.TotalPlay
}
}
func (s *Service) insertSections(c context.Context, sections map[int]map[int64][]*model.RatingStatis) (err error) {
for ctype, tags := range sections {
for tagID, statis := range tags {
_, err = s.insertRatingStatis(c, ctype, tagID, statis)
if err != nil {
return
}
}
}
return
}
func (s *Service) insertRatingStatis(c context.Context, ctype int, tagID int64, statis []*model.RatingStatis) (rows int64, err error) {
return s.dao.InsertRatingStatis(c, assembleRatingStatis(c, ctype, tagID, statis))
}
func assembleRatingStatis(c context.Context, ctype int, tagID int64, statis []*model.RatingStatis) (vals string) {
var buf bytes.Buffer
for _, s := range statis {
buf.WriteString("(")
buf.WriteString(strconv.FormatInt(s.Ups, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.Section, 10))
buf.WriteByte(',')
buf.WriteString(s.Tips)
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.TotalScore, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.CreativityScore, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.InfluenceScore, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.CreditScore, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.Fans, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.Avs, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.Coin, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.Play, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(s.TagID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.Itoa(s.CType))
buf.WriteByte(',')
buf.WriteString("'" + s.CDate.Time().Format(_layout) + "'")
buf.WriteString(")")
buf.WriteByte(',')
}
if buf.Len() > 0 {
buf.Truncate(buf.Len() - 1)
}
vals = buf.String()
buf.Reset()
return
}