solreyes / solreyes.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import web, solr, os, json, urllib2, operator, re, unicodedata
from mako.template import Template
from mako.runtime import Context
from mako.lookup import TemplateLookup
from StringIO import StringIO
from copy import deepcopy
from pkg_resources import resource_stream
from datetime import datetime, timedelta
import logging

logging.basicConfig(level=logging.DEBUG)

class SolrEyesController(object):
    def GET(self):
        # get the args (if available) out of the request
        a = web.input().get("a")
        args = None
        if a is not None:
            a = urllib2.unquote(a)
            args = json.loads(a)
        
        # set up the configuration and the UI properties
        config = Configuration()
        properties = {}
        properties['config'] = config
        
        # if the args are none, then get the default ones from the config
        # FIXME: this is going to generate large URLs, so we probably want
        # a better approach in the long run
        
        initial_request = False
        if args is None:
            args = config.get_default_args()
            initial_request = True
        
        # set the UrlManager for the UI to use
        properties['url_manager'] = UrlManager(config, args)
        
        # create a solr connection and get the results back
        s = Solr(config)
        if initial_request:
            properties['results'] = ResultManager(s.initial(args), config, args)
        else:
            properties['results'] = ResultManager(s.search(args), config, args)
        return self.render(config.template, properties)
    
    def render(self, template_name, properties):
        res_path = os.path.join(os.path.dirname(__file__), 'templates')
        fn = os.path.join(res_path, template_name)
        mylookup = TemplateLookup(directories=[res_path])
        t = Template(filename=fn, lookup=mylookup)
        buf = StringIO()
        ctx = Context(buf, c=properties)
        t.render_context(ctx)
        return buf.getvalue()

class ResultManager(object):
    def __init__(self, results, config, args):
        self.results = results
        self.config = config
        self.args = args if args is not None else self.config.get_default_args()

    def current_sort_order(self):
        if not self.args.has_key('sort'):
            return []
        return self.args['sort']
    
    def current_sort_fields(self):
        if not self.args.has_key('sort'):
            return []
        return [k for k, v in self.args['sort']]

    def get_ordered_facets(self, facet):
        if facet in self.config.facet_fields:
            return self._sort_facets_by_count(self.results.facet_counts['facet_fields'][facet])
        elif facet in self.config.facet_ranges.keys():
            dict = self._get_range_dict(facet)
            return self._sort_facets_by_range(dict)
        elif facet in self.config.facet_dates.keys():
            dict = self._get_date_dict(facet)
            return self._sort_facets_by_range(dict)
    
    def in_args(self, facet, value=None):
        if value is not None:
            return self.args['q'].has_key(facet) and value in self.args['q'][facet]
        else:
            return self.args['q'].has_key(facet)
            
    def get_selected_range_start(self, facet):
        return self.args["q"][facet][0]
    
    def get_selected_range_end(self, facet):
        return self.args["q"][facet][1]
    
    def has_values(self, facet):
        if facet in self.config.facet_fields:
            return len(self.results.facet_counts['facet_fields'][facet]) > 0
        elif facet in self.config.facet_ranges.keys():
            return len(self.results.facet_counts['facet_ranges'][facet]["counts"]) > 0
        elif facet in self.config.facet_dates.keys():
            return len(self.results.facet_counts['facet_dates'][facet]) > 0
        return False
    
    def is_start(self):
        return self.results.start == 0
        
    def is_end(self):
        return self.results.start + self.args['rows'] >= self.results.numFound
    
    def start(self):
        return self.results.start
        
    def finish(self):
        return self.results.start + self.set_size()
        
    def start_offset(self, off):
        return self.results.start + off
        
    def set_size(self):
        return len(self.results.results)
        
    def numFound(self):
        return self.results.numFound
        
    def set(self):
        return self.results.results
    
    def page_size(self):
        return self.args['rows']
    
    def get_str(self, result, field):
        if field in self.config.dynamic_fields.keys():
            return self.config.get_dynamic_value(self.args, result, field)
        
        if result.get(field) is None and field not in self.config.dynamic_fields.keys():
            return ""
        
        if hasattr(result.get(field), "append"):
            return ", ".join([self.asciify(self.config.get_field_display(field, val)) for val in result.get(field)])
        else:
            return self.asciify(self.config.get_field_display(field, result.get(field)))
    
    def asciify(self, string):
        return unicodedata.normalize('NFKD', unicode(string)).encode('ascii','ignore')
    
    def first_page_end(self):
        return self.args['rows']
    
    def last_page_start(self):
        return self.results.numFound - (self.results.numFound % self.args['rows'])
        
    def get_previous(self, num):
        pairs = []
        for i in range(num + 1, 0, -1):
            first = self.results.start - (self.args['rows'] * i)
            if first >= self.args['rows']: # i.e. greater than the first page
                pairs.append((first, first + self.args['rows']))
        return sorted(pairs, key=operator.itemgetter(0))
        
    def get_next(self, num):
        pairs = []
        for i in range(1, num + 1):
            first = self.results.start + (self.args['rows'] * i)
            last_page_size = self.results.numFound % self.args['rows']
            if first + self.args['rows'] <= self.results.numFound - last_page_size: # i.e. less than the last page
                pairs.append((first, first + self.args['rows']))
        return sorted(pairs, key=operator.itemgetter(0))
        
    def _get_range_dict(self, facet):
        dict = self.results.facet_counts['facet_ranges'][facet]["counts"]
        keys = [int(key) for key in dict.keys()]
        keys.sort()
        rdict = {}
        for i in range(len(keys)):
            lower = int(keys[i])
            upper = -1
            if i < len(keys) - 1:
                upper = int(keys[i+1] - 1)
            r = (lower, upper)
            rdict[r] = dict[str(lower)]
        return rdict
        
    def _get_date_dict(self, facet):
        dict = self.results.facet_counts['facet_dates'][facet]
        keys = [(datetime.strptime(d, "%Y-%m-%dT%H:%M:%SZ"), d) for d in dict.keys() if d[0:2].isdigit()]
        keys = sorted(keys, key=operator.itemgetter(0))
        rdict = {}
        for i in range(len(keys)):
            lower, sl = keys[i]
            upper = -1
            if i < len(keys) - 1:
                upper, su = keys[i+1]
                upper = upper - timedelta(seconds=1)
            r = (lower, upper)
            rdict[r] = dict[sl]
        return rdict
    
    def _sort_facets_by_count(self, dict):
        return sorted(dict.iteritems(), key=operator.itemgetter(1), reverse=True)
        
    def _sort_facets_by_range(self, dict):
        # dict = {(a, b) : c, ....} ; we want to sort by a
        # first, cast the dict to tuples
        tups = [(a, b, dict[(a, b)]) for a, b in dict.keys()]
        return sorted(tups, key=operator.itemgetter(0))
        
