-
Notifications
You must be signed in to change notification settings - Fork 0
/
axxess_8520_smdr_util.py
120 lines (85 loc) · 3.06 KB
/
axxess_8520_smdr_util.py
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
import datetime
import logging
import re
import string
import sqlite3
def clean_line(line):
"""Clean any garbage characters from a line"""
# Cleanup 'Q' or 'R' and null characters from beginning or end of line
cleaned_line = string.lstrip(line, 'Q\0')
return string.rstrip(cleaned_line, 'R\0')
def file_to_cdr(filename, record_date):
"""Convert a file to a list of CDRs, split on newline"""
cdr_list = []
if (filename is not None):
f = open(filename)
if (f is not None):
for line in f:
cleaned_line = clean_line(line)
cdr = line_to_cdr(cleaned_line, record_date)
if (cdr is not False):
cdr_list.append(cdr)
f.close()
return cdr_list
return false
def line_to_cdr(line, record_date):
"""Convert a line of text to a list containing CDR fields"""
logging.debug("Line: %s", line)
if (line == '\n'):
return False
m = re.match(r'Station', line)
# Footer?
if (m is not None):
return False
m = re.match(r'TYP', line)
# Other footer?
if (m is not None):
return False
# Call Detail Record?
logging.debug("Matching line against CDR regex")
m = re.match(r'([\w/]*?)\s+?([\d\*]+?)\s+?(\d{5})\s+?[RING\s\.]*?([\d\-]+?)\s+?(\d*?)\s+?(\d\d):(\d\d)\s+?S=(\d+?)\s', line)
if (m is None):
logging.info('Line did not match CDR record')
return False
logging.info('Line matched CDR record')
# Type field returned an empty string?
if (not m.group(1)):
type = 'RG'
else:
type = m.group(1)
if (m.group(2) == '*****'):
ext = 0
else:
ext = int(m.group(2))
trunk = int(m.group(3))
# Strip hyphens from phone number
phone_number = int(m.group(4).replace('-', ''))
if (m.group(5) is not ''):
incoming_ext = int(m.group(5))
else:
incoming_ext = 0
start_time_hh = int(m.group(6))
start_time_mm = int(m.group(7))
duration = int(m.group(8))
if (record_date is None):
record_date = datetime.date.today()
start_time = datetime.datetime.combine(
record_date,
datetime.time(hour=start_time_hh, minute=start_time_mm))
# Log record
logging.debug("Type: %s", type)
logging.debug("Ext: %s", str(ext))
logging.debug("Trunk: %s", str(trunk))
logging.debug("Number: %s", str(phone_number))
logging.debug("Incoming ext: %s", str(incoming_ext))
logging.debug("Start time: %s", str(start_time))
logging.debug("Duration: %s", str(duration))
cdr = [type, ext, trunk, phone_number, incoming_ext, start_time,
datetime.timedelta(seconds=duration)]
return cdr
def insert_cdr_record(cdr, cursor):
"""Insert one CDR field into a database"""
logging.debug("Executing query")
cursor.execute("INSERT INTO logviewer_phonerecord VALUES (NULL,'%s',%d,%d,%d,%d,'%s','%s')" % (cdr[0], cdr[1], cdr[2], cdr[3], cdr[4], cdr[5], cdr[6]))
logging.debug("Cursor.rowcount holds %d", cursor.rowcount)
return cursor.rowcount