diff --git a/build.gradle.kts b/build.gradle.kts index 271164e..9bc9b25 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,7 +3,7 @@ plugins { } group = "name.rycroft.simon" -version = "1.0.0" +version = "1.0.1" repositories { mavenCentral() diff --git a/src/main/java/name/rycroft/simon/Synplex.java b/src/main/java/name/rycroft/simon/Synplex.java index 083529b..bdbd13c 100644 --- a/src/main/java/name/rycroft/simon/Synplex.java +++ b/src/main/java/name/rycroft/simon/Synplex.java @@ -33,8 +33,7 @@ public static void main(String[] args) { do { SyncViews syncViews = injector.getInstance(SyncViews.class); arguments.accounts().forEach(syncViews::sync); - sleep(Duration.ofSeconds(arguments.waitTimeInSeconds())); - } while (!arguments.singleRun()); + } while (!arguments.singleRun() && sleepBetweenRuns(arguments)); } } catch (HelpScreenException ignored) { } catch (ArgumentParserException ignored) { @@ -47,4 +46,9 @@ public static void main(String[] args) { logger.error("Interrupted exception."); } } + + private static boolean sleepBetweenRuns(Arguments arguments) throws ArgumentParserException, InterruptedException { + sleep(Duration.ofSeconds(arguments.waitTimeInSeconds())); + return true; + } } \ No newline at end of file diff --git a/src/main/java/name/rycroft/simon/synplex/Arguments.java b/src/main/java/name/rycroft/simon/synplex/Arguments.java index 51aaa4a..612318e 100644 --- a/src/main/java/name/rycroft/simon/synplex/Arguments.java +++ b/src/main/java/name/rycroft/simon/synplex/Arguments.java @@ -61,6 +61,7 @@ admin account (mum and dad) and a single managed account (girl and boy) in sync. .dest(ACCOUNTS); synchroniseAccountsGroup.addArgument("-s", "--single-run") + .help("Whether the application should exit after the first run, or run continuously.") .dest(SINGLE_RUN) .action(storeTrue()); diff --git a/src/main/java/name/rycroft/simon/synplex/SyncViews.java b/src/main/java/name/rycroft/simon/synplex/SyncViews.java index 32a28d6..3cc7cd7 100644 --- a/src/main/java/name/rycroft/simon/synplex/SyncViews.java +++ b/src/main/java/name/rycroft/simon/synplex/SyncViews.java @@ -89,6 +89,26 @@ AND guid NOT IN ( WHERE account_id = ? ); """; + private static final String UPDATE_METADATA_ITEM_SETTINGS = """ + WITH up AS ( + SELECT lft.* FROM metadata_item_settings lft + INNER JOIN metadata_item_settings rgt ON lft.guid = rgt.guid AND lft.account_id = ? AND rgt.account_id = ? + WHERE lft.updated_at > rgt.updated_at + ) + UPDATE metadata_item_settings SET + rating = (SELECT rating FROM up WHERE guid = metadata_item_settings.guid), + view_offset = (SELECT view_offset FROM up WHERE guid = metadata_item_settings.guid), + last_viewed_at = (SELECT last_viewed_at FROM up WHERE guid = metadata_item_settings.guid), + created_at = (SELECT created_at FROM up WHERE guid = metadata_item_settings.guid), + updated_at = (SELECT updated_at FROM up WHERE guid = metadata_item_settings.guid), + skip_count = (SELECT skip_count FROM up WHERE guid = metadata_item_settings.guid), + last_skipped_at = (SELECT last_skipped_at FROM up WHERE guid = metadata_item_settings.guid), + changed_at = (SELECT changed_at FROM up WHERE guid = metadata_item_settings.guid), + extra_data = (SELECT extra_data FROM up WHERE guid = metadata_item_settings.guid), + last_rated_at = (SELECT last_rated_at FROM up WHERE guid = metadata_item_settings.guid) + WHERE guid IN (SELECT guid FROM up) + AND account_id = ? + """; private static final String UPDATE_METADATA_ITEM_SETTINGS_VIEW_COUNT = """ UPDATE metadata_item_settings SET view_count = ( @@ -96,7 +116,12 @@ SELECT COUNT(*) FROM metadata_item_views WHERE metadata_item_settings.guid = metadata_item_views.guid AND metadata_item_settings.account_id = metadata_item_views.account_id ) - WHERE account_id = ?; + WHERE account_id = ? + AND view_count < ( + SELECT COUNT(*) FROM metadata_item_views + WHERE metadata_item_settings.guid = metadata_item_views.guid + AND metadata_item_settings.account_id = metadata_item_views.account_id + ) """; private final Database database; @@ -107,12 +132,15 @@ public SyncViews(Database database) { public void sync(long id) { try { - logger.info("Syncing views."); + logger.info("Inserting views."); database.update(INSERT_INTO_METADATA_ITEM_VIEWS, id, 1, id); database.update(INSERT_INTO_METADATA_ITEM_VIEWS, 1, id, 1); - logger.info("Syncing settings."); + logger.info("Inserting settings."); database.update(INSERT_INTO_METADATA_ITEM_SETTINGS, id, 1, id); database.update(INSERT_INTO_METADATA_ITEM_SETTINGS, 1, id, 1); + logger.info("Updating settings."); + database.update(UPDATE_METADATA_ITEM_SETTINGS, 1, id, id); + database.update(UPDATE_METADATA_ITEM_SETTINGS, id, id, 1); logger.info("Syncing view counts."); database.update(UPDATE_METADATA_ITEM_SETTINGS_VIEW_COUNT, id); database.update(UPDATE_METADATA_ITEM_SETTINGS_VIEW_COUNT, 1);