From 70644b5e069ebbee6fbdb1fae7dcf8617c174b70 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 21 Feb 2021 18:07:56 +0000 Subject: [PATCH] Combine master_index_{archive,group}_stats Signed-off-by: Graham --- .../openrs2/archive/cache/CacheExporter.kt | 72 +++++++---------- .../openrs2/archive/migrations/V1__init.sql | 81 ++++++++++++------- .../archive/templates/caches/index.html | 18 ++--- .../archive/templates/caches/show.html | 12 +-- 4 files changed, 97 insertions(+), 86 deletions(-) diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt index 3aa4551ff8..3e9ff1d48f 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt @@ -17,16 +17,21 @@ public class CacheExporter @Inject constructor( private val database: Database, private val alloc: ByteBufAllocator ) { - public data class ArchiveStats(val indexes: Long, val validIndexes: Long) { + public data class Stats( + val validIndexes: Long, + val indexes: Long, + val validGroups: Long, + val groups: Long, + val validKeys: Long, + val keys: Long + ) { public val allIndexesValid: Boolean = indexes == validIndexes public val validIndexesFraction: Double = if (indexes == 0L) { 1.0 } else { validIndexes.toDouble() / indexes } - } - public data class GroupStats(val groups: Long, val validGroups: Long, val keys: Long, val validKeys: Long) { public val allGroupsValid: Boolean = groups == validGroups public val validGroupsFraction: Double = if (groups == 0L) { 1.0 @@ -49,8 +54,7 @@ public class CacheExporter @Inject constructor( val timestamp: Instant?, val name: String?, val description: String?, - val archiveStats: ArchiveStats?, - val groupStats: GroupStats? + val stats: Stats? ) public data class Key( @@ -68,12 +72,11 @@ public class CacheExporter @Inject constructor( """ SELECT m.id, g.name, m.build, m.timestamp, m.name, - a.indexes, a.valid_indexes, gs.groups, gs.valid_groups, gs.keys, gs.valid_keys + s.valid_indexes, s.indexes, s.valid_groups, s.groups, s.valid_keys, s.keys FROM master_indexes m JOIN games g ON g.id = m.game_id JOIN containers c ON c.id = m.container_id - LEFT JOIN master_index_archive_stats a ON a.master_index_id = m.id - LEFT JOIN master_index_group_stats gs ON gs.master_index_id = m.id + LEFT JOIN master_index_stats s ON s.master_index_id = m.id ORDER BY g.name ASC, m.build ASC, m.timestamp ASC """.trimIndent() ).use { stmt -> @@ -92,25 +95,19 @@ public class CacheExporter @Inject constructor( val timestamp = rows.getTimestamp(4)?.toInstant() val name = rows.getString(5) - val indexes = rows.getLong(6) - val archiveStats = if (!rows.wasNull()) { - val validIndexes = rows.getLong(7) - ArchiveStats(indexes, validIndexes) - } else { - null - } - - val groups = rows.getLong(8) - val groupStats = if (!rows.wasNull()) { - val validGroups = rows.getLong(9) - val keys = rows.getLong(10) - val validKeys = rows.getLong(11) - GroupStats(groups, validGroups, keys, validKeys) + val validIndexes = rows.getLong(6) + val stats = if (!rows.wasNull()) { + val indexes = rows.getLong(7) + val validGroups = rows.getLong(8) + val groups = rows.getLong(9) + val validKeys = rows.getLong(10) + val keys = rows.getLong(11) + Stats(validIndexes, indexes, validGroups, groups, validKeys, keys) } else { null } - caches += Cache(id, game, build, timestamp, name, description = null, archiveStats, groupStats) + caches += Cache(id, game, build, timestamp, name, description = null, stats) } caches @@ -125,12 +122,11 @@ public class CacheExporter @Inject constructor( """ SELECT g.name, m.build, m.timestamp, m.name, m.description, - a.indexes, a.valid_indexes, gs.groups, gs.valid_groups, gs.keys, gs.valid_keys + s.valid_indexes, s.indexes, s.valid_groups, s.groups, s.valid_keys, s.keys FROM master_indexes m JOIN games g ON g.id = m.game_id JOIN containers c ON c.id = m.container_id - LEFT JOIN master_index_archive_stats a ON a.master_index_id = m.id - LEFT JOIN master_index_group_stats gs ON gs.master_index_id = m.id + LEFT JOIN master_index_stats s ON s.master_index_id = m.id WHERE m.id = ? """.trimIndent() ).use { stmt -> @@ -152,25 +148,19 @@ public class CacheExporter @Inject constructor( val name = rows.getString(4) val description = rows.getString(5) - val indexes = rows.getLong(6) - val archiveStats = if (!rows.wasNull()) { - val validIndexes = rows.getLong(7) - ArchiveStats(indexes, validIndexes) - } else { - null - } - - val groups = rows.getLong(8) - val groupStats = if (!rows.wasNull()) { - val validGroups = rows.getLong(9) - val keys = rows.getLong(10) - val validKeys = rows.getLong(11) - GroupStats(groups, validGroups, keys, validKeys) + val validIndexes = rows.getLong(6) + val stats = if (!rows.wasNull()) { + val indexes = rows.getLong(7) + val validGroups = rows.getLong(8) + val groups = rows.getLong(9) + val validKeys = rows.getLong(10) + val keys = rows.getLong(11) + Stats(validIndexes, indexes, validGroups, groups, validKeys, keys) } else { null } - return@execute Cache(id, game, build, timestamp, name, description, archiveStats, groupStats) + return@execute Cache(id, game, build, timestamp, name, description, stats) } } } diff --git a/archive/src/main/resources/org/openrs2/archive/migrations/V1__init.sql b/archive/src/main/resources/org/openrs2/archive/migrations/V1__init.sql index ec9808c5bd..3a24461a33 100644 --- a/archive/src/main/resources/org/openrs2/archive/migrations/V1__init.sql +++ b/archive/src/main/resources/org/openrs2/archive/migrations/V1__init.sql @@ -160,37 +160,58 @@ JOIN groups g ON g.archive_id = i.archive_id AND g.group_id = ig.group_id AND ( ) JOIN containers c ON c.id = g.container_id AND c.crc32 = ig.crc32; -CREATE MATERIALIZED VIEW master_index_archive_stats (master_index_id, indexes, valid_indexes) AS -SELECT a.master_index_id, COUNT(*), COUNT(i.container_id) -FROM master_index_archives a -LEFT JOIN groups g ON g.archive_id = 255 AND g.group_id = a.archive_id::INTEGER AND - g.version = a.version AND NOT g.version_truncated AND - g.container_id IN (SELECT id FROM containers WHERE crc32 = a.crc32) -LEFT JOIN containers c ON c.id = g.container_id -LEFT JOIN indexes i ON i.container_id = g.container_id AND i.version = a.version -GROUP BY a.master_index_id; - -CREATE UNIQUE INDEX ON master_index_archive_stats (master_index_id); - -CREATE MATERIALIZED VIEW master_index_group_stats (master_index_id, groups, valid_groups, keys, valid_keys, size) AS +CREATE MATERIALIZED VIEW master_index_stats ( + master_index_id, + valid_indexes, + indexes, + valid_groups, + groups, + valid_keys, + keys, + size +) AS SELECT - i.master_index_id, - COUNT(*), - COUNT(g.container_id), - COUNT(*) FILTER (WHERE c.encrypted), - COUNT(*) FILTER (WHERE c.key_id IS NOT NULL), - SUM(length(c.data)) -FROM resolved_indexes i -JOIN index_groups ig ON ig.container_id = i.container_id -LEFT JOIN groups g ON g.archive_id = i.archive_id AND g.group_id = ig.group_id AND ( - (g.version = ig.version AND NOT g.version_truncated) OR - (g.version = ig.version & 65535 AND g.version_truncated) -) AND g.container_id IN (SELECT id FROM containers WHERE crc32 = ig.crc32) -LEFT JOIN containers c ON c.id = g.container_id -LEFT JOIN keys k ON k.id = c.key_id -GROUP BY i.master_index_id; - -CREATE UNIQUE INDEX ON master_index_group_stats (master_index_id); + a.master_index_id, + a.valid_indexes, + a.indexes, + COALESCE(g.valid_groups, 0), + COALESCE(g.groups, 0), + COALESCE(g.valid_keys, 0), + COALESCE(g.keys, 0), + COALESCE(g.size, 0) +FROM ( + SELECT + a.master_index_id, + COUNT(i.container_id) AS valid_indexes, + COUNT(*) AS indexes + FROM master_index_archives a + LEFT JOIN groups g ON g.archive_id = 255 AND g.group_id = a.archive_id::INTEGER AND + g.version = a.version AND NOT g.version_truncated AND + g.container_id IN (SELECT id FROM containers WHERE crc32 = a.crc32) + LEFT JOIN containers c ON c.id = g.container_id + LEFT JOIN indexes i ON i.container_id = g.container_id AND i.version = a.version + GROUP BY a.master_index_id +) a +LEFT JOIN ( + SELECT + i.master_index_id, + COUNT(g.container_id) AS valid_groups, + COUNT(*) AS groups, + COUNT(*) FILTER (WHERE c.key_id IS NOT NULL) AS valid_keys, + COUNT(*) FILTER (WHERE c.encrypted) AS keys, + SUM(length(c.data)) AS size + FROM resolved_indexes i + JOIN index_groups ig ON ig.container_id = i.container_id + LEFT JOIN groups g ON g.archive_id = i.archive_id AND g.group_id = ig.group_id AND ( + (g.version = ig.version AND NOT g.version_truncated) OR + (g.version = ig.version & 65535 AND g.version_truncated) + ) AND g.container_id IN (SELECT id FROM containers WHERE crc32 = ig.crc32) + LEFT JOIN containers c ON c.id = g.container_id + LEFT JOIN keys k ON k.id = c.key_id + GROUP BY i.master_index_id +) g ON g.master_index_id = a.master_index_id; + +CREATE UNIQUE INDEX ON master_index_stats (master_index_id); CREATE VIEW collisions (archive_id, group_id, crc32, truncated_version, containers) AS SELECT g.archive_id, g.group_id, c.crc32, g.version & 65535 AS truncated_version, COUNT(DISTINCT c.id) diff --git a/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html b/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html index 79402526da..5b12b4ae38 100644 --- a/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html +++ b/archive/src/main/resources/org/openrs2/archive/templates/caches/index.html @@ -33,29 +33,29 @@ - + th:text="${cache.stats}? ${cache.stats.validIndexes} + ' / ' + ${cache.stats.indexes}">
+ th:text="${cache.stats}? '(' + ${#numbers.formatPercent(cache.stats.validIndexesFraction, 1, 2)} + ')' : 'Calculating...'"> - + th:text="${cache.stats}? ${#numbers.formatInteger(cache.stats.validGroups, 1, 'COMMA')} + ' / ' + ${#numbers.formatInteger(cache.stats.groups, 1, 'COMMA')}">
+ th:text="${cache.stats}? '(' + ${#numbers.formatPercent(cache.stats.validGroupsFraction, 1, 2)} + ')' : 'Calculating...'"> - + th:text="${cache.stats}? ${#numbers.formatInteger(cache.stats.validKeys, 1, 'COMMA')} + ' / ' + ${#numbers.formatInteger(cache.stats.keys, 1, 'COMMA')}">
+ th:text="${cache.stats}? '(' + ${#numbers.formatPercent(cache.stats.validKeysFraction, 1, 2)} + ')' : 'Calculating...'">
diff --git a/archive/src/main/resources/org/openrs2/archive/templates/caches/show.html b/archive/src/main/resources/org/openrs2/archive/templates/caches/show.html index 0b64b12110..8207b45cb3 100644 --- a/archive/src/main/resources/org/openrs2/archive/templates/caches/show.html +++ b/archive/src/main/resources/org/openrs2/archive/templates/caches/show.html @@ -33,22 +33,22 @@ Indexes - + Calculating... Groups - + Calculating... Keys - + Calculating...