Skip to content

Commit eeee938

Browse files
committed
[ADD] cdr history also by uniqeid
1 parent e5bacf0 commit eeee938

4 files changed

Lines changed: 51 additions & 23 deletions

File tree

const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "1.0.3"
1+
VERSION = "1.0.4"

routers/history_calls.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@
88

99
from dependencies.auth import verify_basic_auth
1010
from exceptions.exceptions import BusinessError
11+
from schemas.config_schema import Id
1112
from services.database import MysqlStrategy, PostgresqlStrategy, SqliteStrategy
1213

1314
log = logging.getLogger("asterisk_agent")
1415
router = APIRouter(tags=["API"], dependencies=[Depends(verify_basic_auth)])
1516

1617

1718
@router.get("/api/calls/hisroty/")
18-
async def calls_history(req: Request, start_date: AwareDatetime, end_date: AwareDatetime):
19-
"""Return calls history
20-
19+
async def calls_history(
20+
req: Request,
21+
start_date: AwareDatetime,
22+
end_date: AwareDatetime,
23+
uniqueid: Id = None,
24+
):
25+
"""
2126
Arguments:
2227
start_date -- start date
2328
end_date -- end date
29+
uniqueid -- id of call in asterisk
2430
2531
Raises:
2632
BusinessError: The start date cannot be greater than or equal to the end date
@@ -37,4 +43,4 @@ async def calls_history(req: Request, start_date: AwareDatetime, end_date: Aware
3743
req.app.state.connector_database
3844
)
3945

40-
return await connector_database.get_cdr(start_date, end_date)
46+
return await connector_database.get_cdr(start_date, end_date, uniqueid)

schemas/config_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
HttpURL = Annotated[Url, UrlConstraints(allowed_schemes=["http", "https"], max_length=2048)]
1212
WsURL = Annotated[Url, UrlConstraints(allowed_schemes=["ws", "wss"], max_length=2048)]
1313
TcpPort = Annotated[int, Field(ge=0, le=65535)]
14+
Id = Annotated[int, Field(ge=1, le=4294967295)]
1415

1516

1617
class DbConfig(BaseModel):

services/database.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)