@@ -42,13 +42,19 @@ def __init__(self, config: Config) -> None:
4242 async def check_cdr_old (self ):
4343 """Check that Asterisk cdr have start column or not"""
4444
45+ async def get_cdr_uniqueid (self , uniqueid ):
46+ """Return calls history
47+
48+ Arguments:
49+ uniqueid -- id of call in asterisk
50+ """
51+
4552 async def get_cdr (self , start_date , end_date , uniqueid = None ):
4653 """Return calls history
4754
4855 Arguments:
4956 start_date -- start date of calls
5057 end_date -- end date of calls
51- uniqueid -- id of call in asterisk
5258 """
5359
5460 async def get_cel (self , start_date , end_date ):
@@ -82,27 +88,32 @@ async def check_cdr_old(self):
8288 self .cdr_start_field = "calldate"
8389 return
8490
85- async def get_cdr (self , start_date , end_date , uniqueid = None ):
91+ async def get_cdr_uniqueid (self , uniqueid ):
92+ import aiosqlite
93+
94+ result = []
95+ async with aiosqlite .connect (self .config .db_host ) as database :
96+ database .row_factory = aiosqlite .Row
97+ async with database .execute (
98+ f"SELECT * FROM { self .config .db_table_cdr_name } where uniqueid= %s limit 1;" ,
99+ [uniqueid ],
100+ ) as cursor :
101+ async for row in cursor :
102+ result .append (row )
103+ return result
104+
105+ async def get_cdr (self , start_date , end_date ):
86106 import aiosqlite
87107
88- # host==path "/var/lib/asterisk/astdb.sqlite3"
89108 result = []
90109 async with aiosqlite .connect (self .config .db_host ) as database :
91110 database .row_factory = aiosqlite .Row
92- if uniqueid :
93- async with database .execute (
94- f"SELECT * FROM { self .config .db_table_cdr_name } where uniqueid= %s limit 1;" ,
95- [uniqueid ],
96- ) as cursor :
97- async for row in cursor :
98- result .append (row )
99- else :
100- async with database .execute (
101- f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
102- [start_date , end_date ],
103- ) as cursor :
104- async for row in cursor :
105- result .append (row )
111+ async with database .execute (
112+ f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
113+ [start_date , end_date ],
114+ ) as cursor :
115+ async for row in cursor :
116+ result .append (row )
106117 return result
107118
108119 async def get_cel (self , start_date , end_date ):
@@ -180,18 +191,23 @@ async def check_cdr_old(self):
180191 self .cdr_start_field = "calldate"
181192 return
182193
183- async def get_cdr (self , start_date , end_date , uniqueid = None ):
194+ async def get_cdr_uniqueid (self , uniqueid ):
184195 conn , cur = await self .get_conn_cur ()
185- if uniqueid :
186- await cur .execute (
187- f"SELECT * FROM { self .config .db_table_cdr_name } where uniqueid = %s limit 1;" ,
188- (uniqueid ),
189- )
190- else :
191- await cur .execute (
192- f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
193- (start_date , end_date ),
194- )
196+ await cur .execute (
197+ f"SELECT * FROM { self .config .db_table_cdr_name } where uniqueid = %s limit 1;" ,
198+ (uniqueid ),
199+ )
200+ rows = await cur .fetchall ()
201+ await cur .close ()
202+ conn .close ()
203+ return rows
204+
205+ async def get_cdr (self , start_date , end_date ):
206+ conn , cur = await self .get_conn_cur ()
207+ await cur .execute (
208+ f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
209+ (start_date , end_date ),
210+ )
195211 rows = await cur .fetchall ()
196212 await cur .close ()
197213 conn .close ()
@@ -254,18 +270,22 @@ async def check_cdr_old(self):
254270 self .cdr_start_field = "calldate"
255271 return
256272
257- async def get_cdr (self , start_date , end_date , uniqueid = None ):
273+ async def get_cdr_uniqueid (self , uniqueid ):
258274 conn , cur = await self .get_conn_cur ()
259- if uniqueid :
260- await cur .execute (
261- f"SELECT * FROM { self .config .db_table_cdr_name } where uniqueid = %s limit 1;" ,
262- (uniqueid ),
263- )
264- else :
265- await cur .execute (
266- f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
267- (start_date , end_date ),
268- )
275+ await cur .execute (
276+ f"SELECT * FROM { self .config .db_table_cdr_name } where uniqueid = %s limit 1;" ,
277+ (uniqueid ),
278+ )
279+ rows = await cur .fetchall ()
280+ await conn .close ()
281+ return rows
282+
283+ async def get_cdr (self , start_date , end_date ):
284+ conn , cur = await self .get_conn_cur ()
285+ await cur .execute (
286+ f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
287+ (start_date , end_date ),
288+ )
269289 rows = await cur .fetchall ()
270290 await conn .close ()
271291 return rows
0 commit comments