RIFT-15726 - optimize download size -> lodash usage in UI
[osm/UI.git] / skyquake / plugins / logging / src / loggingStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 import _cloneDeep from 'lodash/cloneDeep';
19 import _findIndex from 'lodash/findIndex';
20 import _remove from 'lodash/remove';
21 import LoggingActions from './loggingActions.js';
22 import LoggingSource from './loggingSource.js';
23
24 import alt from 'widgets/skyquake_container/skyquakeAltInstance';
25
26 class LoggingStore {
27 constructor() {
28 this.loggingConfig = {};
29 // initialLoggingConfig is the saved state
30 this.initialLoggingConfig = {};
31 this.nulledCategories = [];
32 this.bindActions(LoggingActions);
33 this.registerAsync(LoggingSource);
34 this.exportPublicMethods({
35 updateCategoryDefaultSeverity: this.updateCategoryDefaultSeverity,
36 updateCategoryDefaultSyslogSeverity: this.updateCategoryDefaultSyslogSeverity,
37 updateAllowDuplicateEvents: this.updateAllowDuplicateEvents,
38 addDenyEvent: this.addDenyEvent,
39 updateDenyEvent: this.updateDenyEvent,
40 removeDenyEvent: this.removeDenyEvent,
41 updateSyslogViewerURL: this.updateSyslogViewerURL,
42 resetLoggingConfigData: this.resetLoggingConfigData
43 });
44 }
45
46 getLoggingConfigSuccess = (data) => {
47 console.log("LoggingStore.getLoggingConfigSuccess called. data=", data);
48 // Do we need to do a deep clone?
49 const initialLoggingConfig = _cloneDeep(data);
50 console.log("initialLoggingConfig=", initialLoggingConfig);
51 this.setState({
52 loggingConfig: data,
53 initialLoggingConfig: initialLoggingConfig,
54 isLoading: false
55 });
56 }
57
58 getLoggingConfigError(data) {
59 console.log("LoggongStore.getLoggingConfigError called. data=", data);
60 }
61
62 putLoggingConfigSuccess = (data) => {
63 console.log("LoggingStore.putLoggingConfigSuccess called. data=", data);
64 const initialLoggingConfig = _cloneDeep(this.loggingConfig);
65 this.setState({
66 isLoading: false,
67 initialLoggingConfig: initialLoggingConfig
68 });
69 }
70
71 putLoggingConfigError(data) {
72 console.log("LoggingStore.putLoggingConfigError called. data=", data);
73 }
74
75 resetLoggingConfigData = (data) => {
76 console.log('LoggingStore.resetLoggingConfigData called. data=', data);
77 // Do we need to do a deep clone?
78 const loggingConfig = _cloneDeep(this.initialLoggingConfig);
79 this.setState({
80 loggingConfig: loggingConfig
81 });
82 }
83
84 updateCategoryDefaultSeverity = (catsev) => {
85 console.log("LoggingStore.updateCategoryDefaultSeverity:", catsev);
86 // find the category
87
88 let catIndex = _findIndex(this.loggingConfig.defaultSeverities, function(o) {
89 return o.category == catsev.category;
90 });
91 console.log("catIndex=", catIndex);
92 if (catIndex != -1) {
93 const loggingConfig = this.loggingConfig;
94 loggingConfig.defaultSeverities[catIndex].severity = catsev.severity;
95
96 this.setState({
97 loggingConfig: loggingConfig
98 });
99 } else {
100 console.log("ERROR: catIndex not founds for default category", catsev.category);
101 }
102 }
103
104 updateCategoryDefaultSyslogSeverity = (catsev) => {
105 console.log("LoggingStore.updateCategoryDefaultSyslogSeverity:", catsev);
106 // find the category (name) in the syslog sink
107
108 let self = this;
109 let loggingConfig = _cloneDeep(this.loggingConfig);
110 let syslogSinkIndex = -1;
111 let nulledCategories = this.nulledCategories;
112
113 loggingConfig.sinks && loggingConfig.sinks.map((sink, sinkIndex) => {
114 if (sink.name == 'syslog') {
115 if (sink.filter) {
116 if (sink.filter.category) {
117 let catIndex = _findIndex(sink.filter.category, {
118 name: catsev.name
119 });
120 if (catIndex != -1) {
121 // found it
122 if (catsev.severity == "") {
123 // blank was selected
124 nulledCategories.push(catsev.name);
125 this.setState({
126 nulledCategories: nulledCategories
127 });
128 // TODO/NOTE: Can't delete from model as sending a top-level payload with
129 // missing elements is not allowed!
130 // When backend supports it, in loggingSource change the order of operations
131 // // Delete first followed by save/put.
132 _remove(loggingConfig.sinks[sinkIndex].filter.category, {
133 name: catsev.name
134 });
135 } else {
136 sink.filter.category[catIndex] = catsev;
137 }
138 } else {
139 _remove(nulledCategories, (v) => v == catsev.name);
140 this.setState({
141 nulledCategories: nulledCategories
142 });
143 sink.filter.category.push(catsev);
144 }
145 } else {
146 sink.filter.category = [];
147 sink.filter.category.push(catsev);
148 }
149 } else {
150 sink.filter = {};
151 sink.filter.category = [];
152 sink.filter.category.push(catsev);
153 }
154 }
155 });
156
157 this.setState({
158 loggingConfig: loggingConfig
159 });
160 }
161
162 updateAllowDuplicateEvents = (allowFlag) => {
163 console.log("LoggingStore.updateAllowDuplicateEvents called. allowFlag=", allowFlag);
164 const loggingConfig = this.loggingConfig;
165 loggingConfig.allowDuplicateEvents = allowFlag;
166 this.setState({
167 loggingConfig: loggingConfig
168 });
169 }
170
171 /**
172 * Add a new empty (null) deny event to loggingConfig
173 */
174 addDenyEvent = (event) => {
175 const loggingConfig = this.loggingConfig;
176 loggingConfig.denyEventIDs.push(null);
177 this.setState({
178 loggingConfig: loggingConfig
179 });
180 }
181
182 /**
183 * Update
184 */
185 updateDenyEvent = (index, eventID) => {
186 //console.log("LoggingStore.updateDenyEventID: index=", index);
187 //console.log(" -> eventID=", eventID);
188
189 const loggingConfig = this.loggingConfig;
190 loggingConfig.denyEventIDs[index] = eventID;
191 this.setState({
192 loggingConfig: loggingConfig
193 });
194 }
195
196 /**
197 *
198 */
199 removeDenyEvent = (index) => {
200 // console.log("LoggingStore.removeDenyEvent at index %s", index);
201 const loggingConfig = this.loggingConfig;
202 // Note: we are not validating index
203 loggingConfig.denyEventIDs.splice(index, 1);
204 this.setState({
205 loggingConfig: loggingConfig
206 });
207 }
208
209 /**
210 *
211 */
212 updateSyslogViewerURL = (syslogViewerURL) => {
213 const loggingConfig = this.loggingConfig;
214 loggingConfig.syslogviewer = syslogViewerURL;
215 this.setState({
216 loggingConfig: loggingConfig
217 });
218 }
219 }
220
221 export default alt.createStore(LoggingStore, 'LoggingStore');