/*
* Copyright 2014 Mirantis, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
**/
import $ from 'jquery';
import _ from 'underscore';
import i18n from 'i18n';
import React from 'react';
import utils from 'utils';
import models from 'models';
import {Input, ProgressBar} from 'views/controls';
import {pollingMixin} from 'component_mixins';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import ReactFragment from 'react-addons-create-fragment';
var LogsTab = React.createClass({
mixins: [
pollingMixin(5)
],
shouldDataBeFetched() {
return this.state.to && this.state.logsEntries;
},
fetchData() {
var request;
var logsEntries = this.state.logsEntries;
var from = this.state.from;
var to = this.state.to;
request = this.fetchLogs({from: from, to: to})
.done((data) => {
this.setState({
logsEntries: data.entries.concat(logsEntries),
from: data.from,
to: data.to
});
});
return $.when(request);
},
getInitialState() {
return {
showMoreLogsLink: false,
loading: 'loading',
loadingError: null,
from: -1,
to: 0
};
},
fetchLogs(data) {
return $.ajax({
url: '/api/logs',
dataType: 'json',
data: _.extend(_.omit(this.props.selectedLogs, 'type'), data),
headers: {
'X-Auth-Token': app.keystoneClient.token
}
});
},
showLogs(params) {
this.stopPolling();
var logOptions = this.props.selectedLogs.type == 'remote' ?
_.extend({}, this.props.selectedLogs) : _.omit(this.props.selectedLogs, 'node');
logOptions.level = logOptions.level.toLowerCase();
app.navigate('#cluster/' + this.props.cluster.id + '/logs/' +
utils.serializeTabOptions(logOptions), {trigger: false, replace: true});
params = params || {};
this.fetchLogs(params)
.done((data) => {
var logsEntries = this.state.logsEntries || [];
this.setState({
showMoreLogsLink: data.has_more || false,
logsEntries: params.fetch_older ? logsEntries.concat(data.entries) : data.entries,
loading: 'done',
from: data.from,
to: data.to
});
this.startPolling();
})
.fail((response) => {
this.setState({
logsEntries: undefined,
loading: 'fail',
loadingError: utils.getResponseText(response, i18n('cluster_page.logs_tab.log_alert'))
});
});
},
onShowButtonClick() {
this.setState({
loading: 'loading',
loadingError: null
}, this.showLogs);
},
onShowMoreClick(value) {
this.showLogs({max_entries: value, fetch_older: true, from: this.state.from});
},
render() {
return (