Skip to content

Commit

Permalink
Synplex 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrycroft committed Mar 3, 2024
1 parent 032972a commit f96a6d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "name.rycroft.simon"
version = "1.0.0"
version = "1.0.1"

repositories {
mavenCentral()
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/name/rycroft/simon/Synplex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/java/name/rycroft/simon/synplex/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
34 changes: 31 additions & 3 deletions src/main/java/name/rycroft/simon/synplex/SyncViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,39 @@ 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 = (
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;

Expand All @@ -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);
Expand Down

0 comments on commit f96a6d7

Please sign in to comment.