From deda16f0f5d39760df683bd968cee70863fde7fc Mon Sep 17 00:00:00 2001
From: Stephan Porada <sporada@uni-bielefeld.de>
Date: Mon, 27 Apr 2020 16:16:57 +0200
Subject: [PATCH] Add meta data recv after corpus analysis init

---
 app/corpora/events.py                         | 20 +++++++++++
 app/static/js/nopaque.CorpusAnalysisClient.js | 33 +++++++++++++++++--
 app/static/js/nopaque.Results.js              | 14 +++++++-
 app/static/js/nopaque.callbacks.js            |  8 +++++
 app/templates/corpora/analyse_corpus.html.j2  |  9 ++++-
 5 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/app/corpora/events.py b/app/corpora/events.py
index f8597a67..42e3249d 100644
--- a/app/corpora/events.py
+++ b/app/corpora/events.py
@@ -29,6 +29,26 @@ def init_corpus_analysis(corpus_id):
                                    corpus_id, current_user.id, request.sid)
 
 
+@socketio.on('corpus_analysis_get_meta_data')
+@socketio_login_required
+def corpus_analysis_get_meta_data(corpus_id):
+    # get meta data from db
+    db_corpus = Corpus.query.get(corpus_id)
+    # TODO: Check if current user is actually the creator of the corpus?
+    metadata = {}
+    metadata["corpus_name"] = db_corpus.title
+    metadata["corpus_description"] = db_corpus.description
+    metadata["corpus_creation_date"] = db_corpus.creation_date.isoformat()
+    # get meta data from corpus in cqp server
+    client = corpus_analysis_clients.get(request.sid)
+    client_corpus = client.corpora.get('CORPUS')
+    metadata["corpus_properties"] = client_corpus.attrs['properties']
+    # metadata["corpus_properties"] = client.api.corpus_properties('CORPUS')
+    payload = metadata
+    response = {'code': 200, 'desc': 'Corpus meta data', 'msg': 'OK', 'payload': payload}
+    socketio.emit('corpus_analysis_send_meta_data', response, room=request.sid)
+
+
 @socketio.on('corpus_analysis_query')
 @socketio_login_required
 def corpus_analysis_query(query):
