mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Fixed realtime chart for relational. Removed getDateArray. Added chart min/max dates.
This commit is contained in:
parent
9311f0a183
commit
647dcb5f25
@ -1,6 +1,5 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import PageviewsChart from 'components/metrics/PageviewsChart';
|
import PageviewsChart from 'components/metrics/PageviewsChart';
|
||||||
import { getDateArray } from 'lib/date';
|
|
||||||
import useWebsitePageviews from 'components/hooks/queries/useWebsitePageviews';
|
import useWebsitePageviews from 'components/hooks/queries/useWebsitePageviews';
|
||||||
import { useDateRange } from 'components/hooks';
|
import { useDateRange } from 'components/hooks';
|
||||||
|
|
||||||
@ -19,8 +18,8 @@ export function WebsiteChart({
|
|||||||
const chartData = useMemo(() => {
|
const chartData = useMemo(() => {
|
||||||
if (data) {
|
if (data) {
|
||||||
const result = {
|
const result = {
|
||||||
pageviews: getDateArray(pageviews, startDate, endDate, unit),
|
pageviews,
|
||||||
sessions: getDateArray(sessions, startDate, endDate, unit),
|
sessions,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (compare) {
|
if (compare) {
|
||||||
@ -43,7 +42,15 @@ export function WebsiteChart({
|
|||||||
return { pageviews: [], sessions: [] };
|
return { pageviews: [], sessions: [] };
|
||||||
}, [data, startDate, endDate, unit]);
|
}, [data, startDate, endDate, unit]);
|
||||||
|
|
||||||
return <PageviewsChart data={chartData} unit={unit} isLoading={isLoading} />;
|
return (
|
||||||
|
<PageviewsChart
|
||||||
|
data={chartData}
|
||||||
|
minDate={startDate.toISOString()}
|
||||||
|
maxDate={endDate.toISOString()}
|
||||||
|
unit={unit}
|
||||||
|
isLoading={isLoading}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default WebsiteChart;
|
export default WebsiteChart;
|
||||||
|
@ -12,6 +12,8 @@ export interface BarChartProps extends ChartProps {
|
|||||||
renderYLabel?: (label: string, index: number, values: any[]) => string;
|
renderYLabel?: (label: string, index: number, values: any[]) => string;
|
||||||
XAxisType?: string;
|
XAxisType?: string;
|
||||||
YAxisType?: string;
|
YAxisType?: string;
|
||||||
|
minDate?: number | string;
|
||||||
|
maxDate?: number | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function BarChart(props: BarChartProps) {
|
export function BarChart(props: BarChartProps) {
|
||||||
@ -24,6 +26,8 @@ export function BarChart(props: BarChartProps) {
|
|||||||
XAxisType = 'time',
|
XAxisType = 'time',
|
||||||
YAxisType = 'linear',
|
YAxisType = 'linear',
|
||||||
stacked = false,
|
stacked = false,
|
||||||
|
minDate,
|
||||||
|
maxDate,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const options: any = useMemo(() => {
|
const options: any = useMemo(() => {
|
||||||
@ -32,6 +36,8 @@ export function BarChart(props: BarChartProps) {
|
|||||||
x: {
|
x: {
|
||||||
type: XAxisType,
|
type: XAxisType,
|
||||||
stacked: true,
|
stacked: true,
|
||||||
|
min: minDate,
|
||||||
|
max: maxDate,
|
||||||
time: {
|
time: {
|
||||||
unit,
|
unit,
|
||||||
},
|
},
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { colord } from 'colord';
|
import { colord } from 'colord';
|
||||||
import BarChart from 'components/charts/BarChart';
|
import BarChart from 'components/charts/BarChart';
|
||||||
import { getDateArray } from 'lib/date';
|
|
||||||
import { useLocale, useDateRange, useWebsiteEventsSeries } from 'components/hooks';
|
import { useLocale, useDateRange, useWebsiteEventsSeries } from 'components/hooks';
|
||||||
import { CHART_COLORS } from 'lib/constants';
|
import { CHART_COLORS } from 'lib/constants';
|
||||||
import { renderDateLabels } from 'lib/charts';
|
import { renderDateLabels } from 'lib/charts';
|
||||||
@ -31,10 +30,6 @@ export function EventsChart({ websiteId, className }: EventsChartProps) {
|
|||||||
return obj;
|
return obj;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
Object.keys(map).forEach(key => {
|
|
||||||
map[key] = getDateArray(map[key], startDate, endDate, unit);
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
datasets: Object.keys(map).map((key, index) => {
|
datasets: Object.keys(map).map((key, index) => {
|
||||||
const color = colord(CHART_COLORS[index % CHART_COLORS.length]);
|
const color = colord(CHART_COLORS[index % CHART_COLORS.length]);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { useMemo, useRef } from 'react';
|
import { useMemo, useRef } from 'react';
|
||||||
import { startOfMinute, subMinutes, isBefore } from 'date-fns';
|
import { startOfMinute, subMinutes, isBefore } from 'date-fns';
|
||||||
import PageviewsChart from './PageviewsChart';
|
import PageviewsChart from './PageviewsChart';
|
||||||
import { getDateArray } from 'lib/date';
|
|
||||||
import { DEFAULT_ANIMATION_DURATION, REALTIME_RANGE } from 'lib/constants';
|
import { DEFAULT_ANIMATION_DURATION, REALTIME_RANGE } from 'lib/constants';
|
||||||
import { RealtimeData } from 'lib/types';
|
import { RealtimeData } from 'lib/types';
|
||||||
|
|
||||||
@ -22,8 +21,8 @@ export function RealtimeChart({ data, unit, ...props }: RealtimeChartProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pageviews: getDateArray(data.series.views, startDate, endDate, unit),
|
pageviews: data.series.views,
|
||||||
sessions: getDateArray(data.series.visitors, startDate, endDate, unit),
|
sessions: data.series.visitors,
|
||||||
};
|
};
|
||||||
}, [data, startDate, endDate, unit]);
|
}, [data, startDate, endDate, unit]);
|
||||||
|
|
||||||
@ -37,7 +36,14 @@ export function RealtimeChart({ data, unit, ...props }: RealtimeChartProps) {
|
|||||||
}, [endDate]);
|
}, [endDate]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageviewsChart {...props} unit={unit} data={chartData} animationDuration={animationDuration} />
|
<PageviewsChart
|
||||||
|
{...props}
|
||||||
|
minDate={startDate.toISOString()}
|
||||||
|
maxDate={endDate.toISOString()}
|
||||||
|
unit={unit}
|
||||||
|
data={chartData}
|
||||||
|
animationDuration={animationDuration}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,19 +273,6 @@ export function getMinimumUnit(startDate: number | Date, endDate: number | Date)
|
|||||||
return 'year';
|
return 'year';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDateFromString(str: string) {
|
|
||||||
const [ymd, hms] = str.split(' ');
|
|
||||||
const [year, month, day] = ymd.split('-');
|
|
||||||
|
|
||||||
if (hms) {
|
|
||||||
const [hour, min, sec] = hms.split(':');
|
|
||||||
|
|
||||||
return new Date(+year, +month - 1, +day, +hour, +min, +sec);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Date(+year, +month - 1, +day);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) {
|
export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) {
|
||||||
const arr = [];
|
const arr = [];
|
||||||
const { diff, add, start } = DATE_FUNCTIONS[unit];
|
const { diff, add, start } = DATE_FUNCTIONS[unit];
|
||||||
|
@ -12,19 +12,11 @@ import { filtersToArray } from './params';
|
|||||||
const log = debug('umami:prisma');
|
const log = debug('umami:prisma');
|
||||||
|
|
||||||
const MYSQL_DATE_FORMATS = {
|
const MYSQL_DATE_FORMATS = {
|
||||||
minute: '%Y-%m-%d %H:%i:00',
|
minute: '%Y-%m-%dT%H:%i:00Z',
|
||||||
hour: '%Y-%m-%d %H:00:00',
|
hour: '%Y-%m-%dT%H:00:00Z',
|
||||||
day: '%Y-%m-%d',
|
day: '%Y-%m-%dT00:00:00Z',
|
||||||
month: '%Y-%m-01',
|
month: '%Y-%m-01T00:00:00Z',
|
||||||
year: '%Y-01-01',
|
year: '%Y-01-01T00:00:00Z',
|
||||||
};
|
|
||||||
|
|
||||||
const POSTGRESQL_DATE_FORMATS = {
|
|
||||||
minute: 'YYYY-MM-DD HH24:MI:00',
|
|
||||||
hour: 'YYYY-MM-DD HH24:00:00',
|
|
||||||
day: 'YYYY-MM-DD',
|
|
||||||
month: 'YYYY-MM-01',
|
|
||||||
year: 'YYYY-01-01',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function getAddIntervalQuery(field: string, interval: string): string {
|
function getAddIntervalQuery(field: string, interval: string): string {
|
||||||
@ -68,9 +60,9 @@ function getDateSQL(field: string, unit: string, timezone?: string): string {
|
|||||||
|
|
||||||
if (db === POSTGRESQL) {
|
if (db === POSTGRESQL) {
|
||||||
if (timezone) {
|
if (timezone) {
|
||||||
return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
return `date_trunc('${unit}', ${field} at time zone '${timezone}')`;
|
||||||
}
|
}
|
||||||
return `to_char(date_trunc('${unit}', ${field}), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
return `date_trunc('${unit}', ${field})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (db === MYSQL) {
|
if (db === MYSQL) {
|
||||||
|
@ -22,14 +22,21 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
|||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
select
|
select
|
||||||
${getDateSQL('website_event.created_at', unit, timezone)} x,
|
${getDateSQL('g.created_at', unit, timezone)} as x,
|
||||||
count(distinct website_event.session_id) y
|
count(distinct g.session_id) as y
|
||||||
from website_event
|
from (
|
||||||
|
select
|
||||||
|
website_event.session_id,
|
||||||
|
min(website_event.created_at) as created_at,
|
||||||
|
count(*) y
|
||||||
|
from website_event
|
||||||
${joinSession}
|
${joinSession}
|
||||||
where website_event.website_id = {{websiteId::uuid}}
|
where website_event.website_id = {{websiteId::uuid}}
|
||||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||||
and event_type = {{eventType}}
|
and event_type = {{eventType}}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
|
group by website_event.session_id, website_event.created_at
|
||||||
|
) as g
|
||||||
group by 1
|
group by 1
|
||||||
`,
|
`,
|
||||||
params,
|
params,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user