c83b322d3b74e5d5d0c2aa287177cc2619dad296
[osm/UI.git] / skyquake / plugins / logging / src / loggingSource.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 let rw = require('utils/rw.js');
19 var API_SERVER = rw.getSearchParams(window.location).api_server;
20 var loggingActions = require('./loggingActions.js');
21 var Utils = require('utils/utils.js');
22 import $ from 'jquery';
23
24 function apiUrl(endpoint) {
25 return endpoint.replace(/\/$/,'') + '?api_server=' + API_SERVER;
26 }
27
28 export default {
29 /**
30 * Retrieve the logging configuration data from the Node API server
31 */
32 getLoggingConfig: function() {
33 //console.log("LoggingSource.getLoggingConfig called");
34 return {
35 remote: function(state) {
36 return new Promise(function(resolve, reject) {
37 //console.log("loggingSource.getLoggingConfig called");
38 $.ajax({
39 url: apiUrl('api/aggregate'),
40 type: 'GET',
41 beforeSend: Utils.addAuthorizationStub,
42 success: function(data) {
43 //console.log("LoggingSource.getLoggingConfig success call. data=", data);
44 resolve(data);
45 },
46 error: function(e) {
47 console.log("Error getting logging config details");
48 }
49 });
50 });
51 },
52 success: loggingActions.getLoggingConfigSuccess,
53 error: loggingActions.getLoggingConfigError
54 }
55 },
56 /**
57 * Update the logging configuration data on the server
58 * @param {object} state - Reference to parent store state
59 * @param {object} loggingConfig - logging configuration data
60 * @return {[type]} [description]
61 }
62 */
63 updateLoggingConfig: function () {
64 return {
65 remote: function(state, nulledCategories, loggingConfig) {
66 return new Promise(function(resolve, reject) {
67 let promises = [];
68 let remove = null;
69 // $.ajax({
70 // url: 'https://10.66.202.130:8008/api/config/logging/allow/duplicate',
71 // type: 'DELETE',
72 // beforeSend: Utils.addAuthorizationStub
73 // })
74 if(loggingConfig.hasOwnProperty('allowDuplicateEvents')) {
75 promises.push($.ajax({
76 // url: apiUrl('api/config/default-severity'),
77 url: apiUrl('api/config/allow-duplicate-events'),
78 type: 'PUT',
79 beforeSend: Utils.addAuthorizationStub,
80 data: {
81 allowDuplicateEvents: loggingConfig.allowDuplicateEvents
82 },
83 success: function(data) {
84 resolve(data);
85 },
86 error: function(error) {
87 console.log("There was an error updating the logging config data",
88 error);
89 reject(error);
90 }
91 }))
92 }
93 // if(nulledCategories.length > 0) {
94 // remove = $.ajax({
95 // // url: apiUrl('api/config/default-severity'),
96 // url: apiUrl('api/config/default-syslog-severity/' + nulledCategories.join(',')),
97 // type: 'DELETE',
98 // beforeSend: Utils.addAuthorizationStub,
99 // success: function(data) {
100 // resolve(data);
101 // },
102 // error: function(error) {
103 // console.log("There was an error updating the logging config data",
104 // error);
105 // reject(error);
106 // }
107 // });
108 // promises.push(remove);
109 // }
110 Promise.all(promises).then(function(data) {
111 return $.ajax({
112 url: apiUrl('api/aggregate'),
113 type: 'PUT',
114 beforeSend: Utils.addAuthorizationStub,
115 data: loggingConfig,
116 success: function(data) {
117 resolve(data);
118 },
119 error: function(error) {
120 console.log("There was an error updating the logging config data",
121 error);
122 reject(error);
123 }
124 });
125 }).then(function(data){
126 resolve(data)
127 }, function(){
128 reject(arguments)
129 })
130
131 });
132
133 },
134 success: loggingActions.putLoggingConfigSuccess,
135 error: loggingActions.putLoggingConfigError
136 }
137 }
138 }