Statuses are sent during the refresh POST.

This commit is contained in:
Roland 2019-10-14 11:58:25 -06:00
parent df5eee27d6
commit fe9d3c020c
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
4 changed files with 38 additions and 66 deletions

View file

@ -39,7 +39,8 @@ WROLPi.
1. A WROLPi instance should be able to "factory-reset" itself without any outside services.
1. WROLPi should function completely without any internet services.
1. A user should have easy access to their data if WROLPi fails:
* For example, a user can watch their videos by navigating a short and intuitive directory structure and opening the video in
* For example, a user can watch their videos by navigating a short and intuitive directory structure and opening the
video in.
their preferred video player.
1. WROLPi should be self-documented. The UI should contain a tutorial for USERS as well as the MAINTAINER.
* If the UI isn't functional, the code should be documented such that a user can restore functionality.
* If the UI isn't functional, the code should be documented such that a user can restore functionality.

View file

@ -39,24 +39,16 @@ class SettingsAPI(object):
@cherrypy.expose
class Refresh(object):
@cherrypy.tools.db()
def POST(self, db: DictDB):
refresh_videos(db)
return json.dumps({'success': 'Videos refreshed'})
def GET(self):
def POST(self):
cherrypy.response.headers['Content-Type'] = 'text/event-stream'
def streamer():
with get_db_context() as (db_conn, db):
Video = db['video']
for video in Video.get_where():
yield f'{video["name"]}\n'
time.sleep(0.5)
with get_db_context(commit=True) as (db_conn, db):
yield from refresh_videos(db)
return streamer()
GET._cp_config = {'response.stream': True}
POST._cp_config = {'response.stream': True}
@cherrypy.expose
@ -190,34 +182,38 @@ def refresh_channel_videos(db, channel):
logger.debug(f'{channel["name"]}: Added {video_path}')
insert_video(db, pathlib.Path(video_path), channel)
logger.info(f'{channel["name"]}: Added {len(new_videos)} new videos, {len(existing_paths)} already existed.')
def insert_status(curs, status):
curs.execute('INSERT INTO refresh_status (status) VALUES (%s);', (status,))
final_status = f'{channel["name"]}: Added {len(new_videos)} new videos, {len(existing_paths)} already existed.'
logger.info(final_status)
yield final_status
def refresh_videos(db: DictDB):
"""
Find any videos in the channel directories and add them to the DB. Delete DB records of any videos not in the
file system.
Yields status updates to be passed to the UI.
:param db:
:return:
"""
logger.info('Refreshing video list')
Channel = db['channel']
# Remove any videos that don't exist
yield 'Verifying videos in DB exist in file system'
curs = db.get_cursor()
insert_status(curs, 'Refreshing video list')
deleted_any = False
curs.execute('SELECT id, video_path FROM video')
existing_videos = curs.fetchall()
to_delete = []
for video in existing_videos:
if not pathlib.Path(video['video_path']).is_file():
deleted_any = True
curs.execute('DELETE FROM video WHERE id = %s', (video['id'],))
if deleted_any:
insert_status(curs, 'Removed records of non-existent videos')
else:
insert_status(curs, 'All video records link to real files')
to_delete.append(video['id'])
if to_delete:
yield 'Deleting video files no longer in file system'
curs.execute('DELETE FROM video WHERE id = ANY(%s)', (to_delete,))
for channel in Channel.get_where():
insert_status(curs, f'Searching for videos for channel {channel["name"]}')
yield f'Checking {channel["name"]} for new videos'
with db.transaction(commit=True):
refresh_channel_videos(db, channel)
yield from refresh_channel_videos(db, channel)

View file

@ -47,19 +47,25 @@ class TestAPI(unittest.TestCase):
@test_db_wrapper
def test_refresh(self):
api = APIRoot()
with get_db_context() as (db_conn, db):
Video, Channel, RefreshStatus = db['video'], db['channel'], db['refresh_status']
with get_db_context(commit=True) as (db_conn, db):
Video = db['video']
import_settings_config()
# Insert a bogus video, it should be removed
bogus = Video(video_path='bar').flush()
assert bogus['id'], 'Failed to insert a bogus video for removal'
api.settings.refresh.POST(db)
streamer = api.settings.refresh.POST()
statuses = list(streamer)
self.assertIn('Verifying videos in DB exist in file system', statuses)
self.assertIn('Deleting video files no longer in file system', statuses)
self.assertIn('Checking Big Buck Bunny for new videos', statuses)
self.assertIn('Big Buck Bunny: Added 1 new videos, 0 already existed.', statuses)
with get_db_context() as (db_conn, db):
Video, Channel = db['video'], db['channel']
self.assertEqual(Channel.count(), 1)
self.assertEqual(Video.count(), 1)
# Some statuses were saved
self.assertGreater(RefreshStatus.count(), 0)
@test_db_wrapper
def test_channel(self):
@ -106,26 +112,3 @@ class TestAPI(unittest.TestCase):
# Delete the new channel
result = api.channel.DELETE('examplechannel1', db)
self.assertIn('success', result)
@test_db_wrapper
def test_refresh(self):
# Insert some statuses
with get_db_context() as (db_conn, db):
RefreshStatus = db['refresh_status']
example_statuses = [
'Foo',
'Bar',
'Baz',
'Refresh complete',
'this should not be shown'
]
for status in example_statuses:
RefreshStatus(status=status).flush()
response = self.getPage('/api/videos/settings/refresh')
statuses = '\n'.join([
'Foo',
'Bar',
'Baz',
'Refresh complete'
])

View file

@ -34,11 +34,3 @@ CREATE TABLE video
ALTER TABLE video
ADD COLUMN channel_id INTEGER REFERENCES channel (id);
DROP TABLE IF EXISTS refresh_status;
CREATE TABLE refresh_status
(
id SERIAL PRIMARY KEY,
status_dt TIMESTAMP DEFAULT current_timestamp,
status TEXT
);