|
|
@@ -10,12 +10,28 @@ from datetime import timedelta as td
|
|
|
from typing import Any, Optional
|
|
|
from databases import Database
|
|
|
from quart import jsonify, request, render_template_string, abort
|
|
|
+from quart.json import JSONEncoder
|
|
|
from quart_openapi import Pint, Resource
|
|
|
from http import HTTPStatus
|
|
|
from panoramisk import Manager, Message
|
|
|
from utils import *
|
|
|
+from cel import *
|
|
|
from logging.config import dictConfig
|
|
|
|
|
|
+class ApiJsonEncoder(JSONEncoder):
|
|
|
+ def default(self, o):
|
|
|
+ if isinstance(o, dt):
|
|
|
+ return o.isoformat()
|
|
|
+ if isinstance(o, CELEventChannel):
|
|
|
+ return str(o)
|
|
|
+ if isinstance(o, CELEvent):
|
|
|
+ return o.__dict__
|
|
|
+ if isinstance(o, CELEvents):
|
|
|
+ return o.all
|
|
|
+ if isinstance(o, CELCall):
|
|
|
+ return o.__dict__
|
|
|
+ return JSONEncoder.default(self, o)
|
|
|
+
|
|
|
class PintDB:
|
|
|
def __init__(self, app: Optional[Pint] = None) -> None:
|
|
|
self.init_app(app)
|
|
|
@@ -38,7 +54,7 @@ class PintDB:
|
|
|
main_loop = asyncio.get_event_loop()
|
|
|
|
|
|
app = Pint(__name__, title=os.getenv('APP_TITLE', 'PBX API'), no_openapi=True)
|
|
|
-
|
|
|
+app.json_encoder = ApiJsonEncoder
|
|
|
app.config.update({
|
|
|
'TITLE': os.getenv('APP_TITLE', 'PBX API'),
|
|
|
'APPLICATION_ROOT': os.getenv('APP_APPLICATION_ROOT', None),
|
|
|
@@ -192,37 +208,22 @@ async def getCEL(start=None, end=None, **kwargs):
|
|
|
end = dt.now()
|
|
|
if start is None:
|
|
|
start=(end - td(hours=24))
|
|
|
- async for row in db.iterate(query='''SELECT linkedid,
|
|
|
- uniqueid,
|
|
|
- eventtime,
|
|
|
- eventtype,
|
|
|
- cid_name,
|
|
|
- cid_num,
|
|
|
- cid_ani,
|
|
|
- cid_rdnis,
|
|
|
- cid_dnid,
|
|
|
- exten,
|
|
|
- context,
|
|
|
- channame,
|
|
|
- appname,
|
|
|
- uniqueid,
|
|
|
- linkedid
|
|
|
+ async for row in db.iterate(query='''SELECT *
|
|
|
FROM cel
|
|
|
WHERE linkedid
|
|
|
IN (SELECT DISTINCT(linkedid)
|
|
|
FROM cel
|
|
|
WHERE eventtime
|
|
|
- BETWEEN :start AND :end)
|
|
|
- ORDER BY linkedid,
|
|
|
- uniqueid,
|
|
|
- eventtime;''',
|
|
|
+ BETWEEN :start AND :end);''',
|
|
|
values={'start':start,
|
|
|
'end':end}):
|
|
|
- event = {_k: str(_v) for _k, _v in row.items() if _k != 'linkedid' and _v != ''}
|
|
|
- _cel.setdefault(row['linkedid'],[]).append(event)
|
|
|
+ if row['linkedid'] in _cel:
|
|
|
+ _cel[row['linkedid']].events.add(row)
|
|
|
+ else:
|
|
|
+ _cel[row['linkedid']]=CELCall(row)
|
|
|
cel = []
|
|
|
for _id in sorted(_cel.keys()):
|
|
|
- cel.append({'id':_id,'events':_cel[_id]})
|
|
|
+ cel.append(_cel[_id])
|
|
|
return cel
|
|
|
|
|
|
@app.before_first_request
|