Unify log api.

This commit is contained in:
Thomas E Lackey 2020-06-11 00:28:19 -05:00
parent 22f2c7c600
commit c226486d05
11 changed files with 64 additions and 152 deletions

View File

@ -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 (
<Log log={logBuffer.slice(0)} />
);
};
export default LogPoller;

View File

@ -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 (
<Log log={logBuffer.slice(0)} />
);
}
;
export default SignalLog;

View File

@ -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 && (
<div className={classes.panel}>
<SignalLog />
<LogPoller query={SIGNAL_LOG} />
</div>
)}
</TabContext>

View File

@ -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 && (
<div className={classes.panel}>
<WNSLog />
<LogPoller query={WNS_LOG} />
</div>
)}
</TabContext>

View File

@ -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 (
<Log log={logBuffer.slice(0)} />
);
}
;
export default WNSLog;

View File

@ -0,0 +1,10 @@
#
# Copyright 2020 DxOS.org
#
query ($service: String!, $incremental: Boolean) {
logs(service: $service, incremental: $incremental) {
timestamp
json
}
}

View File

@ -1,10 +0,0 @@
#
# Copyright 2020 DxOS.org
#
query ($incremental: Boolean) {
signal_log(incremental: $incremental) {
timestamp
json
}
}

View File

@ -1,10 +0,0 @@
#
# Copyright 2020 DxOS.org
#
query($incremental: Boolean) {
wns_log(incremental: $incremental) {
timestamp
json
}
}

View File

@ -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"
}
}

View File

@ -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!
}

View File

@ -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 })
};
}
}
};