diff --git a/production_tests.sh b/production_tests.sh index 54493abf9737a9ffe1d33e24f46ff0b40be88f67..b0d7d83ecc39ced5bb9b65c8f57ea6d80bd92290 100755 --- a/production_tests.sh +++ b/production_tests.sh @@ -26,3 +26,5 @@ curl -X POST -H "Content-Type:application/json" -d '{"stationmeta_core": {"codes curl http://127.0.0.1:8000/timeseries/ curl http://127.0.0.1:8000/timeseries/2 curl -X POST -H "Content-Type:application/json" -d '{"timeseries": {"label": "CMA2", "order": 1, "access_rights": 0, "sampling_frequency": 0, "aggregation": 0, "data_start_date": "2003-09-07T15:30:00+02:00", "data_end_date": "2016-12-31T14:30:00+01:00", "measurement_method": "UV absorption", "sampling_height": 7.0, "date_added": "2020-05-15T15:30:00+02:00", "date_modified": "2020-05-16T09:30:00+02:00", "station_id": 2, "variable_id": 7, "additional_metadata":"{}"}}' http://127.0.0.1:8000/timeseries/ + +curl -X POST -H 'Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__' -F "file=@o3_CO002_2012_2017_v1-0.dat" "http://127.0.0.1:8000/data/" diff --git a/toardb/data/crud.py b/toardb/data/crud.py index 8158dcd82acd64e328b678ee35bde951eb2fb89d..be3c2a4430d24f7a3c4f0852c4cbc77fc5840129 100644 --- a/toardb/data/crud.py +++ b/toardb/data/crud.py @@ -24,7 +24,7 @@ def get_data(db: Session, timeseries_id: int): def get_data_by_datetime_and_timeseriesid(db: Session, datetime: dt.datetime, timeseries_id: int): - return db.query(models.Data).filter([models.Data.date_time== datetime, models.Data.timeseries_id == timeseries_id]).first() + return db.query(models.Data).filter([models.Data.datetime== datetime, models.Data.timeseries_id == timeseries_id]).first() def get_all_data(db: Session, skip : int = 0, limit: int = None): @@ -71,7 +71,7 @@ def create_data(db: Session, input_handle: UploadFile = File(...)): fake_cur = fake_conn.cursor() # I really want to return whether the command worked or not! (this issue is now still open) try: - fake_cur.copy_from(buf, 'data', sep=',', columns=('date_time','value','flags','timeseries_id')) + fake_cur.copy_from(buf, 'data', sep=',', columns=('datetime','value','flags','timeseries_id')) fake_conn.commit() message = "Data successfully inserted." except: diff --git a/toardb/data/models.py b/toardb/data/models.py index f933bd917dc5a8a62eaaf6bf7c54df4ef9b777c1..ee43e9097491edd5c947d6d1ba7b137741871d67 100644 --- a/toardb/data/models.py +++ b/toardb/data/models.py @@ -1,5 +1,5 @@ # coding: utf-8 -from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, text +from sqlalchemy import PrimaryKeyConstraint, Column, DateTime, Float, ForeignKey, Integer, text from sqlalchemy.orm import relationship from sqlalchemy.sql.sqltypes import NullType from sqlalchemy.dialects.postgresql import JSONB @@ -10,32 +10,29 @@ metadata = Base.metadata class Data(Base): - """ Table "public.data" - Column | Type | Collation | Nullable | Default - ---------------+--------------------------+-----------+----------+---------------------------------- - id | integer | | not null | nextval('data_id_seq'::regclass) - date_time | timestamp with time zone | | not null | - value | double precision | | not null | - flags | integer | | not null | - timeseries_id | integer | | not null | - Indexes: - "data_pkey" PRIMARY KEY, btree (id) - "data_timeseries_id_date_time_7daa6d17_uniq" UNIQUE CONSTRAINT, btree (timeseries_id, date_time) - "data_date_time_4b248149" btree (date_time) - "data_timeseries_id_a38c5a1a" btree (timeseries_id) - "data_timeseries_id_date_time_7daa6d17_idx" btree (timeseries_id, date_time) - Check constraints: - "data_flags_48c6b0cc_check" CHECK (flags >= 0) - Foreign-key constraints: - "data_timeseries_id_a38c5a1a_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) DEFERRABLE INITIALLY DEFERRED + """ Table "public.data" + Column | Type | Collation | Nullable | Default + ---------------+--------------------------+-----------+----------+--------- + datetime | timestamp with time zone | | not null | + value | double precision | | not null | + flags | integer | | not null | + timeseries_id | integer | | not null | + Indexes: + "data_pkey" PRIMARY KEY, btree (timeseries_id, datetime) + "data_datetime_idx" btree (datetime) + "data_timeseries_id_idx" btree (timeseries_id) + "data_value_idx" btree (value) + Check constraints: + "data_flags_check" CHECK (flags >= 0) + Foreign-key constraints: + "data_timeseries_id_fkey" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) """ - __tablename__ = 'data' + __table_args__ = ( + PrimaryKeyConstraint('timeseries_id', 'datetime'), + ) - id = Column(Integer, primary_key=True, server_default=text("nextval('data_id_seq'::regclass)")) - date_time = Column(DateTime(True), nullable=False, index=True) - value = Column(Float(53), nullable=False) + datetime = Column(DateTime(True), nullable=False, index=True) + value = Column(Float(53), nullable=False, index=True) flags = Column(Integer, nullable=False) - timeseries_id = Column(Integer, nullable=False) -# timeseries_id = Column(ForeignKey('timeseries.id', deferrable=True, initially='DEFERRED'), nullable=False, index=True) -# timeseries = relationship('Timeseries') + timeseries_id = Column(ForeignKey('timeseries.id', deferrable=True, initially='DEFERRED'), nullable=False, index=True) diff --git a/toardb/data/schemas.py b/toardb/data/schemas.py index 4fff1cdc5b2668922c7af8570835065e21765c00..d5f48c4fcfedd3526f29312bf0d200f086bc52f5 100644 --- a/toardb/data/schemas.py +++ b/toardb/data/schemas.py @@ -20,7 +20,6 @@ class DataCreate(DataBase): class Data(DataBase): - id: int class Config: orm_mode = True diff --git a/toardb_dump.sql b/toardb_dump.sql index cda5af7a24d48a0e4e109c8baa646b4142aac834..5a36595db4215f87f81d94228664d9df352b275b 100644 --- a/toardb_dump.sql +++ b/toardb_dump.sql @@ -207,38 +207,15 @@ ALTER SEQUENCE public.auth_user_id_seq OWNED BY public.auth_user.id; -- CREATE TABLE public.data ( - id integer NOT NULL, - date_time timestamp with time zone NOT NULL, - value double precision NOT NULL, - flags integer NOT NULL, - timeseries_id integer NOT NULL, - CONSTRAINT data_flags_48c6b0cc_check CHECK ((flags >= 0)) + datetime timestamp with time zone NOT NULL, + value double precision NOT NULL, + flags integer NOT NULL, + timeseries_id integer NOT NULL, + CONSTRAINT data_flags_check CHECK ((flags >= 0)) ); - ALTER TABLE public.data OWNER TO toaradmin; --- --- Name: data_id_seq; Type: SEQUENCE; Schema: public; Owner: toaradmin --- - -CREATE SEQUENCE public.data_id_seq - AS integer - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - -ALTER TABLE public.data_id_seq OWNER TO toaradmin; - --- --- Name: data_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: toaradmin --- - -ALTER SEQUENCE public.data_id_seq OWNED BY public.data.id; - -- -- Name: organisations; Type: TABLE; Schema: public; Owner: toaradmin -- @@ -867,12 +844,6 @@ ALTER SEQUENCE public.variables_id_seq OWNED BY public.variables.id; ALTER TABLE ONLY public.auth_user ALTER COLUMN id SET DEFAULT nextval('public.auth_user_id_seq'::regclass); --- --- Name: data id; Type: DEFAULT; Schema: public; Owner: toaradmin --- - -ALTER TABLE ONLY public.data ALTER COLUMN id SET DEFAULT nextval('public.data_id_seq'::regclass); - -- -- Name: organisations id; Type: DEFAULT; Schema: public; Owner: toaradmin -- @@ -984,67 +955,67 @@ COPY public.auth_user (id, password, last_login, is_superuser, username, first_n -- Data for Name: data; Type: TABLE DATA; Schema: public; Owner: toaradmin -- -COPY public.data (id, date_time, value, flags, timeseries_id) FROM stdin; -1 2012-12-15 11:00:00+01 33.3530000000000015 0 3 -2 2012-12-15 12:00:00+01 58.8589999999999947 0 3 -3 2012-12-15 13:00:00+01 66.7060000000000031 0 3 -4 2012-12-15 14:00:00+01 64.7439999999999998 0 3 -5 2012-12-15 15:00:00+01 72.5919999999999987 0 3 -6 2012-12-15 16:00:00+01 56.8969999999999985 0 3 -7 2012-12-15 17:00:00+01 41.2010000000000005 0 3 -8 2012-12-15 18:00:00+01 35.3149999999999977 0 3 -9 2012-12-15 19:00:00+01 37.277000000000001 0 3 -10 2012-12-15 20:00:00+01 31.3909999999999982 0 3 -11 2012-12-15 21:00:00+01 23.5430000000000028 0 3 -12 2012-12-15 22:00:00+01 7.84799999999999986 0 3 -13 2012-12-15 23:00:00+01 1.96199999999999997 0 3 -14 2012-12-16 09:00:00+01 3.92399999999999993 0 3 -15 2012-12-16 10:00:00+01 9.8100000000000005 0 3 -16 2012-12-16 11:00:00+01 31.3909999999999982 0 3 -17 2012-12-16 12:00:00+01 52.972999999999999 0 3 -18 2012-12-16 13:00:00+01 66.7060000000000031 0 3 -19 2012-12-16 14:00:00+01 74.554000000000002 0 3 -20 2012-12-16 15:00:00+01 74.554000000000002 0 3 -21 2012-12-16 16:00:00+01 66.7060000000000031 0 3 -22 2012-12-16 17:00:00+01 51.0110000000000028 0 3 -23 2012-12-16 18:00:00+01 41.2010000000000005 0 3 -24 2012-12-16 19:00:00+01 37.277000000000001 0 3 -25 2012-12-16 20:00:00+01 31.3909999999999982 0 3 -26 2012-12-16 21:00:00+01 21.5809999999999995 0 3 -27 2012-12-16 22:00:00+01 13.7340000000000018 0 3 -28 2012-12-16 23:00:00+01 13.7340000000000018 0 3 -29 2012-12-17 00:00:00+01 7.84799999999999986 0 3 -30 2012-12-17 01:00:00+01 15.6959999999999997 0 3 -31 2012-12-17 02:00:00+01 11.7720000000000002 0 2 -32 2012-12-17 03:00:00+01 13.7340000000000018 0 2 -33 2012-12-17 04:00:00+01 19.620000000000001 0 2 -34 2012-12-17 05:00:00+01 15.6959999999999997 0 2 -35 2012-12-17 06:00:00+01 5.88600000000000012 0 2 -36 2012-12-17 07:00:00+01 3.92399999999999993 0 2 -37 2012-12-17 09:00:00+01 1.96199999999999997 0 2 -38 2012-12-17 10:00:00+01 23.5430000000000028 0 2 -39 2012-12-17 11:00:00+01 41.2010000000000005 0 2 -40 2012-12-17 12:00:00+01 43.1630000000000038 0 2 -41 2012-12-17 13:00:00+01 66.7060000000000031 0 2 -42 2012-12-17 14:00:00+01 78.4780000000000086 0 2 -43 2012-12-17 15:00:00+01 64.7439999999999998 0 2 -44 2012-12-17 16:00:00+01 45.1250000000000 0 2 -45 2012-12-17 17:00:00+01 41.2010000000000005 0 2 -46 2012-12-17 18:00:00+01 37.277000000000001 0 2 -47 2012-12-17 19:00:00+01 37.277000000000001 0 2 -48 2012-12-17 20:00:00+01 21.5809999999999995 0 2 -49 2012-12-17 21:00:00+01 3.92399999999999993 0 2 -50 2012-12-17 22:00:00+01 3.92399999999999993 0 2 -51 2012-12-17 23:00:00+01 1.96199999999999997 0 2 -52 2012-12-18 00:00:00+01 5.88600000000000012 0 2 -53 2012-12-18 01:00:00+01 9.8100000000000005 0 2 -54 2012-12-18 02:00:00+01 13.7340000000000018 0 2 -55 2012-12-18 03:00:00+01 7.84799999999999986 0 2 -56 2012-12-18 04:00:00+01 13.7340000000000018 0 2 -57 2012-12-18 05:00:00+01 23.5430000000000028 0 2 -58 2012-12-18 06:00:00+01 5.88600000000000012 0 2 -59 2012-12-18 07:00:00+01 3.92399999999999993 0 2 -60 2012-12-18 08:00:00+01 3.92399999999999993 0 2 +COPY public.data (datetime, value, flags, timeseries_id) FROM stdin; +2012-12-15 11:00:00+01 33.3530000000000015 0 3 +2012-12-15 12:00:00+01 58.8589999999999947 0 3 +2012-12-15 13:00:00+01 66.7060000000000031 0 3 +2012-12-15 14:00:00+01 64.7439999999999998 0 3 +2012-12-15 15:00:00+01 72.5919999999999987 0 3 +2012-12-15 16:00:00+01 56.8969999999999985 0 3 +2012-12-15 17:00:00+01 41.2010000000000005 0 3 +2012-12-15 18:00:00+01 35.3149999999999977 0 3 +2012-12-15 19:00:00+01 37.277000000000001 0 3 +2012-12-15 20:00:00+01 31.3909999999999982 0 3 +2012-12-15 21:00:00+01 23.5430000000000028 0 3 +2012-12-15 22:00:00+01 7.84799999999999986 0 3 +2012-12-15 23:00:00+01 1.96199999999999997 0 3 +2012-12-16 09:00:00+01 3.92399999999999993 0 3 +2012-12-16 10:00:00+01 9.8100000000000005 0 3 +2012-12-16 11:00:00+01 31.3909999999999982 0 3 +2012-12-16 12:00:00+01 52.972999999999999 0 3 +2012-12-16 13:00:00+01 66.7060000000000031 0 3 +2012-12-16 14:00:00+01 74.554000000000002 0 3 +2012-12-16 15:00:00+01 74.554000000000002 0 3 +2012-12-16 16:00:00+01 66.7060000000000031 0 3 +2012-12-16 17:00:00+01 51.0110000000000028 0 3 +2012-12-16 18:00:00+01 41.2010000000000005 0 3 +2012-12-16 19:00:00+01 37.277000000000001 0 3 +2012-12-16 20:00:00+01 31.3909999999999982 0 3 +2012-12-16 21:00:00+01 21.5809999999999995 0 3 +2012-12-16 22:00:00+01 13.7340000000000018 0 3 +2012-12-16 23:00:00+01 13.7340000000000018 0 3 +2012-12-17 00:00:00+01 7.84799999999999986 0 3 +2012-12-17 01:00:00+01 15.6959999999999997 0 3 +2012-12-17 02:00:00+01 11.7720000000000002 0 2 +2012-12-17 03:00:00+01 13.7340000000000018 0 2 +2012-12-17 04:00:00+01 19.620000000000001 0 2 +2012-12-17 05:00:00+01 15.6959999999999997 0 2 +2012-12-17 06:00:00+01 5.88600000000000012 0 2 +2012-12-17 07:00:00+01 3.92399999999999993 0 2 +2012-12-17 09:00:00+01 1.96199999999999997 0 2 +2012-12-17 10:00:00+01 23.5430000000000028 0 2 +2012-12-17 11:00:00+01 41.2010000000000005 0 2 +2012-12-17 12:00:00+01 43.1630000000000038 0 2 +2012-12-17 13:00:00+01 66.7060000000000031 0 2 +2012-12-17 14:00:00+01 78.4780000000000086 0 2 +2012-12-17 15:00:00+01 64.7439999999999998 0 2 +2012-12-17 16:00:00+01 45.1250000000000 0 2 +2012-12-17 17:00:00+01 41.2010000000000005 0 2 +2012-12-17 18:00:00+01 37.277000000000001 0 2 +2012-12-17 19:00:00+01 37.277000000000001 0 2 +2012-12-17 20:00:00+01 21.5809999999999995 0 2 +2012-12-17 21:00:00+01 3.92399999999999993 0 2 +2012-12-17 22:00:00+01 3.92399999999999993 0 2 +2012-12-17 23:00:00+01 1.96199999999999997 0 2 +2012-12-18 00:00:00+01 5.88600000000000012 0 2 +2012-12-18 01:00:00+01 9.8100000000000005 0 2 +2012-12-18 02:00:00+01 13.7340000000000018 0 2 +2012-12-18 03:00:00+01 7.84799999999999986 0 2 +2012-12-18 04:00:00+01 13.7340000000000018 0 2 +2012-12-18 05:00:00+01 23.5430000000000028 0 2 +2012-12-18 06:00:00+01 5.88600000000000012 0 2 +2012-12-18 07:00:00+01 3.92399999999999993 0 2 +2012-12-18 08:00:00+01 3.92399999999999993 0 2 \. -- @@ -1293,12 +1264,6 @@ COPY topology.layer (topology_id, layer_id, schema_name, table_name, feature_col SELECT pg_catalog.setval('public.auth_user_id_seq', 2, true); --- --- Name: data_id_seq; Type: SEQUENCE SET; Schema: public; Owner: toaradmin --- - -SELECT pg_catalog.setval('public.data_id_seq', 61, true); - -- -- Name: organisations_id_seq; Type: SEQUENCE SET; Schema: public; Owner: toaradmin -- @@ -1421,15 +1386,14 @@ ALTER TABLE ONLY public.auth_user -- ALTER TABLE ONLY public.data - ADD CONSTRAINT data_pkey PRIMARY KEY (id); - + ADD CONSTRAINT data_pkey PRIMARY KEY (timeseries_id, datetime); -- --- Name: data data_timeseries_id_date_time_7daa6d17_uniq; Type: CONSTRAINT; Schema: public; Owner: toaradmin +-- Name: data data_timeseries_id_datetime_7daa6d17_uniq; Type: CONSTRAINT; Schema: public; Owner: toaradmin -- ALTER TABLE ONLY public.data - ADD CONSTRAINT data_timeseries_id_date_time_7daa6d17_uniq UNIQUE (timeseries_id, date_time); + ADD CONSTRAINT data_timeseries_id_datetime_7daa6d17_uniq UNIQUE (timeseries_id, datetime); -- -- Name: organisations organisations_pkey; Type: CONSTRAINT; Schema: public; Owner: toaradmin @@ -1605,10 +1569,10 @@ ALTER TABLE ONLY public.variables CREATE INDEX auth_user_username_6821ab7c_like ON public.auth_user USING btree (username varchar_pattern_ops); -- --- Name: data_date_time_4b248149; Type: INDEX; Schema: public; Owner: toaradmin +-- Name: data_datetime_4b248149; Type: INDEX; Schema: public; Owner: toaradmin -- -CREATE INDEX data_date_time_4b248149 ON public.data USING btree (date_time); +CREATE INDEX data_datetime_4b248149 ON public.data USING btree (datetime); -- @@ -1619,10 +1583,11 @@ CREATE INDEX data_timeseries_id_a38c5a1a ON public.data USING btree (timeseries_ -- --- Name: data_timeseries_id_date_time_7daa6d17_idx; Type: INDEX; Schema: public; Owner: toaradmin +-- Name: data_value_idx; Type: INDEX; Schema: public; Owner: toaradmin -- -CREATE INDEX data_timeseries_id_date_time_7daa6d17_idx ON public.data USING btree (timeseries_id, date_time); +CREATE INDEX data_value_idx ON public.data USING btree (value); + -- -- Name: stationmeta_roles_person_id_3bd9c160; Type: INDEX; Schema: public; Owner: toaradmin