Skip to content

Commit 845d152

Browse files
Changed docker images for db and dummy data
1 parent 61226d0 commit 845d152

File tree

5 files changed

+92
-91
lines changed

5 files changed

+92
-91
lines changed

database/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM timescale/timescaledb-ha:pg16.1-ts2.13.1-all
1+
FROM postgis/postgis:16-3.4-alpine
22
COPY ./istsos_schema.sql /docker-entrypoint-initdb.d/11_init_db_istsos_schema.sql
33
COPY ./istsos_schema_versioning.sql /docker-entrypoint-initdb.d/12_init_db_istsos_schema_versioning.sql

database/istsos_schema.sql

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
--CREATE EXTENSION IF NOT exists pg_graphql;
21
CREATE EXTENSION IF NOT exists postgis;
3-
CREATE EXTENSION IF NOT exists unit;
4-
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
52
CREATE EXTENSION IF NOT EXISTS btree_gist;
6-
--CREATE EXTENSION IF NOT exists uri;
73

84
CREATE SCHEMA sensorthings;
95

dummy_data/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Use an official Python runtime as a parent image
2-
FROM python:3.10
2+
FROM python:3.12.4-alpine3.20
33

44
# Set the working directory to /app
55
WORKDIR /dummy_data

dummy_data/generator.py

+88-82
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import os
33
import random
44
from datetime import datetime, time
5+
import asyncio
56

7+
import asyncpg
68
import isodate
7-
import psycopg2
89

910
create_dummy_data = int(os.getenv("DUMMY_DATA"))
1011
delete_dummy_data = int(os.getenv("CLEAR_DATA"))
@@ -18,14 +19,25 @@
1819
else datetime.combine(datetime.now().today(), time.min)
1920
)
2021

21-
connection_url = "dbname={db} user={user} password={password} host='database' port='5432'".format(
22-
db=os.getenv("POSTGRES_DB"),
23-
user=os.getenv("POSTGRES_USER"),
24-
password=os.getenv("POSTGRES_PASSWORD"),
25-
)
22+
pgpool = None
23+
24+
async def get_pool():
25+
"""
26+
Retrieves or creates a connection pool to the PostgreSQL database.
27+
28+
Returns:
29+
asyncpg.pool.Pool: The connection pool object.
30+
"""
31+
global pgpool
32+
if not pgpool:
33+
pgpool = await asyncpg.create_pool(
34+
dsn=f"postgresql://{os.getenv("POSTGRES_USER")}:{os.getenv("POSTGRES_PASSWORD")}@database:5432/{os.getenv("POSTGRES_DB")}"
35+
)
36+
return pgpool
37+
2638

2739

28-
def generate_things(cur):
40+
async def generate_things(conn):
2941
"""
3042
Generate a list of things and insert them into the database.
3143
@@ -44,12 +56,12 @@ def generate_things(cur):
4456

4557
insert_sql = """
4658
INSERT INTO sensorthings."Thing" (description, name, properties)
47-
VALUES (%s, %s, %s)
59+
VALUES ($1, $2, $3)
4860
"""
49-
cur.executemany(insert_sql, things)
61+
await conn.executemany(insert_sql, things)
5062

5163

52-
def generate_locations(cur):
64+
async def generate_locations(conn):
5365
"""
5466
Generate locations and insert them into the database.
5567
@@ -69,12 +81,12 @@ def generate_locations(cur):
6981

7082
insert_sql = """
7183
INSERT INTO sensorthings."Location" (description, name, location, "encodingType")
72-
VALUES (%s, %s, %s, %s)
84+
VALUES ($1, $2, $3, $4)
7385
"""
74-
cur.executemany(insert_sql, locations)
86+
await conn.executemany(insert_sql, locations)
7587

7688

77-
def generate_things_locations(cur):
89+
async def generate_things_locations(conn):
7890
"""
7991
Generate and insert things locations into the database.
8092
@@ -92,12 +104,12 @@ def generate_things_locations(cur):
92104

93105
insert_sql = """
94106
INSERT INTO sensorthings."Thing_Location" (thing_id, location_id)
95-
VALUES (%s, %s)
107+
VALUES ($1, $2)
96108
"""
97-
cur.executemany(insert_sql, things_locations)
109+
await conn.executemany(insert_sql, things_locations)
98110

99111

100-
def generate_historicallocations(cur):
112+
async def generate_historicallocations(conn):
101113
"""
102114
Generate historical locations for things and insert them into the database.
103115
@@ -116,12 +128,12 @@ def generate_historicallocations(cur):
116128

