Beispiel #1
0
def ajax_graph(request):
    INSTANCE = get_instance(request)
    #instance = request.GET.get('instance', '')
    #if instance not in INSTANCES:
    #    raise Http404
    
    #instance = INSTANCES[settings.INSTANCE]
    
    #tag = request.GET.get('tag', '')
    #if not tag:
    print INSTANCE
    network = request.GET.get('network')
    if network == 'authors':
        return ajax_graph_authors(request)
    elif network == 'properties':
        return ajax_graph_properties(request)
    
    
    
    border_w = float(request.GET.get('borderW'))
    border_n = float(request.GET.get('borderN'))
    border_e = float(request.GET.get('borderE'))
    border_s = float(request.GET.get('borderS'))
    author_id = request.GET.get('author', '')
    layout_id = request.GET.get('layout')
    #weight_id = request.GET.get('weight')
    feature = request.GET.get('feature')
    heatmap = request.GET.get('heatmap')
    
    #if '_' in feature:
    #    raise Http404
    #accumulated = int(request.GET.get('accumulated'))
    #print "acc: '%s'" % accumulated
    #accumulated = 1 if accumulated and accumulated != "off" else 0
    """if tag:
        tag = instance.HASHTAGS.get(tag)
        if tag is None:
            raise Http404
        tags_levels = instance.get_environment(tag, True)
        tags = []
        for level in tags_levels[:-1]:
            tags.extend(level)
        network = instance.NETWORKS[tag.tag]
        clusters, positions = network
        edges = []
        for tag in tags:
            edges.append([tag.tag, sorted(((tag.tag, count) for tag, count in tag.co_tags.iteritems()),
                key=lambda (t, c): c, reverse=True)])   
            tag.x, tag.y = positions[tag.tag]
        tags = [[tag.x, tag.y, tag.tag, tag.count, clusters[tag.tag]] for tag in tags]
        sub_tags = []
    else:"""
    
    if layout_id not in [key for name, key, dot_prog in settings.LAYOUTS]:
        raise Http404
    if author_id == '':
        #author_id = None
        author = None
        if feature not in dict(data.FEATURES[INSTANCE]):
            raise Http404
    else:
        try:
            author = Author.from_slug(author_id, INSTANCE)
        except Author.DoesNotExist:
            raise Http404
        #author_id = author.instance_name
        if feature not in dict(data.AUTHOR_FEATURES[INSTANCE]):
            raise Http404
        #print author_id
    """if author_id not in WEIGHTS:
        raise Http404
    if layout_id not in GRAPH_POSITIONS_TREE:
        raise Http404
    if author_id is None:
        tree = GRAPH_POSITIONS_TREE[layout_id]
    else:
        tree = PickledData.objects.get(settings.INSTANCE,
            'graph_positions_tree_%s_%s' % (layout_id, author_id))
    tree_id = (weight_id, accumulated)
    if tree_id not in tree:
        raise Http404
    tree = tree[tree_id]"""
    #print WEIGHTS[weight_id][accumulated].items()[:100]
    #print WEIGHTS[weight_id][accumulated]['http://who.int/icd#ICDCategory']
    
    #print "HI"
            
    STEP = 10
    categories = []
    sql = []
    sql_params = []
    for x in range(STEP):
        for y in range(STEP):
            """print (
                border_w + x * (border_e - border_w) / STEP,
                border_w + (x + 1) * (border_e - border_w) / STEP,
                border_n + y * (border_s - border_n) / STEP,
                border_n + (y + 1) * (border_s - border_n) / STEP
            )"""
            """categories_sub = tree.get(
                border_w + x * (border_e - border_w) / STEP,
                border_w + (x + 1) * (border_e - border_w) / STEP,
                border_n + y * (border_s - border_n) / STEP,
                border_n + (y + 1) * (border_s - border_n) / STEP
            )[-1:]"""
            x_range = (
                border_w + x * (border_e - border_w) / STEP,
                border_w + (x + 1) * (border_e - border_w) / STEP
            )
            y_range = (
                border_n + y * (border_s - border_n) / STEP,
                border_n + (y + 1) * (border_s - border_n) / STEP
            )
            if author is None:
                if feature in data.ACCUMULATED_FILTER[INSTANCE]:
                    table = 'icd_accumulatedcategorymetrics'
                elif feature in data.MULTILANGUAGE_FILTER[INSTANCE]:
                    table = 'icd_multilanguagecategorymetrics'
                elif feature in data.TIMESPAN_FILTER[INSTANCE]:
                    table = 'icd_timespancategorymetrics'
                else:
                    table = 'icd_categorymetrics'
                author_sql = ''
                author_params = []
            else:
                table = 'icd_authorcategorymetrics'
                author_sql = ' AND author_id = %s '
                author_params = [author.pk]
            
            if feature in data.MULTILANGUAGE_FILTER[INSTANCE]:
                sql.append("""SELECT category_id FROM %s USE INDEX (index_pos_%s_%s)
                    WHERE instance=%%s AND (x_%s BETWEEN %%s AND %%s) AND (y_%s BETWEEN %%s AND %%s)
                    AND %s >= 0
                    %s
                    ORDER BY %s DESC
                    LIMIT 1
                """ % (table, layout_id, feature, layout_id, layout_id, feature, author_sql, feature))
                sql_params += [INSTANCE, x_range[0], x_range[1], y_range[0], y_range[1]] + author_params
            else:
                sql.append("""SELECT category_id FROM %s USE INDEX (index_pos_%s_%s)
                    WHERE instance=%%s AND (x_%s BETWEEN %%s AND %%s) AND (y_%s BETWEEN %%s AND %%s)
                    AND %s > 0
                    %s
                    ORDER BY %s DESC
                    LIMIT 1
                """ % (table, layout_id, feature, layout_id, layout_id, feature, author_sql, feature))
                sql_params += [INSTANCE, x_range[0], x_range[1], y_range[0], y_range[1]] + author_params

            filter = {
                'instance': INSTANCE,
                'x_' + str(layout_id) + '__range': x_range,
                'y_' + str(layout_id) + '__range': y_range,
            }
            
            """if author is None:
                categories_sub = CategoryMetrics.objects.filter(**filter)
            else:
                filter['author'] = author
                categories_sub = AuthorCategoryMetrics.objects.filter(**filter)
            #print filter
            categories_sub = categories_sub.order_by('-' + feature).select_related('category')[:1].values_list('category', flat=True)
            categories_sub = [name[len(settings.INSTANCE):] for name in categories_sub]
            #categories += [(category.category, category.get_pos(layout_id), getattr(category, feature)) for category in categories_sub]
            categories += categories_sub"""
    #USING INDEX pos_twopi_index1
    from django.db import connection #, transaction
    
    cursor = connection.cursor()

    sql = ' UNION '.join('(%s)' % statement for statement in sql)
    #print sql
    cursor.execute(sql, sql_params)
    rows = cursor.fetchall()
    #print rows
    categories = [row[0][len(INSTANCE):] for row in rows]
    #print "categories..."
    #print categories
    if author is None:
        if feature in data.ACCUMULATED_FILTER[INSTANCE]:
            min_max = AccumulatedCategoryMetrics.objects
        elif feature in data.MULTILANGUAGE_FILTER[INSTANCE]:
            min_max = MultilanguageCategoryMetrics.objects
        elif feature in data.TIMESPAN_FILTER[INSTANCE]:
            min_max = TimespanCategoryMetrics.objects
        else:
            min_max = CategoryMetrics.objects
    else:
        min_max = AuthorCategoryMetrics.objects.filter(author=author)
    min_max = min_max.filter(instance=INSTANCE).aggregate(min=Min(feature), max=Max(feature))
    min_value = min_max['min']
    max_value = min_max['max']
    
    #print (border_w, border_e, border_n, border_s)
    #categories = GRAPH_POSITIONS_TREE['twopi'].get(border_w, border_e, border_n, border_s)
    #for count, name, x, y in categories:
        #if name == 'http://who.int/icd#ICDCategory':
        #print (count, name, x, y)
    #names = set(name for count, name, x, y in categories)
    #names = set(category.name for category, pos, weight in categories)
    names = set(categories)
    #print names

    add_names = names.copy()
    while add_names:
        new_add_names = set()
        for name in add_names:
            new_add_names.update(succ for succ in data_categories.GRAPH[INSTANCE].successors(name.encode('utf-8')) if succ not in names)
        names.update(new_add_names)
        add_names = new_add_names
    #print "NEW"
    #print names
    #print names
    #categories = [CATEGORIES[name] for count, name, x, y in categories]
    categories = [data_categories.CATEGORIES[INSTANCE][name] for name in names]
    
    #categories = categories[-100:]
    #categories = CATEGORIES.values()
    #print categories
    categories_list = []
    edges = []
    #for count, category, x, y in categories:
    #    category = CATEGORIES[category]
    #for category, pos, weight in categories:
    for category in categories:
        #x, y = GRAPH_POSITIONS[layout_id][category.name]
        #x, y = category.get_pos(layout_id)
        x, y = category.get_pos(layout_id)
        if author is None:
            if feature in data.ACCUMULATED_FILTER[INSTANCE]:
                metrics = category.accumulated_metrics
            elif feature in data.MULTILANGUAGE_FILTER[INSTANCE]:
                metrics = category.multilanguage_metrics
            elif feature in data.TIMESPAN_FILTER[INSTANCE]:
                metrics = category.timespan_metrics
            else:
                metrics = category.metrics
        else:
            metrics = category.author_metrics.get(author=author)
        weight = getattr(metrics, feature)
        #weight = GRAPH.node[category.name]['weight']
        #weight = WEIGHTS[author_id][weight_id][accumulated].get(category.name, 0)
        #category_edges = [[child.name, 1] for child in category.children.all()]
        neighbors = data_categories.GRAPH[INSTANCE].successors(category.name.encode('utf-8')) #+ GRAPH.predecessors(category.name)
        category_edges = [name for name in neighbors if name in names]
        
        #print "Heatmap: %s" % heatmap
        # if heatmap is active, don't get status colors!
        if heatmap == "true":
            categories_list.append([x, y, category.name, weight, unicode(category),
                category.get_short_display(), category.get_absolute_url(), category.get_heatmap_status(category.timespan_metrics.days_after_last_change)])
        # If feature is from multilanguage, get multi language color codes
        elif feature in data.MULTILANGUAGE_FILTER[INSTANCE]:
            #print "Multilanguage Feature"
            categories_list.append([x, y, category.name, weight, unicode(category),
                category.get_short_display(), category.get_absolute_url(), category.get_multilingual_status(feature)])
        # Otherwise get status colors
        else:
            #print "Else Feature"
            categories_list.append([x, y, category.name.encode('utf-8'), weight, unicode(category), category.get_short_display(), category.get_absolute_url(), category.get_display_status()])

        edges.append([category.name, category_edges])
    """tags = [tag for tag in tags[-100:] if tag[1] in instance.HASHTAGS]
    edges = []
    for count, item, x, y in tags:
        tag_edges = instance.CO_HASHTAGS_10[item]
        edges.append([item, tag_edges])"""
    
    #categories = [[x, y, item, count[1], -1] for count, item, x, y in categories]
    #sub_tags = []
    return json_response({
        'nodes': categories_list,
        'edges': edges,
        'minValue': min_value,
        'maxValue': max_value
        #'subTags': sub_tags,
    })