-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cron.php
274 lines (216 loc) · 14.1 KB
/
Cron.php
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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cron {
private $statsd = null;
private $company_tag = null;
private $mysql = null;
private $db_table = null;
public function __construct($statsd, $mysql, $company_tag='null', $db_table='asteriskcdrdb') {
$this->statsd = $statsd;
$this->company_tag = $company_tag;
$this->mysql = $mysql;
$this->db_table = $db_table;
}
public function calls($from, $to) {
$this->print_stdout("Calculate calls for interval ".date("Y-m-d H:i", $from)." - ".date("Y-m-d H:i", $to));
// date_add('2018-03-01 18:11:37', INTERVAL 300 SECOND)
$query = 'SELECT DATE_ADD(calldate, INTERVAL billsec SECOND) as calldate2,calldate,duration,billsec,disposition,channel,dstchannel FROM '.$this->db_table.' WHERE DATE_ADD(calldate, INTERVAL billsec SECOND) >= :from AND DATE_ADD(calldate, INTERVAL billsec SECOND) < :to ORDER BY calldate ASC';
$sth = $this->mysql->prepare($query);
$sth->bindvalue(':from', date("Y-m-d H:i", $from), PDO::PARAM_STR);
$sth->bindvalue(':to', date("Y-m-d H:i", $to), PDO::PARAM_STR);
$sth->execute();
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
if(!empty($rows)) {
$this->print_csv($rows);
}
/*
* Am facut prin ciclu pentru ca la un numar mic de rinduri
* e mai rapit sa le numeri in php decit sa faci request la db,
* citeodata pentru 0 rows.
*/
$totals = array(
'average_waiting' => array(),
'average_duration' => array(),
'duration' => array(),
'count' => array(
'by_company' => array(),
'by_operator' => array()
)
);
if (!empty($rows)) {
foreach ($rows as $key => $item) {
$call_validation = FALSE;
$operator = FALSE;
/*-------------
* INBOUND cals
--------------*/
preg_match("/\SIP\/((?:[0-9]{3}))-/im", $item['dstchannel'], $manager_dst);
if (isset($manager_dst[1]) && is_numeric($manager_dst[1]) && $manager_dst[1] < 999) {
$call_validation = 'inbound';
$operator = $manager_dst[1];
//echo "INBOUND - Manager: ".$manager_dst[1].'\n';
}
/* -----> */
/*-------------
* OUTBOUND cals
--------------*/
preg_match("/\SIP\/((?:[0-9]{3}))-/im", $item['channel'], $manager_src);
if (isset($manager_src[1]) && is_numeric($manager_src[1]) && $manager_src[1] < 999) {
$call_validation = 'outbound';
$operator = $manager_src[1];
//echo "OUTBOUND - Manager: ".$manager_src[1].'\n';
}
/* -----> */
if ($call_validation !== FALSE && in_array($call_validation, array(
'inbound',
'outbound'
)) && $operator !== FALSE && is_numeric($operator) && !empty($item['dstchannel'])) {
/* VALID CALL */
// COUNT - Total by COMPANY
$totals['count']['by_company']['total']['total'] = isset($totals['count']['by_company']['total']['total']) ? $totals['count']['by_company']['total']['total'] + 1 : 1;
// COUNT - Total by COMPANY and call_type
$totals['count']['by_company'][$call_validation]['total'] = isset($totals['count']['by_company'][$call_validation]['total']) ? $totals['count']['by_company'][$call_validation]['total'] + 1 : 1;
// COUNT - Total by OPERATOR
$totals['count']['by_operator'][$operator]['total']['total'] = isset($totals['count']['by_operator'][$operator]['total']['total']) ? $totals['count']['by_operator'][$operator]['total']['total'] + 1 : 1;
// COUNT - Total by OPERATOR and call_type
$totals['count']['by_operator'][$operator][$call_validation]['total'] = isset($totals['count']['by_operator'][$operator][$call_validation]['total']) ? $totals['count']['by_operator'][$operator][$call_validation]['total'] + 1 : 1;
/*-------------
* DISPOSITION
--------------*/
if ($item['disposition'] == "ANSWERED") { // ------------------------------------------------------ ANSWERED
//$totals['duration']['answered'] += $item['duration'];// increment duration
$totals['average_waiting'][$operator][$call_validation]['time'] = isset($totals['average_waiting'][$operator][$call_validation]['time']) ? $totals['average_waiting'][$operator][$call_validation]['time'] + ($item['duration'] - $item['billsec']) : $item['duration'] - $item['billsec']; // increment duration
$totals['average_waiting'][$operator][$call_validation]['count'] = isset($totals['average_waiting'][$operator][$call_validation]['count']) ? $totals['average_waiting'][$operator][$call_validation]['count'] + 1 : 1;
$totals['average_duration'][$operator][$call_validation]['time'] = isset($totals['average_duration'][$operator][$call_validation]['time']) ? $totals['average_duration'][$operator][$call_validation]['time'] + $item['billsec'] : $item['billsec']; // increment duration
$totals['average_duration'][$operator][$call_validation]['count'] = isset($totals['average_duration'][$operator][$call_validation]['count']) ? $totals['average_duration'][$operator][$call_validation]['count'] + 1 : 1;
$totals['duration']['by_company']['total'] = isset($totals['duration']['by_company']['total']) ? $totals['duration']['by_company']['total'] + $item['billsec'] : $item['billsec'];
$totals['duration']['by_company'][$call_validation] = isset($totals['duration']['by_company'][$call_validation]) ? $totals['duration']['by_company'][$call_validation] + $item['billsec'] : $item['billsec']; // increment duration
$totals['duration']['by_operator'][$operator][$call_validation] = isset($totals['duration']['by_operator'][$operator][$call_validation]) ? $totals['duration']['by_operator'][$operator][$call_validation] + $item['billsec'] : $item['billsec']; // increment duration
$totals['duration']['by_operator'][$operator]['total'] = isset($totals['duration']['by_operator'][$operator]['total']) ? $totals['duration']['by_operator'][$operator]['total'] + $item['billsec'] : $item['billsec']; // increment duration
// COUNT - Total answered cals for company
$totals['count']['by_company']['total']['answered'] = isset($totals['count']['by_company']['total']['answered']) ? $totals['count']['by_company']['total']['answered'] + 1 : 1;
// COUNT - Total answered cals by call_type for company
$totals['count']['by_company'][$call_validation]['answered'] = isset($totals['count']['by_company'][$call_validation]['answered']) ? $totals['count']['by_company'][$call_validation]['answered'] + 1 : 1;
// COUNT - Total answered cals for operator
$totals['count']['by_operator'][$operator]['total']['answered'] = isset($totals['count']['by_operator'][$operator]['total']['answered']) ? $totals['count']['by_operator'][$operator]['total']['answered'] + 1 : 1;
// COUNT - Total answered cals by call_type for operator
$totals['count']['by_operator'][$operator][$call_validation]['answered'] = isset($totals['count']['by_operator'][$operator][$call_validation]['answered']) ? $totals['count']['by_operator'][$operator][$call_validation]['answered'] + 1 : 1;
} else if ($item['disposition'] == "NO ANSWER") { // ----------------------------------------------- NO ANSWER
//$totals['duration']['no_answer'] += $item['duration'];// increment duration
// COUNT - Total no_answer cals for company
$totals['count']['by_company']['total']['no_answer'] = isset($totals['count']['by_company']['total']['no_answer']) ? $totals['count']['by_company']['total']['no_answer'] + 1 : 1;
// COUNT - Total answered cals by call_type for company
$totals['count']['by_company'][$call_validation]['no_answer'] = isset($totals['count']['by_company'][$call_validation]['no_answer']) ? $totals['count']['by_company'][$call_validation]['no_answer'] + 1 : 1;
// COUNT - Total no_answer cals for operator
$totals['count']['by_operator'][$operator]['total']['no_answer'] = isset($totals['count']['by_operator'][$operator]['total']['no_answer']) ? $totals['count']['by_operator'][$operator]['total']['no_answer'] + 1 : 1;
// COUNT - Total no_answer cals by call_type for operator
$totals['count']['by_operator'][$operator][$call_validation]['no_answer'] = isset($totals['count']['by_operator'][$operator][$call_validation]['no_answer']) ? $totals['count']['by_operator'][$operator][$call_validation]['no_answer'] + 1 : 1;
} else if ($item['disposition'] == "BUSY") { // ------------------------------------------------------ BUSY
//$totals['duration']['busy'] += $item['duration'];// increment duration
// COUNT - Total busy cals for company
$totals['count']['by_company']['total']['busy'] = isset($totals['count']['by_company']['total']['busy']) ? $totals['count']['by_company']['total']['busy'] + 1 : 1;
// COUNT - Total answered cals by call_type for company
$totals['count']['by_company'][$call_validation]['busy'] = isset($totals['count']['by_company'][$call_validation]['busy']) ? $totals['count']['by_company'][$call_validation]['busy'] + 1 : 1;
// COUNT - Total busy cals for operator
$totals['count']['by_operator'][$operator]['total']['busy'] = isset($totals['count']['by_operator'][$operator]['total']['busy']) ? $totals['count']['by_operator'][$operator]['total']['busy'] + 1 : 1;
// COUNT - Total busy cals for by call_type operator
$totals['count']['by_operator'][$operator][$call_validation]['busy'] = isset($totals['count']['by_operator'][$operator][$call_validation]['busy']) ? $totals['count']['by_operator'][$operator][$call_validation]['busy'] + 1 : 1;
} else if ($item['disposition'] == "FAILED") { // ------------------------------------------------------ FAILED
//$totals['duration']['failed'] += $item['duration'];// increment duration
// COUNT - Total failed cals for company
$totals['count']['by_company']['total']['failed'] = isset($totals['count']['by_company']['total']['failed']) ? $totals['count']['by_company']['total']['failed'] + 1 : 1;
// COUNT - Total answered cals by call_type for company
$totals['count']['by_company'][$call_validation]['failed'] = isset($totals['count']['by_company'][$call_validation]['failed']) ? $totals['count']['by_company'][$call_validation]['failed'] + 1 : 1;
// COUNT - Total failed cals for operator
$totals['count']['by_operator'][$operator]['total']['failed'] = isset($totals['count']['by_operator'][$operator]['total']['failed']) ? $totals['count']['by_operator'][$operator]['total']['failed'] + 1 : 1;
// COUNT - Total failed cals by call_type for operator
$totals['count']['by_operator'][$operator][$call_validation]['failed'] = isset($totals['count']['by_operator'][$operator][$call_validation]['failed']) ? $totals['count']['by_operator'][$operator][$call_validation]['failed'] + 1 : 1;
}
/*----->*/
}
}
}
foreach ($totals['average_waiting'] as $key => $value) {
if (isset($value['outbound']) && is_array($value['outbound'])) {
$avg_w_out = $value['outbound']['time'] / $value['outbound']['count'];
$totals['average_waiting'][$key]['outbound'] = $avg_w_out;
}
if (isset($value['inbound']) && is_array($value['inbound'])) {
$avg_w_in = $value['inbound']['time'] / $value['inbound']['count'];
$totals['average_waiting'][$key]['inbound'] = $avg_w_in;
}
}
foreach ($totals['average_duration'] as $key => $value) {
if (isset($value['outbound']) && is_array($value['outbound'])) {
$avg_d_out = $value['outbound']['time'] / $value['outbound']['count'];
$totals['average_duration'][$key]['outbound'] = $avg_d_out;
}
if (isset($value['inbound']) && is_array($value['inbound'])) {
$avg_d_in = $value['inbound']['time'] / $value['inbound']['count'];
$totals['average_duration'][$key]['inbound'] = $avg_d_in;
}
}
//pr($totals); //die();
// send data to graphiti
foreach ($totals as $key => $group) {
foreach ($group as $key2 => $value) {
if (is_array($value)) {
foreach ($value as $key3 => $item) {
if (is_array($item)) {
foreach ($item as $key4 => $item_f) {
if (is_array($item_f)) {
foreach ($item_f as $key5 => $item5) {
$this->statsd->sendToGraphite("calls." . $this->company_tag . ".". $key."." . $key2 . "." . $key3 . "." . $key4 . "." . $key5, $item5);
}
} else {
$this->statsd->sendToGraphite("calls." . $this->company_tag . "." . $key . "." . $key2 . "." . $key3 . "." . $key4, $item_f);
}
}
} else {
$this->statsd->sendToGraphite("calls." . $this->company_tag . "." . $key . "." . $key2 . "." . $key3, $item);
}
}
} else {
$this->statsd->sendToGraphite("calls." . $this->company_tag . "." . $key . "." . $key2, $value);
}
}
}
//die();
// // save $reference_point_val
// if (isset($totals['count']['by_company']['total']['total']) && $totals['count']['by_company']['total']['total'] > 0) {
// $rows_last_item = end($rows);
// $this->mysql->update('services', array(
// 'reference_point_val' => $rows_last_item['calldate']
// ), array(
// 'sys_name' => $service_key
// ));
// } else {
// $this->mysql->update('services', array(
// 'reference_point_val' => date('Y-m-d H:i:s')
// ), array(
// 'sys_name' => $service_key
// ));
// }
// set_log(isset($totals['count']['by_company']['total']['total']) ? $totals['count']['by_company']['total']['total'] : 0, $service_key);
}
public function print_stderr($msg) {
$date = date("d-m-Y h:i");
file_put_contents('php://stderr', '['.$date.'] '.$msg."\n");
}
public function print_stdout($msg) {
$date = date("d-m-Y h:i");
echo('['.$date.'] '.$msg."\n");
}
public function print_csv($array) {
foreach($array[0] as $i => $j) {
printf("%s, ", $i);
}
print "\n";
foreach($array as $i) {
foreach($i as $j) {
printf("%s, ", $j);
}
print "\n";
}
}
}