Browse Source

Date format change + more accurate SQL queries

Hal De 4 năm trước cách đây
mục cha
commit
2b5238fa35
2 tập tin đã thay đổi với 36 bổ sung30 xóa
  1. 32 23
      app/app.py
  2. 4 7
      app/utils.py

+ 32 - 23
app/app.py

@@ -169,8 +169,11 @@ async def getCDR(start=None, end=None, **kwargs):
                                               dst_cnam,
                                               peeraccount
                                        FROM cdr
+                                       WHERE linkedid
+                                       IN (SELECT DISTINCT(linkedid)
+                                       FROM cdr
                                        WHERE calldate
-                                       BETWEEN :start AND :end
+                                       BETWEEN :start AND :end)
                                        ORDER BY linkedid,
                                                 calldate,
                                                 uniqueid;''',
@@ -205,8 +208,11 @@ async def getCEL(start=None, end=None, **kwargs):
                                               uniqueid,
                                               linkedid
                                        FROM cel
+                                       WHERE linkedid
+                                       IN (SELECT DISTINCT(linkedid)
+                                       FROM cel
                                        WHERE eventtime
-                                       BETWEEN :start AND :end
+                                       BETWEEN :start AND :end)
                                        ORDER BY linkedid,
                                                 uniqueid,
                                                 eventtime;''',
@@ -802,7 +808,7 @@ class DeviceUnBind(Resource):
 
 @app.route('/cdr')
 class CDR(Resource):
-  @app.param('end', 'End of datetime range. Defaults to now. Allowed formats are: timestamp, ISO 8601 and ddmmyyyyHHMMSS', 'query')
+  @app.param('end', 'End of datetime range. Defaults to now. Allowed formats are: timestamp, ISO 8601 or yyyymmddHHMMSS', 'query')
   @app.param('start', 'Start of datetime range. Defaults to end-24h. Allowed formats are: timestamp, ISO 8601 and ddmmyyyyHHMMSS', 'query')
   @app.response(HTTPStatus.OK, 'JSON reply')
   @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
@@ -817,7 +823,7 @@ class CDR(Resource):
 
 @app.route('/cel')
 class CEL(Resource):
-  @app.param('end', 'End of datetime range. Defaults to now. Allowed formats are: timestamp, ISO 8601 and ddmmyyyyHHMMSS', 'query')
+  @app.param('end', 'End of datetime range. Defaults to now. Allowed formats are: timestamp, ISO 8601 or yyyymmddHHMMSS', 'query')
   @app.param('start', 'Start of datetime range. Defaults to end-24h. Allowed formats are: timestamp, ISO 8601 and ddmmyyyyHHMMSS', 'query')
   @app.response(HTTPStatus.OK, 'JSON reply')
   @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
@@ -837,7 +843,7 @@ class Calls(Resource):
   @app.response(HTTPStatus.OK, 'JSON reply')
   @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
   async def get(self):
-    '''Returns aggregated call data JSON.
+    '''Returns aggregated call data JSON. NOT IMPLEMENTED.
     All request arguments are optional.
     '''
     calls = []
@@ -846,7 +852,7 @@ class Calls(Resource):
     cdr = await getCDR(start, end)
     for _call in cdr:
       _call0 = _call['events'][0]
-      context = _call0['dcontext']
+      dcontext = _call0['dcontext']
       call = {'id':_call['id'],
               'start':_call0['calldate'],
               'type': None,
@@ -862,23 +868,26 @@ class Calls(Resource):
                      ('recordingfile','url')):
         if _c in  _call0:
           call[_r] = _call0[_c]
-      src = _call0['src'] if 'src' in _call0 else None
-      dst = _call0['dst'] if 'dst' in _call0 else None
-      if src in (*app.cache['ustates'].keys(), *app.cache['devices']):
-        if dst in (*app.cache['ustates'].keys(),
-                   *app.cache['devices'],
-                   *app.cache['queues'].keys()):
-          call['type'] = 'local'
-        else:
-          call['type'] = 'out'
-          call['numberB'] = _call0['dst']
-      else:
-        call['type'] = 'in'
-        if 'did' in _call0:
-          call['line'] = _call0['did']
-      if len(_call['events']) > 1:
-        for step in _call['events'][1:]:
-          pass
+      # if context in ('from-internal'):
+
+      # if context in ('ext-queues'):
+        # call['type'] = 'in'
+        # if 'did' in _call0:
+          # call['line'] = _call0['did']
+
+
+          # call['type'] = 'local'
+        # else:
+          # call['type'] = 'out'
+          # call['numberB'] = _call0['dst']
+      # else:
+        # call['type'] = 'in'
+        # if 'did' in _call0:
+          # call['line'] = _call0['did']
+      # if len(_call['events']) > 1:
+        # for step in _call['events'][1:]:
+
+          # pass
       calls.append(call)
     return successReply(calls)
 

+ 4 - 7
app/utils.py

@@ -6,10 +6,7 @@ from datetime import timedelta as td
 
 TRUEs = ('true', '1', 'y', 'yes')
 NONEs = (None,'none','')
-externalContexts = ('ext-group',
-                    'ext-queues',
-                    'ext-local',
-                    'from-trunk',
+externalContexts = ('from-trunk',
                     'from-digital',
                     'from-pstn',
                     'from-did-direct')
@@ -63,7 +60,7 @@ def parseDatetime(dateString):
   _dt = None
   if dateString is not None:
     try:
-      _dt = dt.strptime(dateString, '%d%m%Y%H%M%S')
+      _dt = dt.strptime(dateString, '%Y%m%d%H%M%S')
     except ValueError:
       pass
     if _dt is None:
@@ -80,8 +77,8 @@ def parseDatetime(dateString):
 
 def freepbxExternalContext(context):
   if ((context in externalContexts) or
-      (context.startswith('from-trunk')) or
-      (context.startswith('from-did')) or
+      (context.startswith('from-trunk-')) or
+      (context.startswith('from-did-')) or
       (context.startswith('ivr-'))):
     return True
   return False