|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
|
|
|
|
import * as api from '../../api'
|
|
|
|
|
import * as storage from '../../util/storage'
|
|
|
|
|
import { LoadingState } from './graph-util'
|
|
|
|
|
@ -27,38 +27,57 @@ function fetchMainGraph(site, query, metric, interval) {
|
|
|
|
|
return api.get(url.apiPath(site, '/main-graph'), query, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class VisitorGraph extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
this.state = {
|
|
|
|
|
loading: LoadingState.loading,
|
|
|
|
|
topStatData: null,
|
|
|
|
|
graphData: null,
|
|
|
|
|
export default function VisitorGraph(props) {
|
|
|
|
|
const {site, query, lastLoadTimestamp} = props
|
|
|
|
|
const isRealtime = query.period === 'realtime'
|
|
|
|
|
const isDarkTheme = document.querySelector('html').classList.contains('dark') || false
|
|
|
|
|
|
|
|
|
|
const topStatsBoundary = useRef(null)
|
|
|
|
|
|
|
|
|
|
const [loading, setLoading] = useState(LoadingState.loading)
|
|
|
|
|
const [topStatData, setTopStatData] = useState(null)
|
|
|
|
|
const [graphData, setGraphData] = useState(null)
|
|
|
|
|
|
|
|
|
|
const onIntervalUpdate = useCallback((newInterval) => {
|
|
|
|
|
fetchGraphData(getStoredMetric(), newInterval)
|
|
|
|
|
}, [query])
|
|
|
|
|
|
|
|
|
|
const onMetricUpdate = useCallback((newMetric) => {
|
|
|
|
|
fetchGraphData(newMetric, getCurrentInterval(site, query))
|
|
|
|
|
}, [query])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchTopStatsAndGraphData()
|
|
|
|
|
|
|
|
|
|
if (isRealtime) {
|
|
|
|
|
document.addEventListener('tick', fetchTopStatsAndGraphData)
|
|
|
|
|
}
|
|
|
|
|
this.fetchTopStatsAndGraphData = this.fetchTopStatsAndGraphData.bind(this)
|
|
|
|
|
this.fetchGraphData = this.fetchGraphData.bind(this)
|
|
|
|
|
this.onIntervalUpdate = this.onIntervalUpdate.bind(this)
|
|
|
|
|
this.onMetricUpdate = this.onMetricUpdate.bind(this)
|
|
|
|
|
this.boundary = React.createRef()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchTopStatsAndGraphData() {
|
|
|
|
|
const { site, query } = this.props
|
|
|
|
|
|
|
|
|
|
this.setState({loading: LoadingState.loading, topStatData: null, graphData: null})
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('tick', fetchTopStatsAndGraphData)
|
|
|
|
|
}
|
|
|
|
|
}, [query])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (topStatData) { storeTopStatsContainerHeight() }
|
|
|
|
|
}, [topStatData])
|
|
|
|
|
|
|
|
|
|
function fetchTopStatsAndGraphData() {
|
|
|
|
|
setLoading(LoadingState.loading)
|
|
|
|
|
setTopStatData(null)
|
|
|
|
|
setGraphData(null)
|
|
|
|
|
|
|
|
|
|
fetchTopStats(site, query)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
this.setState({ topStatData: res, loading: LoadingState.updatingGraph }, () => {
|
|
|
|
|
this.storeTopStatsContainerHeight()
|
|
|
|
|
})
|
|
|
|
|
setTopStatData(res)
|
|
|
|
|
setLoading(LoadingState.updatingGraph)
|
|
|
|
|
|
|
|
|
|
let metric = this.getStoredMetric()
|
|
|
|
|
let metric = getStoredMetric()
|
|
|
|
|
const availableMetrics = res.top_stats.filter(stat => !!stat.graph_metric).map(stat => stat.graph_metric)
|
|
|
|
|
|
|
|
|
|
if (!availableMetrics.includes(metric)) {
|
|
|
|
|
metric = availableMetrics[0]
|
|
|
|
|
storage.setItem(`metric__${this.props.site.domain}`, metric)
|
|
|
|
|
storage.setItem(`metric__${site.domain}`, metric)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const interval = getCurrentInterval(site, query)
|
|
|
|
|
@ -66,95 +85,63 @@ export default class VisitorGraph extends React.Component {
|
|
|
|
|
return fetchMainGraph(site, query, metric, interval)
|
|
|
|
|
})
|
|
|
|
|
.then((res) => {
|
|
|
|
|
this.setState({ graphData: res, loading: LoadingState.loaded })
|
|
|
|
|
setGraphData(res)
|
|
|
|
|
setLoading(LoadingState.loaded)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchGraphData(metric, interval) {
|
|
|
|
|
const { site, query } = this.props
|
|
|
|
|
|
|
|
|
|
this.setState({loading: LoadingState.updatingGraph, graphData: null})
|
|
|
|
|
function fetchGraphData(metric, interval) {
|
|
|
|
|
setLoading(LoadingState.updatingGraph)
|
|
|
|
|
setGraphData(null)
|
|
|
|
|
|
|
|
|
|
fetchMainGraph(site, query, metric, interval)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
this.setState({graphData: res, loading: LoadingState.loaded})
|
|
|
|
|
setGraphData(res)
|
|
|
|
|
setLoading(LoadingState.loaded)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStoredMetric() {
|
|
|
|
|
return storage.getItem(`metric__${this.props.site.domain}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onIntervalUpdate(newInterval) {
|
|
|
|
|
this.fetchGraphData(this.getStoredMetric(), newInterval)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMetricUpdate(newMetric) {
|
|
|
|
|
this.fetchGraphData(newMetric, getCurrentInterval(this.props.site, this.props.query))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.fetchTopStatsAndGraphData()
|
|
|
|
|
|
|
|
|
|
if (this.props.query.period === 'realtime') {
|
|
|
|
|
document.addEventListener('tick', this.fetchTopStatsAndGraphData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
|
if (this.props.query !== prevProps.query) {
|
|
|
|
|
this.fetchTopStatsAndGraphData()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
document.removeEventListener('tick', this.fetchTopStatsAndGraphData)
|
|
|
|
|
function getStoredMetric() {
|
|
|
|
|
return storage.getItem(`metric__${site.domain}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
storeTopStatsContainerHeight() {
|
|
|
|
|
storage.setItem(`topStatsHeight__${this.props.site.domain}`, document.getElementById('top-stats-container').clientHeight)
|
|
|
|
|
function storeTopStatsContainerHeight() {
|
|
|
|
|
storage.setItem(`topStatsHeight__${site.domain}`, document.getElementById('top-stats-container').clientHeight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function is used for maintaining the main-graph/top-stats container height in the
|
|
|
|
|
// loading process. The container height depends on how many top stat metrics are returned
|
|
|
|
|
// from the API, but in the loading state, we don't know that yet. We can use localStorage
|
|
|
|
|
// to keep track of the Top Stats container height.
|
|
|
|
|
getTopStatsHeight() {
|
|
|
|
|
if (this.state.topStatData) {
|
|
|
|
|
function getTopStatsHeight() {
|
|
|
|
|
if (topStatData) {
|
|
|
|
|
return 'auto'
|
|
|
|
|
} else {
|
|
|
|
|
return `${storage.getItem(`topStatsHeight__${this.props.site.domain}`) || 89}px`
|
|
|
|
|
return `${storage.getItem(`topStatsHeight__${site.domain}`) || 89}px`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { query, site } = this.props;
|
|
|
|
|
const { graphData, topStatData, loading } = this.state;
|
|
|
|
|
|
|
|
|
|
const isDarkTheme = document.querySelector('html').classList.contains('dark') || false
|
|
|
|
|
const isRealtime = query.period === 'realtime'
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={"relative w-full mt-2 bg-white rounded shadow-xl dark:bg-gray-825"}>
|
|
|
|
|
{loading === LoadingState.loading && renderLoader()}
|
|
|
|
|
<FadeIn show={loading !== LoadingState.loading}>
|
|
|
|
|
<div id="top-stats-container" className="flex flex-wrap" ref={this.boundary} style={{ height: this.getTopStatsHeight() }}>
|
|
|
|
|
<TopStats site={site} query={query} onMetricUpdate={this.onMetricUpdate} topStatData={topStatData} tooltipBoundary={this.boundary.current} lastLoadTimestamp={this.props.lastLoadTimestamp} />
|
|
|
|
|
</div>
|
|
|
|
|
<div className="relative px-2">
|
|
|
|
|
{loading === LoadingState.updatingGraph && renderLoader()}
|
|
|
|
|
<div className="absolute right-4 -top-8 py-1 flex items-center">
|
|
|
|
|
{!isRealtime && <StatsExport site={site} query={query} />}
|
|
|
|
|
<SamplingNotice samplePercent={topStatData}/>
|
|
|
|
|
<WithImportedSwitch site={site} topStatData={topStatData} />
|
|
|
|
|
<IntervalPicker site={site} query={query} onIntervalUpdate={this.onIntervalUpdate} />
|
|
|
|
|
</div>
|
|
|
|
|
<LineGraphWithRouter graphData={graphData} darkTheme={isDarkTheme} query={query} />
|
|
|
|
|
return (
|
|
|
|
|
<div className={"relative w-full mt-2 bg-white rounded shadow-xl dark:bg-gray-825"}>
|
|
|
|
|
{loading === LoadingState.loading && renderLoader()}
|
|
|
|
|
<FadeIn show={loading !== LoadingState.loading}>
|
|
|
|
|
<div id="top-stats-container" className="flex flex-wrap" ref={topStatsBoundary} style={{ height: getTopStatsHeight() }}>
|
|
|
|
|
<TopStats site={site} query={query} onMetricUpdate={onMetricUpdate} topStatData={topStatData} tooltipBoundary={topStatsBoundary.current} lastLoadTimestamp={lastLoadTimestamp} />
|
|
|
|
|
</div>
|
|
|
|
|
<div className="relative px-2">
|
|
|
|
|
{loading === LoadingState.updatingGraph && renderLoader()}
|
|
|
|
|
<div className="absolute right-4 -top-8 py-1 flex items-center">
|
|
|
|
|
{!isRealtime && <StatsExport site={site} query={query} />}
|
|
|
|
|
<SamplingNotice samplePercent={topStatData}/>
|
|
|
|
|
<WithImportedSwitch site={site} topStatData={topStatData} />
|
|
|
|
|
<IntervalPicker site={site} query={query} onIntervalUpdate={onIntervalUpdate} />
|
|
|
|
|
</div>
|
|
|
|
|
</FadeIn>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
<LineGraphWithRouter graphData={graphData} darkTheme={isDarkTheme} query={query} />
|
|
|
|
|
</div>
|
|
|
|
|
</FadeIn>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderLoader() {
|
|
|
|
|
|