From c45b1da636afacb26dad4c2b69247b72dfd2ebdd Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 27 Mar 2023 17:31:29 +0200 Subject: [PATCH] Improve search service's readme and components. --- gymboard-app/src/api/main/index.ts | 16 ++++++++++++++++ gymboard-app/src/api/search/index.ts | 2 +- gymboard-app/src/pages/SubmissionPage.vue | 8 ++++---- gymboard-search/.gitignore | 3 +-- gymboard-search/README.md | 6 +++++- .../GymboardSearchApplication.java | 2 +- .../gymboardsearch/index/IndexComponents.java | 4 ++-- .../gymboardsearch/index/JdbcIndexGenerator.java | 13 +++---------- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/gymboard-app/src/api/main/index.ts b/gymboard-app/src/api/main/index.ts index c0b15bc..0af1511 100644 --- a/gymboard-app/src/api/main/index.ts +++ b/gymboard-app/src/api/main/index.ts @@ -9,11 +9,27 @@ export const api = axios.create({ baseURL: process.env.API_URL, }); +/** + * The main Gymboard API namespace, containing various modules that deal with + * different parts of the API, like auth or users. + */ class GymboardApi { public readonly auth = new AuthModule(); public readonly gyms = new GymsModule(); public readonly users = new UsersModule(); public readonly exercises = new ExercisesModule(); public readonly leaderboards = new LeaderboardsModule(); + + /** + * Gets the status of the Gymboard API. + */ + public async getStatus(): Promise { + try { + await api.get('/status'); + return true; + } catch (e) { + return false; + } + } } export default new GymboardApi(); diff --git a/gymboard-app/src/api/search/index.ts b/gymboard-app/src/api/search/index.ts index 9c8a730..0597f91 100644 --- a/gymboard-app/src/api/search/index.ts +++ b/gymboard-app/src/api/search/index.ts @@ -31,7 +31,7 @@ class SearchApi { public async getStatus(): Promise { try { - const response = await api.get(`/status`); + await api.get('/status'); return true; } catch (error) { return false; diff --git a/gymboard-app/src/pages/SubmissionPage.vue b/gymboard-app/src/pages/SubmissionPage.vue index f4c5401..c28a7e6 100644 --- a/gymboard-app/src/pages/SubmissionPage.vue +++ b/gymboard-app/src/pages/SubmissionPage.vue @@ -40,10 +40,10 @@ import { useRoute, useRouter } from 'vue-router'; import { DateTime } from 'luxon'; import { getFileUrl } from 'src/api/cdn'; import { getGymRoute } from 'src/router/gym-routing'; -import {useAuthStore} from "stores/auth-store"; -import {showApiErrorToast} from "src/utils"; -import {useI18n} from "vue-i18n"; -import {useQuasar} from "quasar"; +import {useAuthStore} from 'stores/auth-store'; +import {showApiErrorToast} from 'src/utils'; +import {useI18n} from 'vue-i18n'; +import {useQuasar} from 'quasar'; const submission: Ref = ref(); diff --git a/gymboard-search/.gitignore b/gymboard-search/.gitignore index e91b450..ca7a78c 100644 --- a/gymboard-search/.gitignore +++ b/gymboard-search/.gitignore @@ -32,5 +32,4 @@ build/ ### VS Code ### .vscode/ -gym-index/ -user-index/ +indexes/ diff --git a/gymboard-search/README.md b/gymboard-search/README.md index 082e3dc..4cba432 100644 --- a/gymboard-search/README.md +++ b/gymboard-search/README.md @@ -2,8 +2,12 @@ A simple search API for Gymboard, backed by Apache Lucene. This application includes both indexing of Gyms and other searchable entities, and a public web interface for searching those indexes. -This application is configured with read-only access to the central Gymboard database, for its indexing operations. +This application is configured with read-only access to the central Gymboard database for its indexing operations. ## Developing Currently, this application is designed to boot up and immediately read the latest data from the Gymboard API's database to rebuild its indexes, then continue to do so at scheduled intervals. + +Indexes are stored in the `indexes/` directory. + +**Note:** At some point, it would be a lot more efficient to have this application listen for entity update events published by other services, instead of fully scanning the entire database for each index operation. But for now, that's not needed. diff --git a/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/GymboardSearchApplication.java b/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/GymboardSearchApplication.java index 00502de..3852c43 100644 --- a/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/GymboardSearchApplication.java +++ b/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/GymboardSearchApplication.java @@ -26,7 +26,7 @@ public class GymboardSearchApplication { private final JdbcIndexGenerator gymIndexGenerator; private final JdbcIndexGenerator userIndexGenerator; - @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS) + @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES) public void reIndex() { gymIndexGenerator.generate(); userIndexGenerator.generate(); diff --git a/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/IndexComponents.java b/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/IndexComponents.java index b086f7c..bfc4f6c 100644 --- a/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/IndexComponents.java +++ b/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/IndexComponents.java @@ -36,7 +36,7 @@ public class IndexComponents { @Bean public JdbcIndexGenerator userIndexGenerator(JdbcConnectionSupplier connectionSupplier) throws IOException { return new JdbcIndexGenerator( - Path.of("user-index"), + Path.of("indexes", "users"), connectionSupplier, PlainQueryResultSetSupplier.fromResourceFile("/sql/select-users.sql"), rs -> { @@ -66,7 +66,7 @@ public class IndexComponents { @Bean public JdbcIndexGenerator gymIndexGenerator(JdbcConnectionSupplier connectionSupplier) throws IOException { return new JdbcIndexGenerator( - Path.of("gym-index"), + Path.of("indexes", "gyms"), connectionSupplier, PlainQueryResultSetSupplier.fromResourceFile("/sql/select-gyms.sql"), rs -> { diff --git a/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/JdbcIndexGenerator.java b/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/JdbcIndexGenerator.java index 657da44..304c5d7 100644 --- a/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/JdbcIndexGenerator.java +++ b/gymboard-search/src/main/java/nl/andrewlalis/gymboardsearch/index/JdbcIndexGenerator.java @@ -33,15 +33,7 @@ public class JdbcIndexGenerator { public void generate() { log.info("Generating index at {}.", indexDir); - if (Files.exists(indexDir)) { - try { - FileSystemUtils.deleteRecursively(indexDir); - Files.createDirectories(indexDir); - } catch (IOException e) { - log.error("Failed to reset index directory.", e); - return; - } - } + long start = System.currentTimeMillis(); try ( Connection conn = connectionSupplier.getConnection(); ResultSet rs = resultSetSupplier.supply(conn); @@ -62,7 +54,8 @@ public class JdbcIndexGenerator { log.error("Failed to add document.", e); } } - log.info("Indexed {} entities.", count); + long dur = System.currentTimeMillis() - start; + log.info("Indexed {} entities for {} index in {} ms.", count, indexDir, dur); indexWriter.close(); } catch (Exception e) { log.error("Failed to prepare indexing components.", e);