class UrlManager(object):
    def __init__(self, config, args):
        self.config = config
        self.args = args if args is not None else self.config.get_default_args()
    
    def get_add_url(self, field, value, upper=None):
        myargs = deepcopy(self.args)
        if myargs["q"].has_key(field):
            if upper is None and value not in myargs["q"][field]:
                myargs["q"][field].append(value)
            elif upper is not None:
                myargs["q"][field] = [value, upper]
        else:
            if upper is None:
                myargs["q"][field] = [value]
            else:
                myargs["q"][field] = [value, upper]
        if myargs.has_key('start'):
            del myargs['start']
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)
        
    def get_add_date_url(self, field, value, upper=None):
        myargs = deepcopy(self.args)
        value = '{0.year}-{0.month:{1}}-{0.day:{1}}T{0.hour:{1}}:{0.minute:{1}}:{0.second:{1}}Z'.format(value, '02')
        if upper is not None and upper != -1:
            upper = '{0.year}-{0.month:{1}}-{0.day:{1}}T{0.hour:{1}}:{0.minute:{1}}:{0.second:{1}}Z'.format(upper, '02')
        if myargs["q"].has_key(field):
            if upper is None and value not in myargs["q"][field]:
                myargs["q"][field].append(value)
            elif upper is not None:
                myargs["q"][field] = [value, upper]
        else:
            if upper is None:
                myargs["q"][field] = [value]
            else:
                myargs["q"][field] = [value, upper]
        if myargs.has_key('start'):
            del myargs['start']
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)

    def get_delete_url(self, field, value=None):
        myargs = deepcopy(self.args)
        if value is not None:
            myargs['q'][field].remove(value)
            if len(myargs['q'][field]) == 0:
                del myargs['q'][field]
        else:
            del myargs['q'][field]
        if myargs.has_key('start'):
            del myargs['start']
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)
        
    def get_position_url(self, position):
        myargs = deepcopy(self.args)
        myargs["start"] = position
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)
        
    def get_rpp_url(self, rpp):
        myargs = deepcopy(self.args)
        myargs["rows"] = int(rpp)
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)
        
    def get_sort_url(self, field, direction):
        myargs = deepcopy(self.args)
        if myargs.has_key("sort"):
            isnew = True
            for i in range(len(myargs['sort'])):
                f, d = myargs['sort'][i]
                if f == field:
                    myargs['sort'][i] = [field, direction]
                    isnew = False
                    break
            if isnew:
                myargs['sort'].append([field, direction])
        else:
            myargs["sort"] = [[field, direction]]
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)
        
    def get_unsort_url(self, field):
        myargs = deepcopy(self.args)
        for i in range(len(myargs['sort'])):
            f, direction = myargs['sort'][i]
            if f == field:
                del myargs['sort'][i]
                break
        j = json.dumps(myargs)
        return self.config.base_url + "?a=" + urllib2.quote(j)
        
