mirror of
https://github.com/woodchen-ink/clash-and-dashboard.git
synced 2025-07-18 22:11:56 +08:00
fix(logs): add logs cache
This commit is contained in:
parent
344005aa4e
commit
498e7c1f7c
@ -12,12 +12,16 @@ import Logs from '@containers/Logs'
|
|||||||
import Rules from '@containers/Rules'
|
import Rules from '@containers/Rules'
|
||||||
import Settings from '@containers/Settings'
|
import Settings from '@containers/Settings'
|
||||||
import SlideBar from '@containers/Sidebar'
|
import SlideBar from '@containers/Sidebar'
|
||||||
|
import { getLogsStreamReader } from '@lib/request'
|
||||||
|
|
||||||
export interface AppProps extends I18nProps {
|
export interface AppProps extends I18nProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
@hot(module)
|
@hot(module)
|
||||||
export default class App extends React.Component<AppProps, {}> {
|
export default class App extends React.Component<AppProps, {}> {
|
||||||
|
componentDidMount () {
|
||||||
|
getLogsStreamReader()
|
||||||
|
}
|
||||||
render () {
|
render () {
|
||||||
const routes = [
|
const routes = [
|
||||||
{ path: '/', name: 'Overview', component: Overview, exact: true },
|
{ path: '/', name: 'Overview', component: Overview, exact: true },
|
||||||
|
@ -4,8 +4,7 @@ import { translate } from 'react-i18next'
|
|||||||
import { I18nProps } from '@models'
|
import { I18nProps } from '@models'
|
||||||
import { Card, Header } from '@components'
|
import { Card, Header } from '@components'
|
||||||
import './style.scss'
|
import './style.scss'
|
||||||
import { StreamReader } from '@lib/streamer'
|
import { getLogsStreamReader } from '@lib/request'
|
||||||
import { getExternalControllerConfig, getConfig } from '@lib/request'
|
|
||||||
|
|
||||||
interface Log {
|
interface Log {
|
||||||
type: string
|
type: string
|
||||||
@ -27,11 +26,9 @@ class Logs extends React.Component<LogsProps, LogsState> {
|
|||||||
private streamReader = null
|
private streamReader = null
|
||||||
private listRef = React.createRef<HTMLUListElement>()
|
private listRef = React.createRef<HTMLUListElement>()
|
||||||
async componentDidMount () {
|
async componentDidMount () {
|
||||||
const externalController = await getExternalControllerConfig()
|
this.streamReader = await getLogsStreamReader()
|
||||||
const { data: config } = await getConfig()
|
let logs = this.streamReader.buffer()
|
||||||
const logUrl = `http://${externalController.hostname}:${externalController.port}/logs?level=${config['log-level']}`
|
this.setState({ logs }, () => this.scrollToBottom())
|
||||||
this.streamReader = new StreamReader({ url: logUrl })
|
|
||||||
let logs = []
|
|
||||||
this.streamReader.subscribe('data', (data) => {
|
this.streamReader.subscribe('data', (data) => {
|
||||||
logs = [].concat(this.state.logs, data.map(d => ({ ...d, time: new Date() })))
|
logs = [].concat(this.state.logs, data.map(d => ({ ...d, time: new Date() })))
|
||||||
this.setState({ logs }, () => this.scrollToBottom())
|
this.setState({ logs }, () => this.scrollToBottom())
|
||||||
|
@ -2,8 +2,10 @@ import axios, { AxiosInstance } from 'axios'
|
|||||||
import { Partial, getLocalStorageItem } from '@lib/helper'
|
import { Partial, getLocalStorageItem } from '@lib/helper'
|
||||||
import { isClashX } from '@lib/jsBridge'
|
import { isClashX } from '@lib/jsBridge'
|
||||||
import { rootStores } from '@lib/createStore'
|
import { rootStores } from '@lib/createStore'
|
||||||
|
import { StreamReader } from './streamer'
|
||||||
|
|
||||||
let instance: AxiosInstance
|
let instance: AxiosInstance
|
||||||
|
let logsStreamReader = null
|
||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
port: number
|
port: number
|
||||||
@ -116,3 +118,14 @@ export async function getExternalControllerConfig () {
|
|||||||
|
|
||||||
return { hostname, port, secret }
|
return { hostname, port, secret }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getLogsStreamReader () {
|
||||||
|
if (logsStreamReader) {
|
||||||
|
return logsStreamReader
|
||||||
|
}
|
||||||
|
const externalController = await getExternalControllerConfig()
|
||||||
|
const { data: config } = await getConfig()
|
||||||
|
const logUrl = `http://${externalController.hostname}:${externalController.port}/logs?level=${config['log-level']}`
|
||||||
|
logsStreamReader = new StreamReader({ url: logUrl, bufferLength: 200 })
|
||||||
|
return logsStreamReader
|
||||||
|
}
|
||||||
|
@ -29,7 +29,6 @@ export class ConfigStore {
|
|||||||
|
|
||||||
// otherwise parse ini
|
// otherwise parse ini
|
||||||
const config = yaml.parse(rawConfig)
|
const config = yaml.parse(rawConfig)
|
||||||
console.log(config)
|
|
||||||
const externalController = config['external-controller'] as string || ''
|
const externalController = config['external-controller'] as string || ''
|
||||||
const host = externalController.split(':')
|
const host = externalController.split(':')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user