117129
insert_sql = """
118130
INSERT INTO sensorthings."HistoricalLocation" (time, thing_id)
119-
VALUES (%s, %s)
131+
VALUES ($1, $2)
120132
"""
121-
cur.executemany(insert_sql, historicallocations)
133+
await conn.executemany(insert_sql, historicallocations)
122134

123135

124-
def generate_locations_historicallocations(cur):
136+
async def generate_locations_historicallocations(conn):
125137
"""
126138
Generate a list of tuples representing the relationship between location and historical location.
127139
@@ -141,12 +153,12 @@ def generate_locations_historicallocations(cur):
141153

142154
insert_sql = """
143155
INSERT INTO sensorthings."Location_HistoricalLocation" (location_id, historicallocation_id)
144-
VALUES (%s, %s)
156+
VALUES ($1, $2)
145157
"""
146-
cur.executemany(insert_sql, locations_historicallocations)
158+
await conn.executemany(insert_sql, locations_historicallocations)
147159

148160

149-
def generate_observedProperties(cur):
161+
async def generate_observedProperties(conn):
150162
"""
151163
Generate observed properties and insert them into the database.
152164
@@ -165,12 +177,12 @@ def generate_observedProperties(cur):
165177

166178
insert_sql = """
167179
INSERT INTO sensorthings."ObservedProperty" (name, definition, description)
168-
VALUES (%s, %s, %s)
180+
VALUES ($1, $2, $3)
169181
"""
170-
cur.executemany(insert_sql, observedProperties)
182+
await conn.executemany(insert_sql, observedProperties)
171183

172184

173-
def generate_sensors(cur):
185+
async def generate_sensors(conn):
174186
"""
175187
Generate a list of sensors with random descriptions, names, encoding types, and metadata.
176188
@@ -190,12 +202,12 @@ def generate_sensors(cur):
190202

191203
insert_sql = """
192204
INSERT INTO sensorthings."Sensor" (description, name, "encodingType", metadata)
193-
VALUES (%s, %s, %s, %s)
205+
VALUES ($1, $2, $3, $4)
194206
"""
195-
cur.executemany(insert_sql, sensors)
207+
await conn.executemany(insert_sql, sensors)
196208

197209

198-
def generate_datastreams(cur):
210+
async def generate_datastreams(conn):
199211
"""
200212
Generate datastreams and insert them into the database.
201213
@@ -237,12 +249,12 @@ def generate_datastreams(cur):
237249

238250
insert_sql = """
239251
INSERT INTO sensorthings."Datastream" ("unitOfMeasurement", description, name, "observationType", thing_id, sensor_id, observedproperty_id)
240-
VALUES (%s, %s, %s, %s, %s, %s, %s)
252+
VALUES ($1, $2, $3, $4, $5, $6, $7)
241253
"""
242-
cur.executemany(insert_sql, datastreams)
254+
await conn.executemany(insert_sql, datastreams)
243255

244256

245-
def generate_featuresofinterest(cur):
257+
async def generate_featuresofinterest(conn):
246258
"""
247259
Generate features of interest and insert them into the database.
248260
@@ -262,12 +274,12 @@ def generate_featuresofinterest(cur):
262274

263275
insert_sql = """
264276
INSERT INTO sensorthings."FeaturesOfInterest" (description, name, "encodingType", feature)
265-
VALUES (%s, %s, %s, %s)
277+
VALUES ($1, $2, $3, $4)
266278
"""
267-
cur.executemany(insert_sql, featuresofinterest)
279+
await conn.executemany(insert_sql, featuresofinterest)
268280

269281

270-
def generate_observations(cur):
282+
async def generate_observations(conn):
271283
"""
272284
Generates observations and inserts them into the database.
273285
@@ -298,12 +310,12 @@ def generate_observations(cur):
298310

299311
insert_sql = """
300312
INSERT INTO sensorthings."Observation" ("phenomenonTime", "resultInteger", "resultType", datastream_id, featuresofinterest_id)
301-
VALUES (%s, %s, %s, %s, %s)
313+
VALUES ($1, $2, $3, $4, $5)
302314
"""
303-
cur.executemany(insert_sql, observations)
315+
await conn.executemany(insert_sql, observations)
304316

