Explorar el Código

restored endpoint in python

Gamaliel Espinoza hace 7 años
padre
commit
f390d72b03
Se han modificado 5 ficheros con 86 adiciones y 3 borrados
  1. 1 1
      ondemand/__init__.py
  2. 24 0
      ondemand/endpoint.py
  3. 6 2
      ondemand/service.py
  4. 0 0
      tests/__init__.py
  5. 55 0
      tests/test_general.py

+ 1 - 1
ondemand/__init__.py

@@ -1 +1 @@
-__version__ = '1.1.4'
+__version__ = '1.1.5'

+ 24 - 0
ondemand/endpoint.py

@@ -0,0 +1,24 @@
+from __future__ import absolute_import
+from tornado.web import RequestHandler, Application
+import ondemand
+import psutil
+import os
+
+
+class InformerHandler(RequestHandler):
+    def get(self):
+        settings = self.application.settings
+        process = psutil.Process()
+        queue = settings['queue']
+        return self.write(dict(
+            version=ondemand.__version__,
+            processID=os.getpid(),
+            memoryUsage=process.memory_info().rss,
+            queueSize=queue.qsize(),
+        ))
+
+def setup_endpoint(queue=None):
+    app = Application([(r'/info', InformerHandler)], queue=queue)
+    app.listen(49223)
+    return app
+    

+ 6 - 2
ondemand/service.py

@@ -6,6 +6,7 @@ from fourier.api.client import Client, ConnectionError
 from fourier.boxconfig import parse_config
 from fourier.dejavu.recognize import FilePerSecondRecognizer
 from datetime import datetime, timedelta
+from ondemand.endpoint import setup_endpoint
 from fourier.dejavu import Dejavu
 from Queue import Queue, Empty
 from firebase_admin import credentials
@@ -217,9 +218,12 @@ def cloud_download(ad_key=None):
             cloud_cache[ad_key] = tp
             return tp
 
-app = Application()
+app = setup_endpoint(queue=queue)
 loop = IOLoop.current()
 loop.add_callback(feed_queue)
 
 if __name__ == '__main__':
-    loop.start()
+    try:
+        loop.start()
+    except KeyboardInterrupt:
+        print('process killed')

+ 0 - 0
tests/__init__.py


+ 55 - 0
tests/test_general.py

@@ -0,0 +1,55 @@
+from __future__ import absolute_import
+from ondemand.service import find_repetitions
+from ondemand.service import cloud_download
+import unittest
+import time
+
+
+class TestGeneral(unittest.TestCase):
+
+    def test_find_repetitions(self):
+        found = find_repetitions([
+            {'timestamp': 1519769650, 'confidence': 5},
+            {'timestamp': 1519769655, 'confidence': 5},
+            {'timestamp': 1519769660, 'confidence': 15},
+            {'timestamp': 1519769665, 'confidence': 15},
+            {'timestamp': 1519769670, 'confidence': 25},
+            {'timestamp': 1519769675, 'confidence': 47},
+            {'timestamp': 1519769680, 'confidence': 150},
+            {'timestamp': 1519769685, 'confidence': 47},
+            {'timestamp': 1519769690, 'confidence': 19},
+            {'timestamp': 1519769695, 'confidence': 15},
+            {'timestamp': 1519769700, 'confidence': 15},
+            {'timestamp': 1519769705, 'confidence': 15},
+            {'timestamp': 1519769710, 'confidence': 15},
+            {'timestamp': 1519769720, 'confidence': 15},
+            {'timestamp': 1519769725, 'confidence': 150},
+            {'timestamp': 1519769730, 'confidence': 300},
+            {'timestamp': 1519769735, 'confidence': 5},
+        ])
+        self.assertEquals(len(found), 2)
+        self.assertEquals(found[0], 1519769670)
+        self.assertEquals(found[1], 1519769725)
+
+    def test_find_empty_repetitions(self):
+        found = find_repetitions([])
+        self.assertTrue(not found)
+
+    def test_find_not_enough_data(self):
+        found = find_repetitions([
+            {'timestamp': 1519769650, 'confidence': 5},
+            {'timestamp': 1519769655, 'confidence': 5},
+            {'timestamp': 1519769660, 'confidence': 100},
+            {'timestamp': 1519769665, 'confidence': 15},
+            {'timestamp': 1519769670, 'confidence': 25}
+        ])
+        self.assertTrue(not found)
+
+    def test_cloud_download(self):
+        filename, md5hash = cloud_download(ad_key='-L6EoklO90painUCd7df')
+        print(filename, md5hash)
+        pass
+
+
+if __name__ == '__main__':
+    unittest.main()