def station(station_id=None): repository = Repository() station = repository.read_station(station_id) if station is None: abort(404, "Station not found") statistics = repository.read_station_statistics(station["station_id"]) photos = repository.read_station_photos(station_id) owners = repository.station_owners(station_id) # Now get 3 latest observations from this station filters = {"station_id": station['station_id']} latest_obs = repository.read_observations(filters=filters, limit=3, offset=0) # Get the 3 observations with the best rating best_obs = repository.read_observations(filters=filters, limit=3, offset=0, order="r.rating DESC", expr="r.rating is not NULL") x = {} x['station_id'] = station['station_id'] x['name'] = station['name'] x['coords'] = utils.coords(station['lon'], station['lat']) x['descr'] = station['descr'] x['config'] = station['config'] x['registered'] = station['registered'] x['lastobs'] = statistics["last_los"] x['firstobs'] = statistics["first_aos"] x['cnt'] = statistics["observation_count"] files = [] for photo in photos: y = {} y['filename'] = photo['filename'] y['descr'] = photo['descr'] y['sort'] = photo['sort'] files.append(y) return render_template('station.html', station=x, files=files, owners=owners, latest_obs=latest_obs, best_obs=best_obs)
def test_read_observations(self, repository: Repository): """Check if a list of observations is returned properly.""" obslist = repository.read_observations() # Make sure there are at least 3 observations (more may be added in the future. # This test should be future-proof.) self.assertGreaterEqual(len(obslist), 3) # Check if the data returned matches values from tests/db-data.psql self.check_obs750(obslist[-1]) self.check_obs751(obslist[-2]) self.check_obs752(obslist[-3])
def test_observations_filters(self, repository: Repository): filters: ObservationFilter = {"obs_id": ObservationId(751)} observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 1) self.assertEqual(observations[0]["obs_id"], 751) # Include observations partially (of fully) in date range # AOS before > LOS after filters = { "aos_before": datetime.datetime(2020, 3, 8, 15, 45, 0, 0), "los_after": datetime.datetime(2020, 3, 8, 15, 30, 0) } observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 1) self.assertEqual(observations[0]["obs_id"], 750) filters = {"sat_id": SatelliteId(28654)} observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 2) self.assertEqual(observations[0]["obs_id"], 1276) self.assertEqual(observations[1]["obs_id"], 752) filters = {"station_id": StationId(1)} observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 4) filters = {"notes": "ote"} observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 1) self.assertEqual(observations[0]["notes"], "Note") filters = {"has_tle": True} observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 2) # obs 751 and 1276 self.assertIsNotNone(observations[0]["tle"]) self.assertIsNotNone(observations[1]["tle"]) filters = { "sat_id": SatelliteId(25338), "station_id": StationId(1), "has_tle": True, "los_after": datetime.datetime(2020, 3, 8, 15, 30, 0) } observations = repository.read_observations(filters=filters) self.assertEqual(len(observations), 1) self.assertEqual(observations[0]["obs_id"], 751)
def obslist(limit_and_offset, **filters): '''This function retrieves observations list from a local database and displays it.''' aos_before_org = filters.get("aos_before") if aos_before_org is not None: # Repository uses datetime.datetime structure to bound dates and it is # exact date. # User provides datetime.date (which not include hour) and it means that # list should contain observations from @los_after day 00:00:00 hour to # @aos_before day 23:59:59.999 hour. For handle it we add 1 day to # @aos_before day before send to repository. filters["aos_before"] = aos_before_org + timedelta(days=1) repository = Repository() obslist = repository.read_observations(filters, **limit_and_offset) satellites_list = repository.read_satellites() observation_count = repository.count_observations(filters) stations_list = repository.read_stations() satellites_dict = { sat["sat_id"]: sat["sat_name"] for sat in satellites_list } stations_dict = {s["station_id"]: s["name"] for s in stations_list} for obs in obslist: obs["sat_name"] = satellites_dict[obs["sat_id"]] obs["station_name"] = stations_dict[obs["station_id"]] if aos_before_org is not None: # We send back to user the same date as user provide. filters["aos_before"] = aos_before_org # When database will contain many satellites and stations then we need # refactor this code to lazy, async read satellites and stations. return 'obslist.html', dict(obslist=obslist, item_count=observation_count, satellites=satellites_list, stations=stations_list, filters=filters)
if __name__ == '__main__': stations: Dict[StationId, Station] = { } repository = Repository() observation_count = repository.count_observations() print("There are a total of %d observations" % observation_count) STEP = 100 limit = STEP offset = 0 index = 0 while offset < observation_count: # ToDo: Add filtration when MR with filtration will be merged. observations = repository.read_observations(limit=limit, offset=offset) print("Processing batch of %d observations" % len(observations)) for observation in observations: if observation['tle'] is None: print("No TLE info for observation %d, skipping." % observation['obs_id']) continue station_id = observation["station_id"] if station_id in stations: station = stations[station_id] else: station = repository.read_station(station_id) stations[station_id] = station
def test_observations_limit_and_offset(self, repository: Repository): observations = repository.read_observations(limit=2, offset=1) self.assertEqual(len(observations), 2) self.assertEqual([o["obs_id"] for o in observations], [752, 751])