@@ -42,12 +42,13 @@ 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 (self , start_date , end_date ):
45+ async def get_cdr (self , start_date , end_date , uniqueid = None ):
4646 """Return calls history
4747
4848 Arguments:
4949 start_date -- start date of calls
5050 end_date -- end date of calls
51+ uniqueid -- id of call in asterisk
5152 """
5253
5354 async def get_cel (self , start_date , end_date ):
@@ -81,19 +82,27 @@ async def check_cdr_old(self):
8182 self .cdr_start_field = "calldate"
8283 return
8384
84- async def get_cdr (self , start_date , end_date ):
85+ async def get_cdr (self , start_date , end_date , uniqueid = None ):
8586 import aiosqlite
8687
8788 # host==path "/var/lib/asterisk/astdb.sqlite3"
8889 result = []
8990 async with aiosqlite .connect (self .config .db_host ) as database :
9091 database .row_factory = aiosqlite .Row
91- async with database .execute (
92- f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
93- [start_date , end_date ],
94- ) as cursor :
95- async for row in cursor :
96- result .append (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 )
97106 return result
98107
99108 async def get_cel (self , start_date , end_date ):
@@ -171,12 +180,18 @@ async def check_cdr_old(self):
171180 self .cdr_start_field = "calldate"
172181 return
173182
174- async def get_cdr (self , start_date , end_date ):
183+ async def get_cdr (self , start_date , end_date , uniqueid = None ):
175184 conn , cur = await self .get_conn_cur ()
176- await cur .execute (
177- f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
178- (start_date , end_date ),
179- )
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+ )
180195 rows = await cur .fetchall ()
181196 await cur .close ()
182197 conn .close ()
@@ -239,12 +254,18 @@ async def check_cdr_old(self):
239254 self .cdr_start_field = "calldate"
240255 return
241256
242- async def get_cdr (self , start_date , end_date ):
257+ async def get_cdr (self , start_date , end_date , uniqueid = None ):
243258 conn , cur = await self .get_conn_cur ()
244- await cur .execute (
245- f"SELECT * FROM { self .config .db_table_cdr_name } where { self .cdr_start_field } >= %s and { self .cdr_start_field } <= %s limit 100000;" ,
246- (start_date , end_date ),
247- )
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+ )
248269 rows = await cur .fetchall ()
249270 await conn .close ()
250271 return rows
0 commit comments