Skip to content

Commit 0ad88dd

Browse files
committed
[ADD] history uniqueid as diff route
1 parent eeee938 commit 0ad88dd

4 files changed

Lines changed: 77 additions & 48 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.4"
1+
VERSION = "1.0.5"

routers/history_calls.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,26 @@
1515
router = APIRouter(tags=["API"], dependencies=[Depends(verify_basic_auth)])
1616

1717

18-
@router.get("/api/calls/hisroty/")
19-
async def calls_history(
18+
@router.get("/api/calls/hisroty/uniqueid")
19+
async def calls_history_uniqueid(
2020
req: Request,
21-
start_date: AwareDatetime,
22-
end_date: AwareDatetime,
23-
uniqueid: Id = None,
21+
uniqueid: Id,
2422
):
23+
log.info("HISTORY UNIQUEID")
24+
25+
connector_database: PostgresqlStrategy | MysqlStrategy | SqliteStrategy = (
26+
req.app.state.connector_database
27+
)
28+
29+
return await connector_database.get_cdr_uniqueid(uniqueid)
30+
31+
32+
@router.get("/api/calls/hisroty/")
33+
async def calls_history(req: Request, start_date: AwareDatetime, end_date: AwareDatetime):
2534
"""
2635
Arguments:
2736
start_date -- start date
2837
end_date -- end date
29-
uniqueid -- id of call in asterisk
3038
3139
Raises:
3240
BusinessError: The start date cannot be greater than or equal to the end date
@@ -43,4 +51,4 @@ async def calls_history(
4351
req.app.state.connector_database
4452
)
4553

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

schemas/config_schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
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)]
14+
# Id = Annotated[int, Field(ge=1, le=4294967295)]
15+
Id = Annotated[str, Field(max_length=128, min_length=3)]
1516

1617

1718
class DbConfig(BaseModel):

services/database.py

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

Comments
 (0)