ngits-iot-db 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2025, NG IT Services Sp. z o. o.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,13 @@
1
+ include README.md
2
+ include LICENSE
3
+ include pyproject.toml
4
+ include requirements.txt
5
+ include alembic.ini
6
+ exclude CLAUDE.md
7
+ recursive-include alembic *.py *.mako
8
+ recursive-include models *.py
9
+ recursive-include alembic/versions *.py
10
+ global-exclude *.pyc
11
+ global-exclude __pycache__
12
+ global-exclude .git*
13
+ global-exclude .DS_Store
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: ngits-iot-db
3
+ Version: 0.1.0
4
+ Summary: SQLAlchemy models for IoT sensor data storage with Alembic migrations
5
+ Author-email: "NG IT Services Sp. z o. o." <biuro@ngits.pl>
6
+ Keywords: iot,sqlalchemy,database,models,sensors
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: License :: OSI Approved :: BSD License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Topic :: Database
15
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: SQLAlchemy>=2.0.0
20
+ Requires-Dist: alembic>=1.13.0
21
+ Requires-Dist: psycopg2-binary>=2.9.0
22
+ Provides-Extra: dev
23
+ Requires-Dist: black>=25.1.0; extra == "dev"
24
+ Requires-Dist: isort>=5.13.0; extra == "dev"
25
+ Requires-Dist: mypy>=1.0.0; extra == "dev"
26
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
27
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
28
+ Dynamic: license-file
29
+
30
+ database uri
31
+ ============
32
+
33
+ You can set database uri by setting `IOT_DATABASE_URL` variable.
34
+
35
+ ```shell
36
+ export IOT_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/iot_local
37
+ ```
38
+
39
+
40
+ make migrations
41
+ ===============
42
+
43
+ ```shell
44
+ alembic revision --autogenerate -m "Migration message."
45
+ ```
46
+
47
+ upgrade
48
+ =======
49
+
50
+ ```shell
51
+ # upgrade all
52
+ alembic upgrade head
53
+
54
+ # upgrade all to specific migration
55
+ alembic upgrade <revision_id>
56
+ ```
57
+
58
+ downgrade
59
+ =========
60
+
61
+ ```shell
62
+ # downgrade all
63
+ alembic downgrade base
64
+
65
+ # downgrade all to specific migration
66
+ alembic downgrade <revision_id>
67
+ ```
@@ -0,0 +1,38 @@
1
+ database uri
2
+ ============
3
+
4
+ You can set database uri by setting `IOT_DATABASE_URL` variable.
5
+
6
+ ```shell
7
+ export IOT_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/iot_local
8
+ ```
9
+
10
+
11
+ make migrations
12
+ ===============
13
+
14
+ ```shell
15
+ alembic revision --autogenerate -m "Migration message."
16
+ ```
17
+
18
+ upgrade
19
+ =======
20
+
21
+ ```shell
22
+ # upgrade all
23
+ alembic upgrade head
24
+
25
+ # upgrade all to specific migration
26
+ alembic upgrade <revision_id>
27
+ ```
28
+
29
+ downgrade
30
+ =========
31
+
32
+ ```shell
33
+ # downgrade all
34
+ alembic downgrade base
35
+
36
+ # downgrade all to specific migration
37
+ alembic downgrade <revision_id>
38
+ ```
@@ -0,0 +1,83 @@
1
+ import os
2
+ from logging.config import fileConfig
3
+
4
+ from sqlalchemy import engine_from_config, pool
5
+
6
+ from alembic import context
7
+
8
+ # this is the Alembic Config object, which provides
9
+ # access to the values within the .ini file in use.
10
+ config = context.config
11
+
12
+ # Interpret the config file for Python logging.
13
+ # This line sets up loggers basically.
14
+ if config.config_file_name is not None:
15
+ fileConfig(config.config_file_name)
16
+
17
+
18
+ database_url = os.getenv("IOT_DATABASE_URL")
19
+ if database_url is not None:
20
+ config.set_main_option("sqlalchemy.url", database_url)
21
+
22
+ # add your model's MetaData object here
23
+ # for 'autogenerate' support
24
+ # from myapp import mymodel
25
+ # target_metadata = mymodel.Base.metadata
26
+ from models.base import Base
27
+
28
+ target_metadata = Base.metadata
29
+
30
+ # other values from the config, defined by the needs of env.py,
31
+ # can be acquired:
32
+ # my_important_option = config.get_main_option("my_important_option")
33
+ # ... etc.
34
+
35
+
36
+ def run_migrations_offline() -> None:
37
+ """Run migrations in 'offline' mode.
38
+
39
+ This configures the context with just a URL
40
+ and not an Engine, though an Engine is acceptable
41
+ here as well. By skipping the Engine creation
42
+ we don't even need a DBAPI to be available.
43
+
44
+ Calls to context.execute() here emit the given string to the
45
+ script output.
46
+
47
+ """
48
+ url = config.get_main_option("sqlalchemy.url")
49
+ context.configure(
50
+ url=url,
51
+ target_metadata=target_metadata,
52
+ literal_binds=True,
53
+ dialect_opts={"paramstyle": "named"},
54
+ )
55
+
56
+ with context.begin_transaction():
57
+ context.run_migrations()
58
+
59
+
60
+ def run_migrations_online() -> None:
61
+ """Run migrations in 'online' mode.
62
+
63
+ In this scenario we need to create an Engine
64
+ and associate a connection with the context.
65
+
66
+ """
67
+ connectable = engine_from_config(
68
+ config.get_section(config.config_ini_section, {}),
69
+ prefix="sqlalchemy.",
70
+ poolclass=pool.NullPool,
71
+ )
72
+
73
+ with connectable.connect() as connection:
74
+ context.configure(connection=connection, target_metadata=target_metadata)
75
+
76
+ with context.begin_transaction():
77
+ context.run_migrations()
78
+
79
+
80
+ if context.is_offline_mode():
81
+ run_migrations_offline()
82
+ else:
83
+ run_migrations_online()
@@ -0,0 +1,26 @@
1
+ """${message}
2
+
3
+ Revision ID: ${up_revision}
4
+ Revises: ${down_revision | comma,n}
5
+ Create Date: ${create_date}
6
+
7
+ """
8
+ from typing import Sequence, Union
9
+
10
+ from alembic import op
11
+ import sqlalchemy as sa
12
+ ${imports if imports else ""}
13
+
14
+ # revision identifiers, used by Alembic.
15
+ revision: str = ${repr(up_revision)}
16
+ down_revision: Union[str, None] = ${repr(down_revision)}
17
+ branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
18
+ depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
19
+
20
+
21
+ def upgrade() -> None:
22
+ ${upgrades if upgrades else "pass"}
23
+
24
+
25
+ def downgrade() -> None:
26
+ ${downgrades if downgrades else "pass"}
@@ -0,0 +1,194 @@
1
+ """Init schema
2
+
3
+ Revision ID: 78371c256762
4
+ Revises:
5
+ Create Date: 2024-08-21 00:35:06.960907
6
+
7
+ """
8
+
9
+ from typing import Sequence, Union
10
+
11
+ import sqlalchemy as sa
12
+ from sqlalchemy.dialects import postgresql
13
+
14
+ from alembic import op
15
+
16
+ # revision identifiers, used by Alembic.
17
+ revision: str = "78371c256762"
18
+ down_revision: Union[str, None] = None
19
+ branch_labels: Union[str, Sequence[str], None] = None
20
+ depends_on: Union[str, Sequence[str], None] = None
21
+
22
+
23
+ def upgrade() -> None:
24
+ # ### commands auto generated by Alembic - please adjust! ###
25
+ op.create_table(
26
+ "measurement_electricity",
27
+ sa.Column("measured_ts", sa.DateTime(), nullable=True),
28
+ sa.Column("value", sa.Float(), nullable=True),
29
+ sa.Column("id", sa.Integer(), nullable=False),
30
+ sa.Column("tenant", sa.UUID(), nullable=True),
31
+ sa.Column("external_id", sa.Text(), nullable=True),
32
+ sa.Column("meter_id", sa.Text(), nullable=True),
33
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
34
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
35
+ sa.PrimaryKeyConstraint("id"),
36
+ sa.UniqueConstraint("tenant", "meter_id", "measured_ts"),
37
+ )
38
+ op.create_table(
39
+ "measurement_heat",
40
+ sa.Column("measured_ts", sa.DateTime(), nullable=True),
41
+ sa.Column("value", sa.Float(), nullable=True),
42
+ sa.Column("id", sa.Integer(), nullable=False),
43
+ sa.Column("tenant", sa.UUID(), nullable=True),
44
+ sa.Column("external_id", sa.Text(), nullable=True),
45
+ sa.Column("meter_id", sa.Text(), nullable=True),
46
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
47
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
48
+ sa.PrimaryKeyConstraint("id"),
49
+ sa.UniqueConstraint("tenant", "meter_id", "measured_ts"),
50
+ )
51
+ op.create_table(
52
+ "measurement_raw",
53
+ sa.Column("data", postgresql.JSON(astext_type=sa.Text()), nullable=True),
54
+ sa.Column("type", sa.SmallInteger(), nullable=True),
55
+ sa.Column("is_processed", sa.Boolean(), nullable=True),
56
+ sa.Column("id", sa.Integer(), nullable=False),
57
+ sa.Column("tenant", sa.UUID(), nullable=True),
58
+ sa.Column("external_id", sa.Text(), nullable=True),
59
+ sa.Column("meter_id", sa.Text(), nullable=True),
60
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
61
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
62
+ sa.PrimaryKeyConstraint("id"),
63
+ )
64
+ op.create_table(
65
+ "measurement_water",
66
+ sa.Column("measured_ts", sa.DateTime(), nullable=True),
67
+ sa.Column("value", sa.Float(), nullable=True),
68
+ sa.Column("id", sa.Integer(), nullable=False),
69
+ sa.Column("tenant", sa.UUID(), nullable=True),
70
+ sa.Column("external_id", sa.Text(), nullable=True),
71
+ sa.Column("meter_id", sa.Text(), nullable=True),
72
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
73
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
74
+ sa.PrimaryKeyConstraint("id"),
75
+ sa.UniqueConstraint("tenant", "meter_id", "measured_ts"),
76
+ )
77
+ op.create_table(
78
+ "usage_electricity_daily",
79
+ sa.Column("start_ts", sa.DateTime(), nullable=True),
80
+ sa.Column("end_ts", sa.DateTime(), nullable=True),
81
+ sa.Column("consumption_ts", sa.DateTime(), nullable=True),
82
+ sa.Column("start_value", sa.Float(), nullable=True),
83
+ sa.Column("end_value", sa.Float(), nullable=True),
84
+ sa.Column("consumption", sa.Float(), nullable=True),
85
+ sa.Column("id", sa.Integer(), nullable=False),
86
+ sa.Column("tenant", sa.UUID(), nullable=True),
87
+ sa.Column("external_id", sa.Text(), nullable=True),
88
+ sa.Column("meter_id", sa.Text(), nullable=True),
89
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
90
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
91
+ sa.PrimaryKeyConstraint("id"),
92
+ sa.UniqueConstraint("tenant", "meter_id", "consumption_ts"),
93
+ )
94
+ op.create_table(
95
+ "usage_electricity_monthly",
96
+ sa.Column("start_ts", sa.DateTime(), nullable=True),
97
+ sa.Column("end_ts", sa.DateTime(), nullable=True),
98
+ sa.Column("consumption_ts", sa.DateTime(), nullable=True),
99
+ sa.Column("start_value", sa.Float(), nullable=True),
100
+ sa.Column("end_value", sa.Float(), nullable=True),
101
+ sa.Column("consumption", sa.Float(), nullable=True),
102
+ sa.Column("id", sa.Integer(), nullable=False),
103
+ sa.Column("tenant", sa.UUID(), nullable=True),
104
+ sa.Column("external_id", sa.Text(), nullable=True),
105
+ sa.Column("meter_id", sa.Text(), nullable=True),
106
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
107
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
108
+ sa.PrimaryKeyConstraint("id"),
109
+ sa.UniqueConstraint("tenant", "meter_id", "consumption_ts"),
110
+ )
111
+ op.create_table(
112
+ "usage_heat_daily",
113
+ sa.Column("start_ts", sa.DateTime(), nullable=True),
114
+ sa.Column("end_ts", sa.DateTime(), nullable=True),
115
+ sa.Column("consumption_ts", sa.DateTime(), nullable=True),
116
+ sa.Column("start_value", sa.Float(), nullable=True),
117
+ sa.Column("end_value", sa.Float(), nullable=True),
118
+ sa.Column("consumption", sa.Float(), nullable=True),
119
+ sa.Column("id", sa.Integer(), nullable=False),
120
+ sa.Column("tenant", sa.UUID(), nullable=True),
121
+ sa.Column("external_id", sa.Text(), nullable=True),
122
+ sa.Column("meter_id", sa.Text(), nullable=True),
123
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
124
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
125
+ sa.PrimaryKeyConstraint("id"),
126
+ sa.UniqueConstraint("tenant", "meter_id", "consumption_ts"),
127
+ )
128
+ op.create_table(
129
+ "usage_heat_monthly",
130
+ sa.Column("start_ts", sa.DateTime(), nullable=True),
131
+ sa.Column("end_ts", sa.DateTime(), nullable=True),
132
+ sa.Column("consumption_ts", sa.DateTime(), nullable=True),
133
+ sa.Column("start_value", sa.Float(), nullable=True),
134
+ sa.Column("end_value", sa.Float(), nullable=True),
135
+ sa.Column("consumption", sa.Float(), nullable=True),
136
+ sa.Column("id", sa.Integer(), nullable=False),
137
+ sa.Column("tenant", sa.UUID(), nullable=True),
138
+ sa.Column("external_id", sa.Text(), nullable=True),
139
+ sa.Column("meter_id", sa.Text(), nullable=True),
140
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
141
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
142
+ sa.PrimaryKeyConstraint("id"),
143
+ sa.UniqueConstraint("tenant", "meter_id", "consumption_ts"),
144
+ )
145
+ op.create_table(
146
+ "usage_water_daily",
147
+ sa.Column("start_ts", sa.DateTime(), nullable=True),
148
+ sa.Column("end_ts", sa.DateTime(), nullable=True),
149
+ sa.Column("consumption_ts", sa.DateTime(), nullable=True),
150
+ sa.Column("start_value", sa.Float(), nullable=True),
151
+ sa.Column("end_value", sa.Float(), nullable=True),
152
+ sa.Column("consumption", sa.Float(), nullable=True),
153
+ sa.Column("id", sa.Integer(), nullable=False),
154
+ sa.Column("tenant", sa.UUID(), nullable=True),
155
+ sa.Column("external_id", sa.Text(), nullable=True),
156
+ sa.Column("meter_id", sa.Text(), nullable=True),
157
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
158
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
159
+ sa.PrimaryKeyConstraint("id"),
160
+ sa.UniqueConstraint("tenant", "meter_id", "consumption_ts"),
161
+ )
162
+ op.create_table(
163
+ "usage_water_monthly",
164
+ sa.Column("start_ts", sa.DateTime(), nullable=True),
165
+ sa.Column("end_ts", sa.DateTime(), nullable=True),
166
+ sa.Column("consumption_ts", sa.DateTime(), nullable=True),
167
+ sa.Column("start_value", sa.Float(), nullable=True),
168
+ sa.Column("end_value", sa.Float(), nullable=True),
169
+ sa.Column("consumption", sa.Float(), nullable=True),
170
+ sa.Column("id", sa.Integer(), nullable=False),
171
+ sa.Column("tenant", sa.UUID(), nullable=True),
172
+ sa.Column("external_id", sa.Text(), nullable=True),
173
+ sa.Column("meter_id", sa.Text(), nullable=True),
174
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
175
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
176
+ sa.PrimaryKeyConstraint("id"),
177
+ sa.UniqueConstraint("tenant", "meter_id", "consumption_ts"),
178
+ )
179
+ # ### end Alembic commands ###
180
+
181
+
182
+ def downgrade() -> None:
183
+ # ### commands auto generated by Alembic - please adjust! ###
184
+ op.drop_table("usage_water_monthly")
185
+ op.drop_table("usage_water_daily")
186
+ op.drop_table("usage_heat_monthly")
187
+ op.drop_table("usage_heat_daily")
188
+ op.drop_table("usage_electricity_monthly")
189
+ op.drop_table("usage_electricity_daily")
190
+ op.drop_table("measurement_water")
191
+ op.drop_table("measurement_raw")
192
+ op.drop_table("measurement_heat")
193
+ op.drop_table("measurement_electricity")
194
+ # ### end Alembic commands ###
@@ -0,0 +1,43 @@
1
+ """Add temperature
2
+
3
+ Revision ID: fc391d505594
4
+ Revises: 78371c256762
5
+ Create Date: 2025-06-15 20:03:15.039980
6
+
7
+ """
8
+
9
+ from typing import Sequence, Union
10
+
11
+ import sqlalchemy as sa
12
+
13
+ from alembic import op
14
+
15
+ # revision identifiers, used by Alembic.
16
+ revision: str = "fc391d505594"
17
+ down_revision: Union[str, None] = "78371c256762"
18
+ branch_labels: Union[str, Sequence[str], None] = None
19
+ depends_on: Union[str, Sequence[str], None] = None
20
+
21
+
22
+ def upgrade() -> None:
23
+ # ### commands auto generated by Alembic - please adjust! ###
24
+ op.create_table(
25
+ "measurement_temperature",
26
+ sa.Column("measured_ts", sa.DateTime(), nullable=True),
27
+ sa.Column("value", sa.Float(), nullable=True),
28
+ sa.Column("id", sa.Integer(), nullable=False),
29
+ sa.Column("tenant", sa.UUID(), nullable=True),
30
+ sa.Column("external_id", sa.Text(), nullable=True),
31
+ sa.Column("meter_id", sa.Text(), nullable=True),
32
+ sa.Column("created_ts", sa.DateTime(), nullable=True),
33
+ sa.Column("updated_ts", sa.DateTime(), nullable=True),
34
+ sa.PrimaryKeyConstraint("id"),
35
+ sa.UniqueConstraint("tenant", "meter_id", "measured_ts"),
36
+ )
37
+ # ### end Alembic commands ###
38
+
39
+
40
+ def downgrade() -> None:
41
+ # ### commands auto generated by Alembic - please adjust! ###
42
+ op.drop_table("measurement_temperature")
43
+ # ### end Alembic commands ###
@@ -0,0 +1,116 @@
1
+ # A generic, single database configuration.
2
+
3
+ [alembic]
4
+ # path to migration scripts
5
+ # Use forward slashes (/) also on windows to provide an os agnostic path
6
+ script_location = alembic
7
+
8
+ # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
9
+ # Uncomment the line below if you want the files to be prepended with date and time
10
+ # see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
11
+ # for all available tokens
12
+ # file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13
+
14
+ # sys.path path, will be prepended to sys.path if present.
15
+ # defaults to the current working directory.
16
+ prepend_sys_path = .
17
+
18
+ # timezone to use when rendering the date within the migration file
19
+ # as well as the filename.
20
+ # If specified, requires the python>=3.9 or backports.zoneinfo library.
21
+ # Any required deps can installed by adding `alembic[tz]` to the pip requirements
22
+ # string value is passed to ZoneInfo()
23
+ # leave blank for localtime
24
+ # timezone =
25
+
26
+ # max length of characters to apply to the "slug" field
27
+ # truncate_slug_length = 40
28
+
29
+ # set to 'true' to run the environment during
30
+ # the 'revision' command, regardless of autogenerate
31
+ # revision_environment = false
32
+
33
+ # set to 'true' to allow .pyc and .pyo files without
34
+ # a source .py file to be detected as revisions in the
35
+ # versions/ directory
36
+ # sourceless = false
37
+
38
+ # version location specification; This defaults
39
+ # to alembic/versions. When using multiple version
40
+ # directories, initial revisions must be specified with --version-path.
41
+ # The path separator used here should be the separator specified by "version_path_separator" below.
42
+ # version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
43
+
44
+ # version path separator; As mentioned above, this is the character used to split
45
+ # version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46
+ # If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47
+ # Valid values for version_path_separator are:
48
+ #
49
+ # version_path_separator = :
50
+ # version_path_separator = ;
51
+ # version_path_separator = space
52
+ version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
53
+
54
+ # set to 'true' to search source files recursively
55
+ # in each "version_locations" directory
56
+ # new in Alembic version 1.10
57
+ # recursive_version_locations = false
58
+
59
+ # the output encoding used when revision files
60
+ # are written from script.py.mako
61
+ # output_encoding = utf-8
62
+
63
+ sqlalchemy.url = postgresql://postgres:postgres@localhost:5433/iot_local
64
+
65
+
66
+ [post_write_hooks]
67
+ # post_write_hooks defines scripts or Python functions that are run
68
+ # on newly generated revision scripts. See the documentation for further
69
+ # detail and examples
70
+
71
+ # format using "black" - use the console_scripts runner, against the "black" entrypoint
72
+ # hooks = black
73
+ # black.type = console_scripts
74
+ # black.entrypoint = black
75
+ # black.options = -l 79 REVISION_SCRIPT_FILENAME
76
+
77
+ # lint with attempts to fix using "ruff" - use the exec runner, execute a binary
78
+ # hooks = ruff
79
+ # ruff.type = exec
80
+ # ruff.executable = %(here)s/.venv/bin/ruff
81
+ # ruff.options = --fix REVISION_SCRIPT_FILENAME
82
+
83
+ # Logging configuration
84
+ [loggers]
85
+ keys = root,sqlalchemy,alembic
86
+
87
+ [handlers]
88
+ keys = console
89
+
90
+ [formatters]
91
+ keys = generic
92
+
93
+ [logger_root]
94
+ level = WARN
95
+ handlers = console
96
+ qualname =
97
+
98
+ [logger_sqlalchemy]
99
+ level = WARN
100
+ handlers =
101
+ qualname = sqlalchemy.engine
102
+
103
+ [logger_alembic]
104
+ level = INFO
105
+ handlers =
106
+ qualname = alembic
107
+
108
+ [handler_console]
109
+ class = StreamHandler
110
+ args = (sys.stderr,)
111
+ level = NOTSET
112
+ formatter = generic
113
+
114
+ [formatter_generic]
115
+ format = %(levelname)-5.5s [%(name)s] %(message)s
116
+ datefmt = %H:%M:%S
@@ -0,0 +1,77 @@
1
+ """IoT Database Models - SQLAlchemy models for IoT sensor data storage."""
2
+
3
+ __version__ = "0.1.0"
4
+ __author__ = "NG IT Services Sp. z o.o."
5
+ __email__ = "biuro@ngits.pl"
6
+
7
+ from .base import MeasurementBase, Types, Units
8
+ from .electricity import (
9
+ DailyElectricityUsage,
10
+ ElectricityMeasurement,
11
+ MonthlyElectricityUsage,
12
+ )
13
+ from .heat import DailyHeatUsage, HeatMeasurement, MonthlyHeatUsage
14
+ from .raw import RawMeasurement
15
+ from .temperature import TemperatureMeasurement
16
+ from .water import DailyWaterUsage, MonthlyWaterUsage, WaterMeasurement
17
+
18
+ __all__ = [
19
+ "DailyElectricityUsage",
20
+ "DailyHeatUsage",
21
+ "DailyWaterUsage",
22
+ "ElectricityMeasurement",
23
+ "HeatMeasurement",
24
+ "map_daily_usage_class",
25
+ "map_measurement_class",
26
+ "map_monthly_usage_class",
27
+ "MonthlyElectricityUsage",
28
+ "MonthlyHeatUsage",
29
+ "MonthlyWaterUsage",
30
+ "RawMeasurement",
31
+ "TemperatureMeasurement",
32
+ "Types",
33
+ "Units",
34
+ "WaterMeasurement",
35
+ ]
36
+
37
+
38
+ def map_measurement_class(type_: Types):
39
+ match type_:
40
+ case Types.electricity:
41
+ return ElectricityMeasurement
42
+ case Types.water:
43
+ return WaterMeasurement
44
+ case Types.heat:
45
+ return HeatMeasurement
46
+ case Types.temperature:
47
+ return TemperatureMeasurement
48
+ case _:
49
+ raise NotImplementedError
50
+
51
+
52
+ def map_daily_usage_class(type_: Types):
53
+ match type_:
54
+ case Types.electricity:
55
+ return DailyElectricityUsage
56
+ case Types.water:
57
+ return DailyWaterUsage
58
+ case Types.heat:
59
+ return DailyHeatUsage
60
+ case Types.temperature:
61
+ return None
62
+ case _:
63
+ raise NotImplementedError
64
+
65
+
66
+ def map_monthly_usage_class(type_: Types):
67
+ match type_:
68
+ case Types.electricity:
69
+ return MonthlyElectricityUsage
70
+ case Types.water:
71
+ return MonthlyWaterUsage
72
+ case Types.heat:
73
+ return MonthlyHeatUsage
74
+ case Types.temperature:
75
+ return None
76
+ case _:
77
+ raise NotImplementedError
@@ -0,0 +1,57 @@
1
+ from datetime import datetime
2
+ from enum import Enum as PyEnum
3
+ from enum import IntEnum
4
+
5
+ from sqlalchemy import Column, DateTime, Float, Integer, Text
6
+ from sqlalchemy.dialects.postgresql import UUID
7
+ from sqlalchemy.ext.declarative import declarative_base
8
+
9
+ Base = declarative_base()
10
+
11
+
12
+ class Units(PyEnum):
13
+ m3 = "m3"
14
+ kwh = "kwh"
15
+ degc = "degc"
16
+
17
+
18
+ class Types(IntEnum):
19
+ water = 1
20
+ electricity = 2
21
+ heat = 3
22
+ temperature = 4
23
+
24
+
25
+ class TimeSign(Base):
26
+ __abstract__ = True
27
+
28
+ created_ts = Column(DateTime, default=datetime.utcnow)
29
+ updated_ts = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
30
+
31
+
32
+ class IdentityBase(Base):
33
+ __abstract__ = True
34
+
35
+ id = Column(Integer, primary_key=True)
36
+ tenant = Column(UUID(as_uuid=True))
37
+ external_id = Column(Text, nullable=True)
38
+ meter_id = Column(Text)
39
+
40
+
41
+ class MeasurementBase(IdentityBase, TimeSign, Base):
42
+ __abstract__ = True
43
+
44
+ measured_ts = Column(DateTime)
45
+ value = Column(Float)
46
+
47
+
48
+ class UsageBase(IdentityBase, TimeSign, Base):
49
+ __abstract__ = True
50
+
51
+ start_ts = Column(DateTime)
52
+ end_ts = Column(DateTime)
53
+ consumption_ts = Column(DateTime)
54
+
55
+ start_value = Column(Float)
56
+ end_value = Column(Float)
57
+ consumption = Column(Float)
@@ -0,0 +1,38 @@
1
+ from sqlalchemy import UniqueConstraint
2
+
3
+ from .base import MeasurementBase, UsageBase
4
+
5
+
6
+ class ElectricityMeasurement(MeasurementBase):
7
+ __tablename__ = "measurement_electricity"
8
+ __table_args__ = (
9
+ UniqueConstraint(
10
+ "tenant",
11
+ "meter_id",
12
+ "measured_ts",
13
+ ),
14
+ )
15
+
16
+ RAW_VALUE_ATTR_NAME = "total_energy_consumption_kwh"
17
+
18
+
19
+ class DailyElectricityUsage(UsageBase):
20
+ __tablename__ = "usage_electricity_daily"
21
+ __table_args__ = (
22
+ UniqueConstraint(
23
+ "tenant",
24
+ "meter_id",
25
+ "consumption_ts",
26
+ ),
27
+ )
28
+
29
+
30
+ class MonthlyElectricityUsage(UsageBase):
31
+ __tablename__ = "usage_electricity_monthly"
32
+ __table_args__ = (
33
+ UniqueConstraint(
34
+ "tenant",
35
+ "meter_id",
36
+ "consumption_ts",
37
+ ),
38
+ )
@@ -0,0 +1,37 @@
1
+ from sqlalchemy import UniqueConstraint
2
+
3
+ from .base import MeasurementBase, UsageBase
4
+
5
+
6
+ class HeatMeasurement(MeasurementBase):
7
+ __tablename__ = "measurement_heat"
8
+ __table_args__ = (
9
+ UniqueConstraint(
10
+ "tenant",
11
+ "meter_id",
12
+ "measured_ts",
13
+ ),
14
+ )
15
+ RAW_VALUE_ATTR_NAME = "total_energy_consumption_kwh"
16
+
17
+
18
+ class DailyHeatUsage(UsageBase):
19
+ __tablename__ = "usage_heat_daily"
20
+ __table_args__ = (
21
+ UniqueConstraint(
22
+ "tenant",
23
+ "meter_id",
24
+ "consumption_ts",
25
+ ),
26
+ )
27
+
28
+
29
+ class MonthlyHeatUsage(UsageBase):
30
+ __tablename__ = "usage_heat_monthly"
31
+ __table_args__ = (
32
+ UniqueConstraint(
33
+ "tenant",
34
+ "meter_id",
35
+ "consumption_ts",
36
+ ),
37
+ )
File without changes
@@ -0,0 +1,20 @@
1
+ from sqlalchemy import Boolean, Column, SmallInteger
2
+ from sqlalchemy.dialects.postgresql import JSON
3
+
4
+ from .base import Base, IdentityBase, TimeSign, Types
5
+
6
+
7
+ class RawMeasurement(IdentityBase, TimeSign, Base):
8
+ __tablename__ = "measurement_raw"
9
+
10
+ data = Column(JSON)
11
+ type = Column(SmallInteger)
12
+ is_processed = Column(Boolean, default=False)
13
+
14
+ @property
15
+ def type_enum(self):
16
+ return Types(self.type)
17
+
18
+ @type_enum.setter
19
+ def type_enum(self, type_enum):
20
+ self.type = type_enum.value
@@ -0,0 +1,15 @@
1
+ from sqlalchemy import UniqueConstraint
2
+
3
+ from .base import MeasurementBase, UsageBase
4
+
5
+
6
+ class TemperatureMeasurement(MeasurementBase):
7
+ __tablename__ = "measurement_temperature"
8
+ __table_args__ = (
9
+ UniqueConstraint(
10
+ "tenant",
11
+ "meter_id",
12
+ "measured_ts",
13
+ ),
14
+ )
15
+ RAW_VALUE_ATTR_NAME = "total_degc"
@@ -0,0 +1,37 @@
1
+ from sqlalchemy import UniqueConstraint
2
+
3
+ from .base import MeasurementBase, UsageBase
4
+
5
+
6
+ class WaterMeasurement(MeasurementBase):
7
+ __tablename__ = "measurement_water"
8
+ __table_args__ = (
9
+ UniqueConstraint(
10
+ "tenant",
11
+ "meter_id",
12
+ "measured_ts",
13
+ ),
14
+ )
15
+ RAW_VALUE_ATTR_NAME = "total_m3"
16
+
17
+
18
+ class DailyWaterUsage(UsageBase):
19
+ __tablename__ = "usage_water_daily"
20
+ __table_args__ = (
21
+ UniqueConstraint(
22
+ "tenant",
23
+ "meter_id",
24
+ "consumption_ts",
25
+ ),
26
+ )
27
+
28
+
29
+ class MonthlyWaterUsage(UsageBase):
30
+ __tablename__ = "usage_water_monthly"
31
+ __table_args__ = (
32
+ UniqueConstraint(
33
+ "tenant",
34
+ "meter_id",
35
+ "consumption_ts",
36
+ ),
37
+ )
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: ngits-iot-db
3
+ Version: 0.1.0
4
+ Summary: SQLAlchemy models for IoT sensor data storage with Alembic migrations
5
+ Author-email: "NG IT Services Sp. z o. o." <biuro@ngits.pl>
6
+ Keywords: iot,sqlalchemy,database,models,sensors
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: License :: OSI Approved :: BSD License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Topic :: Database
15
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: SQLAlchemy>=2.0.0
20
+ Requires-Dist: alembic>=1.13.0
21
+ Requires-Dist: psycopg2-binary>=2.9.0
22
+ Provides-Extra: dev
23
+ Requires-Dist: black>=25.1.0; extra == "dev"
24
+ Requires-Dist: isort>=5.13.0; extra == "dev"
25
+ Requires-Dist: mypy>=1.0.0; extra == "dev"
26
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
27
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
28
+ Dynamic: license-file
29
+
30
+ database uri
31
+ ============
32
+
33
+ You can set database uri by setting `IOT_DATABASE_URL` variable.
34
+
35
+ ```shell
36
+ export IOT_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/iot_local
37
+ ```
38
+
39
+
40
+ make migrations
41
+ ===============
42
+
43
+ ```shell
44
+ alembic revision --autogenerate -m "Migration message."
45
+ ```
46
+
47
+ upgrade
48
+ =======
49
+
50
+ ```shell
51
+ # upgrade all
52
+ alembic upgrade head
53
+
54
+ # upgrade all to specific migration
55
+ alembic upgrade <revision_id>
56
+ ```
57
+
58
+ downgrade
59
+ =========
60
+
61
+ ```shell
62
+ # downgrade all
63
+ alembic downgrade base
64
+
65
+ # downgrade all to specific migration
66
+ alembic downgrade <revision_id>
67
+ ```
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ alembic.ini
5
+ pyproject.toml
6
+ requirements.txt
7
+ alembic/env.py
8
+ alembic/script.py.mako
9
+ alembic/versions/78371c256762_init_schema.py
10
+ alembic/versions/fc391d505594_add_temperature.py
11
+ models/__init__.py
12
+ models/base.py
13
+ models/electricity.py
14
+ models/heat.py
15
+ models/py.typed
16
+ models/raw.py
17
+ models/temperature.py
18
+ models/water.py
19
+ ngits_iot_db.egg-info/PKG-INFO
20
+ ngits_iot_db.egg-info/SOURCES.txt
21
+ ngits_iot_db.egg-info/dependency_links.txt
22
+ ngits_iot_db.egg-info/requires.txt
23
+ ngits_iot_db.egg-info/top_level.txt
@@ -0,0 +1,10 @@
1
+ SQLAlchemy>=2.0.0
2
+ alembic>=1.13.0
3
+ psycopg2-binary>=2.9.0
4
+
5
+ [dev]
6
+ black>=25.1.0
7
+ isort>=5.13.0
8
+ mypy>=1.0.0
9
+ pytest>=7.0.0
10
+ pytest-cov>=4.0.0
@@ -0,0 +1,95 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "ngits-iot-db"
7
+ version = "0.1.0"
8
+ description = "SQLAlchemy models for IoT sensor data storage with Alembic migrations"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ authors = [
12
+ {name = "NG IT Services Sp. z o. o.", email = "biuro@ngits.pl"}
13
+ ]
14
+ keywords = ["iot", "sqlalchemy", "database", "models", "sensors"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Intended Audience :: Developers",
18
+ "License :: OSI Approved :: BSD License",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.10",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Topic :: Database",
24
+ "Topic :: Software Development :: Libraries :: Python Modules",
25
+ ]
26
+
27
+ dependencies = [
28
+ "SQLAlchemy>=2.0.0",
29
+ "alembic>=1.13.0",
30
+ "psycopg2-binary>=2.9.0",
31
+ ]
32
+
33
+ [project.optional-dependencies]
34
+ dev = [
35
+ "black>=25.1.0",
36
+ "isort>=5.13.0",
37
+ "mypy>=1.0.0",
38
+ "pytest>=7.0.0",
39
+ "pytest-cov>=4.0.0",
40
+ ]
41
+
42
+ [tool.setuptools.packages.find]
43
+ where = ["."]
44
+ include = ["models*"]
45
+ exclude = ["tests*", "alembic*"]
46
+
47
+ [tool.setuptools.package-data]
48
+ models = ["py.typed"]
49
+
50
+ [tool.black]
51
+ line-length = 88
52
+ target-version = ['py310']
53
+ include = '\.pyi?$'
54
+ extend-exclude = '''
55
+ /(
56
+ # directories
57
+ \.eggs
58
+ | \.git
59
+ | \.hg
60
+ | \.mypy_cache
61
+ | \.tox
62
+ | \.venv
63
+ | _build
64
+ | buck-out
65
+ | build
66
+ | dist
67
+ | alembic/versions
68
+ )/
69
+ '''
70
+
71
+ [tool.isort]
72
+ profile = "black"
73
+ multi_line_output = 3
74
+ include_trailing_comma = true
75
+ force_grid_wrap = 0
76
+ use_parentheses = true
77
+ ensure_newline_before_comments = true
78
+ line_length = 88
79
+ skip_glob = ["alembic/versions/*.py"]
80
+
81
+ [tool.mypy]
82
+ python_version = "3.9"
83
+ warn_return_any = true
84
+ warn_unused_configs = true
85
+ disallow_untyped_defs = true
86
+ disallow_incomplete_defs = true
87
+ check_untyped_defs = true
88
+ disallow_untyped_decorators = true
89
+ no_implicit_optional = true
90
+ warn_redundant_casts = true
91
+ warn_unused_ignores = true
92
+ warn_no_return = true
93
+ warn_unreachable = true
94
+ strict_equality = true
95
+ exclude = ["alembic/"]
@@ -0,0 +1,14 @@
1
+ alembic==1.13.2
2
+ black==25.1.0
3
+ click==8.1.7
4
+ greenlet==3.2.3
5
+ isort==5.13.2
6
+ Mako==1.3.5
7
+ MarkupSafe==2.1.5
8
+ mypy-extensions==1.0.0
9
+ packaging==25.0
10
+ pathspec==0.12.1
11
+ platformdirs==4.2.2
12
+ psycopg2-binary==2.9.10
13
+ SQLAlchemy==2.0.32
14
+ typing_extensions==4.12.2
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+