Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
briancao committed Aug 17, 2023
2 parents 58527c3 + 2c8996b commit 9a7fb1f
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 102 deletions.
65 changes: 40 additions & 25 deletions components/input/MonthSelect.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,64 @@
import { useRef, useState } from 'react';
import { Text, Icon, CalendarMonthSelect, CalendarYearSelect, Button } from 'react-basics';
import { useRef } from 'react';
import {
Text,
Icon,
CalendarMonthSelect,
CalendarYearSelect,
Button,
PopupTrigger,
Popup,
} from 'react-basics';
import { startOfMonth, endOfMonth } from 'date-fns';
import Icons from 'components/icons';
import { useLocale } from 'hooks';
import { formatDate } from 'lib/date';
import { getDateLocale } from 'lib/lang';
import styles from './MonthSelect.module.css';

const MONTH = 'month';
const YEAR = 'year';

export function MonthSelect({ date = new Date(), onChange }) {
const { locale } = useLocale();
const [select, setSelect] = useState(null);
const month = formatDate(date, 'MMMM', locale);
const year = date.getFullYear();
const ref = useRef();

const handleSelect = value => {
setSelect(state => (state !== value ? value : null));
};

const handleChange = date => {
onChange(`range:${startOfMonth(date).getTime()}:${endOfMonth(date).getTime()}`);
setSelect(null);
};

return (
<>
<div ref={ref} className={styles.container}>
<Button className={styles.input} variant="quiet" onClick={() => handleSelect(MONTH)}>
<Text>{month}</Text>
<Icon size="sm">{select === MONTH ? <Icons.Close /> : <Icons.ChevronDown />}</Icon>
</Button>
<Button className={styles.input} variant="quiet" onClick={() => handleSelect(YEAR)}>
<Text>{year}</Text>
<Icon size="sm">{select === YEAR ? <Icons.Close /> : <Icons.ChevronDown />}</Icon>
</Button>
<PopupTrigger>
<Button className={styles.input} variant="quiet">
<Text>{month}</Text>
<Icon size="sm">
<Icons.ChevronDown />
</Icon>
</Button>
<Popup className={styles.popup} alignment="start">
<CalendarMonthSelect
date={date}
locale={getDateLocale(locale)}
onSelect={handleChange}
/>
</Popup>
</PopupTrigger>
<PopupTrigger>
<Button className={styles.input} variant="quiet">
<Text>{year}</Text>
<Icon size="sm">
<Icons.ChevronDown />
</Icon>
</Button>
<Popup className={styles.popup} alignment="start">
<CalendarYearSelect
date={date}
locale={getDateLocale(locale)}
onSelect={handleChange}
/>
</Popup>
</PopupTrigger>
</div>
{select === MONTH && (
<CalendarMonthSelect date={date} locale={getDateLocale(locale)} onSelect={handleChange} />
)}
{select === YEAR && (
<CalendarYearSelect date={date} locale={getDateLocale(locale)} onSelect={handleChange} />
)}
</>
);
}
Expand Down
10 changes: 10 additions & 0 deletions components/input/MonthSelect.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
display: flex;
align-items: center;
justify-content: center;
border: 1px solid var(--base400);
border-radius: var(--border-radius);
}

.input {
Expand All @@ -10,3 +12,11 @@
gap: 10px;
cursor: pointer;
}

.popup {
border: 1px solid var(--base400);
background: var(--base50);
border-radius: var(--border-radius);
padding: 20px;
margin-top: 5px;
}
1 change: 1 addition & 0 deletions components/layout/AppLayout.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
width: 100vw;
grid-column: 1;
grid-row: 1 / 2;
z-index: 1;
}