diff --git a/app/static/js/nopaque.CorpusAnalysisClient.js b/app/static/js/nopaque.CorpusAnalysisClient.js
index 268ba048..14df19ce 100644
--- a/app/static/js/nopaque.CorpusAnalysisClient.js
+++ b/app/static/js/nopaque.CorpusAnalysisClient.js
@@ -7,12 +7,13 @@ class CorpusAnalysisClient {
 
     // socket on event for corpous analysis initialization
     socket.on("corpus_analysis_init", (response) => {
-      var errorText;
+      let errorText;
 
       if (response.code === 200) {
         console.log(`corpus_analysis_init: ${response.code} - ${response.msg}`);
         if (this.callbacks.init != undefined) {
           this.callbacks.init(response.payload);
+          this.callbacks.get_metadata();  // should hold the function getMetaData
         }
         if (this.displays.init != undefined) {
           this.displays.init.setVisibilityByStatus("success");
@@ -30,9 +31,31 @@ class CorpusAnalysisClient {
       }
     });
 
+    // socket on event for recieving meta
+    socket.on('corpus_analysis_send_meta_data', (response) => {
+      let errorText;
+
+      if (response.code === 200) {
+        console.log(`corpus_analysis_send_meta_data: ${response.code} - ${response.msg} - ${response.desc}`);
+        if (this.callbacks.recv_meta_data != undefined) {
+          this.callbacks.recv_meta_data(response.payload);
+        }
+      } else {
+        errorText = `Error ${response.code} - ${response.msg}`;
+        if (this.displays.init.errorContainer != undefined)  {
+          this.displays.init.errorContainer.innerHTML = `<p class="red-text">` +
+                   `<i class="material-icons tiny">error</i> ${errorText}</p>`;
+        }
+        if (this.displays.init != undefined)  {
+          this.displays.init.setVisibilityByStatus("error");
+        }
+        console.error(`corpus_analysis_send_meta_data: ${errorText}`);
+      }
+    });
+
     // socket on event for recieveing query results
     socket.on("corpus_analysis_query", (response) => {
-      var errorText;
+      let errorText;
 
       if (response.code === 200) {
         console.log(`corpus_analysis_query: ${response.code} - ${response.msg}`);
@@ -80,6 +103,12 @@ class CorpusAnalysisClient {
     this.socket.emit("corpus_analysis_init", this.corpusId);
   }
 
+  getMetaData() {
+    // just emits thos to tell the server to gather all meta dat infos and send
+    // those back
+    this.socket.emit("corpus_analysis_get_meta_data", this.corpusId);
+  }
+
   query(queryStr) {
     let displayOptionsData;
     let resultListOptions;
diff --git a/app/static/js/nopaque.Results.js b/app/static/js/nopaque.Results.js
index 2afbc917..28d63ff0 100644
--- a/app/static/js/nopaque.Results.js
+++ b/app/static/js/nopaque.Results.js
@@ -1,13 +1,15 @@
 class Results {
-  constructor(resultsJSON, resultsList) {
+  constructor(resultsJSON, resultsList , metaDataJSON) {
   this.resultsJSON = resultsJSON;
   this.resultsList = resultsList;
+  this.metaDataJSON = metaDataJSON
   }
 
   clearAll() {
     this.resultsList.clear();
     this.resultsList.update();
     this.resultsJSON.init();
+    this.metaDataJSON.init();
   }
 }
 
@@ -73,4 +75,14 @@ class ResultsJSON {
     // start actual download
     this.download(downloadElement, dataStr, resultFilename, "text/json", ".json")
   }
+}
+
+class MetaDataJSON {
+  // Sets empty object structure. Also usefull to delete old results.
+  init() {
+    this["corpus_name"] = "";
+    this["corpus_description"] = "";
+    this["corpus_creation_date"] = "";
+    this["corpus_properties"] = "";
+  }
 }
\ No newline at end of file
diff --git a/app/static/js/nopaque.callbacks.js b/app/static/js/nopaque.callbacks.js
index d283e742..3ccfd0f4 100644
--- a/app/static/js/nopaque.callbacks.js
+++ b/app/static/js/nopaque.callbacks.js
@@ -1,3 +1,11 @@
+function recvMetaData(payload) {
+  results.metaDataJSON.corpus_name = payload.corpus_name;
+  results.metaDataJSON.corpus_description = payload.corpus_description;
+  results.metaDataJSON.corpus_creation_date = payload.corpus_creation_date;
+  results.metaDataJSON.corpus_properties = payload.corpus_properties;
+  console.log(results.metaDataJSON);
+}
+
 function querySetup(payload) {
   // This is called when a query was successfull
   // some hiding and resetting
diff --git a/app/templates/corpora/analyse_corpus.html.j2 b/app/templates/corpora/analyse_corpus.html.j2
index 8cbf4df8..1441126b 100644
--- a/app/templates/corpora/analyse_corpus.html.j2
+++ b/app/templates/corpora/analyse_corpus.html.j2
@@ -365,7 +365,8 @@
     // Init corpus analysis components
     resultsJSON = new ResultsJSON();
     resultsList = new ResultsList("result-list", resultsListOptions);
-    results = new Results(resultsJSON, resultsList);
+    metaDataJSON = new MetaDataJSON();
+    results = new Results(resultsJSON, resultsList, metaDataJSON);
     initDisplay = new CorpusAnalysisDisplay(initDisplayElement);
     queryDisplay = new CorpusAnalysisDisplay(queryDisplayElement);
     client = new CorpusAnalysisClient({{ corpus_id }}, nopaque.socket);
@@ -376,6 +377,12 @@
     client.setCallback("init", () => {
       initModal.close();
     });
+    client.setCallback('get_metadata', () => {
+      client.getMetaData();
+    })
+    client.setCallback('recv_meta_data', (response) => {
+      recvMetaData(response);
+    })
     client.setDisplay("query", queryResultsUserFeedbackElement);
     client.setDisplay("query", queryDisplay);
     client.setCallback("query", (payload) => {
-- 
GitLab