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,37 +89,46 @@ 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}>
<Title>Machine Logs</Title> <div className={classes.titleAndButtonsContainer}>
{logsResponse && ( <Title>Machine Logs</Title>
<div className={classes.buttonsWrapper}> {logsResponse && (
<Info3>{saveMessage}</Info3> <div className={classes.buttonsWrapper}>
<SimpleButton <FeatureButton
className={classes.button} Icon={Download}
onClick={() => { InverseIcon={DownloadActive}
const text = logsResponse.data.logs aria-describedby={id}
.map(it => JSON.stringify(it)) variant="contained"
.join('\n') onClick={handleOpenRangePicker}
const blob = new window.Blob([text], { />
type: 'text/plain;charset=utf-8' <LogsDowloaderPopover
}) title="Download logs"
FileSaver.saveAs( name="machine-logs"
blob, id={id}
`${formatDateFile(new Date())}_${selected.name}` open={open}
) anchorEl={anchorEl}
}}> logs={logsResponse.machineLogs}
DL getTimestamp={log => log.timestamp}
</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>
</div> <Info3>{saveMessage}</Info3>
)} </div>
)}
</div>
</div> </div>
<div className={classes.wrapper}> <div className={classes.wrapper}>
<Sidebar <Sidebar