Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
engine-api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
engine-api
Commits
7fee511a
Commit
7fee511a
authored
4 years ago
by
David Trattnig
Browse files
Options
Downloads
Patches
Plain Diff
Improve clock model construction. engine-clock#2
parent
1179f4d1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/models.py
+51
-20
51 additions, 20 deletions
src/models.py
with
51 additions
and
20 deletions
src/models.py
+
51
−
20
View file @
7fee511a
...
@@ -93,9 +93,9 @@ class PlayLog(db.Model):
...
@@ -93,9 +93,9 @@ class PlayLog(db.Model):
@staticmethod
@staticmethod
def
select_
curr
ent
():
def
select_
rec
ent
():
"""
"""
Selects the
curr
ent
ly
play
ing track
.
Selects the
most rec
ent play
ed track. Equals to the current track if it
'
s still playing
.
"""
"""
db
.
session
.
commit
()
db
.
session
.
commit
()
now
=
datetime
.
datetime
.
now
()
now
=
datetime
.
datetime
.
now
()
...
@@ -104,6 +104,20 @@ class PlayLog(db.Model):
...
@@ -104,6 +104,20 @@ class PlayLog(db.Model):
order_by
(
PlayLog
.
track_start
.
desc
()).
\
order_by
(
PlayLog
.
track_start
.
desc
()).
\
filter
(
PlayLog
.
track_start
<=
str
(
now
)).
first
()
filter
(
PlayLog
.
track_start
<=
str
(
now
)).
first
()
return
track
@staticmethod
def
select_current
():
"""
Selects the currently playing track.
"""
db
.
session
.
commit
()
now
=
datetime
.
datetime
.
now
()
track
=
PlayLog
.
select_recent
()
if
track
:
if
track
:
# Preferably only get playlogs which are known for still being on air
# Preferably only get playlogs which are known for still being on air
if
track
.
track_start
+
datetime
.
timedelta
(
0
,
track
.
track_duration
)
>
now
:
if
track
.
track_start
+
datetime
.
timedelta
(
0
,
track
.
track_duration
)
>
now
:
...
@@ -144,7 +158,7 @@ class PlayLog(db.Model):
...
@@ -144,7 +158,7 @@ class PlayLog(db.Model):
else
:
else
:
result
=
db
.
session
.
query
(
PlayLog
).
\
result
=
db
.
session
.
query
(
PlayLog
).
\
order_by
(
PlayLog
.
track_start
.
desc
()).
\
order_by
(
PlayLog
.
track_start
.
desc
()).
\
filter
(
PlayLog
.
timeslot_id
==
timeslot_id
)
.
\
filter
(
PlayLog
.
timeslot_id
==
timeslot_id
)
playlogs
=
result
.
all
()
playlogs
=
result
.
all
()
return
playlogs
return
playlogs
...
@@ -437,47 +451,64 @@ class ClockInfo(db.Model):
...
@@ -437,47 +451,64 @@ class ClockInfo(db.Model):
data
=
db
.
session
.
query
(
ClockInfo
).
filter
(
ClockInfo
.
log_source
==
source_number
).
first
()
data
=
db
.
session
.
query
(
ClockInfo
).
filter
(
ClockInfo
.
log_source
==
source_number
).
first
()
current_track
=
PlayLog
.
select_current
()
current_track
=
PlayLog
.
select_current
()
current_playlist_id
=
-
1
current_playlist_id
=
-
1
updated_playlist
=
None
playlogs
=
None
if
current_track
:
# Construct the clock `info` object
updated_playlist
=
PlayLog
.
select_for_timeslot
(
current_track
.
timeslot_id
)
updated_playlist
.
sort
(
key
=
lambda
track
:
track
.
track_start
,
reverse
=
False
)
if
data
:
if
data
:
info
[
"
log_source
"
]
=
data
.
log_source
info
[
"
log_source
"
]
=
data
.
log_source
info
[
"
log_time
"
]
=
data
.
log_time
info
[
"
log_time
"
]
=
data
.
log_time
# Get the track currently playing
if
current_track
:
if
current_track
:
info
[
"
current_track
"
]
=
track_schema
.
dump
(
current_track
)
info
[
"
current_track
"
]
=
track_schema
.
dump
(
current_track
)
# Append the missing planned playlist items to the ones played # FIXME do it client-site
if
data
.
current_playlist
:
if
data
.
current_playlist
:
info
[
"
planned_playlist
"
]
=
json
.
loads
(
data
.
current_playlist
)
info
[
"
planned_playlist
"
]
=
json
.
loads
(
data
.
current_playlist
)
current_playlist_id
=
info
[
"
planned_playlist
"
][
"
playlist_id
"
]
for
next_entry
in
info
[
"
planned_playlist
"
][
"
entries
"
]:
# Get the current timeslot
if
next_entry
.
get
(
"
start_date
"
)
and
next_entry
.
get
(
"
start_date
"
)
>
datetime
.
datetime
.
now
():
updated_playlist
[
"
entries
"
].
append
(
next_entry
)
if
data
.
current_timeslot
:
if
data
.
current_timeslot
:
info
[
"
current_timeslot
"
]
=
json
.
loads
(
data
.
current_timeslot
)
info
[
"
current_timeslot
"
]
=
json
.
loads
(
data
.
current_timeslot
)
# Get the most recently played track (because right now nothing might be playing)
most_recent_track
=
PlayLog
.
select_recent
()
# Is the most recent track part of the current timeslot?
if
most_recent_track
.
timeslot_id
==
info
[
"
current_timeslot
"
][
"
timeslot_id
"
]:
# Get the actual playlogs of the current timeslot, until now
playlog_schema
=
PlayLogSchema
(
many
=
True
)
playlogs
=
PlayLog
.
select_for_timeslot
(
most_recent_track
.
timeslot_id
)
playlogs
.
sort
(
key
=
lambda
track
:
track
.
track_start
,
reverse
=
False
)
info
[
"
current_playlist
"
]
=
{
# "playlist_id": current_playlist_id,
"
entries
"
:
playlog_schema
.
dump
(
playlogs
)
}
# Invalid timeslots (e.g. in fallback scenarios) get a virtual start date of the first fallback track
if
info
[
"
current_timeslot
"
][
"
timeslot_id
"
]
==
-
1
:
if
playlogs
and
playlogs
[
0
]:
info
[
"
current_timeslot
"
][
"
timeslot_start
"
]
=
playlogs
[
0
].
track_start
# Get the next timeslot
if
data
.
next_timeslot
:
if
data
.
next_timeslot
:
info
[
"
next_timeslot
"
]
=
json
.
loads
(
data
.
next_timeslot
)
info
[
"
next_timeslot
"
]
=
json
.
loads
(
data
.
next_timeslot
)
playlog_schema
=
PlayLogSchema
(
many
=
True
)
info
[
"
current_playlist
"
]
=
{
"
playlist_id
"
:
current_playlist_id
,
"
entries
"
:
playlog_schema
.
dump
(
updated_playlist
)
}
return
info
return
info
def
save
(
self
):
def
save
(
self
):
db
.
session
.
add
(
self
)
db
.
session
.
add
(
self
)
db
.
session
.
commit
()
db
.
session
.
commit
()
def
update
(
self
):
def
update
(
self
):
db
.
session
.
merge
(
self
)
db
.
session
.
merge
(
self
)
db
.
session
.
commit
()
db
.
session
.
commit
()
class
ClockInfoSchema
(
ma
.
SQLAlchemySchema
):
class
ClockInfoSchema
(
ma
.
SQLAlchemySchema
):
"""
"""
Schema for trackservice entries.
Schema for trackservice entries.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment