Skip to content

Commit

Permalink
feat: add percentages to custom event data (#130)
Browse files Browse the repository at this point in the history
* feat(core): add events percentage to properties endpoint

* fix(dashboard): error page styles relying on mantine preset

* fix(core): return percentages with materialised table

* feat(dashboard): show percentage on home and expanded page
  • Loading branch information
ayuhito authored Aug 30, 2024
1 parent b1003b3 commit 7c69c4b
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 58 deletions.
Binary file modified bun.lockb
Binary file not shown.
21 changes: 19 additions & 2 deletions core/api/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions core/api/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions core/api/oas_validators_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions core/db/duckdb/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/medama-io/medama/model"
)

const (
EventsCountStmt = "COUNT(*) AS events"
EventsPercentageStmt = "ifnull(ROUND(COUNT(*) / (SELECT total_events FROM total), 4), 0) AS events_percentage"
)

// GetWebsiteReferrersSummary returns a summary of the referrers for the given filters.
func (c *Client) GetWebsiteCustomProperties(ctx context.Context, filter *db.Filters) ([]*model.StatsCustomProperties, error) {
var properties []*model.StatsCustomProperties
Expand All @@ -21,16 +26,22 @@ func (c *Client) GetWebsiteCustomProperties(ctx context.Context, filter *db.Filt
//
// Events is the number of events for the custom property
//
// Visitors is the number of unique visitors for the custom property.
query := qb.New()
// Events percentage is the percentage of events for the custom property
query := qb.New().WithMaterialized(
qb.NewCTE("total", qb.New().
Select("COUNT(*) FILTER (WHERE name IS NOT NULL) AS total_events").
From("views").
LeftJoin(EventsJoinStmt).
Where(filter.WhereString())))

// If the property name is empty, return only the property names with their
// aggregated events and visitors. No values.
if filter.PropertyName == nil || filter.PropertyName.Value == "" {
query = query.Select(
"name",
"'' AS value",
"COUNT(*) AS events",
EventsCountStmt,
EventsPercentageStmt,
).
From("views").
LeftJoin(EventsJoinStmt).
Expand All @@ -44,7 +55,8 @@ func (c *Client) GetWebsiteCustomProperties(ctx context.Context, filter *db.Filt
query = query.Select(
"'' AS name",
"value",
"COUNT(*) AS events",
EventsCountStmt,
EventsPercentageStmt,
).
From("views").
LeftJoin(EventsJoinStmt).
Expand Down
11 changes: 11 additions & 0 deletions core/db/duckdb/query/qb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type QueryBuilder struct {
cteClauses []CTE
selectClauses []string
fromClause []string
joinClause string
leftJoinClause string
whereClause string
groupByClause []string
Expand Down Expand Up @@ -47,6 +48,11 @@ func (qb *QueryBuilder) From(tables ...string) *QueryBuilder {
return qb
}

func (qb *QueryBuilder) Join(query string) *QueryBuilder {
qb.joinClause = query
return qb
}

func (qb *QueryBuilder) LeftJoin(query string) *QueryBuilder {
qb.leftJoinClause = query
return qb
Expand Down Expand Up @@ -100,6 +106,11 @@ func (qb *QueryBuilder) Build() string {
query.WriteString(" FROM ")
query.WriteString(strings.Join(qb.fromClause, ", "))

if qb.joinClause != "" {
query.WriteString(" JOIN ")
query.WriteString(qb.joinClause)
}

if qb.leftJoinClause != "" {
query.WriteString(" LEFT JOIN ")
query.WriteString(qb.leftJoinClause)
Expand Down
7 changes: 4 additions & 3 deletions core/model/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ type StatsLanguages struct {
}

type StatsCustomProperties struct {
Name string `db:"name"`
Value string `db:"value"`
Events int `db:"events"`
Name string `db:"name"`
Value string `db:"value"`
Events int `db:"events"`
EventsPercentage float32 `db:"events_percentage"`
}
Loading

0 comments on commit 7c69c4b

Please sign in to comment.