[ooni-probe/master] Implement sorting of measurements in measurements tab from newest to … (#665)

art at torproject.org art at torproject.org
Fri Jan 13 12:39:57 UTC 2017


commit a9d86e09f4a02317e1ba2b6b7380620bd381d7bc
Author: Arturo Filastò <arturo at filasto.net>
Date:   Tue Nov 29 12:10:26 2016 +0000

    Implement sorting of measurements in measurements tab from newest to … (#665)
    
    * Implement sorting of measurements in measurements tab from newest to oldest
    
    * Fail aloud when argument is unknown
---
 ooni/measurements.py  | 15 +++++++++++++--
 ooni/ui/web/server.py |  2 +-
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/ooni/measurements.py b/ooni/measurements.py
index 46a447e..d79fffb 100644
--- a/ooni/measurements.py
+++ b/ooni/measurements.py
@@ -1,4 +1,5 @@
 import json
+import operator
 
 from twisted.internet import defer
 from twisted.internet.threads import deferToThread
@@ -129,7 +130,7 @@ def get_summary(measurement_id):
         return defer.succeed(json.load(f))
 
 
-def list_measurements(compute_size=False):
+def list_measurements(compute_size=False, order=None):
     measurements = []
     measurement_path = FilePath(config.measurements_directory)
     if not measurement_path.exists():
@@ -139,4 +140,14 @@ def list_measurements(compute_size=False):
             measurements.append(get_measurement(measurement_id, compute_size))
         except:
             log.err("Failed to get metadata for measurement {0}".format(measurement_id))
-    return measurements
+
+    if order is None:
+        return measurements
+
+    if order.lower() in ['asc', 'desc']:
+        reverse = {'asc': True, 'desc': False}[order.lower()]
+        measurements.sort(key=operator.itemgetter('test_start_time'),
+                          reverse=reverse)
+        return measurements
+    else:
+        raise ValueError("order must be either 'asc' 'desc' or None")
diff --git a/ooni/ui/web/server.py b/ooni/ui/web/server.py
index fa35af0..bb4e45f 100644
--- a/ooni/ui/web/server.py
+++ b/ooni/ui/web/server.py
@@ -521,7 +521,7 @@ class WebUIAPI(object):
     @xsrf_protect(check=False)
     @requires_true(attrs=['_is_initialized'])
     def api_measurement_list(self, request):
-        measurements = list_measurements()
+        measurements = list_measurements(order='desc')
         return self.render_json({"measurements": measurements}, request)
 
     @app.route('/api/measurement/<string:measurement_id>', methods=["GET"])





More information about the tor-commits mailing list