server/src/app/Http/Controllers/Api/ThemeController.php
author ymh <ymh.work@gmail.com>
Fri, 16 Dec 2016 17:43:07 +0100
changeset 474 245b4df137d3
parent 465 9df336529b2f
permissions -rw-r--r--
Correct themes visualisation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Http\Controllers\Api;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
use CorpusParole\Http\Controllers\Controller;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
use Illuminate\Http\Request;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use EasyRdf\Resource;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
use EasyRdf\Literal;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    11
use Es;
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    12
use Log;
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    13
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use CorpusParole\Services\LexvoResolverInterface;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use CorpusParole\Services\BnfResolverInterface;
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    16
use CorpusParole\Libraries\Filters\CorpusFilterManager;
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
class ThemeController extends Controller
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
{
377
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    21
    public function __construct(LexvoResolverInterface $lexvoResolver, BnfResolverInterface $bnfResolver) {
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        $this->lexvoResolver = $lexvoResolver;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        $this->bnfResolver = $bnfResolver;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    /**
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
     * Display the specified resource.
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
     *
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     * @return \Illuminate\Http\Response
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
     */
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    public function index(Request $request)
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    {
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    33
        $filter = $request->input('filter', 'bnf');
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    34
        $index = $request->input('index', 0);
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    35
        $limit = $request->input('limit', config('corpusparole.theme_default_limit'));
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    36
        $sort = $request->input('sort', 'count');
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    38
        if($filter === 'bnf') {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    39
            $include = ".*\\|bnf\\|.*";
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    40
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    41
        elseif($filter === 'lexvo') {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    42
            $include = ".*\\|lxv\\|.*";
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    43
        }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    44
        elseif($filter === 'olac') {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    45
            $include = ".*\\|olac\\|.*";
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    46
        }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    47
        elseif($filter === 'txt') {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    48
            $include = ".*\\|txt\\|.*";
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    49
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    50
        elseif($filter === 'all') {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    51
            $include = false;
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    52
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    53
        else {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    54
            abort(401,"Value for filter parameter must be in 'bnf', 'lexvo', 'olac', 'txt', 'all'");
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    55
        }
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    57
        if($sort == "count" || $sort == "descending") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    58
            $order_field = "_count";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    59
            $order_dir = "desc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    60
        } elseif($sort == "-count") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    61
            $order_field = "_count";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    62
            $order_dir = "asc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    63
        } elseif ($sort == "label" || $sort == "alphabetical") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    64
            $order_field = "_term";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    65
            $order_dir = "asc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    66
        } elseif ($sort == "-label") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    67
            $order_field = "_term";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    68
            $order_dir = "desc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    69
        } else {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    70
            $order_field = "_count";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    71
            $order_dir = "desc";
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    74
        $filterManager = new CorpusFilterManager();
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    75
        $filters = $filterManager->prepareFilters($request);
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 407
diff changeset
    76
        //unset($filters['themes']);
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    77
        $qFilterParts = $filterManager->buildESFilters($filters);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    78
        $query = $filterManager->buildQuery($qFilterParts);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    79
407
2dba812c7ef2 add a way to build rpm for puppet files, correct elasticsearch provisioning, correct error on elasticsearch queries + tests
ymh <ymh.work@gmail.com>
parents: 406
diff changeset
    80
        $termSize = $limit * ($index+1);
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    81
        $esQuery = [
406
cf0f23803a53 upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents: 378
diff changeset
    82
            'index' => config('elasticsearch.index'),
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    83
            'body' => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    84
                'size' => 0,
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    85
                'query' => $query,
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    86
                'aggs' => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    87
                    "subjects" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    88
                        "nested" => [ "path" => "subject" ],
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    89
                        "aggs" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    90
                            "subjects" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    91
                                "terms" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    92
                                    "field" => "subject.label_code",
407
2dba812c7ef2 add a way to build rpm for puppet files, correct elasticsearch provisioning, correct error on elasticsearch queries + tests
ymh <ymh.work@gmail.com>
parents: 406
diff changeset
    93
                                    "size" => ($termSize==0)?2147483647:$termSize,
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    94
                                    "order" => [ $order_field => $order_dir ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    95
                                ]
474
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 465
diff changeset
    96
                            ],
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 465
diff changeset
    97
                            "subjects_count" => [
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 465
diff changeset
    98
                                "cardinality" => [
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 465
diff changeset
    99
                                    "field" => "subject.label_code"
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 465
diff changeset
   100
                                ]
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   101
                            ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   102
                        ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   103
                    ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   104
                ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   105
            ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   106
        ];
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   108
        if($include) {
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
   109
            $esQuery['body']['aggs']['subjects']['aggs']['subjects']['terms']['include'] = $include;
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   110
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   111
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
   112
        $esRes = Es::search($esQuery);
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        $themes = [];
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   116
        $bucketList = $esRes['aggregations']['subjects']['subjects']['buckets'];
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   117
        if($limit > 0) {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   118
            $bucketList = array_slice($bucketList, $index*$limit, $limit);
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   119
        }
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   121
        foreach($bucketList as $bucket) {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   122
            $parts = explode("|", $bucket['key']);
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   123
            $label = $parts[0];
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   124
            if($parts[1] === 'bnf' || $parts[1] === 'lxv') {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   125
                $url = ['bnf' => config('corpusparole.bnf_ark_base_url'), 'lxv' => config('corpusparole.lexvo_base_url')][$parts[1]].$parts[2];
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   126
            }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   127
            else {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   128
                $url = $parts[2];
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   129
            }
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   130
            $themes[$url] = [
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                "label" => $label,
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   132
                "count" => $bucket['doc_count']
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
            ];
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
474
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 465
diff changeset
   136
        return response()->json(['themes' => $themes, 'meta' => [ 'total' => $esRes['aggregations']['subjects']['subjects_count']['value']] ]);
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
}