コード例 #1
0
ファイル: __init__.py プロジェクト: densho/bkup-ddr-local
def set_docstore_index( request ):
    """Ensure active Elasticsearch index matches active storage; complain if not.
    
    Look at mounted storage. Make an index name based on that.
    If mounted and corresponding index exists in Elasticsearch, make sure it's
    in session.  If index is in session but storage not mounted or Elasticearch
    index doesn't exist, remove from session.
    
    storage_label: label of storage currently in session
    docstore_index_exists: Elasticsearch index exists for storage_label (or not)
    
    @param request:
    @returns: storage_label,docstore_index_exists
    """
    # gather info
    docstore_index = None
    docstore_index_exists = None
    storage_label = request.session.get('storage_label', None)
    if not storage_label:
        storage_label = target_index(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX)
    if storage_label:
        docstore_index = make_index_name(storage_label)
        if docstore_index:
            docstore_index_exists = index_exists(settings.DOCSTORE_HOSTS, docstore_index)
    # rm index from session
    if not (storage_label or docstore_index_exists):
        request.session['docstore_index'] = None
    # add index to session
    if storage_label and docstore_index_exists and not request.session.get('docstore_index',None):
        request.session['docstore_index'] = docstore_index
    return storage_label,docstore_index_exists
コード例 #2
0
def results( request ):
    """Results of a search query or a DDR ID query.
    """
    target_index = docstore.target_index(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX)
    template = 'webui/search/results.html'
    context = {
        'hide_header_search': True,
        'query': '',
        'error_message': '',
        'search_form': SearchForm(),
        'paginator': None,
        'page': None,
        'filters': None,
        'sort': None,
        'docstore_index': settings.DOCSTORE_INDEX,
        'target_index': target_index,
    }
    context['query'] = request.GET.get('query', '')
    # silently strip out bad chars
    query = context['query']
    for char in BAD_CHARS:
        query = query.replace(char, '')
        
    if query:
        context['search_form'] = SearchForm({'query': query})
        
        # prep query for elasticsearch
        model = request.GET.get('model', None)
        filters = {}
        fields = docstore.all_list_fields()
        sort = {'record_created': request.GET.get('record_created', ''),
                'record_lastmod': request.GET.get('record_lastmod', ''),}
        
        # do query and cache the results
        results = docstore.search(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX,
                                  query=query, filters=filters,
                                  model='collection,entity,file', fields=fields, sort=sort)
        if results.get('hits',None) and not results.get('status',None):
            # OK -- prep results for display
            thispage = request.GET.get('page', 1)
            #assert False
            objects = massage_query_results(results, thispage, settings.RESULTS_PER_PAGE)
            paginator = Paginator(objects, settings.RESULTS_PER_PAGE)
            page = paginator.page(thispage)
            context['paginator'] = paginator
            context['page'] = page
        else:
            # FAIL -- elasticsearch error
            context['error_message'] = 'Search query "%s" caused an error. Please try again.' % query
            return render_to_response(
                template, context, context_instance=RequestContext(request, processors=[])
            )
    
    return render_to_response(
        template, context, context_instance=RequestContext(request, processors=[])
    )
コード例 #3
0
def index( request ):
    target_index = docstore.target_index(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX)
    return render_to_response(
        'webui/search/index.html',
        {'hide_header_search': True,
         'search_form': SearchForm,
         'docstore_index': settings.DOCSTORE_INDEX,
         'target_index': target_index,},
        context_instance=RequestContext(request, processors=[])
    )
コード例 #4
0
def admin( request ):
    """Administrative stuff like re-indexing.
    """
    target_index = docstore.target_index(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX)
    server_info = []
    index_names = []
    indices = []
    es = Elasticsearch(hosts=settings.DOCSTORE_HOSTS)
    ping = es.ping()
    no_indices = True
    if ping:
        info = es.info()
        info_status = info['status']
        if info_status == 200:
            info_status_class = 'success'
        else:
            info_status_class = 'error'
        server_info.append( {'label':'status', 'data':info_status, 'class':info_status_class} )
        
        status = es.indices.status()
        shards_success = status['_shards']['successful']
        shards_failed = status['_shards']['failed']
        if shards_failed == 0:
            shards_success_class = 'success'
            shards_failed_class = 'success'
        else:
            shards_success_class = 'error'
            shards_failed_class = 'error'
        server_info.append( {'label':'shards(successful)', 'data':shards_success, 'class':shards_success_class} )
        server_info.append( {'label':'shards(failed)', 'data':shards_failed, 'class':shards_failed_class} )
        # indices
        for name in status['indices'].keys():
            no_indices = False
            server_info.append( {'label':name, 'data':'', 'class':''} )
            size = status['indices'][name]['index']['size_in_bytes']
            ONEPLACE = Decimal(10) ** -1
            size_nice = Decimal(size/1024/1024.0).quantize(ONEPLACE)
            size_formatted = '%sMB (%s bytes)' % (size_nice, size)
            num_docs = status['indices'][name]['docs']['num_docs']
            server_info.append( {'label':'size', 'data':size_formatted, 'class':'info'} )
            server_info.append( {'label':'documents', 'data':num_docs, 'class':'info'} )
            
            index_names.append(name)
            index = {'name':name, 'exists':True}
            indices.append(index)
    indexform = IndexConfirmForm(request=request)
    dropform = None
    if indices:
        dropform = DropConfirmForm(request=request)
    return render_to_response(
        'webui/search/admin.html',
        {'ping': ping,
         'no_indices': no_indices,
         'server_info': server_info,
         'indices': indices,
         'indexform': indexform,
         'dropform': dropform,
         'docstore_index': settings.DOCSTORE_INDEX,
         'target_index': target_index,},
        context_instance=RequestContext(request, processors=[])
    )