From fdcded3422bc35964b00c41c24eb1143ef2f908e Mon Sep 17 00:00:00 2001 From: vassilux Date: Thu, 12 Mar 2015 18:57:42 +0100 Subject: [PATCH] Fix pvsa --- config.json | 92 ++++++++++++++++++++----------------------------- docs/INSTALL.md | 2 +- main.go | 26 +++++++++----- main_test.go | 52 +++++++++++++++++++++++++--- mongo.go | 1 + mysql.go | 9 +++-- tools.go | 10 +++++- 7 files changed, 120 insertions(+), 72 deletions(-) diff --git a/config.json b/config.json index aaaad80..ad024bf 100644 --- a/config.json +++ b/config.json @@ -1,9 +1,9 @@ { - "dbMySqlHost": "192.168.3.20:3306", + "dbMySqlHost": "127.0.0.1:3306", "dbMySqlUser": "root", "dbMySqlPassword": "lepanos", "dbMySqlName": "asteriskcdrdb", - "dbMySqlFetchRowNumber": "81", + "dbMySqlFetchRowNumber": "25", "mongoHost": "127.0.0.1", "mongoDbName" : "revor", "asteriskID": "asterisk1", @@ -14,63 +14,45 @@ "testCallActive": false, "testCallSchedule": 120, "eventsMongoHost": "127.0.0.1", + "purgeCelEvents": false, "notifications": [ "f1com" ], "dialplanContext": [ - { "name": "app-out", "direction": 1}, - { "name": "incomming", "direction": 2}, - { "name": "outgoing", "direction": 1}, - { "name": "DLPN_DialPlan1", "direction": 2}, - { "name": "app-daynight-toggle", "direction": 3}, - { "name": "app-custom-blf-queue", "direction": 3}, - { "name": "iax2-esi-internal", "direction": 3}, - { "name": "test-dnd", "direction": 3}, - { "name": "app-pickup", "direction": 3}, -{ "name": "app-custom-2802-vm-on", "direction": 3}, - { "name": "app-custom-2802-vm-off", "direction": 3}, - { "name": "test-incall-for-kebab", "direction": 1}, - { "name": "test-teletech-alarm-call", "direction": 1}, - { "name": "default", "direction": 2}, - { "name": "ringroups-custom-2", "direction": 2}, - { "name": "app-custom-2802-vm", "direction": 2}, - - { "name": "app-custom-get-vm", "direction": 2}, - { "name": "app-dial-secratary", "direction": 2}, - { "name": "app-dial-secratary", "direction": 2}, - { "name": "test-teletech-alarm-call", "direction": 1}, - { "name": "incomming-calls-f1-playback", "direction": 2}, - { "name": "app-custom-play-office-holiday", "direction": 2}, - { "name": "from-xoip-f1", "direction": 2}, - { "name": "inner-calls", "direction": 3}, - { "name": "ringroups-custom-1", "direction": 2}, - { "name": "ringroups-custom-2", "direction": 2}, - { "name": "ringroups-custom-3", "direction": 2}, - { "name": "DID_span_1", "direction": 2}, - { "name": "DID_span_2", "direction": 2}, - { "name": "DID_span_3", "direction": 2}, - { "name": "DID_span_4", "direction": 2}, - { "name": "DID_span_5", "direction": 2}, - { "name": "DID_span_6", "direction": 2}, - { "name": "CallingRule_0_national", "direction": 1}, - { "name": "queues", "direction": 2}, - { "name": "voicemenu-custom-1", "direction": 2}, - { "name": "incomming-calls-for-recording", "direction": 2}, - { "name": "incomming-calls-f1com", "direction": 1}, - { "name": "from-g200be", "direction": 2}, - { "name": "to-g200be", "direction": 1}, - { "name": "asterisk_guitools", "direction": 3}, - { "name": "simulate-outcall", "direction": 3}, - { "name": "test-teletech-alive-call", "direction": 1}, - { "name": "voicemenu-custom-2", "direction": 2}, - { "name": "voicemenu-custom-1", "direction": 2}, - { "name": "conferences", "direction": 2}, - { "name": "appc-esi-conference", "direction": 3}, - { "name": "app-recorde-prompt", "direction": 3}, - { "name": "appc-esi-conference-room-choise", "direction": 3}, - { "name": "app-set-forward-number", "direction": 3}, - { "name": "app-ext-blf-custom", "direction": 3}, - { "name": "app-custom-9999", "direction": 3} + { "name": "app-alive-test", "direction": 3}, + { "name": "app-custom-2587-day", "direction": 2}, + { "name": "app-custom-6705", "direction": 2}, + { "name": "app-custom-6710", "direction": 2}, + { "name": "app-custom-6711", "direction": 2}, + { "name": "app-custom-blf-queue", "direction": 3}, + { "name": "app-custom-getvoicemail", "direction": 3}, + { "name": "app-custom-op-sugar", "direction": 2}, + { "name": "app-ecoute-pvsa-bry", "direction": 2}, + { "name": "app-iax2-bischheim", "direction": 1}, + { "name": "app-iax2-cofintex", "direction": 1}, + { "name": "app-iax2-ludres", "direction": 1}, + { "name": "app-out", "direction": 1}, + { "name": "app-out-hangup-call-party", "direction": 1}, + { "name": "app-pickup", "direction": 3}, + { "name": "asterisk_guitools", "direction": 3}, + { "name": "default", "direction": 3}, + { "name": "DID_span_1", "direction": 2}, + { "name": "DID_span_2", "direction": 2}, + { "name": "DID_span_3", "direction": 1}, + { "name": "DID_span_4", "direction": 1}, + { "name": "DLPN_DialPlan1", "direction": 1}, + { "name": "DLPN_DialPlan2", "direction": 2}, + { "name": "process-in-fax", "direction": 3}, + { "name": "process-in-Modem-RSI", "direction": 3}, + { "name": "process-in-rsi", "direction": 3}, + { "name": "ringroups-custom-1", "direction": 2}, + { "name": "ringroups-custom-2", "direction": 2}, + { "name": "ringroups-custom-3", "direction": 2}, + { "name": "ringroups-custom-4", "direction": 2}, + { "name": "app-custom-from-pstn", "direction": 2}, + { "name": "stdexten", "direction": 3}, + { "name": "voicemenu-custom-1", "direction": 3} + ] } diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 5707f88..c88f319 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -28,7 +28,7 @@ Crée un lien symbolic ln -s /opt/revor/vorimport_[version] /opt/vorimport/curre ### Configuraiton Copier le fichier config.sample.json en config.json : cp config.sample.json config.json -Addapter ce fichier à la configuraiton de système : le dialplan de iPBX et les besoin du client. +Addapter ce fichier à la configuraiton de système : le dialplan de iPBX et les besoins du client. Context app-alive-test de asterisk dialplan est utilisé pour générer un test cyclique de toute la chaine Ce context peut être ajouter dans /etc/asterisk/extensions_custom.conf. diff --git a/main.go b/main.go index 7a9d46e..237b941 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ type Config struct { AsteriskUser string AsteriskPassword string TestCallActive bool + PurgeCelEvents bool TestCallSchedule int DialplanContext []Context Notifications []string @@ -153,7 +154,7 @@ func init() { } func getInOutStatus(cdr RawCall) (status int, err error) { - config = GetConfig() + //config = GetConfig() log.Tracef("Enter into getInOutStatus") for i := range config.DialplanContext { if config.DialplanContext[i].Name == cdr.Dcontext { @@ -296,20 +297,29 @@ func importJob() { extent = getPeerFromChannel(callDetail.Peer) log.Tracef("Get extent [%s] for peer [%s].", extent, callDetail.Peer) - //break + break } } - if extent != "" { - cdr.Dst = extent + if extent == "" { + cdr.Dst = getPeerFromChannel(cdr.Dstchannel) + log.Tracef("Exten is empty for dstchannel [%s] get dst [%s].", cdr.Dstchannel, cdr.Dst) } else { //must be checked cause by testing - cdr.Dst = cdr.Dst //getPeerFromChannel(cdr.Dstchannel) + if cdr.Dst == "s" { + cdr.Dst = getPeerFromChannel(cdr.Dstchannel) + } else { + cdr.Dst = cdr.Dst + } + + } + + if cdr.Dnid == "" { + cdr.Dnid = cdr.Dst } - } else { - cdr.Dst = cdr.Dnid } // + // err = importCdrToMongo(session, cdr) var importedStatus = 1 if err != nil { @@ -318,7 +328,7 @@ func importJob() { // log.Debugf("Import executed for unique id [%s] with code : [%d], try process the mysql updating.\n", cdr.Uniqueid, importedStatus) - err = udpateMySqlCdrImportStatus(db, cdr.Uniqueid, importedStatus) + err = udpateMySqlCdrImportStatus(db, cdr.Uniqueid, 1) if err != nil { log.Errorf("Can't update the import status for the call with unique id [%s].", cdr.Uniqueid) os.Exit(1) diff --git a/main_test.go b/main_test.go index 0f98562..5043a64 100644 --- a/main_test.go +++ b/main_test.go @@ -1,25 +1,67 @@ package main import ( + "strings" "testing" - "time" + //"time" ) +func getPeerFromChannel(channel string) (peer string) { + //try to find the destination from channel + delim := '-' + if strings.Contains(channel, "@") { + delim = '@' + } + + w := strings.FieldsFunc(channel, func(r rune) bool { + switch r { + + case '/', delim: + return true + } + return false + }) + + if len(w) >= 3 { + return w[len(w)-2] + } else { + return channel + } + +} + func Test_DstChannel(t *testing.T) { var channel = "SIP/6006-01010101" peer := getPeerFromChannel(channel) if peer != "6006" { t.Error("It is not good peer for channel [%s].", channel) + t.Fail() } channel = "DAHDI/g1/0493948400-01010101" peer = getPeerFromChannel(channel) if peer != "0493948400" { t.Error("It is not good peer [%s] for channel [%s].", peer, channel) + t.Fail() + } + t.Log("dstChannelTester test passed.") + channel = "'DAHDI/132-1'" + peer = getPeerFromChannel(channel) + if peer != "132" { + t.Error("It is not good peer [%s] for channel [%s].", peer, channel) + t.Fail() } + + channel = "Local/8129@DLPN_DialPlan1-000000e6;1" + peer = getPeerFromChannel(channel) + if peer != "8129" { + t.Errorf("It is not good peer [%s] for channel [%s].", peer, channel) + t.Fail() + } + t.Log("dstChannelTester test passed.") } -func Test_EventWatcher_MySql(t *testing.T) { +/*func Test_EventWatcher_MySql(t *testing.T) { loadConfig(false) config := GetConfig() eventWatcher := NewEventWatcher(config) @@ -71,9 +113,9 @@ func Test_EventWatcher_MySql(t *testing.T) { t.Fail() } -} +}*/ -func Test_CallOriginator(t *testing.T) { +/*func Test_CallOriginator(t *testing.T) { addr := "192.168.3.20" port := 5038 user := "astmanager" @@ -85,4 +127,4 @@ func Test_CallOriginator(t *testing.T) { time.Sleep(1 * time.Second) -} +}*/ diff --git a/mongo.go b/mongo.go index a4e0354..2925373 100644 --- a/mongo.go +++ b/mongo.go @@ -222,6 +222,7 @@ func processDailyAnalytics(session *mgo.Session, cdr RawCall) (err error) { // func processDidImport(session *mgo.Session, cdr RawCall) (err error) { log.Tracef("Import by did : %s\n", cdr.Dnid) + err = processDidDailyAnalytics(session, cdr) if err != nil { return err diff --git a/mysql.go b/mysql.go index 7b6cec4..e014880 100644 --- a/mysql.go +++ b/mysql.go @@ -22,7 +22,7 @@ func bracket(r rune) bool { */ func getMysqlCdr(db mysql.Conn) (results []RawCall, err error) { log.Tracef("Enter into getMysqlCdr") - myQuery := "SELECT UNIX_TIMESTAMP(calldate) as calldate, clid, src, dst, channel, dcontext, disposition,billsec,duration,uniqueid,dstchannel, dnid, recordfile from asteriskcdrdb.cdr WHERE import = 0 and dcontext NOT LIKE 'app-alive-test' LIMIT 0, " + config.DbMySqlFetchRowNumber + myQuery := "SELECT UNIX_TIMESTAMP(calldate) as calldate, clid, src, dst, channel, dcontext, disposition,billsec,duration,uniqueid,dstchannel, dnid, recordfile from asteriskcdrdb.cdr WHERE import = 0 and dcontext NOT LIKE 'app-alive-test' LIMIT " + config.DbMySqlFetchRowNumber // log.Debugf("Executing request [%s]\r\n", myQuery) rows, res, err := db.Query(myQuery) @@ -128,7 +128,7 @@ func getMySqlCel(db mysql.Conn, uniqueid string) (cel Cel, err error) { func getMysqlCdrTestCall(db mysql.Conn) (results []RawCall, err error) { log.Tracef("Enter into getMysqlCdr") - myQuery := "SELECT UNIX_TIMESTAMP(calldate) as calldate, clid, src, dst, channel, dcontext, disposition,billsec,duration,uniqueid,dstchannel, dnid, recordfile from asteriskcdrdb.cdr WHERE import = 0 and dcontext LIKE 'app-alive-test' LIMIT 0, " + config.DbMySqlFetchRowNumber + myQuery := "SELECT UNIX_TIMESTAMP(calldate) as calldate, clid, src, dst, channel, dcontext, disposition,billsec,duration,uniqueid,dstchannel, dnid, recordfile from asteriskcdrdb.cdr WHERE import = 0 and dcontext LIKE 'app-alive-test' LIMIT " + config.DbMySqlFetchRowNumber // log.Debugf("Executing request [%s]\r\n", myQuery) rows, res, err := db.Query(myQuery) @@ -309,6 +309,11 @@ func deleteMySqlCdrRecord(db mysql.Conn, uniqueid string) (err error) { } func deleteMySqlCelRecord(db mysql.Conn, uniqueid string) (err error) { + + if config.PurgeCelEvents == false { + return nil + } + var query = fmt.Sprintf("DELETE FROM cel WHERE uniqueid = '%s' OR linkedid = '%s'", uniqueid, uniqueid) _, _, err = db.Query(query) diff --git a/tools.go b/tools.go index a8de7d1..4f6032d 100644 --- a/tools.go +++ b/tools.go @@ -6,9 +6,16 @@ import ( ) func getPeerFromChannel(channel string) (peer string) { + //try to find the destination from channel + delim := '-' + if strings.Contains(channel, "@") { + delim = '@' + } + w := strings.FieldsFunc(channel, func(r rune) bool { switch r { - case '/', '-', ' ': + + case '/', delim: return true } return false @@ -19,6 +26,7 @@ func getPeerFromChannel(channel string) (peer string) { } else { return channel } + } func schedule(what func(), delay time.Duration) chan bool {