Project

General

Profile

1
import urllib2
2

    
3
import pysolr
4
from flask import json
5

    
6
from DLI_ObjectResolver import DLIObject
7

    
8

    
9
class QueryResolver(object):
10
    def __init__(self, solr_url, base_url_dnet):
11
        self.solr_url = solr_url
12
        self.solr_server = pysolr.Solr(self.solr_url)
13
        self.base_url_dnet = base_url_dnet
14
        self.facet_renaming = {'entitytype': "Typology", 'provenance': "Content Provider",
15
                               "Content Provider": 'provenance', "Typology": 'entitytype'}
16

    
17
    def get_facet(self, facet, query=None):
18
        data = []
19

    
20
        f = 'entitytype'
21
        if query is not None:
22
            data.append({'facet': self.facet_renaming[f], 'query_name': f, 'values': [
23
                {'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
24
        else:
25
            data.append({'facet': self.facet_renaming[f], 'query_name': f,
26
                         'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in
27
                                    range(0, len(facet[f]), 2)]})
28
        f = 'provenance'
29
        data.append({'facet': self.facet_renaming[f], 'query_name': f,
30
                     'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
31

    
32
        return data
33

    
34
    def get_item(self, persistent_identifier):
35
        query = 'objidentifier:"%s"' % persistent_identifier
36
        response = self.solr_server.search(q=query)
37
        query_response = []
38
        for hit in response:
39
            query_response.append(DLIObject(hit['dnetresourceidentifier'], self.base_url_dnet))
40

    
41
        return {'objects': query_response}
42

    
43
    def get_stats_detail(self):
44
        data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/stats/getLastStats")
45
        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

    
52
    def get_stats(self):
53

    
54
        data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/stats/getAllStats")
55
        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

    
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

    
76
        if len(q) > 0:
77
            w_query = '%s OR (%s AND completionstatus:"complete")^2.0' % (q, q)
78

    
79
        response = self.solr_server.search(w_query, rows=page_size, **params)
80
        query_response = []
81
        for hit in response:
82
            dnet_id = hit['dnetresourceidentifier']
83

    
84
            query_response.append(DLIObject(hit['dnetresourceidentifier'], self.base_url_dnet))
85
        if not only_result:
86
            facet = self.get_facet(response.facets['facet_fields'], q)
87
        else:
88
            facet = self.get_facet(response.facets['facet_fields'])
89
        return {'objects': query_response, 'facet': facet, 'total': response.hits}
90

    
91
    def get_ds_info(self, id, name):
92
        param = ""
93
        value = ""
94
        if (len(id) > 0):
95
            param = "id"
96
            value = id
97
        if len(name) > 0:
98
            param = "name"
99
            value = name.replace(" ", '%20')
100
        url_query = self.base_url_dnet + "/mvc/ui/dli/datasource/info.do?%s=%s" % (param, value)
101
        data = urllib2.urlopen(url_query)
102
        d = json.loads(data.read())
103
        return d
104

    
105
    def get_all_ds(self):
106
        data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/dli/datasource/datasources.do")
107
        d = json.loads(data.read())
108
        return d
(3-3/9)