-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongopeer.go
241 lines (209 loc) · 6.77 KB
/
mongopeer.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
package main
import (
"errors"
"fmt"
log "github.com/cihub/seelog"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"time"
)
//
func processPeerMonthlySummary(session *mgo.Session, cdr RawCall) (err error) {
//
var collectionName = ""
var peer = ""
if cdr.InoutStatus == DIRECTION_CALL_OUT {
collectionName = "monthlypeeroutgoing_summary"
if len(cdr.Src) == 4 {
peer = cdr.Src
} else {
peer = cdr.Dst
}
} else if cdr.InoutStatus == DIRECTION_CALL_IN {
collectionName = "monthlypeerincomming_summary"
peer = cdr.Peer
} else {
return errors.New("Can't detect the call context")
}
if len(peer) > 5 {
serr := fmt.Sprintf("Peer monthly summary something is wrong with the call [%s].Please verify your log.\n", cdr.Uniqueid)
return errors.New(serr)
}
//
var id = fmt.Sprintf("%04d%02d-%s", cdr.Calldate.Year(),
cdr.Calldate.Month(), peer)
//
var metaDate = time.Date(cdr.Calldate.Year(), cdr.Calldate.Month(),
1, 1, 0, 0, 0, time.UTC)
//
var collection = session.DB(config.MongoDbName).C(collectionName)
metaDoc := PeerMetaData{Peer: peer, Dt: metaDate}
doc := PeerSummaryCall{Id: id, Meta: metaDoc, Calls: 0, Missing: 0, Duration: 0}
//
var selector = bson.M{"_id": id, "metadata": metaDoc}
//
missing := 0
if cdr.Disposition > 16 {
missing = 1
}
var change = mgo.Change{
Update: bson.M{"$inc": bson.M{"calls": 1, "missing": missing, "duration": cdr.Billsec}},
ReturnNew: false,
}
//
var info = new(mgo.ChangeInfo)
info, err = collection.Find(selector).Apply(change, &doc)
//check if the can execute changes
if info == nil || info.Updated == 0 {
err = collection.Insert(doc)
if err != nil {
log.Errorf("[peer] Monthly summary insert failed with error : [%v].", err)
return err
}
info, err = collection.Find(selector).Apply(change, &doc)
if info != nil {
log.Debugf("Monthly new record inserted : %s.", doc.Id)
} else {
log.Errorf("Monthly can't be updated, get the error : [%v] for the document : %s", err, doc.Id)
}
}
if err != nil {
log.Debugf("Monthly document [%s] was updated, the update numbers: [%s].\n", doc.Id, info.Updated)
return err
}
return nil
}
//
func processMonthlyAnalytics(session *mgo.Session, cdr RawCall) (err error) {
//
var collectionName = ""
var peer = ""
if cdr.InoutStatus == DIRECTION_CALL_OUT {
collectionName = "monthlypeer_outgoing"
if len(cdr.Src) == 4 {
peer = cdr.Src
} else {
peer = cdr.Dst
}
} else if cdr.InoutStatus == DIRECTION_CALL_IN {
collectionName = "monthlypeer_incomming"
peer = cdr.Peer
} else {
return errors.New("Can't detect the call context")
}
if len(peer) > 5 {
serr := fmt.Sprintf("Monthly analytics something is wrong with the call [%s].Please verify your log.\n", cdr.Uniqueid)
return errors.New(serr)
}
if peer == "" {
var serrr = fmt.Sprintf("Can't get a valide destination for the call with the uniqueid [%s].\n", cdr.Uniqueid)
log.Error(serrr)
log.Flush()
return errors.New(serrr)
}
//
var id = fmt.Sprintf("%04d%02d-%s-%d", cdr.Calldate.Year(),
cdr.Calldate.Month(), peer, cdr.Disposition)
//
var metaDate = time.Date(cdr.Calldate.Year(), cdr.Calldate.Month(),
1, 1, 0, 0, 0, time.UTC)
//
var collection = session.DB(config.MongoDbName).C(collectionName)
metaDoc := MetaData{Dst: peer, Dt: metaDate, Disposition: cdr.Disposition}
doc := MonthlyCall{Id: id, Meta: metaDoc, AnswereWaitTime: 0,
CallMonthly: 0, DurationMonthly: 0}
//
var selector = bson.M{"_id": id, "metadata": metaDoc}
var callsDailyInc = fmt.Sprintf("calls_per_days.%d", cdr.Calldate.Day())
var durationsDailyInc = fmt.Sprintf("durations_per_days.%d", cdr.Calldate.Day())
//
var change = mgo.Change{
Update: bson.M{"$inc": bson.M{"calls": 1, "duration": cdr.Billsec,
"answer_wait_time": cdr.AnswerWaitTime, callsDailyInc: 1, durationsDailyInc: cdr.Billsec},
},
ReturnNew: false,
}
//
var info = new(mgo.ChangeInfo)
info, err = collection.Find(selector).Apply(change, &doc)
//check if the can execute changes
if info == nil || info.Updated == 0 {
log.Debugf("Monthly update can't be executed , get the error: [ %v], Try execute insert.", err)
err = collection.Insert(doc)
if err != nil {
log.Error("Monthly insert failed with error : [%v].", err)
return err
}
info, err = collection.Find(selector).Apply(change, &doc)
if err != nil {
log.Errorf("Monthly analytics can't be updated, get the error : [%v] for the document : %s", err, doc.Id)
return err
}
}
if err != nil {
log.Errorf("Monthly analytics can't be updated, get the error : [%v] for the document : %s", err, doc.Id)
return err
}
return nil
}
func processDailyAnalytics(session *mgo.Session, cdr RawCall) (err error) {
//
var collectionName = ""
var peer = ""
if cdr.InoutStatus == DIRECTION_CALL_OUT {
collectionName = "dailypeer_outgoing"
if len(cdr.Src) == 4 {
peer = cdr.Src
} else {
peer = cdr.Dst
}
} else if cdr.InoutStatus == DIRECTION_CALL_IN {
collectionName = "dailypeer_incomming"
peer = cdr.Peer
} else {
return errors.New("Daily analytics can't detect the call context")
}
if len(peer) > 5 {
serr := fmt.Sprintf("Daily analytics something is wrong with the call [%s] cause peer [%s].Please verify your log.\n", cdr.Uniqueid, peer)
return errors.New(serr)
}
//
var id = fmt.Sprintf("%04d%02d%02d-%s-%d", cdr.Calldate.Year(), cdr.Calldate.Month(),
cdr.Calldate.Day(), peer, cdr.Disposition)
var metaDate = time.Date(cdr.Calldate.Year(), cdr.Calldate.Month(), cdr.Calldate.Day(), 1, 0, 0, 0, time.UTC)
var collection = session.DB(config.MongoDbName).C(collectionName)
metaDoc := MetaData{Dst: peer, Dt: metaDate, Disposition: cdr.Disposition}
doc := DailyCall{Id: id, Meta: metaDoc, AnswereWaitTime: 0, CallDaily: 0,
DurationDaily: 0}
//
var selector = bson.M{"_id": id, "metadata": metaDoc}
var hourlyInc = fmt.Sprintf("calls_per_hours.%d", cdr.Calldate.Hour())
var durationHourlyInc = fmt.Sprintf("durations_per_hours.%d", cdr.Calldate.Hour())
//
var change = mgo.Change{
Update: bson.M{"$inc": bson.M{"calls": 1, "duration": cdr.Billsec,
"answer_wait_time": cdr.AnswerWaitTime, hourlyInc: 1, durationHourlyInc: cdr.Billsec},
},
ReturnNew: false,
}
//
var info = new(mgo.ChangeInfo)
info, err = collection.Find(selector).Apply(change, &doc)
//check if the can execute changes
if info == nil || info.Updated == 0 {
log.Debugf("Daily analytics update can't be executed , get the error: [ %v], Try execute insert.", err)
err = collection.Insert(doc)
if err != nil {
log.Error("Daily analytics insert failed with error : [%v].", err)
return err
}
info, err = collection.Find(selector).Apply(change, &doc)
if err != nil {
log.Debugf("Daily analytics can't be updated, get the error : [%v] for the document : %s", err, doc.Id)
return err
}
}
//
log.Debugf("Document updated : %s\n", doc.Id)
return nil
}