From e3c3991db69997dc50d8d392f14c94502aeceb11 Mon Sep 17 00:00:00 2001 From: Milan Date: Sat, 19 Dec 2020 22:01:26 +0100 Subject: [PATCH] audio_recordings.pm: active audio uploads If audio files become inactive because a new file is uploaded, the new file is marked as active and the old file is marked as not active. This allows fastly querying events and their active upload file. Before this change all event's audio files were found and needed to be filtered afterwards to get a set of unique events. --- install/migrate.sql | 7 +++ lib/calcms/audio_recordings.pm | 80 +++++++++++++++++++++++++--------- lib/calcms/events.pm | 2 +- lib/calcms/series.pm | 26 ++++++----- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/install/migrate.sql b/install/migrate.sql index 4b07b59..2eb73c9 100644 --- a/install/migrate.sql +++ b/install/migrate.sql @@ -270,3 +270,10 @@ ADD COLUMN `update_event_field_content_format` TINYINT(1) UNSIGNED NOT NULL AFTE ALTER TABLE `calcms_events` ADD COLUMN `listen_key` VARCHAR(100) NULL; + +ALTER TABLE `calcms_audio_recordings` +ADD COLUMN `active` TINYINT(1) NOT NULL DEFAULT 0 AFTER `event_id`; + +ALTER TABLE `calcms`.`calcms_audio_recordings` +ADD INDEX `active_index` (`active`); + diff --git a/lib/calcms/audio_recordings.pm b/lib/calcms/audio_recordings.pm index c8c54e5..ba12b8b 100644 --- a/lib/calcms/audio_recordings.pm +++ b/lib/calcms/audio_recordings.pm @@ -77,6 +77,7 @@ sub get($$) { ,project_id ,studio_id ,event_id + ,active ,path ,size ,created_by @@ -128,7 +129,9 @@ sub update($$$) { $query .= ' and id=?'; push @$bind_values, $entry->{id}; } - return db::put( $dbh, $query, $bind_values ); + my $result = db::put( $dbh, $query, $bind_values ); + update_active($config, $dbh, $entry); + return $result; } # insert playout entry @@ -142,25 +145,24 @@ sub insert ($$$) { return undef unless defined $entry->{event_id}; return undef unless defined $entry->{path}; - return db::insert( - $dbh, - 'calcms_audio_recordings', - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - event_id => $entry->{event_id}, - path => $entry->{path}, - size => $entry->{size}, - created_by => $entry->{created_by}, - eventDuration => $entry->{eventDuration}, - audioDuration => $entry->{audioDuration}, - rmsLeft => $entry->{rmsLeft}, - rmsRight => $entry->{rmsRight}, - processed => $entry->{processed}, - mastered => $entry->{mastered} || '0', - } - ); + $entry = { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + event_id => $entry->{event_id}, + path => $entry->{path}, + size => $entry->{size}, + created_by => $entry->{created_by}, + eventDuration => $entry->{eventDuration}, + audioDuration => $entry->{audioDuration}, + rmsLeft => $entry->{rmsLeft}, + rmsRight => $entry->{rmsRight}, + processed => $entry->{processed}, + mastered => $entry->{mastered} || '0', + }; + my $result = db::insert( $dbh, 'calcms_audio_recordings', $entry ); + update_active($config, $dbh, $entry); + return $result; } # delete playout entry @@ -175,12 +177,48 @@ sub delete ($$$) { return undef unless defined $entry->{path}; my $query = qq{ - delete + delete from calcms_audio_recordings where project_id=? and studio_id=? and event_id=? and path=? }; my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{event_id}, $entry->{path} ]; - return db::put( $dbh, $query, $bind_values ); + my $result = db::put( $dbh, $query, $bind_values ); + + update_active($config, $dbh, $entry); + return $result; +} + +sub update_active($$$) { + my $config = shift; + my $dbh = shift; + my $entry = shift; + + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{event_id}; + + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{event_id} ]; + my $query = qq{ + update calcms_audio_recordings + set active=0 + where project_id=? and studio_id=? and event_id=? and active=1 + }; + db::put( $dbh, $query, $bind_values ); + + $query = qq{ + select max(id) id from calcms_audio_recordings + where project_id=? and studio_id=? and event_id=? + }; + my $entries = db::get( $dbh, $query, $bind_values ); + my $max = $entries->[0]; + return undef unless defined $max->{id}; + + $query = qq{ + update calcms_audio_recordings + set active=1 + where id=? + }; + return db::put( $dbh, $query, [$max->{id}] ); } sub error($) { diff --git a/lib/calcms/events.pm b/lib/calcms/events.pm index 4c8165d..cc01c56 100644 --- a/lib/calcms/events.pm +++ b/lib/calcms/events.pm @@ -1148,7 +1148,7 @@ sub get_query($$$) { # add recordings table if ( $params->{recordings} eq '1' ) { - $query .= "\n left join calcms_audio_recordings ar on e.id=ar.event_id"; + $query .= "\n left join calcms_audio_recordings ar on e.id=ar.event_id and ar.active=1"; } if ( scalar @$where_cond > 0 ) { diff --git a/lib/calcms/series.pm b/lib/calcms/series.pm index 917360d..8693b0a 100644 --- a/lib/calcms/series.pm +++ b/lib/calcms/series.pm @@ -495,7 +495,7 @@ sub get_events ($$) { my $conditions = ''; if ( @conditions > 0 ) { - $conditions = ' and ' . join( ' and ', @conditions ); + $conditions = ' where ' . join( ' and ', @conditions ); } my $limit = ''; @@ -504,17 +504,19 @@ sub get_events ($$) { } my $query = qq{ - select * - ,date(start) start_date - ,date(end) end_date - ,weekday(start) weekday - ,weekofyear(start) week_of_year - ,dayofyear(start) day_of_year - ,start_date day - ,id event_id - from calcms_series_events se, calcms_events e - where se.event_id = e.id - $conditions + select se.*,e.* + ,date(start) start_date + ,date(end) end_date + ,weekday(start) weekday + ,weekofyear(start) week_of_year + ,dayofyear(start) day_of_year + ,start_date day + ,e.id event_id + ,ar.path path + from calcms_series_events se + inner join calcms_events e on se.event_id = e.id + left join calcms_audio_recordings ar on se.event_id=ar.event_id and ar.active=1 + $conditions order by start_date desc $limit };