Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Cache visitor graph results locally (#6119)
Artur Pata69508123298c
diff --git a/assets/js/dashboard/dashboard-state-context.tsx b/assets/js/dashboard/dashboard-state-context.tsxindex 3fd6946299..46641ec5dd 100644--- a/assets/js/dashboard/dashboard-state-context.tsx+++ b/assets/js/dashboard/dashboard-state-context.tsx@@ -95,7 +95,7 @@ export default function DashboardStateContextProvider({date:typeof date === 'string' && date.length? dayjs.utc(date)- : nowForSite(site),+ : nowForSite(site).startOf('day'),from:typeof from === 'string' && from.length? dayjs.utc(from)diff --git a/assets/js/dashboard/hooks/api-client.ts b/assets/js/dashboard/hooks/api-client.tsindex 05dcb51887..3d01462ddb 100644--- a/assets/js/dashboard/hooks/api-client.ts+++ b/assets/js/dashboard/hooks/api-client.ts@@ -6,8 +6,15 @@ import {} from '@tanstack/react-query'import * as api from '../api'import { DashboardState } from '../dashboard-state'+import { DashboardPeriod } from '../dashboard-time-periods'+import { Dayjs } from 'dayjs'+import { REALTIME_UPDATE_TIME_MS } from '../util/realtime-update-timer'-const LIMIT = 100+// defines when queries that don't include the current time should be refetched+const HISTORICAL_RESPONSES_STALE_TIME_MS = 12 * 60 * 60 * 1000++// how many items per page for breakdown modals+const PAGINATION_LIMIT = 100/** full endpoint URL */type Endpoint = string@@ -57,7 +64,7 @@ export function usePaginatedGetAPI<const response: TResponse = await api.get(endpoint, dashboardState, {...params,- limit: LIMIT,+ limit: PAGINATION_LIMIT,page: pageParam})@@ -78,7 +85,9 @@ export function usePaginatedGetAPI<return response.results},getNextPageParam: (lastPageResults, _, lastPageIndex) => {- return lastPageResults.length === LIMIT ? lastPageIndex + 1 : null+ return lastPageResults.length === PAGINATION_LIMIT+ ? lastPageIndex + 1+ : null},initialPageParam,placeholderData: (previousData) => previousData@@ -98,3 +107,60 @@ export const cleanToPageOne = <}return data}++export const getStaleTime = (+ /** the start of the current day */+ startOfDay: Dayjs,+ {+ period,+ from,+ to,+ date+ }: Pick<DashboardState, 'period' | 'from' | 'to' | 'date'>+): number => {+ if (DashboardPeriod.custom && to && from) {+ // historical+ if (from.isBefore(startOfDay) && to.isBefore(startOfDay)) {+ return HISTORICAL_RESPONSES_STALE_TIME_MS+ }+ // period includes now+ if (to.diff(from, 'days') < 7) {+ return 5 * 60 * 1000+ }+ if (to.diff(from, 'months') < 1) {+ return 15 * 60 * 1000+ }+ if (to.diff(from, 'months') < 12) {+ return 60 * 60 * 1000+ }+ return 3 * 60 * 60 * 1000+ }++ const historical = date?.isBefore(startOfDay)+ if (historical) {+ return HISTORICAL_RESPONSES_STALE_TIME_MS+ }++ switch (period) {+ case DashboardPeriod.realtime:+ return REALTIME_UPDATE_TIME_MS+ case DashboardPeriod['24h']:+ case DashboardPeriod.day:+ return 5 * 60 * 1000+ case DashboardPeriod['7d']:+ return 15 * 60 * 1000+ case DashboardPeriod['28d']:+ case DashboardPeriod['30d']:+ case DashboardPeriod['91d']:+ case DashboardPeriod['6mo']:+ return 60 * 60 * 1000+ case DashboardPeriod['12mo']:+ case DashboardPeriod.year:+ return 3 * 60 * 60 * 1000+ case DashboardPeriod.all:+ default:+ // err on the side of less caching,+ // to avoid the user refresheshing+ return 15 * 60 * 1000+ }+}diff --git a/assets/js/dashboard/stats/graph/interval-picker.tsx b/assets/js/dashboard/stats/graph/interval-picker.tsxindex d53b20d6d5..c6df5c5242 100644--- a/assets/js/dashboard/stats/graph/interval-picker.tsx+++ b/assets/js/dashboard/stats/graph/interval-picker.tsx@@ -1,11 +1,11 @@-import React, { useRef } from 'react'+import React, { useCallback, useEffect, useRef, useState } from 'react'import { Popover, Transition } from '@headlessui/react'import { ChevronDownIcon } from '@heroicons/react/20/solid'import classNames from 'classnames'import * as storage from '../../util/storage'import { isModifierPressed, isTyping, Keybind } from '../../keybinding'import { useDashboardStateContext } from '../../dashboard-state-context'-import { useSiteContext, PlausibleSite } from '../../site-context'+import { PlausibleSite } from '../../site-context'import { useMatch } from 'react-router-dom'import { rootRoute } from '../../router'import { BlurMenuButtonOnEscape, popover } from '../../components/popover'@@ -22,8 +22,8 @@ const INTERVAL_LABELS: Record<string, string> = {}function validIntervals(- site: PlausibleSite,- dashboardState: DashboardState+ site: Pick<PlausibleSite, 'validIntervalsByPeriod'>,+ dashboardState: Pick<DashboardState, 'period' | 'to' | 'from'>): string[] {if (dashboardState.period === DashboardPeriod.custom &&@@ -44,8 +44,8 @@ function validIntervals(}}-function getDefaultInterval(- dashboardState: DashboardState,+export function getDefaultInterval(+ dashboardState: Pick<DashboardState, 'period' | 'to' | 'from'>,validIntervals: string[]): string {const defaultByPeriod: Record<string, string> = {@@ -92,44 +92,59 @@ function storeInterval(period: string, domain: string, interval: string): void {storage.setItem(`interval__${period}__${domain}`, interval)}-export const getCurrentInterval = function (+export const useStoredInterval = (site: PlausibleSite,- dashboardState: DashboardState-): string {- const options = validIntervals(site, dashboardState)+ { to, from, period }: Pick<DashboardState, 'to' | 'from' | 'period'>+) => {+ const availableIntervals = validIntervals(site, { to, from, period })- const storedInterval = getStoredInterval(dashboardState.period, site.domain)- const defaultInterval = getDefaultInterval(dashboardState, options)+ const isValid = (interval: string | null): interval is string =>+ !!interval && availableIntervals.includes(interval)- if (storedInterval && options.includes(storedInterval)) {- return storedInterval- } else {- return defaultInterval+ const storedInterval = getStoredInterval(period, site.domain)++ const [selectedInterval, setSelectedInterval] = useState<string | null>(null)++ useEffect(() => {+ setSelectedInterval(null)+ }, [availableIntervals])++ const onIntervalClick = useCallback(+ (interval: string) => {+ storeInterval(period, site.domain, interval)+ setSelectedInterval(interval)+ },+ [period, site.domain]+ )++ return {+ selectedInterval: isValid(selectedInterval)+ ? selectedInterval+ : isValid(storedInterval)+ ? storedInterval+ : getDefaultInterval({ to, from, period }, availableIntervals),+ onIntervalClick,+ availableIntervals}}export function IntervalPicker({- onIntervalUpdate+ selectedInterval,+ onIntervalClick,+ options}: {- onIntervalUpdate: (interval: string) => void+ selectedInterval: string+ onIntervalClick: (interval: string) => void+ options: string[]}): JSX.Element | null {const menuElement = useRef<HTMLButtonElement>(null)const { dashboardState } = useDashboardStateContext()- const site = useSiteContext()const dashboardRouteMatch = useMatch(rootRoute.path)if (dashboardState.period == 'realtime') {return null}- const options = validIntervals(site, dashboardState)- const currentInterval = getCurrentInterval(site, dashboardState)-- function updateInterval(interval: string): void {- storeInterval(dashboardState.period, site.domain, interval)- onIntervalUpdate(interval)- }-return (<>{!!dashboardRouteMatch && (@@ -155,7 +170,7 @@ export function IntervalPicker({)}><span data-testid="current-graph-interval">- {INTERVAL_LABELS[currentInterval]}+ {INTERVAL_LABELS[selectedInterval]}</span><ChevronDownIcon className="ml-1 h-4 w-4" aria-hidden="true" /></Popover.Button>@@ -178,10 +193,10 @@ export function IntervalPicker({<buttonkey={option}onClick={() => {- updateInterval(option)+ onIntervalClick(option)closeDropdown()}}- data-selected={option == currentInterval}+ data-selected={option == selectedInterval}className={classNames(popover.items.classNames.navigationLink,popover.items.classNames.selectedOption,diff --git a/assets/js/dashboard/stats/graph/line-graph.js b/assets/js/dashboard/stats/graph/line-graph.jsindex cca26000a1..e9f965ac2f 100644--- a/assets/js/dashboard/stats/graph/line-graph.js+++ b/assets/js/dashboard/stats/graph/line-graph.js@@ -5,11 +5,12 @@ import Chart from 'chart.js/auto'import GraphTooltip from './graph-tooltip'import { buildDataSet, METRIC_LABELS, hasMultipleYears } from './graph-util'import dateFormatter from './date-formatter'-import FadeIn from '../../fade-in'import classNames from 'classnames'import { hasConversionGoalFilter } from '../../util/filters'import { MetricFormatterShort } from '../reports/metric-formatter'import { UIMode, useTheme } from '../../theme-context'+import { Transition } from '@headlessui/react'+import equal from 'fast-deep-equal'const calculateMaximumY = function (dataset) {const yAxisValues = dataset@@ -26,7 +27,6 @@ const calculateMaximumY = function (dataset) {class LineGraph extends React.Component {constructor(props) {super(props)- this.regenerateChart = this.regenerateChart.bind(this)this.updateWindowDimensions = this.updateWindowDimensions.bind(this)}@@ -43,11 +43,53 @@ class LineGraph extends React.Component {}}- regenerateChart() {+ buildXTicksCallback() {+ const { graphData, dashboardState } = this.props+ const shouldShowYear = hasMultipleYears(graphData)+ return function (val, _index, _ticks) {+ if (this.getLabelForValue(val) == '__blank__') return ''++ if (graphData.interval === 'hour' && dashboardState.period !== 'day') {+ const date = dateFormatter({+ interval: 'day',+ longForm: false,+ period: dashboardState.period,+ shouldShowYear+ })(this.getLabelForValue(val))++ const hour = dateFormatter({+ interval: graphData.interval,+ longForm: false,+ period: dashboardState.period,+ shouldShowYear+ })(this.getLabelForValue(val))++ return `${date}, ${hour}`+ }++ if (+ graphData.interval === 'minute' &&+ dashboardState.period !== 'realtime'+ ) {+ return dateFormatter({+ interval: 'hour',+ longForm: false,+ period: dashboardState.period+ })(this.getLabelForValue(val))+ }++ return dateFormatter({+ interval: graphData.interval,+ longForm: false,+ period: dashboardState.period,+ shouldShowYear+ })(this.getLabelForValue(val))+ }+ }++ updateChart() {const { graphData, dashboardState, theme } = this.propsconst metric = this.getGraphMetric()- const graphEl = document.getElementById('main-graph-canvas')- this.ctx = graphEl.getContext('2d')const dataSet = buildDataSet(graphData.plot,graphData.comparison_plot,@@ -56,12 +98,39 @@ class LineGraph extends React.Component {METRIC_LABELS[metric])- return new Chart(this.ctx, {+ const maxY = calculateMaximumY(dataSet)++ this.chart.data.labels = graphData.labels+ this.chart.data.datasets = dataSet+ this.chart.options.scales.y.suggestedMax = maxY+ this.chart.options.scales.yComparison.suggestedMax = maxY+ this.chart.options.scales.y.ticks.callback = MetricFormatterShort[metric]+ this.chart.options.scales.y.ticks.color =+ theme.mode === UIMode.dark ? 'rgb(161, 161, 170)' : undefined+ this.chart.options.scales.y.grid.color =+ theme.mode === UIMode.dark+ ? 'rgba(39, 39, 42, 0.75)'+ : 'rgb(236, 236, 238)'+ this.chart.options.scales.x.ticks.color =+ theme.mode === UIMode.dark ? 'rgb(161, 161, 170)' : undefined+ this.chart.options.scales.x.ticks.callback = this.buildXTicksCallback()+ this.chart.options.plugins.tooltip.external = GraphTooltip(+ graphData,+ metric,+ dashboardState,+ theme+ )++ this.chart.update()+ }++ regenerateChart() {+ const graphEl = document.getElementById('main-graph-canvas')+ this.ctx = graphEl.getContext('2d')++ this.chart = new Chart(this.ctx, {type: 'line',- data: {- labels: graphData.labels,- datasets: dataSet- },+ data: { labels: [], datasets: [] },options: {animation: false,plugins: {@@ -71,7 +140,7 @@ class LineGraph extends React.Component {mode: 'index',intersect: false,position: 'average',- external: GraphTooltip(graphData, metric, dashboardState, theme)+ external: () => {}}},responsive: true,@@ -85,88 +154,17 @@ class LineGraph extends React.Component {scales: {y: {min: 0,- suggestedMax: calculateMaximumY(dataSet),- ticks: {- callback: MetricFormatterShort[metric],- color:- theme.mode === UIMode.dark ? 'rgb(161, 161, 170)' : undefined- },- grid: {- zeroLineColor: 'transparent',- drawBorder: false,- color:- theme.mode === UIMode.dark- ? 'rgba(39, 39, 42, 0.75)'- : 'rgb(236, 236, 238)'- }- },- yComparison: {- min: 0,- suggestedMax: calculateMaximumY(dataSet),- display: false,- grid: { display: false }+ ticks: {},+ grid: { zeroLineColor: 'transparent', drawBorder: false }},- x: {- grid: { display: false },- ticks: {- callback: function (val, _index, _ticks) {- if (this.getLabelForValue(val) == '__blank__') return ''-- const shouldShowYear = hasMultipleYears(graphData)-- if (- graphData.interval === 'hour' &&- dashboardState.period !== 'day'- ) {- const date = dateFormatter({- interval: 'day',- longForm: false,- period: dashboardState.period,- shouldShowYear- })(this.getLabelForValue(val))-- const hour = dateFormatter({- interval: graphData.interval,- longForm: false,- period: dashboardState.period,- shouldShowYear- })(this.getLabelForValue(val))-- // Returns a combination of date and hour. This is because- // small intervals like hour may return multiple days- // depending on the queried period.- return `${date}, ${hour}`- }-- if (- graphData.interval === 'minute' &&- dashboardState.period !== 'realtime'- ) {- return dateFormatter({- interval: 'hour',- longForm: false,- period: dashboardState.period- })(this.getLabelForValue(val))- }-- return dateFormatter({- interval: graphData.interval,- longForm: false,- period: dashboardState.period,- shouldShowYear- })(this.getLabelForValue(val))- },- color:- theme.mode === UIMode.dark ? 'rgb(161, 161, 170)' : undefined- }- }+ yComparison: { min: 0, display: false, grid: { display: false } },+ x: { grid: { display: false }, ticks: {} }},- interaction: {- mode: 'index',- intersect: false- }+ interaction: { mode: 'index', intersect: false }}})++ this.updateChart()}repositionTooltip(e) {@@ -187,7 +185,7 @@ class LineGraph extends React.Component {componentDidMount() {if (this.props.graphData) {- this.chart = this.regenerateChart()+ this.regenerateChart()}window.addEventListener('mousemove', this.repositionTooltip)}@@ -195,27 +193,25 @@ class LineGraph extends React.Component {componentDidUpdate(prevProps) {const { graphData, theme } = this.propsconst tooltip = document.getElementById('chartjs-tooltip-main')+ const dataChanged = !equal(graphData, prevProps.graphData)+ if (dataChanged || theme.mode !== prevProps.theme.mode) {+ if (tooltip) {+ tooltip.style.display = 'none'+ }- if (- graphData !== prevProps.graphData ||- theme.mode !== prevProps.theme.mode- ) {if (graphData) {if (this.chart) {- this.chart.destroy()+ this.updateChart()+ } else {+ this.regenerateChart()}- this.chart = this.regenerateChart()- this.chart.update()- }-- if (tooltip) {- tooltip.style.display = 'none'}}if (!graphData) {if (this.chart) {this.chart.destroy()+ this.chart = null}if (tooltip) {@@ -264,20 +260,29 @@ class LineGraph extends React.Component {render() {const { graphData } = this.props- const canvasClass = classNames('mt-4 select-none', {+ const canvasClass = classNames('select-none', {'cursor-pointer': !['minute', 'hour'].includes(graphData?.interval)})return (- <FadeIn show={graphData}>- <div className="relative h-96 print:h-auto print:pb-8 w-full z-0">- <canvas id="main-graph-canvas" className={canvasClass}></canvas>- </div>- </FadeIn>+ <Transition+ show={true}+ appear={true}+ as={React.Fragment}+ enter="transition ease-in duration-100"+ enterFrom="opacity-0"+ enterTo="opacity-100"+ >+ <canvas id="main-graph-canvas" className={canvasClass} />+ </Transition>)}}+export function LineGraphContainer(props) {+ return <div className="relative my-4 h-92 w-full z-0">{props.children}</div>+}+export default function LineGraphWrapped(props) {const { dashboardState } = useDashboardStateContext()const navigate = useAppNavigate()diff --git a/assets/js/dashboard/stats/graph/notices.tsx b/assets/js/dashboard/stats/graph/notices.tsxindex 39e04cd8c9..42c149849d 100644--- a/assets/js/dashboard/stats/graph/notices.tsx+++ b/assets/js/dashboard/stats/graph/notices.tsx@@ -33,13 +33,3 @@ export const NoticesIcon = ({ notices }: { notices: string[] }) => {</Tooltip>)}--export function getSamplingNotice(topStatData: { samplePercent?: number }) {- const samplePercent = topStatData?.samplePercent-- if (samplePercent && samplePercent < 100) {- return `Stats based on a ${samplePercent}% sample of all visitors`- }-- return null-}diff --git a/assets/js/dashboard/stats/graph/stats-export.tsx b/assets/js/dashboard/stats/graph/stats-export.tsxindex bd300fb24b..8fdfcb4c0d 100644--- a/assets/js/dashboard/stats/graph/stats-export.tsx+++ b/assets/js/dashboard/stats/graph/stats-export.tsx@@ -1,11 +1,14 @@import React, { useState } from 'react'import * as api from '../../api'-import { getCurrentInterval } from './interval-picker'import { useSiteContext } from '../../site-context'import { useDashboardStateContext } from '../../dashboard-state-context'import { Tooltip } from '../../util/tooltip'-export default function StatsExport() {+export default function StatsExport({+ selectedInterval+}: {+ selectedInterval: string+}) {const site = useSiteContext()const { dashboardState } = useDashboardStateContext()const [exporting, setExporting] = useState(false)@@ -50,9 +53,8 @@ export default function StatsExport() {}function renderExportLink() {- const interval = getCurrentInterval(site, dashboardState)const params = api.dashboardStateToSearchParams(dashboardState, [- { interval, comparison: undefined }+ { interval: selectedInterval, comparison: undefined }])const endpoint = `/${encodeURIComponent(site.domain)}/export?${params}`diff --git a/assets/js/dashboard/stats/graph/top-stats.js b/assets/js/dashboard/stats/graph/top-stats.jsindex 419ce687d5..55b268e3a4 100644--- a/assets/js/dashboard/stats/graph/top-stats.js+++ b/assets/js/dashboard/stats/graph/top-stats.js@@ -2,7 +2,6 @@ import React from 'react'import { Tooltip } from '../../util/tooltip'import { SecondsSinceLastLoad } from '../../util/seconds-since-last-load'import classNames from 'classnames'-import * as storage from '../../util/storage'import { formatDateRange } from '../../util/date'import { useDashboardStateContext } from '../../dashboard-state-context'import { useSiteContext } from '../../site-context'@@ -23,7 +22,12 @@ function topStatNumberLong(metric, value) {return formatter(value)}-export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {+export default function TopStats({+ data,+ selectedMetric,+ onMetricClick,+ tooltipBoundary+}) {const { dashboardState } = useDashboardStateContext()const lastLoadTimestamp = useLastLoadContext()const site = useSiteContext()@@ -97,17 +101,6 @@ export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {return null}- function canMetricBeGraphed(stat) {- return stat.graphable- }-- function maybeUpdateMetric(stat) {- if (canMetricBeGraphed(stat)) {- storage.setItem(`metric__${site.domain}`, stat.metric)- onMetricUpdate(stat.metric)- }- }-function blinkingDot() {return (<div@@ -118,13 +111,8 @@ export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {)}- function getStoredMetric() {- return storage.getItem(`metric__${site.domain}`)- }-function renderStatName(stat) {- const isSelected =- canMetricBeGraphed(stat) && stat.metric === getStoredMetric()+ const isSelected = stat.graphable && stat.metric === selectedMetricconst [statDisplayName, statExtraName] = stat.name.split(/(\(.+\))/g)@@ -155,7 +143,7 @@ export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {const className = classNames('px-4 md:px-6 w-1/2 my-4 lg:w-auto group select-none',{- 'cursor-pointer': canMetricBeGraphed(stat),+ 'cursor-pointer': stat.graphable,'lg:border-l border-gray-300 dark:border-gray-700': index > 0,'border-r lg:border-r-0': index % 2 === 0}@@ -165,9 +153,7 @@ export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {key={stat.name}info={tooltip(stat, dashboardState)}className={className}- onClick={() => {- maybeUpdateMetric(stat)- }}+ onClick={stat.graphable ? () => onMetricClick(stat.metric) : () => {}}boundary={tooltipBoundary}>{renderStatName(stat)}@@ -224,5 +210,5 @@ export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {stats.push(blinkingDot())}- return stats || null+ return stats ? <>{stats}</> : null}diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.jsdeleted file mode 100644index f88afa7e9a..0000000000--- a/assets/js/dashboard/stats/graph/visitor-graph.js+++ /dev/null@@ -1,244 +0,0 @@-/* eslint-disable react-hooks/exhaustive-deps */-import React, { useState, useEffect, useRef, useCallback } from 'react'-import * as api from '../../api'-import * as storage from '../../util/storage'-import TopStats from './top-stats'-import { fetchTopStats } from './fetch-top-stats'-import { IntervalPicker, getCurrentInterval } from './interval-picker'-import StatsExport from './stats-export'-import WithImportedSwitch from './with-imported-switch'-import { getSamplingNotice, NoticesIcon } from './notices'-import FadeIn from '../../fade-in'-import * as url from '../../util/url'-import LineGraphWithRouter from './line-graph'-import { useDashboardStateContext } from '../../dashboard-state-context'-import { useSiteContext } from '../../site-context'--function fetchMainGraph(site, dashboardState, metric, interval) {- const params = { metric, interval }- return api.get(url.apiPath(site, '/main-graph'), dashboardState, params)-}--export default function VisitorGraph({ updateImportedDataInView }) {- const { dashboardState } = useDashboardStateContext()- const site = useSiteContext()-- const isRealtime = dashboardState.period === 'realtime'-- const topStatsBoundary = useRef(null)-- const [topStatData, setTopStatData] = useState(null)- const [topStatsLoading, setTopStatsLoading] = useState(true)- const [graphData, setGraphData] = useState(null)- const [graphLoading, setGraphLoading] = useState(true)-- // This state is explicitly meant for the situation where either graph interval- // or graph metric is changed. That results in behaviour where Top Stats stay- // intact, but the graph container alone will display a loading spinner for as- // long as new graph data is fetched.- const [graphRefreshing, setGraphRefreshing] = useState(false)-- const onIntervalUpdate = useCallback(- (newInterval) => {- setGraphData(null)- setGraphRefreshing(true)- fetchGraphData(getStoredMetric(), newInterval)- },- [dashboardState]- )-- const onMetricUpdate = useCallback(- (newMetric) => {- setGraphData(null)- setGraphRefreshing(true)- fetchGraphData(newMetric, getCurrentInterval(site, dashboardState))- },- [dashboardState]- )-- useEffect(() => {- setTopStatData(null)- setTopStatsLoading(true)- setGraphData(null)- setGraphLoading(true)- fetchTopStatsAndGraphData()-- if (isRealtime) {- document.addEventListener('tick', fetchTopStatsAndGraphData)- }-- return () => {- document.removeEventListener('tick', fetchTopStatsAndGraphData)- }- }, [dashboardState])-- useEffect(() => {- if (topStatData) {- storeTopStatsContainerHeight()- }- }, [topStatData])-- async function fetchTopStatsAndGraphData() {- const formattedTopStatsResponse = await fetchTopStats(site, dashboardState)- const { topStats, meta } = formattedTopStatsResponse-- let metric = getStoredMetric()-- const availableMetrics = topStats- .filter((stat) => stat.graphable)- .map((stat) => stat.metric)-- if (!availableMetrics.includes(metric)) {- metric = availableMetrics[0]- storage.setItem(`metric__${site.domain}`, metric)- }-- const interval = getCurrentInterval(site, dashboardState)-- if (typeof updateImportedDataInView === 'function') {- updateImportedDataInView(meta.imports_included)- }-- setTopStatData(formattedTopStatsResponse)- setTopStatsLoading(false)-- fetchGraphData(metric, interval)- }-- function fetchGraphData(metric, interval) {- fetchMainGraph(site, dashboardState, metric, interval).then((res) => {- setGraphData(res)- setGraphLoading(false)- setGraphRefreshing(false)- })- }-- function getStoredMetric() {- return storage.getItem(`metric__${site.domain}`)- }-- 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.- function getTopStatsHeight() {- if (topStatData) {- return 'auto'- } else {- return `${storage.getItem(`topStatsHeight__${site.domain}`) || 89}px`- }- }-- function importedSwitchVisible() {- return (- topStatData &&- !['no_imported_data', 'out_of_range'].includes(- topStatData.meta.imports_skip_reason- )- )- }-- function importedSwitchDisabled() {- return (- topStatData &&- topStatData.meta.imports_skip_reason === 'unsupported_query'- )- }-- function importedSwitchTooltip() {- if (!topStatData) {- return- }-- if (importedSwitchDisabled()) {- return 'Imported data cannot be included'- }-- if (topStatData.meta.imports_included) {- return 'Click to exclude imported data'- }-- return 'Click to include imported data'- }-- function getImportedIntervalUnsupportedNotice() {- const unsupportedInterval = ['hour', 'minute'].includes(- getCurrentInterval(site, dashboardState)- )- const showingImported =- importedSwitchVisible() && dashboardState.with_imported === true-- if (showingImported && unsupportedInterval) {- return 'Interval is too short to graph imported data'- }-- return null- }-- return (- <div- className={- 'col-span-full relative w-full bg-white rounded-md shadow dark:bg-gray-900'- }- >- {(topStatsLoading || graphLoading) && renderLoader()}- <FadeIn show={!(topStatsLoading || graphLoading)}>- <div- id="top-stats-container"- className="flex flex-wrap"- ref={topStatsBoundary}- style={{ height: getTopStatsHeight() }}- >- <TopStats- graphableMetrics={topStatData?.graphable_metrics || []}- data={topStatData}- onMetricUpdate={onMetricUpdate}- tooltipBoundary={topStatsBoundary.current}- />- </div>- <div className="relative px-2">- {graphRefreshing && renderLoader()}- <div className="absolute right-4 -top-8 py-1 flex items-center gap-x-4">- <NoticesIcon- notices={[- getImportedIntervalUnsupportedNotice(),- getSamplingNotice(topStatData)- ].filter((n) => !!n)}- />- {!isRealtime && <StatsExport />}- {importedSwitchVisible() && (- <WithImportedSwitch- tooltipMessage={importedSwitchTooltip()}- disabled={importedSwitchDisabled()}- />- )}- <IntervalPicker onIntervalUpdate={onIntervalUpdate} />- </div>- <LineGraphWithRouter- graphData={{- ...graphData,- interval: getCurrentInterval(site, dashboardState)- }}- />- </div>- </FadeIn>- </div>- )-}--function renderLoader() {- return (- <div className="absolute h-full w-full flex items-center justify-center">- <div className="loading">- <div></div>- </div>- </div>- )-}diff --git a/assets/js/dashboard/stats/graph/visitor-graph.tsx b/assets/js/dashboard/stats/graph/visitor-graph.tsxnew file mode 100644index 0000000000..c88d2061e5--- /dev/null+++ b/assets/js/dashboard/stats/graph/visitor-graph.tsx@@ -0,0 +1,360 @@+import React, { useState, useEffect, useRef, useCallback } from 'react'+import * as api from '../../api'+import * as storage from '../../util/storage'+import TopStats from './top-stats'+import { fetchTopStats } from './fetch-top-stats'+import { IntervalPicker, useStoredInterval } from './interval-picker'+import StatsExport from './stats-export'+import WithImportedSwitch from './with-imported-switch'+import { NoticesIcon } from './notices'+import * as url from '../../util/url'+import LineGraphWithRouter, { LineGraphContainer } from './line-graph'+import { useDashboardStateContext } from '../../dashboard-state-context'+import { PlausibleSite, useSiteContext } from '../../site-context'+import { useQuery, useQueryClient } from '@tanstack/react-query'+import { Metric } from '../../../types/query-api'+import { DashboardPeriod } from '../../dashboard-time-periods'+import { DashboardState } from '../../dashboard-state'+import { nowForSite } from '../../util/date'+import { getStaleTime } from '../../hooks/api-client'++// height of at least one row of top stats+const DEFAULT_TOP_STATS_LOADING_HEIGHT_PX = 85++export default function VisitorGraph({+ updateImportedDataInView+}: {+ updateImportedDataInView?: (v: boolean) => void+}) {+ const topStatsBoundary = useRef<HTMLDivElement>(null)+ const site = useSiteContext()+ const { dashboardState } = useDashboardStateContext()+ const isRealtime = dashboardState.period === DashboardPeriod.realtime+ const queryClient = useQueryClient()+ const startOfDay = nowForSite(site).startOf('day')++ const { selectedInterval, onIntervalClick, availableIntervals } =+ useStoredInterval(site, {+ to: dashboardState.to,+ from: dashboardState.from,+ period: dashboardState.period+ })++ const [selectedMetric, setSelectedMetric] = useState<Metric | null>(+ getStoredMetric(site)+ )+ const onMetricClick = useCallback(+ (metric: Metric) => {+ setStoredMetric(site, metric)+ setSelectedMetric(metric)+ },+ [site]+ )++ const topStatsQuery = useQuery({+ queryKey: ['top-stats', { dashboardState }] as const,+ queryFn: async ({ queryKey }) => {+ const [_, opts] = queryKey+ return await fetchTopStats(site, opts.dashboardState)+ },+ placeholderData: (previousData) => previousData,+ staleTime: ({ queryKey, meta }) => {+ const [_, opts] = queryKey+ return getStaleTime(+ meta!.startOfDay as typeof startOfDay,+ opts.dashboardState+ )+ },+ meta: { startOfDay }+ })++ const mainGraphQuery = useQuery({+ enabled: !!selectedMetric,+ queryKey: [+ 'main-graph',+ { dashboardState, metric: selectedMetric, interval: selectedInterval }+ ] as const,+ queryFn: async ({ queryKey }) => {+ const [_, opts] = queryKey+ const data = await api.get(+ url.apiPath(site, '/main-graph'),+ opts.dashboardState,+ {+ metric: opts.metric,+ interval: opts.interval+ }+ )+ return { ...data, interval: opts.interval }+ },+ placeholderData: (previousData) => previousData,+ staleTime: ({ queryKey, meta }) => {+ const [_, opts] = queryKey+ return getStaleTime(+ meta!.startOfDay as typeof startOfDay,+ opts.dashboardState+ )+ },+ meta: { startOfDay }+ })++ // update metric to one that exists+ useEffect(() => {+ if (topStatsQuery.data) {+ const availableMetrics = topStatsQuery.data.topStats+ .filter((stat) => stat.graphable)+ .map((stat) => stat.metric)++ setSelectedMetric((currentlySelectedMetric) => {+ if (+ currentlySelectedMetric &&+ availableMetrics.includes(currentlySelectedMetric)+ ) {+ return currentlySelectedMetric+ } else {+ return availableMetrics[0]+ }+ })+ }+ }, [topStatsQuery.data])++ const [isRealtimeSilentUpdate, setIsRealtimeSilentUpdate] = useState({+ topStats: false,+ mainGraph: false+ })+ useEffect(() => {+ setIsRealtimeSilentUpdate((current) => ({ ...current, mainGraph: false }))+ }, [selectedMetric])++ useEffect(() => {+ if (!mainGraphQuery.isRefetching) {+ setIsRealtimeSilentUpdate((current) => ({ ...current, mainGraph: false }))+ }+ }, [mainGraphQuery.isRefetching])++ useEffect(() => {+ if (!topStatsQuery.isRefetching) {+ setIsRealtimeSilentUpdate((current) => ({ ...current, topStats: false }))+ }+ }, [topStatsQuery.isRefetching])++ useEffect(() => {+ if (!isRealtime) {+ setIsRealtimeSilentUpdate({+ topStats: false,+ mainGraph: false+ })+ }+ }, [isRealtime])++ // sync import related info+ useEffect(() => {+ if (topStatsQuery.data && typeof updateImportedDataInView === 'function') {+ updateImportedDataInView(+ topStatsQuery.data.meta.imports_included as boolean+ )+ }+ }, [topStatsQuery.data, updateImportedDataInView])++ // fetch realtime stats+ const refetchTopStats = topStatsQuery.refetch+ const refetchMainGraph = mainGraphQuery.refetch++ useEffect(() => {+ const onTick = () => {+ setIsRealtimeSilentUpdate({ topStats: true, mainGraph: true })+ queryClient.invalidateQueries({+ predicate: ({ queryKey }) => {+ const realtimeTopStatsOrMainGraphQuery =+ ['top-stats', 'main-graph'].includes(queryKey[0] as string) &&+ typeof queryKey[1] === 'object' &&+ (queryKey[1] as { dashboardState?: DashboardState })?.dashboardState+ ?.period === DashboardPeriod.realtime++ return realtimeTopStatsOrMainGraphQuery+ }+ })+ refetchTopStats()+ refetchMainGraph()+ }++ if (isRealtime) {+ document.addEventListener('tick', onTick)+ }++ return () => {+ document.removeEventListener('tick', onTick)+ }+ }, [queryClient, isRealtime, refetchTopStats, refetchMainGraph])++ const importedSwitchVisible = !['no_imported_data', 'out_of_range'].includes(+ topStatsQuery.data?.meta.imports_skip_reason as string+ )++ const importedIntervalUnsupportedNotice =+ ['hour', 'minute'].includes(selectedInterval) &&+ importedSwitchVisible &&+ dashboardState.with_imported+ ? 'Interval is too short to graph imported data'+ : null++ const { heightPx } = useGuessTopStatsHeight(site, topStatsBoundary)++ const showFullLoader =+ topStatsQuery.isFetching &&+ topStatsQuery.isStale &&+ !isRealtimeSilentUpdate.topStats++ const showGraphLoader =+ mainGraphQuery.isFetching &&+ mainGraphQuery.isStale &&+ !isRealtimeSilentUpdate.mainGraph &&+ !showFullLoader++ return (+ <div className="col-span-full relative w-full bg-white rounded-md shadow dark:bg-gray-900">+ <>+ <div+ id="top-stats-container"+ className="flex flex-wrap relative"+ ref={topStatsBoundary}+ >+ {topStatsQuery.data ? (+ <TopStats+ data={topStatsQuery.data}+ selectedMetric={selectedMetric}+ onMetricClick={onMetricClick}+ tooltipBoundary={topStatsBoundary.current}+ />+ ) : (+ // prevent the top stats area from jumping on initial load+ <div+ style={{+ height: `${heightPx}px`+ }}+ ></div>+ )}+ </div>+ <div className="relative px-2">+ {topStatsQuery.data && (+ <div className="absolute right-4 -top-8 py-1 flex items-center gap-x-4">+ <NoticesIcon+ notices={+ [importedIntervalUnsupportedNotice].filter(+ (n) => !!n+ ) as string[]+ }+ />+ {!isRealtime && (+ <StatsExport selectedInterval={selectedInterval} />+ )}+ {importedSwitchVisible && (+ <WithImportedSwitch+ tooltipMessage={+ topStatsQuery.data.meta.imports_skip_reason ===+ 'unsupported_query'+ ? 'Imported data cannot be included'+ : topStatsQuery.data.meta.imports_included+ ? 'Click to exclude imported data'+ : 'Click to include imported data'+ }+ disabled={+ topStatsQuery.data.meta.imports_skip_reason ===+ 'unsupported_query'+ }+ />+ )}+ <IntervalPicker+ selectedInterval={selectedInterval}+ onIntervalClick={onIntervalClick}+ options={availableIntervals}+ />+ </div>+ )}+ <LineGraphContainer>+ {mainGraphQuery.data && (+ <>+ {!showGraphLoader && (+ <LineGraphWithRouter+ graphData={{+ ...mainGraphQuery.data+ }}+ />+ )}+ {showGraphLoader && <Loader />}+ </>+ )}+ </LineGraphContainer>+ </div>+ </>+ {(!(topStatsQuery.data && mainGraphQuery.data) || showFullLoader) && (+ <Loader />+ )}+ </div>+ )+}++const Loader = () => {+ return (+ <div className="absolute inset-0 bg-white rounded-md dark:bg-gray-900 flex items-center justify-center">+ <div className="loading">+ <div></div>+ </div>+ </div>+ )+}++function getStoredMetricKey(site: Pick<PlausibleSite, 'domain'>) {+ return storage.getDomainScopedStorageKey('metric', site.domain)+}++function getStoredMetric(site: Pick<PlausibleSite, 'domain'>) {+ return storage.getItem(getStoredMetricKey(site)) as Metric | null+}++function setStoredMetric(site: Pick<PlausibleSite, 'domain'>, metric: Metric) {+ storage.setItem(getStoredMetricKey(site), metric)+}++function getStoredTopStatsHeightKey(site: Pick<PlausibleSite, 'domain'>) {+ return storage.getDomainScopedStorageKey('topStatsHeight', site.domain)+}++function getStoredTopStatsHeight(site: Pick<PlausibleSite, 'domain'>) {+ return storage.getItem(getStoredTopStatsHeightKey(site)) as string+}++function setStoredTopStatsHeight(+ site: Pick<PlausibleSite, 'domain'>,+ heightPx: string+) {+ storage.setItem(getStoredTopStatsHeightKey(site), heightPx)+}++function useGuessTopStatsHeight(+ site: Pick<PlausibleSite, 'domain'>,+ topStatsBoundary: React.RefObject<HTMLDivElement>+): { heightPx: string } {+ useEffect(() => {+ const resizeObserver = new ResizeObserver(() => {+ if (topStatsBoundary.current) {+ setStoredTopStatsHeight(+ site,+ `${Math.max(topStatsBoundary.current.clientHeight, DEFAULT_TOP_STATS_LOADING_HEIGHT_PX)}`+ )+ }+ })++ if (topStatsBoundary.current) {+ resizeObserver.observe(topStatsBoundary.current)+ }++ return () => {+ resizeObserver.disconnect()+ }+ }, [site, topStatsBoundary])++ return {+ heightPx:+ getStoredTopStatsHeight(site) ?? DEFAULT_TOP_STATS_LOADING_HEIGHT_PX+ }+}diff --git a/assets/js/dashboard/util/realtime-update-timer.js b/assets/js/dashboard/util/realtime-update-timer.jsindex 5650192dc5..07d773883b 100644--- a/assets/js/dashboard/util/realtime-update-timer.js+++ b/assets/js/dashboard/util/realtime-update-timer.js@@ -1,8 +1,8 @@-const THIRTY_SECONDS = 30000+export const REALTIME_UPDATE_TIME_MS = 30_000const tickEvent = new Event('tick')export function start() {setInterval(() => {document.dispatchEvent(tickEvent)- }, THIRTY_SECONDS)+ }, REALTIME_UPDATE_TIME_MS)}diff --git a/assets/package-lock.json b/assets/package-lock.jsonindex 8df87abc1e..b32a3b16d4 100644--- a/assets/package-lock.json+++ b/assets/package-lock.json@@ -24,6 +24,7 @@"classnames": "^2.3.1","d3": "^7.9.0","dayjs": "^1.11.7",+ "fast-deep-equal": "^3.1.3","iframe-resizer": "^4.3.2","react": "^18.3.1","react-dom": "^18.3.1",@@ -5367,7 +5368,8 @@},"node_modules/fast-deep-equal": {"version": "3.1.3",- "dev": true,+ "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",+ "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==","license": "MIT"},"node_modules/fast-glob": {diff --git a/assets/package.json b/assets/package.jsonindex b48778a56a..559481596e 100644--- a/assets/package.json+++ b/assets/package.json@@ -28,6 +28,7 @@"classnames": "^2.3.1","d3": "^7.9.0","dayjs": "^1.11.7",+ "fast-deep-equal": "^3.1.3","iframe-resizer": "^4.3.2","react": "^18.3.1","react-dom": "^18.3.1",