fix: get machine logs page up-to-date

This commit is contained in:
Taranto 2020-01-23 14:00:55 +00:00 committed by Josh Harvey
parent 1559c7690c
commit bde27cce4c

View file

@ -1,14 +1,14 @@
import { useQuery, useMutation } from '@apollo/react-hooks' import { useQuery, useMutation } from '@apollo/react-hooks'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import { gql } from 'apollo-boost' import { gql } from 'apollo-boost'
import FileSaver from 'file-saver'
import moment from 'moment' import moment from 'moment'
import * as R from 'ramda' import * as R from 'ramda'
import React, { useState } from 'react' import React, { useState } from 'react'
import LogsDowloaderPopover from 'src/components/LogsDownloaderPopper'
import Sidebar from 'src/components/Sidebar' import Sidebar from 'src/components/Sidebar'
import Title from 'src/components/Title' import Title from 'src/components/Title'
import { SimpleButton } from 'src/components/buttons' import { FeatureButton, SimpleButton } from 'src/components/buttons'
import { import {
Table, Table,
TableHead, TableHead,
@ -18,8 +18,10 @@ import {
TableCell TableCell
} from 'src/components/table' } from 'src/components/table'
import { Info3 } from 'src/components/typography' import { Info3 } from 'src/components/typography'
import { ReactComponent as DownloadActive } from 'src/styling/icons/button/download/white.svg'
import { ReactComponent as Download } from 'src/styling/icons/button/download/zodiac.svg'
import styles from './Logs.styles.js' import styles from './Logs.styles'
const useStyles = makeStyles(styles) const useStyles = makeStyles(styles)
@ -55,20 +57,18 @@ const formatDate = date => {
return moment(date).format('YYYY-MM-DD HH:mm') return moment(date).format('YYYY-MM-DD HH:mm')
} }
const formatDateFile = date => {
return moment(date).format('YYYY-MM-DD_HH-mm')
}
const Logs = () => { const Logs = () => {
const classes = useStyles() const classes = useStyles()
const [selected, setSelected] = useState(null) const [selected, setSelected] = useState(null)
const [saveMessage, setSaveMessage] = useState(null) const [saveMessage, setSaveMessage] = useState(null)
const [anchorEl, setAnchorEl] = useState(null)
const deviceId = selected?.deviceId const deviceId = selected?.deviceId
const { data: machineResponse } = useQuery(GET_MACHINES) const { data: machineResponse } = useQuery(GET_MACHINES)
const [saveSupportLogs, { loading }] = useMutation(SUPPORT_LOGS, { const [sendSnapshot, { loading }] = useMutation(SUPPORT_LOGS, {
variables: { deviceId }, variables: { deviceId },
onError: () => setSaveMessage('Failure saving snapshot'), onError: () => setSaveMessage('Failure saving snapshot'),
onCompleted: () => setSaveMessage('✓ Saved latest snapshot') onCompleted: () => setSaveMessage('✓ Saved latest snapshot')
@ -89,38 +89,47 @@ const Logs = () => {
return R.path(['deviceId'])(selected) === it.deviceId return R.path(['deviceId'])(selected) === it.deviceId
} }
const handleOpenRangePicker = event => {
setAnchorEl(anchorEl ? null : event.currentTarget)
}
const open = Boolean(anchorEl)
const id = open ? 'date-range-popover' : undefined
return ( return (
<> <>
<div className={classes.titleWrapper}> <div className={classes.titleWrapper}>
<div className={classes.titleAndButtonsContainer}>
<Title>Machine Logs</Title> <Title>Machine Logs</Title>
{logsResponse && ( {logsResponse && (
<div className={classes.buttonsWrapper}> <div className={classes.buttonsWrapper}>
<Info3>{saveMessage}</Info3> <FeatureButton
<SimpleButton Icon={Download}
className={classes.button} InverseIcon={DownloadActive}
onClick={() => { aria-describedby={id}
const text = logsResponse.data.logs variant="contained"
.map(it => JSON.stringify(it)) onClick={handleOpenRangePicker}
.join('\n') />
const blob = new window.Blob([text], { <LogsDowloaderPopover
type: 'text/plain;charset=utf-8' title="Download logs"
}) name="machine-logs"
FileSaver.saveAs( id={id}
blob, open={open}
`${formatDateFile(new Date())}_${selected.name}` anchorEl={anchorEl}
) logs={logsResponse.machineLogs}
}}> getTimestamp={log => log.timestamp}
DL />
</SimpleButton>
<SimpleButton <SimpleButton
className={classes.button} className={classes.button}
disabled={loading} disabled={loading}
onClick={saveSupportLogs}> onClick={sendSnapshot}>
Share with Lamassu Share with Lamassu
</SimpleButton> </SimpleButton>
<Info3>{saveMessage}</Info3>
</div> </div>
)} )}
</div> </div>
</div>
<div className={classes.wrapper}> <div className={classes.wrapper}>
<Sidebar <Sidebar
displayName={it => it.name} displayName={it => it.name}