305317

306-
def create_data():
318+
async def create_data():
307319
"""
308320
Generates dummy data and inserts it into the database.
309321
@@ -314,29 +326,25 @@ def create_data():
314326
315327
After the creation is complete, the database connection is closed.
316328
"""
317-
conn = psycopg2.connect(connection_url)
318-
cur = conn.cursor()
319-
try:
320-
generate_things(cur)
321-
generate_locations(cur)
322-
generate_things_locations(cur)
323-
generate_historicallocations(cur)
324-
generate_locations_historicallocations(cur)
325-
generate_observedProperties(cur)
326-
generate_sensors(cur)
327-
generate_datastreams(cur)
328-
generate_featuresofinterest(cur)
329-
generate_observations(cur)
330-
conn.commit()
331-
except Exception as e:
332-
conn.rollback()
333-
print(f"An error occurred: {e}")
334-
finally:
335-
cur.close()
336-
conn.close()
337-
338-
339-
def delete_data():
329+
pool = await get_pool()
330+
async with pool.acquire() as conn:
331+
async with conn.transaction():
332+
try:
333+
await generate_things(conn)
334+
await generate_locations(conn)
335+
await generate_things_locations(conn)
336+
await generate_historicallocations(conn)
337+
await generate_locations_historicallocations(conn)
338+
await generate_observedProperties(conn)
339+
await generate_sensors(conn)
340+
await generate_datastreams(conn)
341+
await generate_featuresofinterest(conn)
342+
await generate_observations(conn)
343+
except Exception as e:
344+
print(f"An error occured: {e}")
345+
346+
347+
async def delete_data():
340348
"""
341349
Deletes all data from the sensorthings tables in the database.
342350
@@ -358,30 +366,28 @@ def delete_data():
358366
359367
After the deletion is complete, the database connection is closed.
360368
"""
361-
conn = psycopg2.connect(connection_url)
362-
cur = conn.cursor()
363-
try:
364-
cur.execute('DELETE FROM sensorthings."Thing"')
365-
cur.execute('DELETE FROM sensorthings."Location"')
366-
cur.execute('DELETE FROM sensorthings."Thing_Location"')
367-
cur.execute('DELETE FROM sensorthings."HistoricalLocation"')
368-
cur.execute('DELETE FROM sensorthings."Location_HistoricalLocation"')
369-
cur.execute('DELETE FROM sensorthings."ObservedProperty"')
370-
cur.execute('DELETE FROM sensorthings."Sensor"')
371-
cur.execute('DELETE FROM sensorthings."Datastream"')
372-
cur.execute('DELETE FROM sensorthings."FeaturesOfInterest"')
373-
cur.execute('DELETE FROM sensorthings."Observation"')
374-
conn.commit()
375-
except Exception as e:
376-
conn.rollback()
377-
print(f"An error occurred: {e}")
378-
finally:
379-
cur.close()
380-
conn.close()
369+
pool = await get_pool()
370+
async with pool.acquire() as conn:
371+
async with conn.transaction():
372+
try:
373+
await conn.execute('DELETE FROM sensorthings."Thing"')
374+
await conn.execute('DELETE FROM sensorthings."Location"')
375+
await conn.execute('DELETE FROM sensorthings."Thing_Location"')
376+
await conn.execute('DELETE FROM sensorthings."HistoricalLocation"')
377+
await conn.execute('DELETE FROM sensorthings."Location_HistoricalLocation"')
378+
await conn.execute('DELETE FROM sensorthings."ObservedProperty"')
379+
await conn.execute('DELETE FROM sensorthings."Sensor"')
380+
await conn.execute('DELETE FROM sensorthings."Datastream"')
381+
await conn.execute('DELETE FROM sensorthings."FeaturesOfInterest"')
382+
await conn.execute('DELETE FROM sensorthings."Observation"')
383+
except Exception as e:
384+
print(f"An error occurred: {e}")
385+
finally:
386+
await conn.close()
381387

382388

383389
if __name__ == "__main__":
384390
if delete_dummy_data:
385-
delete_data()
391+
asyncio.run(delete_data())
386392
if create_dummy_data:
387-
create_data()
393+
asyncio.run(create_data())

dummy_data/requirements.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
psycopg2
2-
PyYAML
31
python-dotenv
4-
isodate
2+
isodate
3+
asyncpg

0 commit comments

Comments
 (0)