Merge pull request #476 from gnarlex/event-type-selection

Dashboard: Dropdown for filtering events by their type
This commit is contained in:
Mike Cao 2021-02-16 19:05:19 -08:00 committed by GitHub
commit 3a0195ed50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 138 additions and 9 deletions

View File

@ -1,18 +1,66 @@
import React from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import MetricsTable from './MetricsTable'; import MetricsTable from './MetricsTable';
import Tag from 'components/common/Tag'; import Tag from 'components/common/Tag';
import DropDown from 'components/common/DropDown';
import { eventTypeFilter } from 'lib/filters';
import useDateRange from 'hooks/useDateRange';
import useFetch from 'hooks/useFetch';
import usePageQuery from 'hooks/usePageQuery';
import useShareToken from 'hooks/useShareToken';
import { TOKEN_HEADER } from 'lib/constants';
import styles from './EventsTable.module.css';
const EVENT_FILTER_DEFAULT = {
value: 'EVENT_FILTER_DEFAULT',
label: <FormattedMessage id="label.all-events" defaultMessage="All events" />,
};
export default function EventsTable({ websiteId, ...props }) { export default function EventsTable({ websiteId, ...props }) {
const [eventType, setEventType] = useState(EVENT_FILTER_DEFAULT.value);
const {
query: { url },
} = usePageQuery();
const shareToken = useShareToken();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate, modified } = dateRange;
const { data, loading, error } = useFetch(
`/api/website/${websiteId}/event-types`,
{
params: {
start_at: +startDate,
end_at: +endDate,
url,
},
headers: { [TOKEN_HEADER]: shareToken?.token },
},
[modified],
);
const eventTypes = data ? [...new Set(data.map(({ x }) => x))] : [];
const dropDownOptions = [EVENT_FILTER_DEFAULT, ...eventTypes.map(t => ({ value: t, label: t }))];
return ( return (
<>
{!loading && !error && eventTypes.length > 1 && (
<div className={styles.filter}>
<DropDown value={eventType} options={dropDownOptions} onChange={setEventType} />
</div>
)}
<MetricsTable <MetricsTable
{...props} {...props}
title={<FormattedMessage id="metrics.events" defaultMessage="Events" />} title={<FormattedMessage id="metrics.events" defaultMessage="Events" />}
type="event" type="event"
metric={<FormattedMessage id="metrics.actions" defaultMessage="Actions" />} metric={<FormattedMessage id="metrics.actions" defaultMessage="Actions" />}
websiteId={websiteId} websiteId={websiteId}
dataFilter={eventTypeFilter}
filterOptions={eventType === EVENT_FILTER_DEFAULT.value ? [] : [eventType]}
renderLabel={({ x }) => <Label value={x} />} renderLabel={({ x }) => <Label value={x} />}
/> />
</>
); );
} }

View File

@ -0,0 +1,6 @@
.filter {
display: flex;
justify-content: flex-start;
align-items: center;
margin-bottom: 15px;
}

View File

@ -5,6 +5,7 @@
"label.administrator": "Administrator", "label.administrator": "Administrator",
"label.all": "Alle", "label.all": "Alle",
"label.all-websites": "Alle Webseiten", "label.all-websites": "Alle Webseiten",
"label.all-events": "Alle Ereignisse",
"label.back": "Zurück", "label.back": "Zurück",
"label.cancel": "Abbrechen", "label.cancel": "Abbrechen",
"label.change-password": "Passwort ändern", "label.change-password": "Passwort ändern",

View File

@ -5,6 +5,7 @@
"label.administrator": "Administrator", "label.administrator": "Administrator",
"label.all": "All", "label.all": "All",
"label.all-websites": "All websites", "label.all-websites": "All websites",
"label.all-events": "All events",
"label.back": "Back", "label.back": "Back",
"label.cancel": "Cancel", "label.cancel": "Cancel",
"label.change-password": "Change password", "label.change-password": "Change password",

View File

@ -5,6 +5,7 @@
"label.administrator": "Administrador", "label.administrator": "Administrador",
"label.all": "Todos", "label.all": "Todos",
"label.all-websites": "Todos los sitios", "label.all-websites": "Todos los sitios",
"label.all-events": "Todos los eventos",
"label.back": "Atrás", "label.back": "Atrás",
"label.cancel": "Cancelar", "label.cancel": "Cancelar",
"label.change-password": "Cambiar contraseña", "label.change-password": "Cambiar contraseña",

View File

@ -5,6 +5,7 @@
"label.administrator": "Administrateur", "label.administrator": "Administrateur",
"label.all": "Tout", "label.all": "Tout",
"label.all-websites": "Tous les sites web", "label.all-websites": "Tous les sites web",
"label.all-events": "Tous les événements",
"label.back": "Retour", "label.back": "Retour",
"label.cancel": "Annuler", "label.cancel": "Annuler",
"label.change-password": "Changer le mot de passe", "label.change-password": "Changer le mot de passe",

View File

@ -113,6 +113,17 @@ export const refFilter = (data, { domain, domainOnly, raw }) => {
return Object.keys(map).map(key => ({ x: key, y: map[key], w: links[key] })); return Object.keys(map).map(key => ({ x: key, y: map[key], w: links[key] }));
}; };
export const eventTypeFilter = (data, types) => {
if (!types || types.length === 0) {
return data;
}
return data.filter(({ x }) => {
const [event] = x.split('\t');
return types.some(type => type === event);
});
};
export const browserFilter = data => export const browserFilter = data =>
data.map(({ x, y }) => ({ x: BROWSERS[x] || x, y })).filter(({ x }) => x); data.map(({ x, y }) => ({ x: BROWSERS[x] || x, y })).filter(({ x }) => x);

View File

@ -501,6 +501,41 @@ export function getEventMetrics(
); );
} }
export function getEventTypes(
website_id,
start_at,
end_at,
timezone = 'utc',
unit = 'day',
filters = {},
) {
const params = [website_id, start_at, end_at];
const { url } = filters;
let urlFilter = '';
if (url) {
urlFilter = `and url=$${params.length + 1}`;
params.push(decodeURIComponent(url));
}
return rawQuery(
`
select
event_type x,
${getDateQuery('created_at', unit, timezone)} t,
count(*) y
from event
where website_id=$1
and created_at between $2 and $3
${urlFilter}
group by 1, 2
order by 2
`,
params,
);
}
export async function getRealtimeData(websites, time) { export async function getRealtimeData(websites, time) {
const [pageviews, sessions, events] = await Promise.all([ const [pageviews, sessions, events] = await Promise.all([
getPageviews(websites, time), getPageviews(websites, time),

View File

@ -0,0 +1,25 @@
import { getEventTypes } from 'lib/queries';
import { ok, methodNotAllowed, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
export default async (req, res) => {
if (req.method === 'GET') {
if (!(await allowQuery(req))) {
return unauthorized(res);
}
const { id, start_at, end_at, url } = req.query;
const websiteId = +id;
const startDate = new Date(+start_at);
const endDate = new Date(+end_at);
const eventTypes = await getEventTypes(websiteId, startDate, endDate, undefined, undefined, {
url,
});
return ok(res, eventTypes);
}
return methodNotAllowed(res);
};