(request)
return HttpResponse('{}', content_type='application/json')
Utility ajax functions for polling couch and celerymon
Utility ajax functions for polling couch and celerymon
Utility ajax functions for polling couch and celerymon
def system_ajax(request):
    """
    Utility ajax functions for polling couch and celerymon
    """
    type = request.GET.get('api', None)
    task_limit = getattr(settings, 'CELERYMON_TASK_LIMIT', 12)
    celery_monitoring = getattr(settings, 'CELERY_FLOWER_URL', None)
    if type == "_active_tasks":
        server = Server(settings.COUCH_DATABASE)
        try:
            tasks = [x for x in server.active_tasks() if x['type'] == "indexer"]
        except HTTPError as e:
            if e.response.status_code == 403:
                return JsonResponse({'error': "Unable to access CouchDB Tasks (unauthorized)."}, status_code=500)
            else:
                return JsonResponse({'error': "Unable to access CouchDB Tasks."}, status_code=500)

        if not is_bigcouch():
            return JsonResponse(tasks, safe=False)
        else:
            # group tasks by design doc
            task_map = defaultdict(dict)
            for task in tasks:
                meta = task_map[task['design_document']]
                tasks = meta.get('tasks', [])
                tasks.append(task)
                meta['tasks'] = tasks

            design_docs = []
            for dd, meta in task_map.items():
                meta['design_document'] = dd[len('_design/'):]
                total_changes = sum(task['total_changes'] for task in meta['tasks'])
                for task in meta['tasks']:
                    task['progress_contribution'] = task['changes_done'] * 100 // total_changes

                design_docs.append(meta)
            return JsonResponse(design_docs, safe=False)
    elif type == "_stats":
        return JsonResponse({})
    elif type == "_logs":
        pass
    elif type == 'pillowtop':
        pillow_meta = get_all_pillows_json()
        return JsonResponse(sorted(pillow_meta, key=lambda m: m['name'].lower()), safe=False)

    if celery_monitoring:
        if type == "flower_poll":
            ret = []
            try:
                all_tasks = requests.get(
                    celery_monitoring + '/api/tasks',
                    params={'limit': task_limit},
                    timeout=3,
                ).json()
            except Exception as ex:
                return JsonResponse({'error': "Error with getting from celery_flower: %s" % ex}, status_code=500)

            for task_id, traw in all_tasks.items():
                # it's an array of arrays - looping through [<id>, {task_info_dict}]
                if 'name' in traw and traw['name']:
                    traw['name'] = '.'.join(traw['name'].split('.')[-2:])
                else:
                    traw['name'] = None
                ret.append(traw)
            ret = sorted(ret, key=lambda x: x['succeeded'], reverse=True)
            return HttpResponse(json.dumps(ret), content_type='application/json')
    return HttpResponse('{}', content_type='application/json')
def system_ajax ( request ) : type = request . GET . get ( 'api' , None ) task_limit = getattr ( settings , 'CELERYMON_TASK_LIMIT' , 12 ) celery_monitoring = getattr ( settings , 'CELERY_FLOWER_URL' , None ) if type == "_active_tasks" : server = Server ( settings . COUCH_DATABASE ) try : tasks = [ x for x in server . active_tasks ( ) if x [ 'type' ] == "indexer" ] except HTTPError as e : if e . response . status_code == 403 : return JsonResponse ( { 'error' : "Unable to access CouchDB Tasks (unauthorized)." } , status_code = 500 ) else : return JsonResponse ( { 'error' : "Unable to access CouchDB Tasks." } , status_code = 500 ) if not is_bigcouch ( ) : return JsonResponse ( tasks , safe = False ) else : # group tasks by design doc task_map = defaultdict ( dict ) for task in tasks : meta = task_map [ task [ 'design_document' ] ] tasks = meta . get ( 'tasks' , [ ] ) tasks . append ( task ) meta [ 'tasks' ] = tasks design_docs = [ ] for dd , meta in task_map . items ( ) : meta [ 'design_document' ] = dd [ len ( '_design/' ) : ] total_changes = sum ( task [ 'total_changes' ] for task in meta [ 'tasks' ] ) for task in meta [ 'tasks' ] : task [ 'progress_contribution' ] = task [ 'changes_done' ] * 100 // total_changes design_docs . append ( meta ) return JsonResponse ( design_docs , safe = False ) elif type == "_stats" : return JsonResponse ( { } ) elif type == "_logs" : pass elif type == 'pillowtop' : pillow_meta = get_all_pillows_json ( ) return JsonResponse ( sorted ( pillow_meta , key = lambda m : m [ 'name' ] . lower ( ) ) , safe = False ) if celery_monitoring : if type == "flower_poll" : ret = [ ] try : all_tasks = requests . get ( celery_monitoring + '/api/tasks' , params = { 'limit' : task_limit } , timeout = 3 , ) . json ( ) except Exception as ex : return JsonResponse ( { 'error' : "Error with getting from celery_flower: %s" % ex } , status_code = 500 ) for task_id , traw in all_tasks . items ( ) : # it's an array of arrays - looping through [<id>, {task_info_dict}] if 'name' in traw and traw [ 'name' ] : traw [ 'name' ] = '.' . join ( traw [ 'name' ] . split ( '.' ) [ - 2 : ] ) else : traw [ 'name' ] = None ret . append ( traw ) ret = sorted ( ret , key = lambda x : x [ 'succeeded' ] , reverse = True ) return HttpResponse ( json . dumps ( ret ) , content_type = 'application/json' ) return HttpResponse ( '{}' , content_type = 'application/json' )