.body {
Expand Down
26 changes: 4 additions & 22 deletions components/pages/reports/PopupForm.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { createPortal } from 'react-dom';
import { useDocumentClick, useKeyDown } from 'react-basics';
import classNames from 'classnames';
import styles from './PopupForm.module.css';

export function PopupForm({ element, className, children, onClose }) {
const { right, top } = element.getBoundingClientRect();
const style = { position: 'absolute', left: right, top };

useKeyDown('Escape', onClose);

useDocumentClick(e => {
if (e.target !== element && !element?.parentElement?.contains(e.target)) {
onClose();
}
});

const handleClick = e => {
e.stopPropagation();
};

return createPortal(
<div className={classNames(styles.form, className)} style={style} onClick={handleClick}>
export function PopupForm({ className, style, children }) {
return (
<div className={classNames(styles.form, className)} style={style}>
{children}
</div>,
document.body,
</div>
);
}

Expand Down
2 changes: 1 addition & 1 deletion components/pages/reports/PopupForm.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
background: var(--base50);
min-width: 300px;
padding: 20px;
margin-left: 30px;
border: 1px solid var(--base400);
border-radius: var(--border-radius);
box-shadow: 0 0 0 5px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
8 changes: 4 additions & 4 deletions components/pages/reports/insights/InsightsParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ export function InsightsParameters() {
<Icons.Plus />
</Icon>
</TooltipPopup>
<Popup position="bottom" alignment="start">
{(close, element) => {
<Popup position="bottom" alignment="start" className={styles.popup}>
{close => {
return (
<PopupForm element={element} onClose={close}>
<PopupForm onClose={close}>
{id === 'fields' && (
<FieldSelectForm
items={fieldOptions}
Expand Down Expand Up @@ -114,7 +114,7 @@ export function InsightsParameters() {
return (
<FormRow key={label} label={label} action={<AddButton id={id} onAdd={handleAdd} />}>
<ParameterList items={parameterData[id]} onRemove={index => handleRemove(id, index)}>
{({ name, filter, value, label }) => {
{({ name, filter, value }) => {
return (
<div className={styles.parameter}>
{id === 'fields' && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
.op {
font-weight: bold;
}

.popup {
margin-top: -10px;
margin-left: 30px;
}
2 changes: 1 addition & 1 deletion components/pages/reports/retention/RetentionParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function RetentionParameters() {
return (
<Form ref={ref} values={parameters} onSubmit={handleSubmit} preventSubmit={true}>
<BaseParameters showDateSelect={false} />
<FormRow label={formatMessage(labels.dateRange)}>
<FormRow label={formatMessage(labels.date)}>
<MonthSelect date={startDate} onChange={handleDateChange} />
</FormRow>
<FormButtons>
Expand Down
8 changes: 7 additions & 1 deletion components/pages/reports/retention/RetentionReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import ReportMenu from '../ReportMenu';
import ReportBody from '../ReportBody';
import Magnet from 'assets/magnet.svg';
import { REPORT_TYPES } from 'lib/constants';
import { parseDateRange } from 'lib/date';
import { endOfMonth, startOfMonth } from 'date-fns';

const defaultParameters = {
type: REPORT_TYPES.retention,
parameters: {},
parameters: {
dateRange: parseDateRange(
`range:${startOfMonth(new Date()).getTime()}:${endOfMonth(new Date()).getTime()}`,
),
},
};

export default function RetentionReport({ reportId }) {
Expand Down
61 changes: 28 additions & 33 deletions components/pages/reports/retention/RetentionTable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useContext } from 'react';
import { GridTable, GridColumn } from 'react-basics';
import classNames from 'classnames';
import { ReportContext } from '../Report';
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
Expand All @@ -16,14 +15,26 @@ export function RetentionTable() {
return <EmptyPlaceholder />;
}

const rows = data.reduce((arr, { date, visitors }) => {
if (!arr.find(a => a.date === date)) {
return arr.concat({ date, visitors });
const days = [1, 2, 3, 4, 5, 6, 7, 14, 21, 28];

const rows = data.reduce((arr, row) => {
const { date, visitors, day } = row;
if (day === 0) {
return arr.concat({
date,
visitors,
records: days
.reduce((arr, day) => {
arr[day] = data.find(x => x.date === date && x.day === day);
return arr;
}, [])
.filter(n => n),
});
}
return arr;
}, []);

const days = [1, 2, 3, 4, 5, 6, 7, 14, 21, 30];
const totalDays = rows.length;

return (
<>
Expand All @@ -37,47 +48,31 @@ export function RetentionTable() {
</div>
))}
</div>
{rows.map(({ date, visitors }, i) => {
{rows.map(({ date, visitors, records }, rowIndex) => {
return (
<div key={i} className={styles.row}>
<div key={rowIndex} className={styles.row}>
<div className={styles.date}>{formatDate(`${date} 00:00:00`, 'PP')}</div>
<div className={styles.visitors}>{visitors}</div>
{days.map((n, day) => {
{days.map(day => {
if (totalDays - rowIndex < day) {
return null;
}
const percentage = records[day]?.percentage;
return (
<div key={day} className={styles.cell}>
{data.find(row => row.date === date && row.day === day)?.percentage.toFixed(2)}
<div
key={day}
className={classNames(styles.cell, { [styles.empty]: !percentage })}
>
{percentage ? `${percentage.toFixed(2)}%` : ''}
</div>
);
})}
</div>
);
})}
</div>
<DataTable data={data} />
</>
);
}

function DataTable({ data }) {
return (
<GridTable data={data || []}>
<GridColumn name="date" label={'Date'}>
{row => row.date}
</GridColumn>
<GridColumn name="day" label={'Day'}>
{row => row.day}
</GridColumn>
<GridColumn name="visitors" label={'Visitors'}>
{row => row.visitors}
</GridColumn>
<GridColumn name="returnVisitors" label={'Return Visitors'}>
{row => row.returnVisitors}
</GridColumn>
<GridColumn name="percentage" label={'Percentage'}>
{row => row.percentage}
</GridColumn>
</GridTable>
);
}

export default RetentionTable;
6 changes: 5 additions & 1 deletion components/pages/reports/retention/RetentionTable.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
justify-content: center;
width: 60px;
height: 60px;
background: var(--blue100);
background: var(--blue200);
border-radius: var(--border-radius);
}

Expand All @@ -46,3 +46,7 @@
font-size: var(--font-size-sm);
font-weight: 400;
}

.empty {
background: var(--blue100);
}
50 changes: 50 additions & 0 deletions db/mysql/migrations/03_metric_performance_index/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- CreateIndex
CREATE INDEX `event_data_website_id_created_at_idx` ON `event_data`(`website_id`, `created_at`);

-- CreateIndex
CREATE INDEX `event_data_website_id_created_at_event_key_idx` ON `event_data`(`website_id`, `created_at`, `event_key`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_idx` ON `session`(`website_id`, `created_at`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_hostname_idx` ON `session`(`website_id`, `created_at`, `hostname`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_browser_idx` ON `session`(`website_id`, `created_at`, `browser`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_os_idx` ON `session`(`website_id`, `created_at`, `os`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_device_idx` ON `session`(`website_id`, `created_at`, `device`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_screen_idx` ON `session`(`website_id`, `created_at`, `screen`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_language_idx` ON `session`(`website_id`, `created_at`, `language`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_country_idx` ON `session`(`website_id`, `created_at`, `country`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_subdivision1_idx` ON `session`(`website_id`, `created_at`, `subdivision1`);

-- CreateIndex
CREATE INDEX `session_website_id_created_at_city_idx` ON `session`(`website_id`, `created_at`, `city`);

-- CreateIndex
CREATE INDEX `website_event_website_id_created_at_url_path_idx` ON `website_event`(`website_id`, `created_at`, `url_path`);

-- CreateIndex
CREATE INDEX `website_event_website_id_created_at_url_query_idx` ON `website_event`(`website_id`, `created_at`, `url_query`);

-- CreateIndex
CREATE INDEX `website_event_website_id_created_at_referrer_domain_idx` ON `website_event`(`website_id`, `created_at`, `referrer_domain`);

-- CreateIndex
CREATE INDEX `website_event_website_id_created_at_page_title_idx` ON `website_event`(`website_id`, `created_at`, `page_title`);

-- CreateIndex
CREATE INDEX `website_event_website_id_created_at_event_name_idx` ON `website_event`(`website_id`, `created_at`, `event_name`);
Loading

0 comments on commit 9a7fb1f

Please sign in to comment.