From 1cebd25d68d9777635f510815e1f05a78c65f224 Mon Sep 17 00:00:00 2001
From: Tom Frederik Leimbrock <tom.leimbrock@mni.thm.de>
Date: Sun, 20 Jun 2021 21:54:34 +0200
Subject: [PATCH] Improve the performance of theme cloud administration

The keyword update function has a runtime that is quadratic
to the number of keywords, since the function examines all
keywords and has to search all previous keywords each time.
This commit ensures that this function is called only once
in ngOnInit instead of three times previously, as the
additional calls are redundant, reducing the administration
call time to one-third.

Closes #119
---
 .../topic-cloud-administration.component.html |  2 +-
 .../topic-cloud-administration.component.ts   | 19 ++++++++++---------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.html b/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.html
index b2550e6e0..570992b11 100644
--- a/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.html
+++ b/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.html
@@ -256,4 +256,4 @@
       </div>
     </mat-expansion-panel>
   </mat-accordion>
-</mat-dialog-content>
\ No newline at end of file
+</mat-dialog-content>
diff --git a/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts b/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts
index c89afd352..db5ac69e4 100644
--- a/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts
+++ b/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts
@@ -73,11 +73,10 @@ export class TopicCloudAdministrationComponent implements OnInit, OnDestroy {
 
   ngOnInit(): void {
     this.deviceType = localStorage.getItem('deviceType');
-    this.wsCommentServiceService.getCommentStream(localStorage.getItem('roomId')).subscribe(_ => this.updateKeywords());
+    this.wsCommentServiceService.getCommentStream(localStorage.getItem('roomId')).subscribe();
     this.blacklistSubscription = this.topicCloudAdminService.getBlacklist().subscribe(list => this.blacklist = list);
     this.profanitywordlist = this.topicCloudAdminService.getProfanityListFromStorage();
     this.profanitylistSubscription = this.topicCloudAdminService.getCustomProfanityList().subscribe(list => {
-      this.updateKeywords();
       this.profanitywordlist = list;
     });
     this.isCreatorOrMod = this.data.user.role !== UserRole.PARTICIPANT;
@@ -101,21 +100,23 @@ export class TopicCloudAdministrationComponent implements OnInit, OnDestroy {
   updateKeywords(){
     this.commentService.getFilteredComments(localStorage.getItem('roomId')).subscribe(comments => {
       this.keywords = [];
-      comments.map(comment => {
-        let keywords = comment.keywordsFromQuestioner;
+      comments.forEach(comment => {
+        let keywords: string[];
         if (this.keywordORfulltext === KeywordOrFulltext[KeywordOrFulltext.keyword]){
           keywords = comment.keywordsFromSpacy;
         } else if (this.keywordORfulltext === KeywordOrFulltext[KeywordOrFulltext.both]){
           keywords = comment.keywordsFromQuestioner.concat(comment.keywordsFromSpacy);
+        } else {
+          keywords = comment.keywordsFromQuestioner;
         }
 
         if (!keywords){
           keywords = [];
         }
 
-        keywords.map(_keyword => {
+        keywords.forEach(_keyword => {
           const existingKey = this.checkIfKeywordExists(_keyword);
-          if (existingKey){
+          if (existingKey) {
             existingKey.vote += comment.score;
             if (this.checkIfCommentExists(existingKey.comments, comment.id)){
               existingKey.comments.push(comment);
@@ -209,7 +210,7 @@ export class TopicCloudAdministrationComponent implements OnInit, OnDestroy {
   }
 
   deleteKeyword(key: Keyword, message?: string): void{
-    key.comments.map(comment => {
+    key.comments.forEach(comment => {
       const changes = new TSMap<string, any>();
       let keywords = comment.keywordsFromQuestioner;
       keywords.splice(keywords.indexOf(key.keyword, 0), 1);
@@ -250,7 +251,7 @@ export class TopicCloudAdministrationComponent implements OnInit, OnDestroy {
     if (key2){
       this.openConfirmDialog('merge-message', 'merge', key, key2);
     } else {
-      key.comments.map(comment => {
+      key.comments.forEach(comment => {
         const changes = new TSMap<string, any>();
         let keywords = comment.keywordsFromQuestioner;
         for (let i = 0; i < keywords.length; i++){
@@ -306,7 +307,7 @@ export class TopicCloudAdministrationComponent implements OnInit, OnDestroy {
 
   mergeKeywords(key1: Keyword, key2: Keyword) {
     if (key1 !== undefined && key2 !== undefined){
-      key1.comments.map(comment => {
+      key1.comments.forEach(comment => {
         if (this.checkIfCommentExists(key2.comments, comment.id)){
           const changes = new TSMap<string, any>();
           let keywords = comment.keywordsFromQuestioner;
-- 
GitLab