From 37da93069e1d414a9f21583f70e642a62c828fb9 Mon Sep 17 00:00:00 2001
From: Matija Obreza <matija.obreza@croptrust.org>
Date: Sun, 13 Nov 2022 17:52:53 +0100
Subject: [PATCH] ES: Reindexing got stuck with STOP_REINDEXING set to true.
 This allows for resetting tese cluster-wide flags.

(cherry picked from commit 527d116a3c4ba59e53c2539cdda7328e0f258cdd)
---
 .../service/genesys/ElasticSearchService.ts   | 20 +++++++++++++++++++
 .../ui-express/locales/en/translations.json   |  4 +++-
 .../ui-express/src/cluster/actions/admin.ts   |  8 ++++++++
 .../ui-express/src/cluster/translations.json  |  4 +++-
 .../cluster/ui/admin/ElasticSearchPage.tsx    |  8 ++++++--
 .../ui/dashboard/DatasetFilePreviewPage.tsx   |  2 +-
 6 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/workspaces/client/src/service/genesys/ElasticSearchService.ts b/workspaces/client/src/service/genesys/ElasticSearchService.ts
index dc278579a..0499cace2 100644
--- a/workspaces/client/src/service/genesys/ElasticSearchService.ts
+++ b/workspaces/client/src/service/genesys/ElasticSearchService.ts
@@ -9,6 +9,7 @@ import AliasMetaData from '@genesys-pgr/client/model/AliasMetaData';
 
 const URL_STOP_REINDEX = '/api/v1/admin/elastic/action/stop-reindex';
 const URL_STOP_REINDEX_ALL = '/api/v1/admin/elastic/action/stop-reindex-all';
+const URL_ALLOW_REINDEX_ALL = '/api/v1/admin/elastic/action/allow-reindex-all';
 const URL_REMOVE_ELASTIC_FILTERED = '/api/v1/admin/elastic/action/remove/filtered';
 const URL_REMOVE_ELASTIC_FILTERED_ACCN = '/api/v1/admin/elastic/action/remove/accn';
 const URL_REINDEX_ELASTIC_CONTENT = '/api/v1/admin/elastic/action/reindex';
@@ -70,6 +71,25 @@ class ElasticSearchService {
     }).then(({ data }) => data as boolean);
   }
 
