Exemplo n.º 1
0
def metal_step(request):
    all_points = []

    client = database.getClient()
    fs = GridFS(client.trace_database)

    for traceFile in fs.find():
        if 'sourceTypes' in traceFile.metadata and "vastplace_example" in traceFile.metadata[
                'sourceTypes']:
            points = get_example_points_for_source(traceFile._id)
            all_points += points

    all_points = sorted(all_points, key=lambda x: x[0])

    buf = plotStep([
        {
            'x': [u[0] - all_points[0][0] for u in all_points],
            'y': [u[1] for u in all_points],
            'label': "metal points",
            'color': 'green'
        },
    ],
                   xlabel=' Time(s)',
                   ylabel='Metal',
                   legend=False)

    return HttpResponse(buf, content_type='image/svg+xml')
Exemplo n.º 2
0
def metal_bar(request):
    bins = {30000: 0, 60000: 0}

    client = database.getClient()
    fs = GridFS(client.trace_database)

    for traceFile in fs.find():
        if 'sourceTypes' in traceFile.metadata and "vastplace_example" in traceFile.metadata[
                'sourceTypes']:
            points = get_example_points_for_source(traceFile._id)
            for point in points:
                if point[1] <= 30000:
                    bins[30000] += 1
                else:
                    bins[60000] += 1

    buf = plotBarGraph([{
        'X': [int(u) for u in bins.keys()],
        'Y': [bins[k] for k in bins],
        'label': "Bin count",
        'width': 5000,
        'color': 'red'
    }, {
        'X': [int(u) + 5000 for u in bins.keys()],
        'Y': [20 for k in bins],
        'label': "always 20",
        'width': 5000,
        'color': 'green'
    }],
                       xlabel='Sample bar graph',
                       ylabel='The y axis label',
                       legend=True)

    return HttpResponse(buf, content_type='image/svg+xml')
Exemplo n.º 3
0
def bargraph(request, targetId_str):

    bins_lower_keys = [0, 14, 21, 28, 35, 60]

    # Not all bins are the same size, adjust the widths
    widths = [
        bins_lower_keys[i + 1] - bins_lower_keys[i]
        for i in range(len(bins_lower_keys) - 1)
    ]
    widths.append(widths[-1])

    x_labels = [
        str(bins_lower_keys[i]) + ' - ' + str(bins_lower_keys[i + 1])
        for i in range(len(bins_lower_keys) - 1)
    ]
    x_labels.append('>' + str(bins_lower_keys[-1]))

    #matplotlib would center around the lower key, add some offsets depending on the withs
    bins_Xs = []
    for bInd, bin_lower in enumerate(bins_lower_keys):
        bins_Xs.append(bin_lower + 0.5 * widths[bInd])

    barData = {k: 0 for k in bins_lower_keys}

    target_ids = targetId_str.split(',')

    client = database.getClient()
    db = client.trace_database
    fs = GridFS(db)

    for traceFile in fs.find(
        {'_id': {
            '$in': [ObjectId(u) for u in target_ids]
        }}):
        point_collection = client.point_database.sensors.find({
            'sourceId':
            ObjectId(traceFile._id),
            'sensorType':
            'PM2.5'
        })
        for point in point_collection:
            bin_key = max(
                [k for k in bins_lower_keys if k <= point['sensorValue']])
            barData[bin_key] += 1

    buf = plotBarGraph([{
        'X': bins_Xs,
        'Y': [barData[k] for k in sorted(barData.keys())],
        'label': "PM2.5",
        'width': widths,
        'color': 'green'
    }],
                       xlabel='PM2.5',
                       ylabel='Occurence',
                       x_ticks=bins_Xs,
                       x_tick_labels=x_labels,
                       legend=True)

    return HttpResponse(buf, content_type='image/svg+xml')
Exemplo n.º 4
0
def get_example_points_for_source(sourceId):
    retval = []
    client = database.getClient()
    points = client.point_database.sensors.find({'sourceId': sourceId})

    for point in points:
        if point['sensorType'] == "metal":
            retval.append((point['vTimestamp'], point['sensorValue']))
    return retval
