Project

General

Profile

1 38675 sandro.lab
import urllib2
2 39510 sandro.lab
3 38675 sandro.lab
from flask import json
4
import pysolr
5
6 39510 sandro.lab
from DLIObject import DLIObject
7 38675 sandro.lab
8 39510 sandro.lab
9 38675 sandro.lab
class QueryResolver(object):
10
    def __init__(self, solr_url):
11
        self.solr_url = solr_url
12
        self.solr_server = pysolr.Solr(self.solr_url)
13
        self.facet_renaming = {'entitytype': "Typology", 'provenance': "Content Provider",
14
                               "Content Provider": 'provenance', "Typology": 'entitytype'}
15
16
    def get_facet(self, facet, query=None):
17
        data = []
18
19
        f = 'entitytype'
20
        if query is not None:
21 39510 sandro.lab
            data.append({'facet': self.facet_renaming[f], 'query_name': f, 'values': [
22 38675 sandro.lab
                {'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
23
        else:
24 39510 sandro.lab
            data.append({'facet': self.facet_renaming[f], 'query_name': f,
25 38675 sandro.lab
                         'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in
26
                                    range(0, len(facet[f]), 2)]})
27
        f = 'provenance'
28 39510 sandro.lab
        data.append({'facet': self.facet_renaming[f], 'query_name': f,
29 38675 sandro.lab
                     'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
30
31
        return data
32
33
    def get_item(self, persistent_identifier):
34
        query = 'objidentifier:"%s"' % persistent_identifier
35
        response = self.solr_server.search(q=query)
36
        query_response = []
37
        for hit in response:
38
            result = hit['__result'][0]
39
            query_response.append(DLIObject(result.encode('utf8')))
40
41
        return {'objects': query_response}
42
43
    def get_stats_detail(self):
44 39521 sandro.lab
        data = urllib2.urlopen("http://node0-p-dli.d4science.org:8080/dli/mvc/ui/stats/getLastStats")
45 39510 sandro.lab
        result = []
46
        d = json.loads(data.read())
47
        for k in d:
48
            if 'Deduction' not in k['datasource']:
49
                result.append(k)
50
        return result
51 38675 sandro.lab
52
    def get_stats(self):
53
54 39521 sandro.lab
        data = urllib2.urlopen("http://node0-p-dli.d4science.org:8080/dli/mvc/ui/stats/getAllStats")
55 39510 sandro.lab
        result = {}
56
        d = json.loads(data.read())
57
        for item in d:
58
            result[item] = []
59
            for k in d[item]:
60
                if 'Deduction' not in k['datasource']:
61
                    result[item].append(k)
62
        return result
63 38675 sandro.lab
64
    def query(self, q, browse_fields, start=None, page_size=10, only_result=False, filter_query=None):
65
        params = {}
66
        if browse_fields is not None:
67
            params['facet'] = 'true',
68
            params['facet.field'] = browse_fields
69
            params['facet.mincount'] = 1
70
71
        if filter_query is not None:
72
            params['fq'] = filter_query
73
        if start is not None:
74
            params['start'] = start
75
        response = self.solr_server.search(q, rows=page_size, **params)
76
        query_response = []
77
        for hit in response:
78
            result = hit['__result'][0]
79
            query_response.append(DLIObject(result.encode('utf8')))
80
        if not only_result:
81
            facet = self.get_facet(response.facets['facet_fields'], q)
82
        else:
83
            facet = self.get_facet(response.facets['facet_fields'])
84
        return {'objects': query_response, 'facet': facet, 'total': response.hits}