-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodid.go
204 lines (187 loc) · 5.81 KB
/
mongodid.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
package main
import (
"fmt"
log "github.com/cihub/seelog"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"time"
)
//
func isDid(session *mgo.Session, value string) (err error) {
collection := session.DB(config.MongoDbName).C("dids")
var selector = bson.M{"did": value}
//
var did Did
err = collection.Find(selector).One(&did)
//
if err == nil && did.Did == value {
return nil
}
return err
}
//
func processDidImport(session *mgo.Session, cdr RawCall) (err error) {
log.Debugf("Import by dnid : %s\n", cdr.Dnid)
if cdr.Did == "" {
return
}
err = processDidDailyAnalytics(session, cdr)
if err != nil {
return err
}
err = processDidMonthlyAnalytics(session, cdr)
if err != nil {
return err
}
err = processDidMonthlyAnalyticsSummary(session, cdr)
if err != nil {
return err
}
return nil
}
//
//To import data for monthly did summary, just the state of answered and non answered calls
//with calls duration
func processDidMonthlyAnalyticsSummary(session *mgo.Session, cdr RawCall) (err error) {
//
var collectionName = ""
var did = cdr.Did
collectionName = "monthlydid_summary"
//
var id = fmt.Sprintf("%04d%02d-%s", cdr.Calldate.Year(),
cdr.Calldate.Month(), did)
//
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 := DidMetaData{Did: did, Dt: metaDate}
doc := DidSummaryCall{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 {
log.Debugf("Monthly update can't be executed , get the error: [ %v], Try execute insert.", err)
err = collection.Insert(doc)
if err != nil {
log.Errorf("[did] Monthly insert failed with error : [%v].", err)
return err
}
info, err = collection.Find(selector).Apply(change, &doc)
if info != nil {
log.Debugf("Monthly did new record inserted : %s.", doc.Id)
} else {
log.Errorf("Monthly did can't be updated, get the error : [%v] for the document : %s", err, doc.Id)
return err
}
}
return nil
}
//
func processDidMonthlyAnalytics(session *mgo.Session, cdr RawCall) (err error) {
//
var collectionName = ""
var dst = cdr.Did
collectionName = "monthlydid_incomming"
//
var id = fmt.Sprintf("%04d%02d-%s-%d", cdr.Calldate.Year(),
cdr.Calldate.Month(), dst, 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: dst, Disposition: cdr.Disposition, Dt: metaDate}
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.Errorf("[did] Monthly insert failed with error : [%v].", err)
return err
}
info, err = collection.Find(selector).Apply(change, &doc)
if info != nil {
log.Debugf("Monthly did new record inserted : %s.", doc.Id)
} else {
if err != nil {
log.Debugf("Document [%s] was updated, the update numbers: [%s].\n", doc.Id, info.Updated)
return err
}
}
}
return nil
}
//
func processDidDailyAnalytics(session *mgo.Session, cdr RawCall) (err error) {
//
var collectionName = ""
var dst = cdr.Did
collectionName = "dailydid_incomming"
//
var id = fmt.Sprintf("%04d%02d%02d-%s-%d", cdr.Calldate.Year(), cdr.Calldate.Month(),
cdr.Calldate.Day(), dst, 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: dst, 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 did update can't be executed , get the error: [ %v], Try execute insert.", err)
err = collection.Insert(doc)
if err != nil {
log.Error("Daily did insert failed with error : [%v].", err)
return err
}
info, err = collection.Find(selector).Apply(change, &doc)
if info != nil {
log.Debugf("Daily did document updated with success for the document : %s", doc.Id)
return err
}
}
//
return nil
}