+  /**
+   * allowReindexAll at /api/v1/admin/elastic/action/allow-reindex-all
+   *
+   * @param xhrConfig additional xhr config
+   */
+  public allowReindexAll = (xhrConfig?: AxiosRequestConfig): Promise<boolean> => {
+
+    const apiUrl = URL_ALLOW_REINDEX_ALL;
+    // log.debug(`Fetching from ${apiUrl}`);
+    const content = { /* No content in request body */ };
+
+    return this._axios.request({
+      ...xhrConfig,
+      url: apiUrl,
+      method: 'POST',
+      ...content,
+    }).then(({ data }) => data as boolean);
+  }
+
   /**
    * removeElasticFiltered at /api/v1/admin/elastic/action/remove/filtered
    *
diff --git a/workspaces/ui-express/locales/en/translations.json b/workspaces/ui-express/locales/en/translations.json
index 34c2b33b9..1ff59d5b1 100644
--- a/workspaces/ui-express/locales/en/translations.json
+++ b/workspaces/ui-express/locales/en/translations.json
@@ -1244,6 +1244,7 @@
           "accessionFilter": "Accession filter",
           "instituteFilter": "Institute filter",
           "reindexStopped": "Reindex stopped",
+          "reindexEnabled": "Reindexing enabled",
           "alias": "Alias",
           "index": "Index",
           "action": {
@@ -1252,7 +1253,8 @@
             "moveAlias": "Move alias",
             "deleteAlias": "Delete alias",
             "deleteIndex": "Delete index",
-            "stopReindex": "Stop reindex"
+            "stopReindex": "Stop reindexing",
+            "allowReindex": "Enable reindexing"
           }
         },
         "hazelcast": {
diff --git a/workspaces/ui-express/src/cluster/actions/admin.ts b/workspaces/ui-express/src/cluster/actions/admin.ts
index 2f045aa7c..75d6350c3 100644
--- a/workspaces/ui-express/src/cluster/actions/admin.ts
+++ b/workspaces/ui-express/src/cluster/actions/admin.ts
@@ -19,6 +19,7 @@ const apiReindexElasticFilteredAccession = createPureApiCaller(ElasticSearchServ
 const apiRemoveElasticFilteredAccession = createPureApiCaller(ElasticSearchService.removeElasticFilteredAccession);
 const apiViewIndexesAndAliases = createApiCaller(ElasticSearchService.viewIndexesAndAliases, ADMIN_RECEIVE_INDEXES_AND_ALIASES);
 const apiStopReindex = createPureApiCaller(ElasticSearchService.stopReindexAll);
+const apiAllowReindex = createPureApiCaller(ElasticSearchService.allowReindexAll);
 
 export const reindexElasticContent = (type: string) => (dispatch) => {
   return dispatch(apiReindexElasticContent(type))
@@ -52,6 +53,13 @@ export const stopReindex = () => (dispatch) => {
     });
 };
 
+export const allowReindex = () => (dispatch) => {
+  return dispatch(apiAllowReindex())
+    .then(() => {
+      dispatch(showSnackbar('cluster.admin.p.elastic.reindexEnabled'));
+    });
+};
+
 export const submitElasticAliasIndexForm = () => (dispatch) => {
   return dispatch(submit(ELASTICSEARCH_ALIAS_INDEX_FORM))
 };
diff --git a/workspaces/ui-express/src/cluster/translations.json b/workspaces/ui-express/src/cluster/translations.json
index 9cda8c9da..89bd98fcf 100644
--- a/workspaces/ui-express/src/cluster/translations.json
+++ b/workspaces/ui-express/src/cluster/translations.json
@@ -16,6 +16,7 @@
         "accessionFilter": "Accession filter",
         "instituteFilter": "Institute filter",
         "reindexStopped": "Reindex stopped",
+        "reindexEnabled": "Reindexing enabled",
         "alias": "Alias",
         "index": "Index",
         "action": {
@@ -24,7 +25,8 @@
           "moveAlias": "Move alias",
           "deleteAlias": "Delete alias",
           "deleteIndex": "Delete index",
-          "stopReindex": "Stop reindex"
+          "stopReindex": "Stop reindexing",
+          "allowReindex": "Enable reindexing"
         }
       },
       "hazelcast": {
diff --git a/workspaces/ui-express/src/cluster/ui/admin/ElasticSearchPage.tsx b/workspaces/ui-express/src/cluster/ui/admin/ElasticSearchPage.tsx
index 892dbbb10..e0c8b338c 100644
--- a/workspaces/ui-express/src/cluster/ui/admin/ElasticSearchPage.tsx
+++ b/workspaces/ui-express/src/cluster/ui/admin/ElasticSearchPage.tsx
@@ -24,7 +24,7 @@ import AliasDialog from 'cluster/ui/c/AliasDialog';
 
 // actions
 import {
-  stopReindex, reindexElasticContent,
+  stopReindex, allowReindex, reindexElasticContent,
   reindexElasticFilteredAccession,
   removeElasticFilteredAccession, submitElasticAliasIndexForm,
   viewIndexesAndAliases
@@ -257,7 +257,7 @@ class ElasticSearchPage extends React.Component<PropsFromRedux & WithTranslation
   };
 
   public render() {
-    const { t, classes, indexesAndAliases, stopReindex, suggestions } = this.props;
+    const { t, classes, indexesAndAliases, stopReindex, allowReindex, suggestions } = this.props;
     const { sidebarOpened, selectedModel, selectedFilter, withFilter, selectedAliasList, error } = this.state;
 
     const suggestionTerms = new Map();
@@ -306,6 +306,9 @@ class ElasticSearchPage extends React.Component<PropsFromRedux & WithTranslation
             <Button variant="contained" onClick={ stopReindex }>
               { t('cluster.admin.p.elastic.action.stopReindex') }
             </Button>
+            <Button variant="contained" onClick={ allowReindex }>
+              { t('cluster.admin.p.elastic.action.allowReindex') }
+            </Button>
           </div>
           <h2>{ t('cluster.admin.p.elastic.elasticReindex') }</h2>
           <div className={ classes.section }>
@@ -402,6 +405,7 @@ const mapStateToProps = (state, ownProps) => ({
 
 const mapDispatch = {
   stopReindex,
+  allowReindex,
   reindexElasticFilteredAccession,
   removeElasticFilteredAccession,
   viewIndexesAndAliases,
diff --git a/workspaces/ui-express/src/datasets/ui/dashboard/DatasetFilePreviewPage.tsx b/workspaces/ui-express/src/datasets/ui/dashboard/DatasetFilePreviewPage.tsx
index e49d0dfaa..351e42431 100644
--- a/workspaces/ui-express/src/datasets/ui/dashboard/DatasetFilePreviewPage.tsx
+++ b/workspaces/ui-express/src/datasets/ui/dashboard/DatasetFilePreviewPage.tsx
@@ -85,7 +85,7 @@ const DatasetFilePreviewPage = ({ getDatasetFileData, getDatasetFilePreview, uui
   }, [ uuid, filePreview, sheet, previewCall, getDatasetFileData ])
 
   const fileDescriptor = React.useMemo((): Partial<Descriptor> => {
-    const amphibian = filePreview?.sheets[sheet]?.descriptors[selectedColumn] ?? undefined;
+    const amphibian = filePreview?.sheets?.[sheet]?.descriptors?.[selectedColumn];
     if (amphibian) {
       const sampleCount = amphibian?.examples.length ?? 0;
       const numberCount = amphibian.examples.filter(isNumeric).length;
-- 
GitLab