Project

General

Profile

1 38675 sandro.lab
import urllib2
2
from flask import json
3
from DLIObject import DLIObject
4
import pysolr
5
6
7
class QueryResolver(object):
8
    def __init__(self, solr_url):
9
        self.solr_url = solr_url
10
        self.solr_server = pysolr.Solr(self.solr_url)
11
        self.facet_renaming = {'entitytype': "Typology", 'provenance': "Content Provider",
12
                               "Content Provider": 'provenance', "Typology": 'entitytype'}
13
14
    def get_facet(self, facet, query=None):
15
        data = []
16
17
        f = 'entitytype'
18
        if query is not None:
19
            data.append({'facet': self.facet_renaming[f], 'query_name':f, 'values': [
20
                {'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
21
        else:
22
            data.append({'facet': self.facet_renaming[f], 'query_name':f,
23
                         'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in
24
                                    range(0, len(facet[f]), 2)]})
25
        f = 'provenance'
26
        data.append({'facet': self.facet_renaming[f], 'query_name':f,
27
                     'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
28
29
        return data
30
31
32
    """
33
    def get_pointing_to(self, query, pointing_type):
34
35
        result = self.solr_server.search(q=query, rows=10, **{
36
            'facet': 'true',
37
            'facet.field': ['relatedobjtype'],
38
            'fq': 'entitytype:"%s"' % pointing_type
39
        })
40
        fields = result.facets['facet_fields']
41
        return [{'key': fields['relatedobjtype'][i], 'value': fields['relatedobjtype'][i + 1]} for i in
42
                range(0, len(fields['relatedobjtype']), 2)]
43
    """
44
45
    def get_item(self, persistent_identifier):
46
        query = 'objidentifier:"%s"' % persistent_identifier
47
        response = self.solr_server.search(q=query)
48
        query_response = []
49
        for hit in response:
50
            result = hit['__result'][0]
51
            query_response.append(DLIObject(result.encode('utf8')))
52
53
        return {'objects': query_response}
54
55
    def get_stats_detail(self):
56
57
        data =urllib2.urlopen("http://node0.d.dli.research-infrastructures.eu:8080/dli/mvc/ui/stats/getLastStats")
58
        return json.loads(data.read())
59
60
        """
61
62
        response =self.solr_server.search(q='*:*', rows=0, **{
63
               'facet':'true',
64
               'facet.field':['provenance', 'relationprovenance']
65
            })
66
        res = response.facets['facet_fields']
67
        data= res['provenance']
68
        repo_name = [data[i] for i in range(len(data)) if (i % 2) == 0]
69
        stat_dict = {}
70
        total_item = [(data[i], data[i+1]) for i in range(len(data)) if (i % 2) == 0]
71
        for repo in  repo_name:
72
            stat_dict[repo] ={'totalObject': 0, 'totalPublication': 0, 'totalDatasets': 0, 'totalRelations': 0}
73
        for item in total_item:
74
            stat_dict[item[0]]['totalObject'] =item[1]
75
        data= res['relationprovenance']
76
        total_rels = [(data[i], data[i+1]) for i in range(len(data)) if (i % 2) == 0]
77
        for item in total_rels:
78
            if item[0] not in stat_dict:
79
                 stat_dict[item[0]] ={'totalObject': 0, 'totalPublication': 0, 'totalDatasets': 0, 'totalRelations': 0}
80
            stat_dict[item[0]]['totalRelations'] =item[1]
81
        response =self.solr_server.search(q='*:*', rows=0, **{
82
           'facet':'true',
83
           'facet.field':['provenance'],
84
            'fq': ['entitytype:"publication"']
85
        })
86
        res = response.facets['facet_fields']
87
        data= res['provenance']
88
        total_pubs = [(data[i], data[i+1]) for i in range(len(data)) if (i % 2) == 0]
89
        for item in total_pubs:
90
            if item[0] not in stat_dict:
91
                 stat_dict[item[0]] = {'totalObject': 0, 'totalPublication': 0, 'totalDatasets': 0, 'totalRelations': 0}
92
            stat_dict[item[0]]['totalPublication'] =item[1]
93
94
        response =self.solr_server.search(q='*:*', rows=0, **{
95
               'facet':'true',
96
               'facet.field':['provenance'],
97
                'fq': ['entitytype:"dataset"'] })
98
        res = response.facets['facet_fields']
99
        data= res['provenance']
100
        total_datasets = [(data[i], data[i+1]) for i in range(len(data)) if (i % 2) == 0]
101
        for item in total_datasets:
102
            if item[0] not in stat_dict:
103
                 stat_dict[item[0]] ={}
104
            stat_dict[item[0]]['totalDatasets'] =item[1]
105
106
        stats =[]
107
108
        for item in stat_dict:
109
            data  = stat_dict[item]
110
            data['name'] = item
111
            stats.append(data)
112
113
        return stats
114
        """
115
116
117
    def get_stats(self):
118
119
        data =urllib2.urlopen("http://node0.d.dli.research-infrastructures.eu:8080/dli/mvc/ui/stats/getAllStats")
120
        return json.loads(data.read())
121
122
123
124
125
    def query(self, q, browse_fields, start=None, page_size=10, only_result=False, filter_query=None):
126
        params = {}
127
        if browse_fields is not None:
128
            params['facet'] = 'true',
129
            params['facet.field'] = browse_fields
130
            params['facet.mincount'] = 1
131
132
        if filter_query is not None:
133
            params['fq'] = filter_query
134
        if start is not None:
135
            params['start'] = start
136
        response = self.solr_server.search(q, rows=page_size, **params)
137
        query_response = []
138
        for hit in response:
139
            result = hit['__result'][0]
140
            query_response.append(DLIObject(result.encode('utf8')))
141
        if not only_result:
142
            facet = self.get_facet(response.facets['facet_fields'], q)
143
        else:
144
            facet = self.get_facet(response.facets['facet_fields'])
145
        return {'objects': query_response, 'facet': facet, 'total': response.hits}