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 { normalizeLogs } from '../../helpers/helpers'; const DOWNLOAD_LOG_FILENAME = 'dns-logs.txt'; class Logs extends Component { componentDidMount() { // get logs on initialization if queryLogIsEnabled if (this.props.dashboard.queryLogEnabled) { this.props.getLogs(); } } componentDidUpdate(prevProps) { // get logs when queryLog becomes enabled if (this.props.dashboard.queryLogEnabled && !prevProps.dashboard.queryLogEnabled) { this.props.getLogs(); } } renderLogs(logs) { const columns = [{ Header: 'Time', accessor: 'time', maxWidth: 150, }, { Header: 'Domain name', accessor: 'domain', }, { Header: 'Type', accessor: 'type', maxWidth: 100, }, { Header: 'Response', accessor: 'response', Cell: (row) => { const responses = row.value; if (responses.length > 0) { const liNodes = responses.map((response, index) => (
  • {response}
  • )); return (); } return 'Empty'; }, }]; if (logs) { const normalizedLogs = normalizeLogs(logs); return (); } 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 && }
    ); } 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;