Exemplo n.º 5
0
def air_map_get_points_bysource(srcId):
    retval = []
    client = database.getClient()
    point_collection = client.point_database.sensors.find(
        {'sourceId': ObjectId(srcId)})
    GPS = (-1, -1)
    for point in point_collection:
        if point['sensorType'] == 'GPS':
            GPS = point['sensorValue']
        elif point['sensorType'] == 'PM2.5':
            if GPS != (-1, -1):
                retval.append(GPS + [point['sensorValue']])
    client.close()
    return retval
Exemplo n.º 6
0
def metal_rest(request):
    bins = {'under 30000': 0, 'over 30000': 0}

    client = database.getClient()
    fs = GridFS(client.trace_database)

    for traceFile in fs.find():
        if 'sourceTypes' in traceFile.metadata and "vastplace_example" in traceFile.metadata[
                'sourceTypes']:
            points = get_example_points_for_source(traceFile._id)
            for point in points:
                if point[1] <= 30000:
                    bins['under 30000'] += 1
                else:
                    bins['over 30000'] += 1
    return Response(bins, status=status.HTTP_200_OK)
Exemplo n.º 7
0
    def getEvents(self):
        retval = []

        client = database.getClient()
        file_db = client.trace_database
        fs = GridFS(file_db)
        traceFile = fs.get(ObjectId(self.fileId))

        line = traceFile.readline()
        while len(line) > 0:
            data = line.rstrip('\n').split('\t')
            if len(data) >= 1:
                if data[1] == "GPS":
                    timestamp, _, lat, lon = data
                    lat = float(lat)
                    lon = float(lon)

                    retval.append({
                        "sourceId": ObjectId(self.fileId),
                        "sensorName": "example_module",
                        "sensorID": "example_module",
                        "vTimestamp": int(timestamp),
                        "tstype": "epoch",
                        "sensorType": "GPS",
                        "sensorValue": (lat, lon)
                    })
                elif data[1] == "metal":
                    timestamp, _, value = data
                    retval.append({
                        "sourceId": ObjectId(self.fileId),
                        "sensorName": "example_module",
                        "sensorID": "example_module",
                        "vTimestamp": int(timestamp),
                        "tstype": "epoch",
                        "sensorType": "metal",
                        "sensorValue": float(value)
                    })

            line = traceFile.readline()

        return retval
Exemplo n.º 8
0
def grafana_query_median_impl(request):
    request_dict = json.loads(request.body)
    responseData = [{"target": request_dict['targets'], "datapoints": []}]
    values = []

    target_ids = request_dict['targets'][0]['target'][:-len('.median')].lstrip(
        '(').rstrip(')').split('|')

    from_dt = datetime.strptime(request_dict["range"]["from"],
                                '%Y-%m-%dT%H:%M:%S.%fZ')
    to_dt = datetime.strptime(request_dict["range"]["to"],
                              '%Y-%m-%dT%H:%M:%S.%fZ')

    from_ts = float(from_dt.strftime('%s'))
    to_ts = float(to_dt.strftime('%s'))

    client = database.getClient()
    db = client.trace_database
    fs = GridFS(db)
    for traceFile in fs.find(
        {'_id': {
            '$in': [ObjectId(u) for u in target_ids]
        }}):
        point_collection = client.point_database.sensors.find({
            'sourceId':
            ObjectId(traceFile._id),
            'sensorType':
            'PM2.5',
            'vTimestamp': {
                '$gt': from_ts,
                '$lt': to_ts
            }
        })
        for point in point_collection:
            values.append(int(point["sensorValue"]))

    responseData[0]["datapoints"].append(
        [np.median(values), float(from_ts + to_ts) / 2])
    return responseData
Exemplo n.º 9
0
def grafana_sources_search(request, format=None):
    request_dict = json.loads(request.body)

    source_type = None

    if request_dict['target'] == "mobile_ids":
        source_type = "ambassadair_mobile"
    elif request_dict['target'] == "static_ids":
        source_type = "ambassadair_static"

    client = database.getClient()
    db = client.trace_database
    fs = GridFS(db)
    responseData = []
    for traceFile in fs.find():
        if 'sourceTypes' in traceFile.metadata and source_type in traceFile.metadata[
                'sourceTypes']:
            responseData.append({
                "text": traceFile.filename,
                "value": str(traceFile._id)
            })

    return Response(responseData, status=status.HTTP_200_OK)
