From 4a60ef903bc0e8a42d522fe5ab05ade65e2d56d1 Mon Sep 17 00:00:00 2001 From: Chris Pastl <chris@crispybits.app> Date: Mon, 16 Oct 2023 17:45:45 +0200 Subject: [PATCH] Refactor: change type of 'trackDuration' to float --- CHANGELOG.md | 1 + schemas/openapi-tank.json | 4 ++-- src/aura_engine/scheduling/models.py | 8 ++++---- src/aura_engine/scheduling/programme.py | 6 +++--- src/aura_engine/scheduling/utils.py | 2 +- src/aura_tank_api/models/file.py | 4 ++-- src/aura_tank_api/models/playlist_entry.py | 4 ++-- tests/json/tank-api-v1-playlists-1.json | 4 ++-- tests/test_scheduling_api_tank.py | 2 +- 9 files changed, 18 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c96f4dbd..5f2d8b37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Provide properties in API schemas in CamelCase notation (aura#141) +- Use datatype float instead of int for `trackDuration` (#132) ### Removed diff --git a/schemas/openapi-tank.json b/schemas/openapi-tank.json index ca1010dc..44daaabc 100644 --- a/schemas/openapi-tank.json +++ b/schemas/openapi-tank.json @@ -27,7 +27,7 @@ "type": "string" }, "duration": { - "type": "integer" + "type": "number" }, "id": { "type": "integer" @@ -128,7 +128,7 @@ "PlaylistEntry": { "properties": { "duration": { - "type": "integer" + "type": "number" }, "file": { "$ref": "#/components/schemas/File" diff --git a/src/aura_engine/scheduling/models.py b/src/aura_engine/scheduling/models.py index b6a5f8f6..3cd6c1fb 100644 --- a/src/aura_engine/scheduling/models.py +++ b/src/aura_engine/scheduling/models.py @@ -28,10 +28,10 @@ import time import sqlalchemy as sa from sqlalchemy import ( - BigInteger, Column, ColumnDefault, DateTime, + Float, ForeignKey, Integer, String, @@ -407,10 +407,10 @@ class Playlist(DB.Model, AuraDatabaseModel): """Return the total length of the playlist in seconds. Returns: - (Integer): Length in seconds + (Float): Length in seconds """ - total = 0 + total: float = 0 for entry in self.entries: total += entry.duration @@ -447,7 +447,7 @@ class PlaylistEntry(DB.Model, AuraDatabaseModel): # Data entry_num = Column(Integer) - duration = Column(BigInteger) + duration = Column(Float) volume = Column(Integer, ColumnDefault(100)) source = Column(String(1024)) entry_start = Column(DateTime) diff --git a/src/aura_engine/scheduling/programme.py b/src/aura_engine/scheduling/programme.py index 3b42d0ca..dcd197ef 100644 --- a/src/aura_engine/scheduling/programme.py +++ b/src/aura_engine/scheduling/programme.py @@ -450,7 +450,7 @@ class ProgrammeStore: entry_db.entry_start = datetime.fromtimestamp(time_marker) entry_db.artificial_playlist_id = playlist_db.artificial_id entry_db.entry_num = entry_num - entry_db.duration = SU.nano_to_seconds(entry["duration"]) + entry_db.duration = entry["duration"] if "uri" in entry: entry_db.source = entry["uri"] @@ -497,8 +497,8 @@ class ProgrammeStore: """ total_seconds = (timeslot_db.timeslot_end - timeslot_db.timeslot_start).total_seconds() - total_duration = SU.seconds_to_nano(total_seconds) - actual_duration = 0 + total_duration = float(total_seconds) + actual_duration: float = 0 missing_duration = [] idx = 0 diff --git a/src/aura_engine/scheduling/utils.py b/src/aura_engine/scheduling/utils.py index 1ce21d16..71ecda10 100644 --- a/src/aura_engine/scheduling/utils.py +++ b/src/aura_engine/scheduling/utils.py @@ -167,7 +167,7 @@ class M3UPlaylistProcessor: "title": metadata[1].split(" - ")[1], } }, - "duration": SU.seconds_to_nano(int(metadata[0])), + "duration": float(metadata[0]), "uri": "file://" + lines[i + 1], } entries.append(entry) diff --git a/src/aura_tank_api/models/file.py b/src/aura_tank_api/models/file.py index 91a95e0a..2e45bc18 100644 --- a/src/aura_tank_api/models/file.py +++ b/src/aura_tank_api/models/file.py @@ -17,7 +17,7 @@ class File: """ Attributes: created (Union[Unset, str]): - duration (Union[Unset, int]): + duration (Union[Unset, float]): id (Union[Unset, int]): metadata (Union[Unset, FileMetadata]): show_name (Union[Unset, str]): @@ -27,7 +27,7 @@ class File: """ created: Union[Unset, str] = UNSET - duration: Union[Unset, int] = UNSET + duration: Union[Unset, float] = UNSET id: Union[Unset, int] = UNSET metadata: Union[Unset, "FileMetadata"] = UNSET show_name: Union[Unset, str] = UNSET diff --git a/src/aura_tank_api/models/playlist_entry.py b/src/aura_tank_api/models/playlist_entry.py index c575c632..49d0e349 100644 --- a/src/aura_tank_api/models/playlist_entry.py +++ b/src/aura_tank_api/models/playlist_entry.py @@ -15,12 +15,12 @@ T = TypeVar("T", bound="PlaylistEntry") class PlaylistEntry: """ Attributes: - duration (Union[Unset, int]): + duration (Union[Unset, float]): file (Union[Unset, File]): uri (Union[Unset, str]): """ - duration: Union[Unset, int] = UNSET + duration: Union[Unset, float] = UNSET file: Union[Unset, "File"] = UNSET uri: Union[Unset, str] = UNSET additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict) diff --git a/tests/json/tank-api-v1-playlists-1.json b/tests/json/tank-api-v1-playlists-1.json index 4c4b3348..efb6a8ad 100644 --- a/tests/json/tank-api-v1-playlists-1.json +++ b/tests/json/tank-api-v1-playlists-1.json @@ -8,7 +8,7 @@ "entries":[ { "uri":"file://musikprogramm/2", - "duration":199040000000, + "duration":199040000000.0, "file":{ "id":2, "created":"2023-02-28T14:42:09.540485+01:00", @@ -27,7 +27,7 @@ "album":"Test Album" }, "size":36496517, - "duration":199040000000 + "duration":199040000000.0 } } ] diff --git a/tests/test_scheduling_api_tank.py b/tests/test_scheduling_api_tank.py index 06749ef6..dbd7db12 100644 --- a/tests/test_scheduling_api_tank.py +++ b/tests/test_scheduling_api_tank.py @@ -61,7 +61,7 @@ class TestApiTank(unittest.TestCase): self.assertIsNotNone(1, playlist.entries) self.assertEqual(1, len(entries)) self.assertEqual("file://musikprogramm/2", entries[0].uri) - self.assertEqual(199040000000, entries[0].duration) + self.assertEqual(199040000000.0, entries[0].duration) file = playlist.entries[0].file self.assertEqual(2, file.id) -- GitLab