Skip to content

Commit

Permalink
fix: map dashboard filters to db enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Mar 3, 2024
1 parent 493b5ee commit f02baa9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
16 changes: 16 additions & 0 deletions core/db/filter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package db

import (
"strconv"
"strings"

"github.com/medama-io/medama/api"
"github.com/medama-io/medama/model"
)

// FilterField represents a mapping of the filter field to the database column.
Expand Down Expand Up @@ -106,6 +108,20 @@ func NewFilter(field FilterField, param interface{}) *Filter {
case api.OptFilterFixed:
if v.IsSet() {
value, operation = FilterFixedToValues(v.Value)

// Convert the value to the enum integer for the database
//nolint:exhaustive // No other fields use uint8 enums
switch field {
case FilterBrowser:
value = strconv.Itoa(int(model.NewBrowserName(value)))
case FilterOS:
value = strconv.Itoa(int(model.NewOSName(value)))
case FilterDevice:
value = strconv.Itoa(int(model.NewDeviceTypeString(value)))
default:
// Do nothing
}

} else {
return nil
}
Expand Down
Empty file modified core/generate.sh
100644 → 100755
Empty file.
15 changes: 15 additions & 0 deletions core/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ func NewDeviceType(desktop bool, mobile bool, tablet bool, tv bool) DeviceType {
}
}

func NewDeviceTypeString(name string) DeviceType {
switch name {
case "Desktop":
return DesktopDevice
case "Mobile":
return MobileDevice
case "Tablet":
return TabletDevice
case "TV":
return TVDevice
default:
return UnknownDevice
}
}

func (d DeviceType) String() string {
switch d {
case DesktopDevice:
Expand Down
7 changes: 6 additions & 1 deletion dashboard/app/components/stats/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ const filterOptions: FilterOptions = {
Firefox: { label: 'Firefox', value: 'Firefox' },
Opera: { label: 'Opera', value: 'Opera' },
Safari: { label: 'Safari', value: 'Safari' },
Unknown: { label: 'Unknown', value: 'Unknown' },
},
},
os: {
label: 'OS',
type: 'fixed',
choices: {
Windows: { label: 'Windows', value: 'Windows' },
'Mac OS': { label: 'Mac OS', value: 'MacOS' },
MacOS: { label: 'MacOS', value: 'MacOS' },
Linux: { label: 'Linux', value: 'Linux' },
iOS: { label: 'iOS', value: 'iOS' },
Android: { label: 'Android', value: 'Android' },
ChromeOS: { label: 'ChromeOS', value: 'ChromeOS' },
Unknown: { label: 'Unknown', value: 'Unknown' },
},
},
device: {
Expand All @@ -117,6 +120,8 @@ const filterOptions: FilterOptions = {
Desktop: { label: 'Desktop', value: 'Desktop' },
Mobile: { label: 'Mobile', value: 'Mobile' },
Tablet: { label: 'Tablet', value: 'Tablet' },
TV: { label: 'TV', value: 'TV' },
Unknown: { label: 'Unknown', value: 'Unknown' },
},
},
country: {
Expand Down
21 changes: 12 additions & 9 deletions dashboard/app/components/stats/StatsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const pageviewsPercentage: DataTableColumn = {
};
const bounceRate: DataTableColumn = {
accessor: 'bounce_rate',
title: 'Bounce Rate %',
title: 'Bounce %',
render: (record: DataRow) =>
formatPercentage((record.bounces ?? 0) / (record.visitors ?? 0)),
};
Expand Down Expand Up @@ -133,22 +133,25 @@ const QueryTable = ({ query, data }: QueryTableProps) => {
setPageSize(newSize);
};

useEffect(() => {
const from = (page - 1) * pageSize;
const to = from + pageSize;
setRecords(data.slice(from, to));
}, [data, page, pageSize]);

// Sorting
const [sortStatus, setSortStatus] = useState<DataTableSortStatus<DataRow>>({
columnAccessor: 'visitors',
direction: 'desc',
});

useEffect(() => {
// Calculate the range of records to display
const from = (page - 1) * pageSize;
const to = from + pageSize;

// Sort and slice the data to page size
const temp = [...data].sort(sortBy(sortStatus.columnAccessor));
setRecords(sortStatus.direction === 'desc' ? temp.reverse() : temp);
}, [sortStatus, data]);
setRecords(
sortStatus.direction === 'desc'
? temp.reverse().slice(from, to)
: temp.slice(from, to)
);
}, [sortStatus, data, page, pageSize]);

// Define columns based on query
const columns: DataTableColumn[] = [];
Expand Down

0 comments on commit f02baa9

Please sign in to comment.