import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ReactTable from 'react-table'; import { saveAs } from 'file-saver/FileSaver'; import PageTitle from '../ui/PageTitle'; import Card from '../ui/Card'; import Loading from '../ui/Loading'; import Tooltip from '../ui/Tooltip'; import './Logs.css'; const DOWNLOAD_LOG_FILENAME = 'dns-logs.txt'; class Logs extends Component { componentDidMount() { this.getLogs(); } componentDidUpdate(prevProps) { // get logs when queryLog becomes enabled if (this.props.dashboard.queryLogEnabled && !prevProps.dashboard.queryLogEnabled) { this.props.getLogs(); } } getLogs = () => { // get logs on initialization if queryLogIsEnabled if (this.props.dashboard.queryLogEnabled) { this.props.getLogs(); } } renderTooltip(isFiltered, rule) { if (rule) { return (isFiltered && ); } return ''; } renderLogs(logs) { const columns = [{ Header: 'Time', accessor: 'time', maxWidth: 110, }, { Header: 'Domain name', accessor: 'domain', Cell: (row) => { const response = row.value; return (
{response}
); }, }, { Header: 'Type', accessor: 'type', maxWidth: 60, }, { Header: 'Response', accessor: 'response', Cell: (row) => { const responses = row.value; const { reason } = row.original; const isFiltered = row ? reason.indexOf('Filtered') === 0 : false; const parsedFilteredReason = reason.replace('Filtered', 'Filtered by '); const rule = row && row.original && row.original.rule; if (isFiltered) { return (
{this.renderTooltip(isFiltered, rule)} {parsedFilteredReason}
); } if (responses.length > 0) { const liNodes = responses.map((response, index) => (
  • {response}
  • )); return (
    { this.renderTooltip(isFiltered, rule)}
    ); } return (
    { this.renderTooltip(isFiltered, rule) } Empty
    ); }, }, { Header: 'Client', accessor: 'client', maxWidth: 250, }, ]; if (logs) { return ( { // highlight filtered requests if (!rowInfo) { return {}; } return { className: (rowInfo.original.reason.indexOf('Filtered') === 0 ? 'red' : ''), }; }} />); } return undefined; } handleDownloadButton = async (e) => { e.preventDefault(); const data = await this.props.downloadQueryLog(); const jsonStr = JSON.stringify(data); const dataBlob = new Blob([jsonStr], { type: 'text/plain;charset=utf-8' }); saveAs(dataBlob, DOWNLOAD_LOG_FILENAME); }; renderButtons(queryLogEnabled) { return (
    {queryLogEnabled && } {queryLogEnabled && }
    ); } render() { const { queryLogs, dashboard } = this.props; const { queryLogEnabled } = dashboard; return (
    {this.renderButtons(queryLogEnabled)} {queryLogEnabled && queryLogs.processing && } {queryLogEnabled && !queryLogs.processing && this.renderLogs(queryLogs.logs)}
    ); } } Logs.propTypes = { getLogs: PropTypes.func, queryLogs: PropTypes.object, dashboard: PropTypes.object, toggleLogStatus: PropTypes.func, downloadQueryLog: PropTypes.func, }; export default Logs;