update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / logging / src / loggingStore.js
index 860ff9f..a8bb337 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 
+ *
  *   Copyright 2016 RIFT.IO Inc
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,7 +15,9 @@
  *   limitations under the License.
  *
  */
-import _ from 'lodash';
+import _cloneDeep from 'lodash/cloneDeep';
+import _findIndex from 'lodash/findIndex';
+import _remove from 'lodash/remove';
 import LoggingActions from './loggingActions.js';
 import LoggingSource from './loggingSource.js';
 
@@ -26,10 +28,12 @@ class LoggingStore {
     this.loggingConfig = {};
     // initialLoggingConfig is the saved state
     this.initialLoggingConfig = {};
+    this.nulledCategories = [];
     this.bindActions(LoggingActions);
     this.registerAsync(LoggingSource);
     this.exportPublicMethods({
       updateCategoryDefaultSeverity: this.updateCategoryDefaultSeverity,
+      updateCategoryDefaultSyslogSeverity: this.updateCategoryDefaultSyslogSeverity,
       updateAllowDuplicateEvents: this.updateAllowDuplicateEvents,
       addDenyEvent: this.addDenyEvent,
       updateDenyEvent: this.updateDenyEvent,
@@ -42,7 +46,7 @@ class LoggingStore {
   getLoggingConfigSuccess = (data) => {
       console.log("LoggingStore.getLoggingConfigSuccess called. data=", data);
       // Do we need to do a deep clone?
-      const initialLoggingConfig = _.cloneDeep(data);
+      const initialLoggingConfig = _cloneDeep(data);
       console.log("initialLoggingConfig=", initialLoggingConfig);
       this.setState({
       loggingConfig: data,
@@ -57,8 +61,9 @@ class LoggingStore {
 
   putLoggingConfigSuccess = (data) => {
     console.log("LoggingStore.putLoggingConfigSuccess called. data=", data);
-    const initialLoggingConfig = _.cloneDeep(this.loggingConfig);
+    const initialLoggingConfig = _cloneDeep(this.loggingConfig);
     this.setState({
+      nulledCategories: [],
       isLoading: false,
       initialLoggingConfig: initialLoggingConfig
     });
@@ -71,7 +76,7 @@ class LoggingStore {
   resetLoggingConfigData = (data) => {
     console.log('LoggingStore.resetLoggingConfigData called. data=', data);
     // Do we need to do a deep clone?
-    const loggingConfig = _.cloneDeep(this.initialLoggingConfig);
+    const loggingConfig = _cloneDeep(this.initialLoggingConfig);
     this.setState({
       loggingConfig: loggingConfig
     });
@@ -81,7 +86,7 @@ class LoggingStore {
     console.log("LoggingStore.updateCategoryDefaultSeverity:", catsev);
     // find the category
 
-    let catIndex = _.findIndex(this.loggingConfig.defaultSeverities, function(o) {
+    let catIndex = _findIndex(this.loggingConfig.defaultSeverities, function(o) {
       return o.category == catsev.category;
     });
     console.log("catIndex=", catIndex);
@@ -97,6 +102,64 @@ class LoggingStore {
     }
   }
 
+  updateCategoryDefaultSyslogSeverity = (catsev) => {
+    console.log("LoggingStore.updateCategoryDefaultSyslogSeverity:", catsev);
+    // find the category (name) in the syslog sink
+
+    let self = this;
+    let loggingConfig = _cloneDeep(this.loggingConfig);
+    let syslogSinkIndex = -1;
+    let nulledCategories = this.nulledCategories;
+
+    loggingConfig.sinks && loggingConfig.sinks.map((sink, sinkIndex) => {
+      if (sink.name == 'syslog') {
+        if (sink.filter) {
+          if (sink.filter.category) {
+            let catIndex = _findIndex(sink.filter.category, {
+              name: catsev.name
+            });
+            if (catIndex != -1) {
+              // found it
+              if (catsev.severity == "") {
+                // blank was selected
+                nulledCategories.push(catsev.name);
+                this.setState({
+                  nulledCategories: nulledCategories
+                });
+                // TODO/NOTE: Can't delete from model as sending a top-level payload with
+                // missing elements is not allowed!
+                // When backend supports it, in loggingSource change the order of operations
+                // // Delete first followed by save/put.
+                _remove(loggingConfig.sinks[sinkIndex].filter.category, {
+                  name: catsev.name
+                });
+              } else {
+                sink.filter.category[catIndex] = catsev;
+              }
+            } else {
+              _remove(nulledCategories, (v) => v == catsev.name);
+              this.setState({
+                nulledCategories: nulledCategories
+              });
+              sink.filter.category.push(catsev);
+            }
+          } else {
+            sink.filter.category = [];
+            sink.filter.category.push(catsev);
+          }
+        } else {
+          sink.filter = {};
+          sink.filter.category = [];
+          sink.filter.category.push(catsev);
+        }
+      }
+    });
+
+    this.setState({
+      loggingConfig: loggingConfig
+    });
+  }
+
   updateAllowDuplicateEvents = (allowFlag) => {
     console.log("LoggingStore.updateAllowDuplicateEvents called. allowFlag=", allowFlag);
     const loggingConfig = this.loggingConfig;
@@ -156,4 +219,4 @@ class LoggingStore {
   }
 }
 
-export default alt.createStore(LoggingStore);
+export default alt.createStore(LoggingStore, 'LoggingStore');