<commit_before>/*
 * Copyright 2018 Expedia Group
 *
 *       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 React from 'react';
import PropTypes from 'prop-types';
import {observer} from 'mobx-react';
import _ from 'lodash';
import {Link} from 'react-router-dom';

import formatters from '../../../utils/formatters';
import linkBuilder from '../../../utils/linkBuilder';

@observer
export default class AlertHistory extends React.Component {
    static propTypes = {
        alertDetailsStore: PropTypes.object.isRequired,
        operationName: PropTypes.string.isRequired,
        serviceName: PropTypes.string.isRequired,
        historyWindow: PropTypes.number.isRequired
    };

    static timeAgoFormatter(cell) {
        return formatters.toTimeago(cell);
    }

    static timestampFormatter(cell) {
        return formatters.toTimestring(cell);
    }

    static durationColumnFormatter(start, end) {
        return formatters.toDurationStringInSecAndMin(end - start);
    }

    static timeBufferAroundAlert = 10 * 60 * 1000; // 10 mins

    constructor(props) {
        super(props);

        this.trendLinkCreator = this.trendLinkCreator.bind(this);
        this.traceLinkCreator = this.traceLinkCreator.bind(this);
    }

    trendLinkCreator(startTimestamp, endTimestamp) {
        const from = (startTimestamp / 1000) - AlertHistory.timeBufferAroundAlert;
        const until = (endTimestamp / 1000) + AlertHistory.timeBufferAroundAlert;

        return linkBuilder.createTrendsLink({
            serviceName: this.props.serviceName,
            operationName: this.props.operationName,
            from,
            until
        });
    }

    traceLinkCreator(startTimestamp, endTimestamp) {
        const from = startTimestamp / 1000;
        const until = endTimestamp / 1000;

        return linkBuilder.createTracesLink({
            serviceName: this.props.serviceName,
            operationName: this.props.operationName,
            from,
            until
        });
    }

    render() {
        const sortedHistoryResults = _.orderBy(this.props.alertDetailsStore.alertHistory, alert => alert.startTimestamp, ['desc']);

        return (
            <div className="col-md-6 alert-history">
                <header>
                    <h4>History
                        <span className="h6"> ({sortedHistoryResults.length} times unhealthy in last {this.props.historyWindow / 86400000} day)</span>
                    </h4><commit_msg>update alert links to use usb<commit_after>/*
 * Copyright 2018 Expedia Group
 *
 *       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 React from 'react';
import PropTypes from 'prop-types';
import {observer} from 'mobx-react';
import _ from 'lodash';
import {Link} from 'react-router-dom';

import formatters from '../../../utils/formatters';
import linkBuilder from '../../../utils/linkBuilder';

@observer
export default class AlertHistory extends React.Component {
    static propTypes = {
        alertDetailsStore: PropTypes.object.isRequired,
        operationName: PropTypes.string.isRequired,
        serviceName: PropTypes.string.isRequired,
        historyWindow: PropTypes.number.isRequired
    };

    static timeAgoFormatter(cell) {
        return formatters.toTimeago(cell);
    }

    static timestampFormatter(cell) {
        return formatters.toTimestring(cell);
    }

    static durationColumnFormatter(start, end) {
        return formatters.toDurationStringInSecAndMin(end - start);
    }

    static timeBufferAroundAlert = 10 * 60 * 1000; // 10 mins

    constructor(props) {
        super(props);

        this.trendLinkCreator = this.trendLinkCreator.bind(this);
        this.traceLinkCreator = this.traceLinkCreator.bind(this);
    }

    trendLinkCreator(startTimestamp, endTimestamp) {
        const from = (startTimestamp / 1000) - AlertHistory.timeBufferAroundAlert;
        const to = (endTimestamp / 1000) + AlertHistory.timeBufferAroundAlert;

        return linkBuilder.universalSearchTrendsLink({
            serviceName: this.props.serviceName,
            operationName: this.props.operationName,
            time: {
                from,
                to
            }
        });
    }

    traceLinkCreator(startTimestamp, endTimestamp) {
        const from = startTimestamp / 1000;
        const to = endTimestamp / 1000;

        return linkBuilder.universalSearchTracesLink({
            serviceName: this.props.serviceName,
            operationName: this.props.operationName,
            time: {
                from,
                to
            }
        });
    }

    render() {
        const sortedHistoryResults = _.orderBy(this.props.alertDetailsStore.alertHistory, alert => alert.startTimestamp, ['desc']);

        return (
            <div className="col-md-6 alert-history">
                <header>
                    <h4>History
                        <span className="h6"> ({sortedHistoryResults.length} times unhealthy in last {this.props.historyWindow / 86400000} day)</span>
                    </h4>
{'fraction_non_alphanumeric': 0.09267660401814647, 'fraction_numerical': 0.014906027219701879, 'mean_word_length': 2.4332591768631815, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1452142', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1566, 'n_words': 438}