Beispiel #1
0
    def get(self, request, format=None):
        #Latest tour used for the total number of riders
        tour = TourConfig.objects.latest('pk') # Get the latest tour

        df = DateFormat(datetime.now())
        end_interval = long(df.U())
        poll_rate = tour.server_polling_rate
        poll_range = tour.server_polling_range

        #Sever's poling range+polling interval ago.
        start_interval = end_interval - poll_rate -poll_range

        rider_count = Rider.objects.count()
        riders = {}

        cursor = connection.cursor()
        cursor.execute(
            """SELECT DISTINCT ON(rider_id) rider_id, speed, ST_X(coords), ST_Y(coords)
                   FROM location_update_location
                WHERE time BETWEEN %s AND %s LIMIT %s""",
                [start_interval, end_interval, rider_count])
        for row in cursor.fetchall():
            (rider_id, speed, lon, lat)=row
            if not riders.has_key(rider_id):
                riders[rider_id] = []
            riders[rider_id].append((int(speed), lon, lat))
        riders = riders.values()
        return Response(
                {'locations': riders}, status=status.HTTP_200_OK)
Beispiel #2
0
def update_num_riders():
    df = DateFormat(datetime.now())
    # Search within past 15 minutes
    tm = long(df.U()) - 900
    number_riders = Location.objects.filter(
        time__gte=tm).distinct('rider').count()

    cache.set(settings.JSON_KEYS['RIDER_CNT'], number_riders)
    return number_riders
Beispiel #3
0
 def to_python(self, value):
     # Validate Date-time input against the accepted formats
     dt = None
     try:
         dt = datetime.strptime(' '.join(value), '%Y-%m-%d %H:%M')
     except ValueError:
         try:
             dt = datetime.strptime(' '.join(value), '%Y-%m-%d %H:%M:%S')
         except ValueError:
             raise ValidationError(
                 'Please enter a valid DateTime (Y-m-d H:M:S')
     if dt is not None:
         df = DateFormat(dt)
         dt = int(df.U())
     return dt
Beispiel #4
0
    def get(self, request, format=None):
        df = DateFormat(datetime.now())
        mins_ago = long(df.U()) - 500 #10 mins
        riders = {}

        cursor = connection.cursor()
        cursor.execute(
            """SELECT rider_id, speed, ST_X(coords), ST_Y(coords)
                FROM (
                    SELECT rider_id, speed, coords,
                    RANK() OVER (PARTITION BY rider_id ORDER BY time DESC) as rank
                        FROM location_update_location
                    WHERE speed != 0)dt
                WHERE dt.rank <= 10""")

        for row in cursor.fetchall():
            (rider_id, speed, lon, lat)=row
            if not riders.has_key(rider_id):
                riders[rider_id] = []
            riders[rider_id].append((int(speed), lon, lat))
        riders = riders.values()
        return Response(
                    {'locations': riders}, status=status.HTTP_200_OK)
Beispiel #5
0
 def to_python(self, value):
     dt = super(UnixDateTimeField, self).to_python(value)
     if dt is not None:
         df = DateFormat(dt)
         dt = int(df.U())
     return dt