mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-18 15:23:38 +01:00
Updated polling logic.
This commit is contained in:
parent
fdc92d087b
commit
9737127bb1
@ -6,7 +6,15 @@ import BarChart from './BarChart';
|
|||||||
import useTheme from 'hooks/useTheme';
|
import useTheme from 'hooks/useTheme';
|
||||||
import { THEME_COLORS } from 'lib/constants';
|
import { THEME_COLORS } from 'lib/constants';
|
||||||
|
|
||||||
export default function PageviewsChart({ websiteId, data, unit, records, className, loading }) {
|
export default function PageviewsChart({
|
||||||
|
websiteId,
|
||||||
|
data,
|
||||||
|
unit,
|
||||||
|
records,
|
||||||
|
className,
|
||||||
|
loading,
|
||||||
|
animationDuration = 300,
|
||||||
|
}) {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const [theme] = useTheme();
|
const [theme] = useTheme();
|
||||||
const primaryColor = tinycolor(THEME_COLORS[theme].primary);
|
const primaryColor = tinycolor(THEME_COLORS[theme].primary);
|
||||||
@ -76,7 +84,7 @@ export default function PageviewsChart({ websiteId, data, unit, records, classNa
|
|||||||
]}
|
]}
|
||||||
unit={unit}
|
unit={unit}
|
||||||
records={records}
|
records={records}
|
||||||
animationDuration={visible ? 300 : 0}
|
animationDuration={visible ? animationDuration : 0}
|
||||||
onUpdate={handleUpdate}
|
onUpdate={handleUpdate}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
/>
|
/>
|
||||||
|
@ -35,11 +35,16 @@ function mapData(data) {
|
|||||||
export default function RealtimeDashboard() {
|
export default function RealtimeDashboard() {
|
||||||
const [data, setData] = useState();
|
const [data, setData] = useState();
|
||||||
const [website, setWebsite] = useState();
|
const [website, setWebsite] = useState();
|
||||||
|
const [lastTime, setLastTime] = useState();
|
||||||
const { data: init, loading } = useFetch('/api/realtime', { type: 'init' });
|
const { data: init, loading } = useFetch('/api/realtime', { type: 'init' });
|
||||||
const { data: updates } = useFetch(
|
const { data: updates } = useFetch(
|
||||||
'/api/realtime',
|
'/api/realtime',
|
||||||
{ type: 'update' },
|
{ type: 'update', start_at: lastTime },
|
||||||
{ disabled: !init?.token, interval: 5000, headers: { 'x-umami-token': init?.token } },
|
{
|
||||||
|
disabled: !init?.token,
|
||||||
|
interval: 5000,
|
||||||
|
headers: { 'x-umami-token': init?.token },
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const chartData = useMemo(() => {
|
const chartData = useMemo(() => {
|
||||||
@ -48,8 +53,6 @@ export default function RealtimeDashboard() {
|
|||||||
const startDate = subMinutes(endDate, 30);
|
const startDate = subMinutes(endDate, 30);
|
||||||
const unit = 'minute';
|
const unit = 'minute';
|
||||||
|
|
||||||
console.log({ data });
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pageviews: getDateArray(mapData(data.pageviews), startDate, endDate, unit),
|
pageviews: getDateArray(mapData(data.pageviews), startDate, endDate, unit),
|
||||||
sessions: getDateArray(mapData(data.sessions), startDate, endDate, unit),
|
sessions: getDateArray(mapData(data.sessions), startDate, endDate, unit),
|
||||||
@ -70,6 +73,7 @@ export default function RealtimeDashboard() {
|
|||||||
events: filterTime(state.events, time).concat(events),
|
events: filterTime(state.events, time).concat(events),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
setLastTime(Date.now());
|
||||||
}, [updates, init]);
|
}, [updates, init]);
|
||||||
|
|
||||||
if (!init || loading || !data) {
|
if (!init || loading || !data) {
|
||||||
|
@ -11,10 +11,7 @@ export default function useFetch(url, params = {}, options = {}) {
|
|||||||
const [error, setError] = useState();
|
const [error, setError] = useState();
|
||||||
const [loading, setLoadiing] = useState(false);
|
const [loading, setLoadiing] = useState(false);
|
||||||
const { basePath } = useRouter();
|
const { basePath } = useRouter();
|
||||||
const keys = Object.keys(params)
|
const { update = [], onDataLoad = () => {}, disabled, headers, interval, delay = 0 } = options;
|
||||||
.sort()
|
|
||||||
.map(key => params[key]);
|
|
||||||
const { update = [], onDataLoad = () => {}, disabled, headers } = options;
|
|
||||||
|
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
try {
|
try {
|
||||||
@ -43,10 +40,11 @@ export default function useFetch(url, params = {}, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
console.log('effect', params);
|
||||||
if (url && !disabled) {
|
if (url && !disabled) {
|
||||||
const { interval, delay = 0 } = options;
|
if (!data) {
|
||||||
|
setTimeout(() => loadData(), delay);
|
||||||
setTimeout(() => loadData(), delay);
|
}
|
||||||
|
|
||||||
const id = interval ? setInterval(() => loadData(), interval) : null;
|
const id = interval ? setInterval(() => loadData(), interval) : null;
|
||||||
|
|
||||||
@ -54,7 +52,7 @@ export default function useFetch(url, params = {}, options = {}) {
|
|||||||
clearInterval(id);
|
clearInterval(id);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [url, disabled, ...keys, ...update]);
|
}, [data, url, disabled, ...update]);
|
||||||
|
|
||||||
return { data, status, error, loading };
|
return { data, status, error, loading };
|
||||||
}
|
}
|
||||||
|
@ -28,3 +28,7 @@ export function getQueryString(params = {}) {
|
|||||||
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function makeUrl(url, params) {
|
||||||
|
return `${url}${getQueryString(params)}`;
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { getQueryString } from './url';
|
import { makeUrl } from './url';
|
||||||
|
|
||||||
export const apiRequest = (method, url, body, headers) =>
|
export const apiRequest = (method, url, body, headers) =>
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
@ -20,10 +20,10 @@ export const apiRequest = (method, url, body, headers) =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const get = (url, params, headers) =>
|
export const get = (url, params, headers) =>
|
||||||
apiRequest('get', `${url}${getQueryString(params)}`, undefined, headers);
|
apiRequest('get', makeUrl(url, params), undefined, headers);
|
||||||
|
|
||||||
export const del = (url, params, headers) =>
|
export const del = (url, params, headers) =>
|
||||||
apiRequest('delete', `${url}${getQueryString(params)}`, undefined, headers);
|
apiRequest('delete', makeUrl(url, params), undefined, headers);
|
||||||
|
|
||||||
export const post = (url, params, headers) =>
|
export const post = (url, params, headers) =>
|
||||||
apiRequest('post', url, JSON.stringify(params), headers);
|
apiRequest('post', url, JSON.stringify(params), headers);
|
||||||
|
@ -16,7 +16,7 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
const { type } = req.query;
|
const { type, start_at } = req.query;
|
||||||
const { user_id } = req.auth;
|
const { user_id } = req.auth;
|
||||||
|
|
||||||
if (type === 'init') {
|
if (type === 'init') {
|
||||||
@ -37,7 +37,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
const { websites } = await parseToken(token);
|
const { websites } = await parseToken(token);
|
||||||
|
|
||||||
const [pageviews, sessions, events] = await getData(websites, new Date());
|
const [pageviews, sessions, events] = await getData(websites, new Date(+start_at));
|
||||||
|
|
||||||
return ok(res, { pageviews, sessions, events });
|
return ok(res, { pageviews, sessions, events });
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user