class Solr(object):
    def __init__(self, config):
        self.config = config
        self.solr = solr.Solr(self.config.solr_url)
        self.select = solr.SearchHandler(self.solr, self.config.request_handler, arg_separator="__")

    def search(self, args):
        return self._do_query(args)
        
    def initial(self, args):
        args = deepcopy(args)
        args["q"]["*"] = "*"
        args["rows"] = 0
        return self._do_query(args)
        
    def _do_query(self, args, arg_separator="__"):
        # we build all our solr args in one dictionary for convenience
        solr_args = {}
        solr_args['facet'] = "on"
        
        # Except the fields required explicitly by the solrpy api:
        
        #def __call__(self, q=None, fields=None, highlight=None,
        #         score=True, sort=None, sort_order="asc", **params):
        
        # do q separately
        qs = []
        for field in args['q'].keys():
            if field in self.config.facet_fields:
                for value in args['q'][field]:
                    qs.append(field + ":\"" + value + "\"")
            elif field in self.config.facet_ranges.keys():
                limit = args['q'][field][1]
                if limit == -1:
                    limit = self.config.facet_ranges[field]["infinity"]
                qs.append(field + ":[" + str(args["q"][field][0]) + " TO " + str(limit) + "]")
            elif field in self.config.facet_dates.keys():
                limit = args['q'][field][1]
                if limit == -1:
                    now = datetime.today().strftime("%Y-%m-%dT%H:%M:%SZ")
                    limit = self.config.facet_dates[field].get("inifinity", now)
                qs.append(field + ":[" + str(args["q"][field][0]) + " TO " + str(limit) + "]")
            elif field == "*":
                for value in args['q'][field]:
                    qs.append(field + ":" + value)
        q = " AND ".join(qs)
        
        # start position (for paging)
        if args.has_key("start"):
            solr_args["start"] = args["start"]
        
        # set up the ranged search parameters
        for f in args["facet_range"].keys():
            if solr_args.has_key("facet.range"):
                solr_args["facet.range"].append(f)
            else:
                solr_args["facet.range"] = [f]
            
            if args["facet_range"][f].has_key("mincount"):
                solr_args["f." + f + ".facet.mincount"] = args["facet_range"][f]["mincount"]
            
            if args["facet_range"][f].has_key("min"):
                solr_args["f." + f + ".facet.range.start"] = args["facet_range"][f]["min"]
                
            if args["facet_range"][f].has_key("max"):
                solr_args["f." + f + ".facet.range.end"] = args["facet_range"][f]["max"]
                
            if args["facet_range"][f].has_key("gap"):
                solr_args["f." + f + ".facet.range.gap"] = args["facet_range"][f]["gap"]
        
        # set up the date range parameters
        for f in args["facet_date"].keys():
            if solr_args.has_key("facet.date"):
                solr_args["facet.date"].append(f)
            else:
                solr_args["facet.date"] = [f]
            
            if args["facet_date"][f].has_key("mincount"):
                solr_args["f." + f + ".facet.mincount"] = args["facet_date"][f]["mincount"]
            
            if args["facet_date"][f].has_key("min"):
                solr_args["f." + f + ".facet.date.start"] = args["facet_date"][f]["min"]
                
            if args["facet_date"][f].has_key("max"):
                solr_args["f." + f + ".facet.date.end"] = args["facet_date"][f]["max"]
            else:
                solr_args["f." + f + ".facet.date.end"] = datetime.today().strftime("%Y-%m-%dT%H:%M:%SZ")
                
            if args["facet_date"][f].has_key("gap"):
                solr_args["f." + f + ".facet.date.gap"] = args["facet_date"][f]["gap"]
        
        # facet mincount from config
        solr_args["facet.mincount"] = self.config.facet_mincount
        
        # number of rows to return
        if args.has_key("rows"):
            solr_args["rows"] = args["rows"]
            
        # plain facet fields
        solr_args["facet.field"] = args["facet_field"]
        
        # sort options
        if args.has_key("sort"):
            sort_parts = []
            for sort_field, direction in args['sort']:
                sort_parts.append(sort_field + " " + direction)
            solr_args["sort"] = ", ".join(sort_parts)
        
        # solrpy convert the keywordargs
        solrpy_args = self.solrpyise(solr_args, arg_separator)
        
        results = self.select(q=q, **solrpy_args)
        return results
    
    def solrpyise(self, dict, arg_separator="__"):
        edict = {}
        for key in dict.keys():
            ekey = self.esc(key, arg_separator)
            edict[str(ekey)] = dict[key]
        return edict
    
    def esc(self, solr_field, arg_separator="__"):
        return solr_field.replace(".", arg_separator)

