filter geostat by area + tests
authorymh <ymh.work@gmail.com>
Tue, 04 Oct 2016 13:53:56 +0200
changeset 320 0fce13da58af
parent 319 78990a8a069b
child 321 aefaad270b9b
filter geostat by area + tests
server/src/app/Console/Commands/IndexDocuments.php
server/src/app/Exceptions/Handler.php
server/src/app/Http/Controllers/Api/GeoStatsController.php
server/src/config/corpusparole.php
server/src/tests/Controllers/GeoStatsControllerTest.php
--- a/server/src/app/Console/Commands/IndexDocuments.php	Tue Oct 04 11:58:39 2016 +0200
+++ b/server/src/app/Console/Commands/IndexDocuments.php	Tue Oct 04 13:53:56 2016 +0200
@@ -65,7 +65,8 @@
             'settings' => [
                 'number_of_shards' => config('elasticsearch.shards'),
                 'number_of_replicas' => config('elasticsearch.replicas'),
-                'index.mapping.ignore_malformed' => True
+                'index.mapping.ignore_malformed' => True,
+                'index.requests.cache.enable' => True
             ],
             'mappings' => [
                 'document' => [
--- a/server/src/app/Exceptions/Handler.php	Tue Oct 04 11:58:39 2016 +0200
+++ b/server/src/app/Exceptions/Handler.php	Tue Oct 04 13:53:56 2016 +0200
@@ -38,6 +38,12 @@
      */
     public function render($request, Exception $e)
     {
+        if ( $request->isXmlHttpRequest() || $request->wantsJson() ) {
+            return response()->json([
+                'message' => class_basename( $e ) . ' in ' . basename( $e->getFile() ) . ' line ' . $e->getLine() . ( ( $message = $e->getMessage() ) ? ': ' . $e->getMessage() : '.' ),
+                'trace' => $e->getTrace()
+            ], 500);
+        }
         return parent::render($request, $e);
     }
 }
--- a/server/src/app/Http/Controllers/Api/GeoStatsController.php	Tue Oct 04 11:58:39 2016 +0200
+++ b/server/src/app/Http/Controllers/Api/GeoStatsController.php	Tue Oct 04 13:53:56 2016 +0200
@@ -7,6 +7,7 @@
 use CorpusParole\Http\Requests;
 use CorpusParole\Http\Controllers\Controller;
 use Es;
+use Log;
 
 class GeoStatsController extends Controller
 {
@@ -17,10 +18,22 @@
      */
     public function index(Request $request)
     {
+        $area = $request->input('area');
+        $filter = [
+            'match_all' => []
+        ];
+        if(!is_null($area) && $area !== config('corpusparole.geonames_earth_geonamesid')) {
+            $filter = [
+                'term' => [
+                    "geonames_hierarchy" => $area
+                ]
+            ];
+        }
         $query = [
             'index' => env('ELASTICSEARCH_INDEX'),
             'body' => [
                 "size" => 0,
+                "query" => $filter,
                 "aggs" => [
                     "geos" => [
                         "terms" => [
--- a/server/src/config/corpusparole.php	Tue Oct 04 11:58:39 2016 +0200
+++ b/server/src/config/corpusparole.php	Tue Oct 04 13:53:56 2016 +0200
@@ -130,6 +130,7 @@
     'geonames_max_ids' => 500,
     'geonames_hierarchy_webservice_url' => 'http://api.geonames.org/hierarchyJSON',
     'geonames_username' => env('GEONAMES_USERNAME'),
+    'geonames_earth_geonamesid' => '6295630',
 
     'bo_client_environment' => [
         "modulePrefix" => "bo-client",
--- a/server/src/tests/Controllers/GeoStatsControllerTest.php	Tue Oct 04 11:58:39 2016 +0200
+++ b/server/src/tests/Controllers/GeoStatsControllerTest.php	Tue Oct 04 13:53:56 2016 +0200
@@ -8,6 +8,9 @@
             'index' => env('ELASTICSEARCH_INDEX'),
             'body' => [
                 "size" => 0,
+                "query" => [
+                    'match_all' => []
+                ],
                 "aggs" => [
                     "geos" => [
                         "terms" => [
@@ -60,4 +63,105 @@
             '3027939' => 851
         ]]);
     }
+
+
+    public function testGetIndexEarth()
+    {
+        $query = [
+            'index' => env('ELASTICSEARCH_INDEX'),
+            'body' => [
+                "size" => 0,
+                "query" => [
+                    'match_all' => []
+                ],
+                "aggs" => [
+                    "geos" => [
+                        "terms" => [
+                            "size" => 0,
+                            "field" => "geonames_hierarchy"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        Es::shouldReceive('search')
+                ->once()
+                ->with($query)
+                ->andReturn(json_decode("{
+  \"took\" : 17,
+  \"timed_out\" : false,
+  \"_shards\" : {
+    \"total\" : 1,
+    \"successful\" : 1,
+    \"failed\" : 0
+  },
+  \"hits\" : {
+    \"total\" : 3011,
+    \"max_score\" : 0.0,
+    \"hits\" : [ ]
+  },
+  \"aggregations\" : {
+    \"geos\" : {
+      \"doc_count_error_upper_bound\" : 0,
+      \"sum_other_doc_count\" : 0,
+      \"buckets\" : []
+    }
+  }
+}", true));
+
+        $this->get('/api/v1/stats/geostats/?area='.config('corpusparole.geonames_earth_geonamesid'))->assertTrue($this->response->isOk(), $this->response->content());
+    }
+
+
+    public function testGetIndexArea()
+    {
+        $query = [
+            'index' => env('ELASTICSEARCH_INDEX'),
+            'body' => [
+                "size" => 0,
+                "query" => [
+                    'term' => [
+                        "geonames_hierarchy" => "code_area"
+                    ]
+                ],
+                "aggs" => [
+                    "geos" => [
+                        "terms" => [
+                            "size" => 0,
+                            "field" => "geonames_hierarchy"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        Es::shouldReceive('search')
+                ->once()
+                ->with($query)
+                ->andReturn(json_decode("{
+  \"took\" : 17,
+  \"timed_out\" : false,
+  \"_shards\" : {
+    \"total\" : 1,
+    \"successful\" : 1,
+    \"failed\" : 0
+  },
+  \"hits\" : {
+    \"total\" : 3011,
+    \"max_score\" : 0.0,
+    \"hits\" : [ ]
+  },
+  \"aggregations\" : {
+    \"geos\" : {
+      \"doc_count_error_upper_bound\" : 0,
+      \"sum_other_doc_count\" : 0,
+      \"buckets\" : []
+    }
+  }
+}", true));
+
+        $this->get('/api/v1/stats/geostats/?area=code_area')->assertTrue($this->response->isOk(), $this->response->content());
+    }
+
 }