Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the last commits to #160 #161

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions migrations/versions/d3c34effeefa_lighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def upgrade_ecosystems() -> None:
sa.Column("day", sa.Time(), nullable=False, default=time(8)))
batch_op.add_column(
sa.Column("night", sa.Time(), nullable=False, default=time(20)))

op.rename_table("lightings", "nycthemeral_cycles")
# ### end Alembic commands ###


Expand All @@ -67,6 +69,8 @@ def downgrade_ecosystems() -> None:
batch_op.drop_column("lighting")
batch_op.drop_column("day")
batch_op.drop_column("night")

op.rename_table("nycthemeral_cycles", "lightings")
# ### end Alembic commands ###


Expand Down
9 changes: 5 additions & 4 deletions src/ouranos/aggregator/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ouranos.core.database.models.app import ServiceName
from ouranos.core.database.models.gaia import (
ActuatorRecord, ActuatorState, Chaos, CrudRequest, Ecosystem, Engine,
EnvironmentParameter, Hardware, HealthRecord, Lighting, Place, CameraPicture,
EnvironmentParameter, Hardware, HealthRecord, NycthemeralCycle, Place, CameraPicture,
SensorAlarm, SensorDataRecord, SensorDataCache)
from ouranos.core.exceptions import NotRegisteredError
from ouranos.core.utils import humanize_list, Tokenizer
Expand Down Expand Up @@ -444,7 +444,7 @@ async def on_environmental_parameters(
nycthemeral_cycle = ecosystem["nycthemeral_cycle"]
# TODO: handle target
nycthemeral_cycle.pop("target")
await Lighting.update_or_create(
await NycthemeralCycle.update_or_create(
session, ecosystem_uid=uid, values=nycthemeral_cycle)
environment_parameters_in_config: list[str] = []
for param in ecosystem["climate"]:
Expand Down Expand Up @@ -505,6 +505,7 @@ async def on_chaos_parameters(
)

@registration_required
@dispatch_to_application
async def on_nycthemeral_info(
self,
sid: UUID, # noqa
Expand All @@ -526,7 +527,7 @@ async def on_nycthemeral_info(
nycthemeral_cycle = payload["data"]
# TODO: handle target
target = nycthemeral_cycle.pop("target")
await Lighting.update_or_create(
await NycthemeralCycle.update_or_create(
session, ecosystem_uid=uid, values=nycthemeral_cycle)

self.logger.debug(
Expand Down Expand Up @@ -1041,7 +1042,7 @@ async def on_light_data(
"evening_start": ecosystem["evening_start"],
"evening_end": ecosystem["evening_end"]
}
await Lighting.update_or_create(
await NycthemeralCycle.update_or_create(
session, ecosystem_uid=payload["uid"], values=light_info)
self.logger.debug(
f"Logged light data from ecosystem(s): {humanize_list(ecosystems_to_log)}"
Expand Down
8 changes: 4 additions & 4 deletions src/ouranos/core/database/models/gaia.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Ecosystem(Base, CachedCRUDMixin, InConfigMixin):

# relationships
engine: Mapped["Engine"] = relationship(back_populates="ecosystems", lazy="selectin")
lighting: Mapped["Lighting"] = relationship(back_populates="ecosystem", uselist=False, lazy="selectin")
lighting: Mapped["NycthemeralCycle"] = relationship(back_populates="ecosystem", uselist=False, lazy="selectin")
chaos: Mapped["Chaos"] = relationship(back_populates="ecosystem", uselist=False, lazy="selectin")
environment_parameters: Mapped[list["EnvironmentParameter"]] = relationship(back_populates="ecosystem")
plants: Mapped[list["Plant"]] = relationship(back_populates="ecosystem")
Expand Down Expand Up @@ -535,7 +535,7 @@ class Place(Base, CRUDMixin):
latitude: Mapped[float] = mapped_column()

# relationships
lightings: Mapped[list[Lighting]] = relationship(back_populates="target")
lightings: Mapped[list[NycthemeralCycle]] = relationship(back_populates="target")
engine: Mapped["Engine"] = relationship(back_populates="places")

def __repr__(self) -> str:
Expand All @@ -544,8 +544,8 @@ def __repr__(self) -> str:
)


class Lighting(Base, CRUDMixin):
__tablename__ = "lightings"
class NycthemeralCycle(Base, CRUDMixin):
__tablename__ = "nycthemeral_cycles"

ecosystem_uid: Mapped[str] = mapped_column(sa.ForeignKey("ecosystems.uid"), primary_key=True)
span: Mapped[gv.NycthemeralSpanMethod] = mapped_column(default=gv.NycthemeralSpanMethod.fixed)
Expand Down
6 changes: 3 additions & 3 deletions src/ouranos/web_server/routes/gaia/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from gaia_validators import safe_enum_from_name

from ouranos.core.database.models.gaia import (
Ecosystem, EnvironmentParameter, Lighting)
Ecosystem, EnvironmentParameter, NycthemeralCycle)
from ouranos.core.dispatchers import DispatcherFactory
from ouranos.core.utils import timeWindow
from ouranos.web_server.auth import is_operator
Expand Down Expand Up @@ -283,7 +283,7 @@ async def get_ecosystems_light(
session, ecosystems_id=ecosystems_id, in_config=in_config)
response = []
for ecosystem in ecosystems:
lighting = await Lighting.get(session, ecosystem_uid=ecosystem.uid)
lighting = await NycthemeralCycle.get(session, ecosystem_uid=ecosystem.uid)
if lighting is not None:
response.append(
{
Expand All @@ -301,7 +301,7 @@ async def get_ecosystem_lighting(
session: Annotated[AsyncSession, Depends(get_session)],
):
ecosystem = await ecosystem_or_abort(session, ecosystem_uid)
lighting = await Lighting.get(session, ecosystem_uid=ecosystem.uid)
lighting = await NycthemeralCycle.get(session, ecosystem_uid=ecosystem.uid)
if lighting is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand Down
8 changes: 4 additions & 4 deletions tests/aggregator/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ouranos.aggregator.events import GaiaEvents
from ouranos.core.database.models.gaia import (
ActuatorState, Chaos, Ecosystem, Engine, EnvironmentParameter,
Hardware, HealthRecord, Lighting, Place, SensorAlarm, SensorDataCache,
Hardware, HealthRecord, NycthemeralCycle, Place, SensorAlarm, SensorDataCache,
SensorDataRecord)
from ouranos.core.exceptions import NotRegisteredError
from ouranos.core.utils import create_time_window
Expand Down Expand Up @@ -184,7 +184,7 @@ async def test_on_environmental_parameters(
g_data.engine_sid, [g_data.environmental_payload])

async with ecosystem_aware_db.scoped_session() as session:
light = await Lighting.get(session, ecosystem_uid=g_data.ecosystem_uid)
light = await NycthemeralCycle.get(session, ecosystem_uid=g_data.ecosystem_uid)
assert light.lighting == g_data.sky["lighting"]

environment_parameter = await EnvironmentParameter.get(
Expand Down Expand Up @@ -227,7 +227,7 @@ async def test_on_nycthemeral_info(
g_data.engine_sid, [g_data.nycthemeral_info_payload])

async with ecosystem_aware_db.scoped_session() as session:
lighting = await Lighting.get(session, ecosystem_uid=g_data.ecosystem_uid)
lighting = await NycthemeralCycle.get(session, ecosystem_uid=g_data.ecosystem_uid)
assert lighting.ecosystem_uid == g_data.ecosystem_uid
assert lighting.span == g_data.sky["span"]
assert lighting.lighting == g_data.sky["lighting"]
Expand Down Expand Up @@ -488,7 +488,7 @@ async def test_on_light_data(
await events_handler.on_light_data(g_data.engine_sid, [g_data.light_data_payload])

async with ecosystem_aware_db.scoped_session() as session:
light = await Lighting.get(session, ecosystem_uid=g_data.ecosystem_uid)
light = await NycthemeralCycle.get(session, ecosystem_uid=g_data.ecosystem_uid)
assert light.morning_start == g_data.light_data["morning_start"]
assert light.morning_end == g_data.light_data["morning_end"]
assert light.evening_start == g_data.light_data["evening_start"]
Expand Down
4 changes: 2 additions & 2 deletions tests/web_server/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
WikiTopic)
from ouranos.core.database.models.gaia import (
ActuatorRecord, ActuatorState, Ecosystem, Engine, EnvironmentParameter,
GaiaWarning, Hardware, HealthRecord, Lighting, SensorDataCache,
GaiaWarning, Hardware, HealthRecord, NycthemeralCycle, SensorDataCache,
SensorDataRecord)
from ouranos.core.database.models.system import (
System, SystemDataCache, SystemDataRecord)
Expand Down Expand Up @@ -49,7 +49,7 @@ async def add_ecosystems(db: AsyncSQLAlchemyWrapper):
await EnvironmentParameter.create(
session, ecosystem_uid=uid, parameter=parameter, values=environment_parameter)

await Lighting.create(
await NycthemeralCycle.create(
session,
ecosystem_uid=uid,
values={
Expand Down
Loading