You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2020/05/05 11:29:37 UTC

[incubator-streampipes] 04/06: Add NotificationCount view to installation process

This is an automated email from the ASF dual-hosted git repository.

riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git

commit e353e36d3806c824a5490842777c1d68a5509c08
Author: Dominik Riemer <ri...@fzi.de>
AuthorDate: Sun May 3 21:33:34 2020 +0200

    Add NotificationCount view to installation process
---
 .../manager/setup/CouchDbInstallationStep.java     | 31 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/setup/CouchDbInstallationStep.java b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/setup/CouchDbInstallationStep.java
index 2dc0051..fac9eb8 100644
--- a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/setup/CouchDbInstallationStep.java
+++ b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/setup/CouchDbInstallationStep.java
@@ -103,17 +103,36 @@ public class CouchDbInstallationStep implements InstallationStep {
     private Message addNotificationView() {
         try {
             DesignDocument userDocument = prepareDocument("_design/notificationtypes");
-            Map<String, MapReduce> views = new HashMap<>();
+            DesignDocument notificationCountDocument = prepareDocument("_design/unread");
 
+            Map<String, MapReduce> notificationTypeViews = new HashMap<>();
             MapReduce notificationTypeFunction = new MapReduce();
             notificationTypeFunction.setMap("function (doc) { var vizName = doc.title.replace(/\\s/g, '-'); var indexName = doc.correspondingPipelineId + '-' + vizName; emit([indexName, doc.createdAtTimestamp], doc);}");
-
-            views.put("notificationtypes", notificationTypeFunction);
-
-            userDocument.setViews(views);
+            notificationTypeViews.put("notificationtypes", notificationTypeFunction);
+            userDocument.setViews(notificationTypeViews);
             Response resp = Utils.getCouchDbNotificationClient().design().synchronizeWithDb(userDocument);
 
-            if (resp.getError() != null) return Notifications.error(PREPARING_NOTIFICATIONS_TEXT);
+            Map<String, MapReduce> notificationCountTypeViews = new HashMap<>();
+            MapReduce countFunction = new MapReduce();
+            countFunction.setMap("function (doc) {\n" +
+                    "  var user = doc.targetedAt; \n" +
+                    "  if (!doc.read) {\n" +
+                    "    emit(user, 1);\n" +
+                    "  }\n" +
+                    "}");
+            countFunction.setReduce("function (keys, values, rereduce) {\n" +
+                    "  if (rereduce) {\n" +
+                    "    return sum(values);\n" +
+                    "  } else {\n" +
+                    "    return values.length;\n" +
+                    "  }\n" +
+                    "}");
+            notificationCountTypeViews.put("unread", countFunction);
+            notificationCountDocument.setViews(notificationCountTypeViews);
+            Response countResp =
+                    Utils.getCouchDbNotificationClient().design().synchronizeWithDb(notificationCountDocument);
+
+            if (resp.getError() != null && countResp != null) return Notifications.error(PREPARING_NOTIFICATIONS_TEXT);
             else return Notifications.success(PREPARING_NOTIFICATIONS_TEXT);
         } catch (Exception e) {
             return Notifications.error(PREPARING_NOTIFICATIONS_TEXT);