From 6aacd7bfce907ad4be9e0edec25bb84bc52ee406 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Wed, 16 Aug 2023 21:21:28 -0700 Subject: [PATCH] feat: attempt to fix any streams marked as unended --- core/core.go | 2 ++ core/data/replays.go | 4 +++- db/query.sql | 3 +++ db/query.sql.go | 9 +++++++++ replays/replays.go | 21 +++++++++++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 replays/replays.go diff --git a/core/core.go b/core/core.go index a048af0d4..cb02484a5 100644 --- a/core/core.go +++ b/core/core.go @@ -17,6 +17,7 @@ import ( "github.com/owncast/owncast/core/webhooks" "github.com/owncast/owncast/models" "github.com/owncast/owncast/notifications" + "github.com/owncast/owncast/replays" "github.com/owncast/owncast/utils" "github.com/owncast/owncast/yp" ) @@ -58,6 +59,7 @@ func Start() error { user.SetupUsers() auth.Setup(data.GetDatastore()) + replays.Setup() fileWriter.SetupFileWriterReceiverService(&handler) diff --git a/core/data/replays.go b/core/data/replays.go index 201396591..354d15508 100644 --- a/core/data/replays.go +++ b/core/data/replays.go @@ -1,6 +1,8 @@ package data -import "database/sql" +import ( + "database/sql" +) func createRecordingTables(db *sql.DB) { createSegmentsTableSQL := `CREATE TABLE IF NOT EXISTS video_segments ( diff --git a/db/query.sql b/db/query.sql index 0219e2c25..e0990f8b0 100644 --- a/db/query.sql +++ b/db/query.sql @@ -163,3 +163,6 @@ SELECT id AS clip_id, stream_id, clipped_by, clip_title, timestamp AS clip_times -- name: GetFinalSegmentForStream :one SELECT id, stream_id, output_configuration_id, path, relative_timestamp, timestamp FROM video_segments WHERE stream_id = $1 ORDER BY relative_timestamp DESC LIMIT 1; + +-- name: FixUnfinishedStreams :exec +UPDATE streams SET end_time = (SELECT timestamp FROM video_segments WHERE stream_id = streams.id) WHERE end_time IS NULL; diff --git a/db/query.sql.go b/db/query.sql.go index c9a9701a8..f2ce5f125 100644 --- a/db/query.sql.go +++ b/db/query.sql.go @@ -205,6 +205,15 @@ func (q *Queries) DoesInboundActivityExist(ctx context.Context, arg DoesInboundA return count, err } +const fixUnfinishedStreams = `-- name: FixUnfinishedStreams :exec +UPDATE streams SET end_time = (SELECT timestamp FROM video_segments WHERE stream_id = streams.id) WHERE end_time IS NULL +` + +func (q *Queries) FixUnfinishedStreams(ctx context.Context) error { + _, err := q.db.ExecContext(ctx, fixUnfinishedStreams) + return err +} + const getAllClips = `-- name: GetAllClips :many SELECT rc.id AS id, rc.clip_title, rc.stream_id, rc.relative_start_time, rc.relative_end_time, (rc.relative_end_time - rc.relative_start_time) AS duration_seconds, rc.timestamp, s.stream_title AS stream_title FROM replay_clips rc diff --git a/replays/replays.go b/replays/replays.go new file mode 100644 index 000000000..3e889394f --- /dev/null +++ b/replays/replays.go @@ -0,0 +1,21 @@ +package replays + +import ( + "context" + + "github.com/owncast/owncast/core/data" + log "github.com/sirupsen/logrus" +) + +// Setup will setup the replay package. +func Setup() { + fixUnfinishedStreams() +} + +// fixUnfinishedStreams will find streams with no end time and attempt to +// give them an end time based on the last segment assigned to that stream. +func fixUnfinishedStreams() { + if err := data.GetDatastore().GetQueries().FixUnfinishedStreams(context.Background()); err != nil { + log.Warnln(err) + } +}