Exemplo n.º 1
0
def getIPAddress(request, addr):
    data = Lookup.instance().lookup(addr)

    if data:
        return HttpResponse(json.dumps(data))
    else:
        return Http404
Exemplo n.º 2
0
def getIPAddress(request, addr):
    data = Lookup.instance().lookup(addr)

    if data:
        return HttpResponse(json.dumps(data))
    else:
        return Http404
Exemplo n.º 3
0
    def process(cls, widget, job, data):
        """Class method to generate list of circle objects.

        Subclass should manipulate this list into specific
        JSON structures as needed.
        """

        request = get_request()
        maps_version = request.user.userprofile.maps_version
        post_process = POST_PROCESS_MAP[maps_version]

        columns = job.get_columns()

        ColInfo = namedtuple('ColInfo', ['col', 'dataindex'])

        keycol = None
        latcol = None
        longcol = None
        valuecol = None
        labelcol = None
        for i, c in enumerate(columns):
            if c.name == widget.options.key:
                keycol = ColInfo(c, i)
            elif c.name == widget.options.latitude:
                latcol = ColInfo(c, i)
            elif c.name == widget.options.longitude:
                longcol = ColInfo(c, i)
            elif c.name == widget.options.value:
                valuecol = ColInfo(c, i)
            elif c.name == widget.options.label:
                labelcol = ColInfo(c, i)

        # Array of circle objects for each data row
        Circle = namedtuple('Circle',
                            ['title', 'lat', 'long', 'value', 'size',
                             'units', 'formatter'])
        circles = []

        if data:
            valmax = None
            if valuecol.col.isnumeric:
                values = zip(*data)[valuecol.dataindex]
                filtered = filter(bool, values)
                if filtered:
                    valmax = max(filtered)
                else:
                    valmax = 1

            geolookup = None

            if valuecol.col.datatype == 'bytes':
                formatter = 'formatBytes'
            elif valuecol.col.datatype == 'metric':
                formatter = 'formatMetric'
            else:
                formatter = None

            for rawrow in data:
                val = rawrow[valuecol.dataindex]

                # skip empty result values which are not explicitly zero
                if val is None or val == '':
                    continue

                if valmax:
                    marker_size = 15 * (val / valmax)
                else:
                    marker_size = 5

                if keycol:
                    key = rawrow[keycol.dataindex]

                    # XXXCJ - this is a hack for Profiler based host groups,
                    # need to generalize this, probably via options
                    if widget.table().options['groupby'] == 'host_group':
                        try:
                            geo = Location.objects.get(name=key)
                        except ObjectDoesNotExist:
                            continue
                    else:
                        # Perform geolookup on the key
                        # (should be an IP address...)
                        if geolookup is None:
                            geolookup = Lookup.instance()
                        geo = geolookup.lookup(key)

                    if geo:
                        # Found a match, create a Circle
                        circle = Circle(title=geo.name,
                                        lat=geo.latitude,
                                        long=geo.longitude,
                                        value=val,
                                        size=marker_size,
                                        units=valuecol.col.units,
                                        formatter=formatter)
                        circles.append(circle)
                else:
                    # use lat/long columns instead of lookups
                    lat = rawrow[latcol.dataindex]
                    long = rawrow[longcol.dataindex]
                    title = rawrow[labelcol.dataindex] if labelcol else val

                    circle = Circle(title=title,
                                    lat=lat,
                                    long=long,
                                    value=val,
                                    size=marker_size,
                                    units=valuecol.col.units,
                                    formatter=formatter)
                    circles.append(circle)

        else:
            # no data just return empty circles list
            pass

        data = {
            "chartTitle": widget.title.format(**job.actual_criteria),
            "circles": circles,
            "minbounds": widget.options.min_bounds
        }

        return post_process(data)
Exemplo n.º 4
0
    def process(cls, widget, job, data):
        """Class method to generate list of circle objects.

        Subclass should manipulate this list into specific
        JSON structures as needed.
        """

        request = get_request()
        maps_version = request.user.userprofile.maps_version
        post_process = POST_PROCESS_MAP[maps_version]

        columns = job.get_columns()

        ColInfo = namedtuple('ColInfo', ['col', 'dataindex'])

        keycol = None
        latcol = None
        longcol = None
        valuecol = None
        labelcol = None
        for i, c in enumerate(columns):
            if c.name == widget.options.key:
                keycol = ColInfo(c, i)
            elif c.name == widget.options.latitude:
                latcol = ColInfo(c, i)
            elif c.name == widget.options.longitude:
                longcol = ColInfo(c, i)
            elif c.name == widget.options.value:
                valuecol = ColInfo(c, i)
            elif c.name == widget.options.label:
                labelcol = ColInfo(c, i)

        # Array of circle objects for each data row
        Circle = namedtuple(
            'Circle',
            ['title', 'lat', 'long', 'value', 'size', 'units', 'formatter'])
        circles = []

        if data:
            valmax = None
            if valuecol.col.isnumeric:
                values = zip(*data)[valuecol.dataindex]
                filtered = filter(bool, values)
                if filtered:
                    valmax = max(filtered)
                else:
                    valmax = 1

            geolookup = None

            if valuecol.col.datatype == 'bytes':
                formatter = 'formatBytes'
            elif valuecol.col.datatype == 'metric':
                formatter = 'formatMetric'
            else:
                formatter = None

            for rawrow in data:
                val = rawrow[valuecol.dataindex]

                # skip empty result values which are not explicitly zero
                if val is None or val == '':
                    continue

                if valmax:
                    marker_size = 15 * (val / valmax)
                else:
                    marker_size = 5

                if keycol:
                    key = rawrow[keycol.dataindex]

                    # XXXCJ - this is a hack for Profiler based host groups,
                    # need to generalize this, probably via options
                    if widget.table().options['groupby'] == 'host_group':
                        try:
                            geo = Location.objects.get(name=key)
                        except ObjectDoesNotExist:
                            continue
                    else:
                        # Perform geolookup on the key
                        # (should be an IP address...)
                        if geolookup is None:
                            geolookup = Lookup.instance()
                        geo = geolookup.lookup(key)

                    if geo:
                        # Found a match, create a Circle
                        circle = Circle(title=geo.name,
                                        lat=geo.latitude,
                                        long=geo.longitude,
                                        value=val,
                                        size=marker_size,
                                        units=valuecol.col.units,
                                        formatter=formatter)
                        circles.append(circle)
                else:
                    # use lat/long columns instead of lookups
                    lat = rawrow[latcol.dataindex]
                    long = rawrow[longcol.dataindex]
                    title = rawrow[labelcol.dataindex] if labelcol else val

                    circle = Circle(title=title,
                                    lat=lat,
                                    long=long,
                                    value=val,
                                    size=marker_size,
                                    units=valuecol.col.units,
                                    formatter=formatter)
                    circles.append(circle)

        else:
            # no data just return empty circles list
            pass

        data = {
            "chartTitle": widget.title.format(**job.actual_criteria),
            "circles": circles,
            "minbounds": widget.options.min_bounds
        }

        return post_process(data)