def test_empty_fix(self): """ Tracking server accepts empty fixes """ # Create fake fix message message = self.create_fix_message(123456, 0) utcnow_return_value = datetime(year=2005, month=4, day=13) with patch('skylines.tracking.server.datetime') as datetime_mock: datetime_mock.combine.side_effect = \ lambda *args, **kw: datetime.combine(*args, **kw) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message self.server.datagramReceived(message, self.HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() eq_(len(fixes), 1) fix = fixes[0] eq_(fix.ip, self.HOST_PORT[0]) eq_(fix.time, utcnow_return_value) eq_(fix.location_wkt, None) eq_(fix.track, None) eq_(fix.ground_speed, None) eq_(fix.airspeed, None) eq_(fix.altitude, None) eq_(fix.vario, None) eq_(fix.engine_noise_level, None)
def trafficRequestReceived(self, host, port, key, payload): if len(payload) != 8: return pilot = User.by_tracking_key(key) if pilot is None: log.err("No such pilot: %d" % key) return data = struct.unpack('!II', payload) or_filters = [] flags = data[0] if flags & TRAFFIC_FLAG_FOLLOWEES: subq = db.session \ .query(Follower.destination_id) \ .filter(Follower.source_id == pilot.id) \ .subquery() or_filters.append(TrackingFix.pilot_id.in_(subq)) if flags & TRAFFIC_FLAG_CLUB: subq = db.session \ .query(User.id) \ .filter(User.club_id == pilot.club_id) \ .subquery() or_filters.append(TrackingFix.pilot_id.in_(subq)) if len(or_filters) == 0: return query = TrackingFix.query() \ .distinct(TrackingFix.pilot_id) \ .filter(and_(TrackingFix.time >= datetime.utcnow() - timedelta(hours=2), TrackingFix.pilot_id != pilot.id, TrackingFix.location_wkt != None, TrackingFix.altitude != None, or_(*or_filters))) \ .order_by(TrackingFix.pilot_id, TrackingFix.time.desc()) \ .limit(32) response = '' count = 0 for fix in query: location = fix.location if location is None: continue t = fix.time t = t.hour * 3600000 + t.minute * 60000 + t.second * 1000 + t.microsecond / 1000 response += struct.pack('!IIiihHI', fix.pilot_id, t, int(location.latitude * 1000000), int(location.longitude * 1000000), int(fix.altitude), 0, 0) count += 1 response = struct.pack('!HBBI', 0, 0, count, 0) + response response = struct.pack('!IHHQ', MAGIC, 0, TYPE_TRAFFIC_RESPONSE, 0) + response response = set_crc(response) self.transport.write(response, (host, port))
def test_empty_fix(server, test_user): """ Tracking server accepts empty fixes """ # Create fake fix message message = create_fix_message(test_user.tracking_key, 0) utcnow_return_value = datetime(year=2005, month=4, day=13) with patch("skylines.tracking.server.datetime") as datetime_mock: datetime_mock.combine.side_effect = lambda *args, **kw: datetime.combine( *args, **kw) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() assert len(fixes) == 1 fix = fixes[0] assert fix.ip == HOST_PORT[0] assert fix.time == utcnow_return_value assert fix.location_wkt is None assert fix.track is None assert fix.ground_speed is None assert fix.airspeed is None assert fix.altitude is None assert fix.vario is None assert fix.engine_noise_level is None
def _get_flight_path2(pilot, last_update=None): query = TrackingFix.query() \ .filter(and_(TrackingFix.pilot == pilot, TrackingFix.location != None, TrackingFix.altitude != None, TrackingFix.max_age_filter(12))) if pilot.tracking_delay > 0 and not pilot.is_readable(g.current_user): query = query.filter(TrackingFix.delay_filter(pilot.tracking_delay)) query = query.order_by(TrackingFix.time) start_fix = query.first() if not start_fix: return None start_time = start_fix.time.hour * 3600 + start_fix.time.minute * 60 + start_fix.time.second if last_update: query = query.filter(TrackingFix.time >= start_fix.time + timedelta(seconds=(last_update - start_time))) result = [] for fix in query: location = fix.location if location is None: continue time_delta = fix.time - start_fix.time time = start_time + time_delta.days * 86400 + time_delta.seconds result.append((time, location.latitude, location.longitude, fix.altitude, fix.engine_noise_level, fix.elevation)) return result
def test_empty_fix(server, test_user): """ Tracking server accepts empty fixes """ # Create fake fix message message = create_fix_message(test_user.tracking_key, 0) utcnow_return_value = datetime(year=2005, month=4, day=13) with patch("skylines.tracking.server.datetime") as datetime_mock: datetime_mock.combine.side_effect = lambda *args, **kw: datetime.combine(*args, **kw) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() assert len(fixes) == 1 fix = fixes[0] assert fix.ip == HOST_PORT[0] assert fix.time == utcnow_return_value assert fix.location_wkt is None assert fix.track is None assert fix.ground_speed is None assert fix.airspeed is None assert fix.altitude is None assert fix.vario is None assert fix.engine_noise_level is None
def _get_flight_path2(pilot, last_update=None): query = TrackingFix.query().filter( and_( TrackingFix.pilot == pilot, TrackingFix.location != None, TrackingFix.altitude != None, TrackingFix.max_age_filter(12), TrackingFix.time_visible <= datetime.utcnow(), ) ) query = query.order_by(TrackingFix.time) start_fix = query.first() if not start_fix: return None start_time = ( start_fix.time.hour * 3600 + start_fix.time.minute * 60 + start_fix.time.second ) if last_update: query = query.filter( TrackingFix.time >= start_fix.time + timedelta(seconds=(last_update - start_time)) ) result = [] for fix in query: location = fix.location if location is None: continue time_delta = fix.time - start_fix.time time = start_time + time_delta.days * 86400 + time_delta.seconds result.append( FlightPathFix( datetime=fix.time, seconds_of_day=time, location={ "latitude": location.latitude, "longitude": location.longitude, }, gps_altitude=fix.altitude, enl=fix.engine_noise_level, track=fix.track, groundspeed=fix.ground_speed, tas=fix.airspeed, elevation=fix.elevation, ) ) return result
def test_empty_tracking_key(server): """ Tracking server declines fixes without tracking key """ # Create fake fix message message = create_fix_message(0, 0) # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received assert TrackingFix.query().count() == 0
def test_empty_tracking_key(self): """ Tracking server declines fixes without tracking key """ # Create fake fix message message = self.create_fix_message(0, 0) # Send fake ping message self.server.datagramReceived(message, self.HOST_PORT) # Check if the message was properly received eq_(TrackingFix.query().count(), 0)
def test_real_fix(server, test_user): """ Tracking server accepts real fixes """ utcnow_return_value = datetime(year=2013, month=1, day=1, hour=12, minute=34, second=56) # Create fake fix message now = utcnow_return_value now_s = ((now.hour * 60) + now.minute) * 60 + now.second message = create_fix_message( test_user.tracking_key, now_s * 1000, latitude=52.7, longitude=7.52, track=234, ground_speed=33.25, airspeed=32.0, altitude=1234, vario=2.25, enl=10, ) with patch("skylines.tracking.server.datetime") as datetime_mock: datetime_mock.combine.side_effect = lambda *args, **kw: datetime.combine( *args, **kw) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() assert len(fixes) == 1 fix = fixes[0] assert fix.ip == HOST_PORT[0] assert fix.time == utcnow_return_value assert fix.time_visible == utcnow_return_value + timedelta(minutes=2) assert fix.location.to_wkt() == "POINT(7.52 52.7)" assert fix.track == 234 assert fix.ground_speed == 33.25 assert fix.airspeed == 32.0 assert fix.altitude == 1234 assert fix.vario == 2.25 assert fix.engine_noise_level == 10
def test_real_fix(server, test_user): """ Tracking server accepts real fixes """ utcnow_return_value = datetime( year=2013, month=1, day=1, hour=12, minute=34, second=56 ) # Create fake fix message now = utcnow_return_value now_s = ((now.hour * 60) + now.minute) * 60 + now.second message = create_fix_message( test_user.tracking_key, now_s * 1000, latitude=52.7, longitude=7.52, track=234, ground_speed=33.25, airspeed=32.0, altitude=1234, vario=2.25, enl=10, ) with patch("skylines.tracking.server.datetime") as datetime_mock: datetime_mock.combine.side_effect = lambda *args, **kw: datetime.combine( *args, **kw ) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() assert len(fixes) == 1 fix = fixes[0] assert fix.ip == HOST_PORT[0] assert fix.time == utcnow_return_value assert fix.time_visible == utcnow_return_value + timedelta(minutes=2) assert fix.location.to_wkt() == "POINT(7.52 52.7)" assert fix.track == 234 assert fix.ground_speed == 33.25 assert fix.airspeed == 32.0 assert fix.altitude == 1234 assert fix.vario == 2.25 assert fix.engine_noise_level == 10
def _get_flight_path2(pilot, last_update=None): query = TrackingFix.query().filter( and_( TrackingFix.pilot == pilot, TrackingFix.location != None, TrackingFix.altitude != None, TrackingFix.max_age_filter(12), TrackingFix.time_visible <= datetime.utcnow(), )) query = query.order_by(TrackingFix.time) start_fix = query.first() if not start_fix: return None start_time = (start_fix.time.hour * 3600 + start_fix.time.minute * 60 + start_fix.time.second) if last_update: query = query.filter(TrackingFix.time >= start_fix.time + timedelta(seconds=(last_update - start_time))) result = [] for fix in query: location = fix.location if location is None: continue time_delta = fix.time - start_fix.time time = start_time + time_delta.days * 86400 + time_delta.seconds result.append( FlightPathFix( datetime=fix.time, seconds_of_day=time, location={ "latitude": location.latitude, "longitude": location.longitude, }, gps_altitude=fix.altitude, enl=fix.engine_noise_level, track=fix.track, groundspeed=fix.ground_speed, tas=fix.airspeed, elevation=fix.elevation, )) return result
def test_real_fix(self): """ Tracking server accepts real fixes """ utcnow_return_value = datetime(year=2013, month=1, day=1, hour=12, minute=34, second=56) # Create fake fix message now = utcnow_return_value now_s = ((now.hour * 60) + now.minute) * 60 + now.second message = self.create_fix_message(123456, now_s * 1000, latitude=52.7, longitude=7.52, track=234, ground_speed=33.25, airspeed=32., altitude=1234, vario=2.25, enl=10) with patch('skylines.tracking.server.datetime') as datetime_mock: datetime_mock.combine.side_effect = \ lambda *args, **kw: datetime.combine(*args, **kw) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message self.server.datagramReceived(message, self.HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() eq_(len(fixes), 1) fix = fixes[0] eq_(fix.ip, self.HOST_PORT[0]) eq_(fix.time, utcnow_return_value) eq_(fix.location.to_wkt(), 'POINT(7.52 52.7)') eq_(fix.track, 234) eq_(fix.ground_speed, 33.25) eq_(fix.airspeed, 32.) eq_(fix.altitude, 1234) eq_(fix.vario, 2.25) eq_(fix.engine_noise_level, 10)
def test_failing_fix(self): """ Tracking server handles SQLAlchemyError gracefully """ # Mock the transaction commit to fail commitmock = Mock(side_effect=SQLAlchemyError()) with patch.object(db.session, 'commit', commitmock): # Create fake fix message message = self.create_fix_message(123456, 0) # Send fake ping message self.server.datagramReceived(message, self.HOST_PORT) # Check if the message was properly received eq_(TrackingFix.query().count(), 0) ok_(commitmock.called)
def test_failing_fix(server, test_user): """ Tracking server handles SQLAlchemyError gracefully """ # Mock the transaction commit to fail commitmock = Mock(side_effect=SQLAlchemyError()) with patch.object(db.session, 'commit', commitmock): # Create fake fix message message = create_fix_message(test_user.tracking_key, 0) # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received assert TrackingFix.query().count() == 0 assert commitmock.called
def test_failing_fix(server, test_user): """ Tracking server handles SQLAlchemyError gracefully """ # Mock the transaction commit to fail commitmock = Mock(side_effect=SQLAlchemyError()) with patch.object(db.session, "commit", commitmock): # Create fake fix message message = create_fix_message(test_user.tracking_key, 0) # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received assert TrackingFix.query().count() == 0 assert commitmock.called
def test_failing_fix(server, test_user): """ Tracking server handles SQLAlchemyError gracefully """ now = datetime.utcnow() now_s = ((now.hour * 60) + now.minute) * 60 + now.second # Mock the transaction commit to fail commitmock = Mock(side_effect=SQLAlchemyError()) with patch.object(db.session, 'commit', commitmock): # Create fake fix message message = create_fix_message(test_user.tracking_key, now_s * 1000) # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received assert TrackingFix.query().count() == 0 assert commitmock.called
def test_failing_fix(server, test_user): """ Tracking server handles SQLAlchemyError gracefully """ now = datetime.utcnow() now_s = ((now.hour * 60) + now.minute) * 60 + now.second # Mock the transaction commit to fail commitmock = Mock(side_effect=SQLAlchemyError()) with patch.object(db.session, "commit", commitmock): # Create fake fix message message = create_fix_message(test_user.tracking_key, now_s * 1000) # Send fake ping message server.handle(message, HOST_PORT) # Check if the message was properly received assert TrackingFix.query().count() == 0 assert commitmock.called
def _get_flight_path2(pilot, last_update=None): query = TrackingFix.query() \ .filter(and_(TrackingFix.pilot == pilot, TrackingFix.location != None, TrackingFix.altitude != None, TrackingFix.max_age_filter(12))) if pilot.tracking_delay > 0 and not pilot.is_readable(g.current_user): query = query.filter(TrackingFix.delay_filter(pilot.tracking_delay)) query = query.order_by(TrackingFix.time) start_fix = query.first() if not start_fix: return None start_time = start_fix.time.hour * 3600 + start_fix.time.minute * 60 + start_fix.time.second if last_update: query = query.filter(TrackingFix.time >= start_fix.time + timedelta(seconds=(last_update - start_time))) result = [] for fix in query: location = fix.location if location is None: continue time_delta = fix.time - start_fix.time time = start_time + time_delta.days * 86400 + time_delta.seconds result.append(FlightPathFix(datetime=fix.time, seconds_of_day=time, location={'latitude': location.latitude, 'longitude': location.longitude}, gps_altitude=fix.altitude, enl=fix.engine_noise_level, track=fix.track, groundspeed=fix.ground_speed, tas=fix.airspeed, elevation=fix.elevation)) return result
def test_real_fix(self): """ Tracking server accepts real fixes """ utcnow_return_value = datetime(year=2013, month=1, day=1, hour=12, minute=34, second=56) # Create fake fix message now = utcnow_return_value now_s = ((now.hour * 60) + now.minute) * 60 + now.second message = self.create_fix_message( 123456, now_s * 1000, latitude=52.7, longitude=7.52, track=234, ground_speed=33.25, airspeed=32., altitude=1234, vario=2.25, enl=10) with patch('skylines.tracking.server.datetime') as datetime_mock: datetime_mock.combine.side_effect = \ lambda *args, **kw: datetime.combine(*args, **kw) # Connect utcnow mockup datetime_mock.utcnow.return_value = utcnow_return_value # Send fake ping message self.server.datagramReceived(message, self.HOST_PORT) # Check if the message was properly received and written to the database fixes = TrackingFix.query().all() eq_(len(fixes), 1) fix = fixes[0] eq_(fix.ip, self.HOST_PORT[0]) eq_(fix.time, utcnow_return_value) eq_(fix.location.to_wkt(), 'POINT(7.52 52.7)') eq_(fix.track, 234) eq_(fix.ground_speed, 33.25) eq_(fix.airspeed, 32.) eq_(fix.altitude, 1234) eq_(fix.vario, 2.25) eq_(fix.engine_noise_level, 10)
def run(self, user): result = TrackingFix.query(pilot_id=user).delete() db.session.commit() print '%d live tracks cleared.' % result
def tearDown(self): # Clear the database TrackingFix.query().delete() db.session.commit()
def run(self, user): result = TrackingFix.query(pilot_id=user).delete() db.session.commit() print("%d live tracks cleared." % result)
def server(app, db_session): _server.TrackingServer.__init__ = Mock(return_value=None) server = _server.TrackingServer() server.app = app yield server TrackingFix.query().delete()
def trafficRequestReceived(self, host, port, key, payload): if len(payload) != 8: return pilot = User.by_tracking_key(key) if pilot is None: log("%s TRAFFIC_REQUEST unknown pilot (key: %x)" % (host, key)) return data = struct.unpack('!II', payload) or_filters = [] flags = data[0] if flags & TRAFFIC_FLAG_FOLLOWEES: subq = db.session \ .query(Follower.destination_id) \ .filter(Follower.source_id == pilot.id) \ .subquery() or_filters.append(TrackingFix.pilot_id.in_(subq)) if flags & TRAFFIC_FLAG_CLUB: subq = db.session \ .query(User.id) \ .filter(User.club_id == pilot.club_id) \ .subquery() or_filters.append(TrackingFix.pilot_id.in_(subq)) if len(or_filters) == 0: return # Add a db.Column to the inner query with # numbers ordered by time for each pilot row_number = db.over(db.func.row_number(), partition_by=TrackingFix.pilot_id, order_by=TrackingFix.time.desc()) # Create inner query subq = db.session \ .query(TrackingFix.id, row_number.label('row_number')) \ .join(TrackingFix.pilot) \ .filter(TrackingFix.pilot_id != pilot.id) \ .filter(TrackingFix.max_age_filter(2)) \ .filter(TrackingFix.delay_filter(User.tracking_delay_interval())) \ .filter(TrackingFix.location_wkt != None) \ .filter(TrackingFix.altitude != None) \ .filter(or_(*or_filters)) \ .subquery() # Create outer query that orders by time and # only selects the latest fix query = TrackingFix.query() \ .filter(TrackingFix.id == subq.c.id) \ .filter(subq.c.row_number == 1) \ .order_by(TrackingFix.time.desc()) \ .limit(32) response = '' count = 0 for fix in query: location = fix.location if location is None: continue t = fix.time t = t.hour * 3600000 + t.minute * 60000 + t.second * 1000 + t.microsecond / 1000 response += struct.pack('!IIiihHI', fix.pilot_id, t, int(location.latitude * 1000000), int(location.longitude * 1000000), int(fix.altitude), 0, 0) count += 1 response = struct.pack('!HBBI', 0, 0, count, 0) + response response = struct.pack('!IHHQ', MAGIC, 0, TYPE_TRAFFIC_RESPONSE, 0) + response response = set_crc(response) self.socket.sendto(response, (host, port)) log("%s TRAFFIC_REQUEST %s -> %d locations" % (host, unicode(pilot).encode('utf8', 'ignore'), count))
# # Clear all live tracks for a certain user. # import sys import os import argparse from config import to_envvar sys.path.append(os.path.dirname(sys.argv[0])) parser = argparse.ArgumentParser(description='Clear all live tracks for a certain user.') parser.add_argument('--config', metavar='config.ini', help='path to the configuration INI file') parser.add_argument('user', type=int, help='a user ID') args = parser.parse_args() if not to_envvar(args.config): parser.error('Config file "{}" not found.'.format(args.config)) from skylines import db from skylines.model import TrackingFix result = TrackingFix.query(pilot_id=args.user).delete() db.session.commit() print '%d live tracks cleared.' % result
def tearDown(self): # Clear the database TrackingFix.query().delete()