From ccd425882dacb0a6ffc467543eb674d16d2ef99e Mon Sep 17 00:00:00 2001 From: Vishmayraj Date: Mon, 23 Mar 2026 19:05:07 +0530 Subject: [PATCH] feat(thing): expose phenomenonTime on Thing entity --- api/app/models/thing.py | 2 ++ api/app/sta2rest/sta2rest.py | 2 ++ database/istsos_schema.sql | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/api/app/models/thing.py b/api/app/models/thing.py index bb746c76..e0f81659 100644 --- a/api/app/models/thing.py +++ b/api/app/models/thing.py @@ -14,6 +14,7 @@ from app.db.sqlalchemy_db import SCHEMA_NAME, Base from sqlalchemy.dialects.postgresql.json import JSON +from sqlalchemy.dialects.postgresql.ranges import TSTZRANGE from sqlalchemy.orm import relationship from sqlalchemy.sql.schema import Column, ForeignKey from sqlalchemy.sql.sqltypes import Integer, String, Text @@ -36,6 +37,7 @@ class Thing(Base): name = Column(String(255), nullable=False) description = Column(Text, nullable=False) properties = Column(JSON) + phenomenon_time = Column("phenomenonTime", TSTZRANGE) commit_id = Column(Integer, ForeignKey(f"{SCHEMA_NAME}.Commit.id")) location = relationship( "Location", secondary=Thing_Location, back_populates="thing" diff --git a/api/app/sta2rest/sta2rest.py b/api/app/sta2rest/sta2rest.py index 69029c94..0d2d5bde 100644 --- a/api/app/sta2rest/sta2rest.py +++ b/api/app/sta2rest/sta2rest.py @@ -128,6 +128,7 @@ class STA2REST: "name", "description", "properties", + "phenomenon_time", ], "ThingTravelTime": [ "id", @@ -138,6 +139,7 @@ class STA2REST: "name", "description", "properties", + "phenomenon_time", "system_time_validity", ], "HistoricalLocation": [ diff --git a/database/istsos_schema.sql b/database/istsos_schema.sql index e69627f8..814e1c3d 100644 --- a/database/istsos_schema.sql +++ b/database/istsos_schema.sql @@ -72,6 +72,21 @@ CREATE OR REPLACE FUNCTION "Datastreams@iot.navigationLink"(sensorthings."Thing" SELECT '/Things(' || $1.id || ')/Datastreams'; $$ LANGUAGE SQL; +CREATE OR REPLACE FUNCTION "phenomenonTime"(sensorthings."Thing") RETURNS tstzrange AS $$ +BEGIN + RETURN ( + SELECT tstzrange( + min(lower(d."phenomenonTime")), + max(upper(d."phenomenonTime")), + '[]' + ) + FROM sensorthings."Datastream" d + WHERE d."thing_id" = $1.id + AND d."phenomenonTime" IS NOT NULL + ); +END; +$$ LANGUAGE plpgsql STABLE; + CREATE TABLE IF NOT EXISTS sensorthings."Thing_Location" ( "thing_id" BIGINT NOT NULL REFERENCES sensorthings."Thing"(id) ON DELETE CASCADE, "location_id" BIGINT NOT NULL REFERENCES sensorthings."Location"(id) ON DELETE CASCADE,