Use JSON over strings as payload format

This commit is contained in:
Thomas Nordquist
2019-04-03 00:39:02 +02:00
parent 27f5e8a7eb
commit d3598d8417
19 changed files with 145 additions and 37 deletions

View File

@@ -4,6 +4,7 @@ import BarChart from '@material-ui/icons/BarChart'
import DateFormatter from '../../helper/DateFormatter'
import History from '../History'
import { TopicViewModel } from '../../../TopicViewModel'
import { Base64Message } from '../../../../../backend/src/Model/Base64Message';
const PlotHistory = React.lazy(() => import('./PlotHistory'))
@@ -56,7 +57,11 @@ class MessageHistory extends React.Component<Props, State> {
selected: message && message === this.props.selected,
}))
const numericMessages = history.filter(message => !isNaN(parseFloat(message.value)))
const numericMessages = history
.map((message: q.Message) => {
const number = message.value ? parseFloat(Base64Message.toUnicodeString(message.value)) : NaN
return { x: number, y: message.received.getTime() }
}).filter(data => !isNaN(data.x))
const showPlot = numericMessages.length >= 2
return (
@@ -72,10 +77,10 @@ class MessageHistory extends React.Component<Props, State> {
)
}
public renderPlot(numericMessages: q.Message[]) {
public renderPlot(data: {x: number, y: number}[]) {
return (
<React.Suspense fallback={<div>Loading...</div>}>
<PlotHistory messages={numericMessages} />
<PlotHistory data={data} />
</React.Suspense>
)
}

View File

@@ -3,10 +3,11 @@ import * as React from 'react'
import DateFormatter from '../../helper/DateFormatter'
import { default as ReactResizeDetector } from 'react-resize-detector'
import 'react-vis/dist/style.css'
import { Base64Message } from '../../../../../backend/src/Model/Base64Message';
const { XYPlot, LineMarkSeries, Hint, YAxis, HorizontalGridLines } = require('react-vis')
interface Props {
messages: q.Message[]
data: {x: number, y: number}[]
}
interface Stats {
@@ -25,12 +26,7 @@ class PlotHistory extends React.Component<Props, Stats> {
}
public render() {
const data = this.props.messages.map((message) => {
return {
x: message.received.getTime(),
y: parseFloat(message.value),
}
})
const data = this.props.data
return (
<div>

View File

@@ -27,6 +27,7 @@ import {
withStyles,
Theme,
} from '@material-ui/core'
import { Base64Message } from '../../../../../backend/src/Model/Base64Message';
interface Props {
node?: q.TreeNode<any>
@@ -50,7 +51,7 @@ class ValuePanel extends React.Component<Props, State> {
const { node, classes } = this.props
const { detailsStyle, summaryStyle } = this.panelStyle()
const copyValue = node && node.message ? <Copy value={node.message.value} /> : null
const copyValue = (node && node.message && node.message.value) ? <Copy value={Base64Message.toUnicodeString(node.message.value)} /> : null
return (
<ExpansionPanel key="value" defaultExpanded={true}>

View File

@@ -5,6 +5,7 @@ import { AppState } from '../../../reducers'
import { connect } from 'react-redux'
import { default as ReactResizeDetector } from 'react-resize-detector'
import { ValueRendererDisplayMode } from '../../../reducers/Settings'
import { Base64Message } from '../../../../../backend/src/Model/Base64Message';
interface Props {
message: q.Message
@@ -42,25 +43,33 @@ class ValueRenderer extends React.Component<Props, State> {
compareMessage = message
}
if (!message.value) {
return null
}
const compareValue = compareMessage.value || message.value
const str = Base64Message.toUnicodeString(message.value)
const compareStr = Base64Message.toUnicodeString(compareValue)
let json
try {
json = JSON.parse(message.value)
json = JSON.parse(str)
} catch (error) {
return this.renderRawValue(message.value, compareMessage.value)
return this.renderRawValue(str, compareStr)
}
if (typeof json === 'string') {
return this.renderRawValue(message.value, compareMessage.value)
return this.renderRawValue(str, compareStr)
} else if (typeof json === 'number') {
return this.renderRawValue(message.value, compareMessage.value)
return this.renderRawValue(str, compareStr)
} else if (typeof json === 'boolean') {
return this.renderRawValue(message.value, compareMessage.value)
return this.renderRawValue(str, compareStr)
} else {
const current = this.messageToPrettyJson(message) || message.value
const compare = this.messageToPrettyJson(compareMessage) || compareMessage.value
const current = this.messageToPrettyJson(str) || str
const compare = this.messageToPrettyJson(compareStr) || compareStr
const language = current && compare ? 'json' : undefined
return this.renderDiff(current, compare, language)
return this.renderDiff(str, compareStr, language)
}
}
@@ -75,13 +84,9 @@ class ValueRenderer extends React.Component<Props, State> {
)
}
private messageToPrettyJson(message?: q.Message): string | undefined {
if (!message || !message.value) {
return undefined
}
private messageToPrettyJson(str: string): string | undefined {
try {
const json = JSON.parse(message.value)
const json = JSON.parse(str)
return JSON.stringify(json, undefined, ' ')
} catch {
return undefined