Exemplo n.º 10
0
def air_map(request):
    tiling_level = 17
    response = None
    client = database.getClient()
    db = client.trace_database
    fs = GridFS(db)
    responseData = {'trajectories': [], 'points': [], 'rectangles': []}
    tiledMeasures = {}
    for traceFile in fs.find():
        if 'sourceTypes' in traceFile.metadata and (
                "ambassadair_mobile" in traceFile.metadata['sourceTypes']
                or "ambassadair_static" in traceFile.metadata['sourceTypes']):
            points = air_map_get_points_bysource(traceFile._id)
            responseData['trajectories'].append({
                'points': [[lon, lat] for lat, lon, _ in points],
                'id':
                traceFile._id
            })
            for lat, lon, val in points:
                tile_number = osm_latlon_to_tile_number(lon, lat, tiling_level)
                if tile_number not in tiledMeasures:
                    tiledMeasures[tile_number] = []
                tiledMeasures[tile_number].append(val)

    for tileX, tileY in tiledMeasures:
        x1, y1 = osm_tile_number_to_latlon(tileX, tileY, tiling_level)
        x2, y2 = osm_tile_number_to_latlon(tileX + 1, tileY + 1, tiling_level)
        mean = np.mean(tiledMeasures[tileX, tileY])
        mean_str = "%.3f" % mean
        if mean > 10:
            color = 'rgba(255, 0, 0, 0.3)'
        else:
            color = 'rgba(0, 255, 0, 0.3)'
        responseData['rectangles'].append((x1, x2, y1, y2, mean_str, color))

    client.close()
    return render(request, 'mapper/map.html', responseData)
Exemplo n.º 11
0
def metal_cdf(request):
    all_points = []

    client = database.getClient()
    fs = GridFS(client.trace_database)

    for traceFile in fs.find():
        if 'sourceTypes' in traceFile.metadata and "vastplace_example" in traceFile.metadata[
                'sourceTypes']:
            points = get_example_points_for_source(traceFile._id)
            all_points += [p[1] for p in points]

    buf = plotCDF([
        {
            'data': all_points,
            'label': "metal points",
            'color': 'green'
        },
    ],
                  xlabel=' Metal',
                  ylabel='CDF',
                  legend=False)

    return HttpResponse(buf, content_type='image/svg+xml')
Exemplo n.º 12
0
    def getEvents(self):
        retval = []

        client = database.getClient()
        file_db = client.trace_database
        fs = GridFS(file_db)
        traceFile = fs.get(ObjectId(self.fileId))

        #Copy the original db file
        outPath = '/tmp/mongo_tmpfile_' + self.fileId
        outF = open(outPath, 'w')
        line = traceFile.readline()
        while len(line) > 0:
            outF.write(line)
            line = traceFile.readline()
        outF.close()

        fIn = open(outPath)
        line = fIn.readline()
        lastGpsFix = (-1, -1)
        while len(line) > 0:
            data = line.rstrip('\n').split(',')
            if len(data) >= 2:
                date_data, lat, lon, value = data
                lat = float(lat)
                lon = float(lon)
                value = float(value)

                ts_str = date_data[:-6]
                tz = date_data[-6:]

                dt = datetime.strptime(ts_str[:26], '%Y-%m-%dT%H:%M:%S.%f')
                timestamp = float(dt.strftime('%s'))

                #Use timezone information to convert back to UTC (+00:00)
                if tz[0] == '+':
                    offsetSign = -1
                else:
                    offsetSign = 1
                offset_hours, offset_minutes = tz[1:].split(':')
                offset_hours = int(offset_hours)
                offset_minutes = int(offset_minutes)

                timestamp += offsetSign * (offset_hours * 3600 +
                                           offset_minutes * 60)

                if (lat, lon) != lastGpsFix:
                    retval.append({
                        "sourceId": ObjectId(self.fileId),
                        "sensorName": "ambassadair",
                        "sensorID": "ambassadair",
                        "vTimestamp": timestamp,
                        "tstype": "epoch",
                        "sensorType": "GPS",
                        "sensorValue": (lat, lon)
                    })
                    lastGpsFix = (lat, lon)

                retval.append({
                    "sourceId": ObjectId(self.fileId),
                    "sensorName": "ambassadair",
                    "sensorID": "ambassadair",
                    "vTimestamp": timestamp,
                    "tstype": "epoch",
                    "sensorType": "PM2.5",
                    "sensorValue": value
                })

            line = fIn.readline()

        return retval