d8f0d70a6daf5464df6285a43f86739c4f2a3002
[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 _ from 'lodash';
19 import LoggingActions from './loggingActions.js';
20 import LoggingSource from './loggingSource.js';
21
22 import alt from 'widgets/skyquake_container/skyquakeAltInstance';
23
24 class LoggingStore {
25 constructor() {
26 this.loggingConfig = {};
27 // initialLoggingConfig is the saved state
28 this.initialLoggingConfig = {};
29 this.nulledCategories = [];
30 this.bindActions(LoggingActions);
31 this.registerAsync(LoggingSource);
32 this.exportPublicMethods({
33 updateCategoryDefaultSeverity: this.updateCategoryDefaultSeverity,
34 updateCategoryDefaultSyslogSeverity: this.updateCategoryDefaultSyslogSeverity,
35 updateAllowDuplicateEvents: this.updateAllowDuplicateEvents,
36 addDenyEvent: this.addDenyEvent,
37 updateDenyEvent: this.updateDenyEvent,
38 removeDenyEvent: this.removeDenyEvent,
39 updateSyslogViewerURL: this.updateSyslogViewerURL,
40 resetLoggingConfigData: this.resetLoggingConfigData
41 });
42 }
43
44 getLoggingConfigSuccess = (data) => {
45 console.log("LoggingStore.getLoggingConfigSuccess called. data=", data);
46 // Do we need to do a deep clone?
47 const initialLoggingConfig = _.cloneDeep(data);
48 console.log("initialLoggingConfig=", initialLoggingConfig);
49 this.setState({
50 loggingConfig: data,
51 initialLoggingConfig: initialLoggingConfig,
52 isLoading: false
53 });
54 }
55
56 getLoggingConfigError(data) {
57 console.log("LoggongStore.getLoggingConfigError called. data=", data);
58 }
59
60 putLoggingConfigSuccess = (data) => {
61 console.log("LoggingStore.putLoggingConfigSuccess called. data=", data);
62 const initialLoggingConfig = _.cloneDeep(this.loggingConfig);
63 this.setState({
64 isLoading: false,
65 initialLoggingConfig: initialLoggingConfig
66 });
67 }
68
69 putLoggingConfigError(data) {
70 console.log("LoggingStore.putLoggingConfigError called. data=", data);
71 }
72
73 resetLoggingConfigData = (data) => {
74 console.log('LoggingStore.resetLoggingConfigData called. data=', data);
75 // Do we need to do a deep clone?
76 const loggingConfig = _.cloneDeep(this.initialLoggingConfig);
77 this.setState({
78 loggingConfig: loggingConfig
79 });
80 }
81
82 updateCategoryDefaultSeverity = (catsev) => {
83 console.log("LoggingStore.updateCategoryDefaultSeverity:", catsev);
84 // find the category
85
86 let catIndex = _.findIndex(this.loggingConfig.defaultSeverities, function(o) {
87 return o.category == catsev.category;
88 });
89 console.log("catIndex=", catIndex);
90 if (catIndex != -1) {
91 const loggingConfig = this.loggingConfig;
92 loggingConfig.defaultSeverities[catIndex].severity = catsev.severity;
93
94 this.setState({
95 loggingConfig: loggingConfig
96 });
97 } else {
98 console.log("ERROR: catIndex not founds for default category", catsev.category);
99 }
100 }
101
102 updateCategoryDefaultSyslogSeverity = (catsev) => {
103 console.log("LoggingStore.updateCategoryDefaultSyslogSeverity:", catsev);
104 // find the category (name) in the syslog sink
105
106 let self = this;
107 let loggingConfig = _.cloneDeep(this.loggingConfig);
108 let syslogSinkIndex = -1;
109 let nulledCategories = this.nulledCategories;
110
111 loggingConfig.sinks && loggingConfig.sinks.map((sink, sinkIndex) => {
112 if (sink.name == 'syslog') {
113 if (sink.filter) {
114 if (sink.filter.category) {
115 let catIndex = _.findIndex(sink.filter.category, {
116 name: catsev.name
117 });
118 if (catIndex != -1) {
119 // found it
120 if (catsev.severity == "") {
121 // blank was selected
122 nulledCategories.push(catsev.name);
123 this.setState({
124 nulledCategories: nulledCategories
125 });
126 // TODO/NOTE: Can't delete from model as sending a top-level payload with
127 // missing elements is not allowed!
128 // When backend supports it, in loggingSource change the order of operations
129 // Delete first followed by save/put.
130 // _.remove(loggingConfig.sinks[sinkIndex].filter.category, {
131 // name: catsev.name
132 // });
133 } else {
134 sink.filter.category[catIndex] = catsev;
135 }
136 } else {
137 sink.filter.category.push(catsev);
138 }
139 } else {
140 sink.filter.category = [];
141 sink.filter.category.push(catsev);
142 }
143 } else {
144 sink.filter = {};
145 sink.filter.category = [];
146 sink.filter.category.push(catsev);
147 }
148 }
149 });
150
151 this.setState({
152 loggingConfig: loggingConfig
153 });
154 }
155
156 updateAllowDuplicateEvents = (allowFlag) => {
157 console.log("LoggingStore.updateAllowDuplicateEvents called. allowFlag=", allowFlag);
158 const loggingConfig = this.loggingConfig;
159 loggingConfig.allowDuplicateEvents = allowFlag;
160 this.setState({
161 loggingConfig: loggingConfig
162 });
163 }
164
165 /**
166 * Add a new empty (null) deny event to loggingConfig
167 */
168 addDenyEvent = (event) => {
169 const loggingConfig = this.loggingConfig;
170 loggingConfig.denyEventIDs.push(null);
171 this.setState({
172 loggingConfig: loggingConfig
173 });
174 }
175
176 /**
177 * Update
178 */
179 updateDenyEvent = (index, eventID) => {
180 //console.log("LoggingStore.updateDenyEventID: index=", index);
181 //console.log(" -> eventID=", eventID);
182
183 const loggingConfig = this.loggingConfig;
184 loggingConfig.denyEventIDs[index] = eventID;
185 this.setState({
186 loggingConfig: loggingConfig
187 });
188 }
189
190 /**
191 *
192 */
193 removeDenyEvent = (index) => {
194 // console.log("LoggingStore.removeDenyEvent at index %s", index);
195 const loggingConfig = this.loggingConfig;
196 // Note: we are not validating index
197 loggingConfig.denyEventIDs.splice(index, 1);
198 this.setState({
199 loggingConfig: loggingConfig
200 });
201 }
202
203 /**
204 *
205 */
206 updateSyslogViewerURL = (syslogViewerURL) => {
207 const loggingConfig = this.loggingConfig;
208 loggingConfig.syslogviewer = syslogViewerURL;
209 this.setState({
210 loggingConfig: loggingConfig
211 });
212 }
213 }
214
215 export default alt.createStore(LoggingStore);