class Configuration(object):
    def __init__(self):
        # extract the configuration from the json object
        f = resource_stream(__name__, 'config.json')
        c = ""
        for line in f:
            if line.strip().startswith("#"):
                continue
            else:
                c += line
        self.cfg = json.loads(c)
        
        # build a map for display of fields
        self.facet_display_values = {}
        for field, props in self.cfg['facet_fields'].iteritems():
            self.facet_display_values[field] = props["display"]
        for field, props in self.cfg['facet_ranges'].iteritems():
            self.facet_display_values[field] = props["display"]
        for field, props in self.cfg['facet_dates'].iteritems():
            self.facet_display_values[field] = props["display"]
        
    def __getattr__(self, attr):
        return self.cfg.get(attr, None)
        
    def get_facet_display(self, facet_name):
        return self.facet_display_values.get(facet_name, facet_name)
        
    def get_default_args(self):
        fields = deepcopy(self.facet_fields.keys())
        ranges = deepcopy(self.facet_ranges)
        dates = deepcopy(self.facet_dates)
        for k, v in ranges.iteritems():
            del v['display']
        for k, v in dates.iteritems():
            del v['display']
        return {
            "q" : {},
            "start" : 0,
            "facet_field" : fields,
            "facet_range" : ranges,
            "facet_date" : dates,
            "rows" : self.default_results_per_page
        }
        
    def get_value_display(self, facet, value):
        if self.facet_value_functions.has_key(facet):
            d = self.facet_value_functions[facet]
            func_name = d.keys()[0]
            args = d[func_name]
            func = globals()[func_name]
            return func(value, args)
        else:
            return value
            
    def get_field_display(self, field, value):
        if self.display_value_functions.has_key(field):
            d = self.display_value_functions[field]
            func_name = d.keys()[0]
            args = d[func_name]
            func = globals()[func_name]
            return func(str(value), args)
        else:
            return value
            
    def get_dynamic_value(self, args, result, field):
        if self.dynamic_fields.has_key(field):
            d = self.dynamic_fields[field]
            func_name = d.keys()[0]
            fargs = d[func_name]
            func = globals()[func_name]
            return func(args, result, fargs)
        else:
            return value
            
    def get_field_name(self, field):
        return self.display_fields.get(field, field)
        
    def display_upper(self, facet, lower, upper):
        if self.upper_display_functions.has_key(facet):
            d = self.upper_display_functions[facet]
            func_name = d.keys()[0]
            args = d[func_name]
            func = globals()[func_name]
            return func(lower, upper, args)
        else:
            return True

def value_map(value, d):
    return d.get(value, value)

def regex_map(value, dict):
    capture = re.findall(dict["expression"], value)
    if len(capture) == 1:
        return capture[0]
    return value

def years_different(lower, upper, dict):
    lyear = regex_map(lower, {"expression" : "([\\d]{4})-.*"})
    uyear = regex_map(upper, {"expression" : "([\\d]{4})-.*"})
    return lyear != uyear

def date_range_count(args, result, dict):
    if not args['q'].has_key('bounding_field'):
        values = result.get(dict['results_field'])
        return str(len(values))
    start, end = args['q'][dict['bounding_field']]
    sdate = datetime.strptime(start, "%Y-%m-%dT%H:%M:%SZ")
    edate = datetime.strptime(end, "%Y-%m-%dT%H:%M:%SZ")
    values = result.get(dict['results_field'])
    count = 0
    for val in values:
        vdate = datetime.strptime(val, "%Y-%m-%dT%H:%M:%SZ")
        if vdate >= sdate and vdate <= edate:
            count += 1
    return str(count)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.