From c226486d053427533c49b9501d1fdd59ebdcfcad Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Thu, 11 Jun 2020 00:28:19 -0500 Subject: [PATCH] Unify log api. --- .../console-app/src/components/LogPoller.js | 42 +++++++++++++++++ .../src/containers/panels/signal/SignalLog.js | 46 ------------------- .../src/containers/panels/signal/Signaling.js | 6 ++- .../src/containers/panels/wns/WNS.js | 6 ++- .../src/containers/panels/wns/WNSLog.js | 46 ------------------- packages/console-app/src/gql/logs.graphql | 10 ++++ .../console-app/src/gql/signal_log.graphql | 10 ---- packages/console-app/src/gql/wns_log.graphql | 10 ---- packages/console-app/src/version.json | 2 +- packages/console-server/src/gql/api.graphql | 6 +-- packages/console-server/src/resolvers/log.js | 32 +------------ 11 files changed, 64 insertions(+), 152 deletions(-) create mode 100644 packages/console-app/src/components/LogPoller.js delete mode 100644 packages/console-app/src/containers/panels/signal/SignalLog.js delete mode 100644 packages/console-app/src/containers/panels/wns/WNSLog.js create mode 100644 packages/console-app/src/gql/logs.graphql delete mode 100644 packages/console-app/src/gql/signal_log.graphql delete mode 100644 packages/console-app/src/gql/wns_log.graphql diff --git a/packages/console-app/src/components/LogPoller.js b/packages/console-app/src/components/LogPoller.js new file mode 100644 index 0000000..7a501af --- /dev/null +++ b/packages/console-app/src/components/LogPoller.js @@ -0,0 +1,42 @@ +// +// Copyright 2020 DxOS.org +// + +import React, { useContext } from 'react'; +import { useQuery } from '@apollo/react-hooks'; + +import { ConsoleContext, useQueryStatusReducer } from '../hooks'; + +import Log from './Log'; + +const MAX_LINES = 1000; +const logBuffer = []; + +const LogPoller = ({ service, query }) => { + const { config } = useContext(ConsoleContext); + const data = useQueryStatusReducer(useQuery(query, { + pollInterval: config.api.intervalLog, + variables: { service, incremental: logBuffer.length !== 0 } + })); + + if (!data) { + return null; + } + + const { incremental, lines } = JSON.parse(data.wns_log.json); + + if (!incremental && lines.length) { + logBuffer.length = 0; + } + + logBuffer.push(...lines); + if (logBuffer.length > MAX_LINES) { + logBuffer.splice(0, logBuffer.length - MAX_LINES); + } + + return ( + + ); +}; + +export default LogPoller; diff --git a/packages/console-app/src/containers/panels/signal/SignalLog.js b/packages/console-app/src/containers/panels/signal/SignalLog.js deleted file mode 100644 index 3ecd0bf..0000000 --- a/packages/console-app/src/containers/panels/signal/SignalLog.js +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright 2020 DxOS.org -// - -import React, { useContext } from 'react'; -import { useQuery } from '@apollo/react-hooks'; - -import SIGNAL_LOG from '../../../gql/signal_log.graphql'; - -import { ConsoleContext } from '../../../hooks'; - -import Log from '../../../components/Log'; - -const MAX_LINES = 1000; -const logBuffer = []; - -const SignalLog = () => { - const { config } = useContext(ConsoleContext); - const { data, refetch, startPolling, stopPolling } = useQuery(SIGNAL_LOG, { - variables: { incremental: false } - }); - - if (!data) { - return null; - } - - const { incremental, lines } = JSON.parse(data.signal_log.json); - - if (!incremental) { - stopPolling(); - refetch({ incremental: true }); - startPolling(config.api.intervalLog); - } - - logBuffer.push(...lines); - if (logBuffer.length > MAX_LINES) { - logBuffer.splice(0, logBuffer.length - MAX_LINES); - } - - return ( - - ); -} -; - -export default SignalLog; diff --git a/packages/console-app/src/containers/panels/signal/Signaling.js b/packages/console-app/src/containers/panels/signal/Signaling.js index 6fd5323..37dd380 100644 --- a/packages/console-app/src/containers/panels/signal/Signaling.js +++ b/packages/console-app/src/containers/panels/signal/Signaling.js @@ -9,11 +9,13 @@ import Tab from '@material-ui/core/Tab'; import Tabs from '@material-ui/core/Tabs'; import TabContext from '@material-ui/lab/TabContext'; +import SIGNAL_LOG from '../../../gql/logs.graphql'; + import Panel from '../../../components/Panel'; import Toolbar from '../../../components/Toolbar'; -import SignalLog from './SignalLog'; import SignalStatus from './SignalStatus'; +import LogPoller from "../../../components/LogPoller"; const TAB_STATUS = 'status'; const TAB_LOG = 'log'; @@ -62,7 +64,7 @@ const Signal = () => { {tab === TAB_LOG && (
- +
)} diff --git a/packages/console-app/src/containers/panels/wns/WNS.js b/packages/console-app/src/containers/panels/wns/WNS.js index 0d7c80e..00476f7 100644 --- a/packages/console-app/src/containers/panels/wns/WNS.js +++ b/packages/console-app/src/containers/panels/wns/WNS.js @@ -9,10 +9,12 @@ import Tab from '@material-ui/core/Tab'; import Tabs from '@material-ui/core/Tabs'; import TabContext from '@material-ui/lab/TabContext'; +import WNS_LOG from '../../../gql/wns_log.graphql'; + +import LogPoller from "../../../components/LogPoller"; import Panel from '../../../components/Panel'; import Toolbar from '../../../components/Toolbar'; -import WNSLog from './WNSLog'; import WNSRecords, { WNSRecordType } from './WNSRecords'; import WNSStatus from './WNSStatus'; @@ -76,7 +78,7 @@ const WNS = () => { {tab === TAB_LOG && (
- +
)} diff --git a/packages/console-app/src/containers/panels/wns/WNSLog.js b/packages/console-app/src/containers/panels/wns/WNSLog.js deleted file mode 100644 index 265c406..0000000 --- a/packages/console-app/src/containers/panels/wns/WNSLog.js +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright 2020 DxOS.org -// - -import React, { useContext } from 'react'; -import { useQuery } from '@apollo/react-hooks'; - -import WNS_LOG from '../../../gql/wns_log.graphql'; - -import { ConsoleContext } from '../../../hooks'; - -import Log from '../../../components/Log'; - -const MAX_LINES = 1000; -const logBuffer = []; - -const WNSLog = () => { - const { config } = useContext(ConsoleContext); - const { data, refetch, startPolling, stopPolling } = useQuery(WNS_LOG, { - variables: { incremental: false } - }); - - if (!data) { - return null; - } - - const { incremental, lines } = JSON.parse(data.signal_log.json); - - if (!incremental) { - stopPolling(); - refetch({ incremental: true }); - startPolling(config.api.intervalLog); - } - - logBuffer.push(...lines); - if (logBuffer.length > MAX_LINES) { - logBuffer.splice(0, logBuffer.length - MAX_LINES); - } - - return ( - - ); -} -; - -export default WNSLog; diff --git a/packages/console-app/src/gql/logs.graphql b/packages/console-app/src/gql/logs.graphql new file mode 100644 index 0000000..fb4b122 --- /dev/null +++ b/packages/console-app/src/gql/logs.graphql @@ -0,0 +1,10 @@ +# +# Copyright 2020 DxOS.org +# + +query ($service: String!, $incremental: Boolean) { + logs(service: $service, incremental: $incremental) { + timestamp + json + } +} diff --git a/packages/console-app/src/gql/signal_log.graphql b/packages/console-app/src/gql/signal_log.graphql deleted file mode 100644 index a39a708..0000000 --- a/packages/console-app/src/gql/signal_log.graphql +++ /dev/null @@ -1,10 +0,0 @@ -# -# Copyright 2020 DxOS.org -# - -query ($incremental: Boolean) { - signal_log(incremental: $incremental) { - timestamp - json - } -} diff --git a/packages/console-app/src/gql/wns_log.graphql b/packages/console-app/src/gql/wns_log.graphql deleted file mode 100644 index 4bf829d..0000000 --- a/packages/console-app/src/gql/wns_log.graphql +++ /dev/null @@ -1,10 +0,0 @@ -# -# Copyright 2020 DxOS.org -# - -query($incremental: Boolean) { - wns_log(incremental: $incremental) { - timestamp - json - } -} diff --git a/packages/console-app/src/version.json b/packages/console-app/src/version.json index 90b5e24..618f306 100644 --- a/packages/console-app/src/version.json +++ b/packages/console-app/src/version.json @@ -1,7 +1,7 @@ { "build": { "name": "@dxos/console-app", - "buildDate": "2020-06-10T22:43:33.469Z", + "buildDate": "2020-06-11T05:01:31.306Z", "version": "1.0.0-beta.0" } } diff --git a/packages/console-server/src/gql/api.graphql b/packages/console-server/src/gql/api.graphql index 8f6f991..31121b0 100644 --- a/packages/console-server/src/gql/api.graphql +++ b/packages/console-server/src/gql/api.graphql @@ -13,16 +13,12 @@ type JSONResult { # type Query { - app_log(incremental: Boolean): JSONResult! + logs(service: String!, incremental: Boolean): JSONResult! app_status: JSONResult! - ipfs_log(incremental: Boolean): JSONResult! ipfs_status: JSONResult! - ipfs_swarm_log(incremental: Boolean): JSONResult! ipfs_swarm_status: JSONResult! - signal_log(incremental: Boolean): JSONResult! signal_status: JSONResult! system_status: JSONResult! - wns_log(incremental: Boolean): JSONResult! wns_status: JSONResult! } diff --git a/packages/console-server/src/resolvers/log.js b/packages/console-server/src/resolvers/log.js index 9be338d..8cdbaec 100644 --- a/packages/console-server/src/resolvers/log.js +++ b/packages/console-server/src/resolvers/log.js @@ -53,40 +53,12 @@ const getLogs = async (name, incremental = false, lines = 100) => { export const logResolvers = { Query: { - wns_log: async (_, { incremental }) => { - const lines = await getLogs('wns-lite', incremental); + logs: async (_, { service, incremental }) => { + const lines = await getLogs(service, incremental); return { timestamp: new Date().toUTCString(), json: JSON.stringify({ incremental, lines }) }; }, - signal_log: async (_, { incremental }) => { - const lines = await getLogs('signal', incremental); - return { - timestamp: new Date().toUTCString(), - json: JSON.stringify({ incremental, lines }) - }; - }, - ipfs_log: async (_, { incremental }) => { - const lines = await getLogs('ipfs', incremental); - return { - timestamp: new Date().toUTCString(), - json: JSON.stringify({ incremental, lines }) - }; - }, - ipfs_swarm_log: async (_, { incremental }) => { - const lines = await getLogs('ipfs-swarm-connect', incremental); - return { - timestamp: new Date().toUTCString(), - json: JSON.stringify({ incremental, lines }) - }; - }, - app_log: async (_, { incremental }) => { - const lines = await getLogs('app', incremental); - return { - timestamp: new Date().toUTCString(), - json: JSON.stringify({ incremental, lines }) - }; - } } };