Kaynağa Gözat

impl getInfoForAdmin

Yuki Takei 6 yıl önce
ebeveyn
işleme
6c9c38572f

+ 12 - 4
src/server/routes/apiv3/search.js

@@ -26,17 +26,25 @@ module.exports = (crowi) => {
    *    get:
    *      tags: [Search]
    *      summary: /search/indices
-   *      description: Get current status of indices and tasks
+   *      description: Get current status of indices
    *      responses:
    *        200:
-   *          description: Status of indices and tasks
+   *          description: Status of indices
    *          content:
    *            application/json:
    *              schema:
    *                properties:
    */
   router.get('/indices', helmet.noCache(), accessTokenParser, loginRequired, adminRequired, async(req, res) => {
-    res.status(200).send({});
+    // connect to search service
+    try {
+      const search = crowi.getSearcher();
+      const info = await search.getInfoForAdmin();
+      return res.status(200).send({ info });
+    }
+    catch (err) {
+      return res.apiv3Err(err);
+    }
   });
 
   /**
@@ -46,7 +54,7 @@ module.exports = (crowi) => {
    *    put:
    *      tags: [Search]
    *      summary: /search/indices
-   *      description: Init indices and tasks
+   *      description: Init indices
    *      responses:
    *        200:
    *          description: Return 200

+ 46 - 24
src/server/service/search-delegator/elasticsearch.js

@@ -65,6 +65,38 @@ class ElasticsearchDelegator {
     this.indexName = indexName;
   }
 
+  /**
+   * return information object to connect to ES
+   * @return {object} { host, httpAuth, indexName}
+   */
+  getConnectionInfo() {
+    let indexName = 'crowi';
+    let host = this.esUri;
+    let httpAuth = '';
+
+    const elasticsearchUri = this.configManager.getConfig('crowi', 'app:elasticsearchUri');
+
+    const url = new URL(elasticsearchUri);
+    if (url.pathname !== '/') {
+      host = `${url.protocol}//${url.host}`;
+      indexName = url.pathname.substring(1); // omit heading slash
+
+      if (url.username != null && url.password != null) {
+        httpAuth = `${url.username}:${url.password}`;
+      }
+    }
+
+    return {
+      host,
+      httpAuth,
+      indexName,
+    };
+  }
+
+  async init() {
+    return this.initIndices();
+  }
+
   async getInfo() {
     const info = await this.client.nodes.info();
     if (!info._nodes || !info.nodes) {
@@ -94,38 +126,28 @@ class ElasticsearchDelegator {
     return { esVersion, esNodeInfos };
   }
 
-  /**
-   * return information object to connect to ES
-   * @return {object} { host, httpAuth, indexName}
-   */
-  getConnectionInfo() {
-    let indexName = 'crowi';
-    let host = this.esUri;
-    let httpAuth = '';
+  async getInfoForAdmin() {
+    const { client, indexName, aliasName } = this;
 
-    const elasticsearchUri = this.configManager.getConfig('crowi', 'app:elasticsearchUri');
+    const tmpIndexName = `${indexName}-tmp`;
 
-    const url = new URL(elasticsearchUri);
-    if (url.pathname !== '/') {
-      host = `${url.protocol}//${url.host}`;
-      indexName = url.pathname.substring(1); // omit heading slash
+    const isExistsMainIndex = await client.indices.exists({ index: indexName });
+    const isMainIndexHasAlias = isExistsMainIndex && await client.indices.existsAlias({ name: aliasName, index: indexName });
 
-      if (url.username != null && url.password != null) {
-        httpAuth = `${url.username}:${url.password}`;
-      }
-    }
+    const isExistsTmpIndex = await client.indices.exists({ index: tmpIndexName });
+    const isTmpIndexHasAlias = isExistsTmpIndex && await client.indices.existsAlias({ name: aliasName, index: tmpIndexName });
+
+    const isNormalized = isExistsMainIndex && isMainIndexHasAlias && !isExistsTmpIndex && !isTmpIndexHasAlias;
 
     return {
-      host,
-      httpAuth,
-      indexName,
+      isExistsMainIndex,
+      isExistsTmpIndex,
+      isMainIndexHasAlias,
+      isTmpIndexHasAlias,
+      isNormalized,
     };
   }
 
-  async init() {
-    return this.initIndices();
-  }
-
   /**
    * build index
    */

+ 4 - 0
src/server/service/search.js

@@ -68,6 +68,10 @@ class SearchService {
     return this.delegator.getInfo();
   }
 
+  async getInfoForAdmin() {
+    return this.delegator.getInfoForAdmin();
+  }
+
   async buildIndex() {
     return this.delegator.buildIndex();
   }