def task_daily_download_from_api_meteologica(test_env=True): pony = PonyManager(envinfo.DB_CONF) pony.define_all_models() pony.binddb(create_tables=False) configdb = ns.load('conf/config_meteologica.yaml') with orm.db_session: downloadMeterForecasts(pony.db, configdb, test_env=test_env)
def importPlantCLI(yaml): yamlFilename = click.format_filename(yaml) logger.debug(yamlFilename) from conf import envinfo pony = PonyManager(envinfo.DB_CONF) pony.define_all_models() pony.binddb(create_tables=False) importPlantsFromFile(pony.db, yamlFilename)
def setUpClass(cls): from conf import envinfo assert envinfo.SETTINGS_MODULE == 'conf.settings.testing' cls.pony = PonyManager(envinfo.DB_CONF) cls.pony.define_all_models() cls.pony.binddb(create_tables=True) from api_server.plantmonitor_api import api cls.api = api
def computeIntegralMetrics(): from conf import envinfo pony = PonyManager(envinfo.DB_CONF) pony.define_all_models() pony.binddb() irradiance = integrateIrradiance(pony.db) insertHourlySensorIrradiationMetrics(pony.db, irradiance, 'integratedIrradiation_wh_m2') orm.flush() expectedEnergy = integrateExpectedPower(pony.db) insertHourlySensorIrradiationMetrics(pony.db, expectedEnergy, 'expected_energy_wh')
def setUp(self): from conf import envinfo self.assertEqual(envinfo.SETTINGS_MODULE, 'conf.settings.testing') os.environ['PGTZ'] = 'UTC' database_info = envinfo.DB_CONF self.pony = PonyManager(database_info) self.pony.define_solar_models() self.pony.binddb(create_tables=True)
def check_db_connection(): from conf import envinfo pony = PonyManager(envinfo.DB_CONF) pony.define_all_models() pony.binddb() pony.db.check_tables() with orm.db_session: plants = pony.db.Plant.select()[:].to_list() print(plants)
def list_plants(self): from conf import envinfo database_info = envinfo.DB_CONF solar_db = PonyManager(database_info) solar_db.define_solar_models() solar_db.binddb(create_tables=False) with orm.db_session: plant_info = orm.select( (p.name, p.location.latitude, p.location.longitude) for p in solar_db.db.Plant)[:] return plant_info
def task_meters_erp_to_orm(): pony = PonyManager(envinfo.DB_CONF) pony.define_all_models() pony.binddb(create_tables=False) r = ReadingsFacade(pony.db) try: # TODO mock measures or fake meters r.transfer_ERP_readings_to_model() except Exception as err: logger.error("[ERROR] %s" % err) raise
def setUpORM(self): from conf import envinfo self.assertEqual(envinfo.SETTINGS_MODULE, 'conf.settings.testing') orm.rollback() self.pony = PonyManager(envinfo.DB_CONF) self.pony.define_all_models() self.pony.binddb(create_tables=True) self.pony.db.drop_all_tables(with_all_data=True) self.pony.db.create_tables() self.maxDiff = None
def main(): args = parseArguments() configfile = args.get('--config', 'conf/config.yaml') configdb = ns.load(configfile) configdb.update(args) from conf import envinfo pony = PonyManager(envinfo.DB_CONF) pony.define_all_models() pony.binddb() downloadMeterForecasts(pony.db, configdb)
def task(): try: plantname = envinfo.ACTIVEPLANT_CONF['activeplant'] # TODO remove legacy if plantname == 'Alcolea': legacyInfluxUpload() plant = ProductionPlant() apiconfig = envinfo.API_CONFIG if not plant.load('conf/modmap.yaml', plantname): logger.error('Error loading yaml definition file...') sys.exit(-1) plant_data = get_plant_reading(plant) if not plant_data: logger.error( "Getting reading from {} failed. Aborting.".format(plantname)) return pony_manager = PonyManager(envinfo.DB_CONF) pony_manager.define_all_models() pony_manager.binddb() ponyStorage = PonyMetricStorage(pony_manager.db) apiStorage = ApiMetricStorage(apiconfig) logger.info("**** Saving data in database ****") logger.debug("plant_data: {}".format(plant_data)) ponyStorage.insertPlantData(plant_data) logger.info("**** Saving data in Api ****") result = apiStorage.insertPlantData(plant_data) logger.debug("api response: {}".format(result)) except Exception as err: logger.exception("[ERROR] %s" % err)
def setUp(self): from conf import envinfo self.assertEqual(envinfo.SETTINGS_MODULE, 'conf.settings.testing') orm.rollback() self.pony = PonyManager(envinfo.DB_CONF) self.pony.define_all_models() self.pony.binddb(create_tables=True) self.pony.db.drop_all_tables(with_all_data=True) self.storage = PonyMetricStorage(self.pony.db) self.maxDiff = None # orm.set_sql_debug(True) self.pony.db.create_tables() # database.generate_mapping(create_tables=True) orm.db_session.__enter__()
def sun_events_update(self, start, end, plants=None): from conf import envinfo database_info = envinfo.DB_CONF solar_db = PonyManager(database_info) solar_db.define_solar_models() solar_db.binddb(create_tables=True) with orm.db_session: if not plants: target_plants = orm.select(p for p in solar_db.db.Plant)[:] else: target_plants = solar_db.db.Plant.select( lambda p: p.name in plants)[:] if not target_plants: print("No plants in database. Returning.") return for plant in target_plants: if not plant.location: print("Plant {} doesn't have a location".format( plant.name)) continue lat = plant.location.latitude lon = plant.location.longitude sun_events_gen = SunEventsGenerator(lat, lon) sun_events = sun_events_gen.generate_sunevents(start=start, end=end) print(sun_events) solar_db.db.SolarEvent.insertPlantSolarEvents( plant, sun_events)
#TODO find a way to not use Union because of time class Device(BaseModel): id: str readings: List[Dict[str, Union[int, datetime.datetime, None]]] class PlantReading(BaseModel): plant: str version: str time: datetime.datetime devices: List[Device] api = FastAPI() from conf import envinfo pony_manager = PonyManager(envinfo.DB_CONF) pony_manager.define_all_models() pony_manager.binddb() storage = PonyMetricStorage(pony_manager.db) @api.get('/version') def api_version(): return dict( version='1.0', ) @api.put('/plant/{plant_id}/readings') async def api_putPlantReadings(plant_id: str, plant_reading: PlantReading): logger.info("Putting plant data into plant {} : {}".format(plant_id, plant_reading)) with orm.db_session: