Skip to content

Commit

Permalink
clean-up route & func names
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Nov 14, 2018
1 parent 9be2f9d commit e92b4b9
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion assets/src/js/components/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class Chart extends Component {
fetchData(props) {
this.setState({ loading: true })

Client.request(`/sites/${props.siteId}/stats/site/groupby/day?before=${props.before}&after=${props.after}`)
Client.request(`/sites/${props.siteId}/stats/site?before=${props.before}&after=${props.after}`)
.then((d) => {
// request finished; check if params changed in the meantime
if( this.paramsChanged(props, this.props)) {
Expand Down
2 changes: 1 addition & 1 deletion assets/src/js/components/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Sidebar extends Component {
fetchData(props) {
this.setState({ loading: true })

Client.request(`/sites/${props.siteId}/stats/site?before=${props.before}&after=${props.after}`)
Client.request(`/sites/${props.siteId}/stats/site/agg?before=${props.before}&after=${props.after}`)
.then((data) => {
// request finished; check if timestamp range is still the one user wants to see
if(this.paramsChanged(props, this.props)) {
Expand Down
4 changes: 2 additions & 2 deletions assets/src/js/components/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Table extends Component {
fetchData(props) {
this.setState({ loading: true });

Client.request(`/sites/${props.siteId}/${props.endpoint}?before=${props.before}&after=${props.after}&limit=${this.state.limit}`)
Client.request(`/sites/${props.siteId}/stats/${props.endpoint}/agg?before=${props.before}&after=${props.after}&limit=${this.state.limit}`)
.then((d) => {
// request finished; check if timestamp range is still the one user wants to see
if( this.paramsChanged(props, this.props) ) {
Expand All @@ -51,7 +51,7 @@ class Table extends Component {
});

// fetch totals too
Client.request(`/sites/${props.siteId}/${props.endpoint}/pageviews?before=${props.before}&after=${props.after}`)
Client.request(`/sites/${props.siteId}/stats/${props.endpoint}/agg/pageviews?before=${props.before}&after=${props.after}`)
.then((d) => {
this.setState({
total: d
Expand Down
4 changes: 2 additions & 2 deletions assets/src/js/pages/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ class Dashboard extends Component {
<Chart siteId={state.site.id} before={state.before} after={state.after} />
</div>
<div class="box box-pages">
<Table endpoint="stats/pages" headers={["Top pages", "Views", "Uniques"]} siteId={state.site.id} before={state.before} after={state.after} />
<Table endpoint="pages" headers={["Top pages", "Views", "Uniques"]} siteId={state.site.id} before={state.before} after={state.after} />
</div>
<div class="box box-referrers">
<Table endpoint="stats/referrers" headers={["Top referrers", "Views", "Uniques"]} siteId={state.site.id} before={state.before} after={state.after} showHostname="true" />
<Table endpoint="referrers" headers={["Top referrers", "Views", "Uniques"]} siteId={state.site.id} before={state.before} after={state.after} showHostname="true" />
</div>
</div>

Expand Down
8 changes: 4 additions & 4 deletions pkg/api/page_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"net/http"
)

// URL: /api/stats/page
func (api *API) GetPageStatsHandler(w http.ResponseWriter, r *http.Request) error {
// URL: /api/sites/{id:[0-9]+}/stats/pages/agg
func (api *API) GetAggregatedPageStatsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetAggregatedPageStats(params.SiteID, params.StartDate, params.EndDate, params.Limit)
result, err := api.database.SelectAggregatedPageStats(params.SiteID, params.StartDate, params.EndDate, params.Limit)
if err != nil {
return err
}
return respond(w, http.StatusOK, envelope{Data: result})
}

func (api *API) GetPageStatsPageviewsHandler(w http.ResponseWriter, r *http.Request) error {
func (api *API) GetAggregatedPageStatsPageviewsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetAggregatedPageStatsPageviews(params.SiteID, params.StartDate, params.EndDate)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions pkg/api/referrer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import (
"net/http"
)

// URL: /api/stats/referrer
func (api *API) GetReferrerStatsHandler(w http.ResponseWriter, r *http.Request) error {
func (api *API) GetAggregatedReferrerStatsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetAggregatedReferrerStats(params.SiteID, params.StartDate, params.EndDate, params.Limit)
result, err := api.database.SelectAggregatedReferrerStats(params.SiteID, params.StartDate, params.EndDate, params.Limit)
if err != nil {
return err
}
return respond(w, http.StatusOK, envelope{Data: result})
}

// URL: /api/stats/referrer/pageviews
func (api *API) GetReferrerStatsPageviewsHandler(w http.ResponseWriter, r *http.Request) error {
func (api *API) GetAggregatedReferrerStatsPageviewsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetAggregatedReferrerStatsPageviews(params.SiteID, params.StartDate, params.EndDate)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func (api *API) Routes() *mux.Router {
r.Handle("/api/sites/{id:[0-9]+}", api.Authorize(HandlerFunc(api.DeleteSiteHandler))).Methods(http.MethodDelete)

r.Handle("/api/sites/{id:[0-9]+}/stats/site", api.Authorize(HandlerFunc(api.GetSiteStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/site/groupby/day", api.Authorize(HandlerFunc(api.GetSiteStatsPerDayHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/site/agg", api.Authorize(HandlerFunc(api.GetAggregatedSiteStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/site/realtime", api.Authorize(HandlerFunc(api.GetSiteStatsRealtimeHandler))).Methods(http.MethodGet)

r.Handle("/api/sites/{id:[0-9]+}/stats/pages", api.Authorize(HandlerFunc(api.GetPageStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/pages/pageviews", api.Authorize(HandlerFunc(api.GetPageStatsPageviewsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/pages/agg", api.Authorize(HandlerFunc(api.GetAggregatedPageStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/pages/agg/pageviews", api.Authorize(HandlerFunc(api.GetAggregatedPageStatsPageviewsHandler))).Methods(http.MethodGet)

r.Handle("/api/sites/{id:[0-9]+}/stats/referrers", api.Authorize(HandlerFunc(api.GetReferrerStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/referrers/pageviews", api.Authorize(HandlerFunc(api.GetReferrerStatsPageviewsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/referrers/agg", api.Authorize(HandlerFunc(api.GetAggregatedReferrerStatsHandler))).Methods(http.MethodGet)
r.Handle("/api/sites/{id:[0-9]+}/stats/referrers/agg/pageviews", api.Authorize(HandlerFunc(api.GetAggregatedReferrerStatsPageviewsHandler))).Methods(http.MethodGet)

r.Handle("/health", HandlerFunc(api.Health)).Methods(http.MethodGet)

Expand Down
12 changes: 6 additions & 6 deletions pkg/api/site_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
)

// URL: /api/stats/site
func (api *API) GetSiteStatsHandler(w http.ResponseWriter, r *http.Request) error {
// URL: /api/sites/{id:[0-9]+}/stats/site/agg
func (api *API) GetAggregatedSiteStatsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetAggregatedSiteStats(params.SiteID, params.StartDate, params.EndDate)
if err != nil {
Expand All @@ -14,7 +14,7 @@ func (api *API) GetSiteStatsHandler(w http.ResponseWriter, r *http.Request) erro
return respond(w, http.StatusOK, envelope{Data: result})
}

// URL: /api/stats/site/realtime
// URL: /api/sites/{id:[0-9]+}/stats/site/realtime
func (api *API) GetSiteStatsRealtimeHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetRealtimeVisitorCount(params.SiteID)
Expand All @@ -24,10 +24,10 @@ func (api *API) GetSiteStatsRealtimeHandler(w http.ResponseWriter, r *http.Reque
return respond(w, http.StatusOK, envelope{Data: result})
}

// URL: /api/stats/site/groupby/day
func (api *API) GetSiteStatsPerDayHandler(w http.ResponseWriter, r *http.Request) error {
// URL: /api/sites/{id:[0-9]+}/stats/site
func (api *API) GetSiteStatsHandler(w http.ResponseWriter, r *http.Request) error {
params := GetRequestParams(r)
result, err := api.database.GetSiteStatsPerDay(params.SiteID, params.StartDate, params.EndDate)
result, err := api.database.SelectSiteStats(params.SiteID, params.StartDate, params.EndDate)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ type Datastore interface {

// site stats
GetSiteStats(int64, time.Time) (*models.SiteStats, error)
GetSiteStatsPerDay(int64, time.Time, time.Time) ([]*models.SiteStats, error)
SaveSiteStats(*models.SiteStats) error
GetAggregatedSiteStats(int64, time.Time, time.Time) (*models.SiteStats, error)
SelectSiteStats(int64, time.Time, time.Time) ([]*models.SiteStats, error)
GetRealtimeVisitorCount(int64) (int64, error)
SaveSiteStats(*models.SiteStats) error

// pageviews
InsertPageviews([]*models.Pageview) error
Expand All @@ -42,13 +42,13 @@ type Datastore interface {
// page stats
GetPageStats(int64, time.Time, int64, int64) (*models.PageStats, error)
SavePageStats(*models.PageStats) error
GetAggregatedPageStats(int64, time.Time, time.Time, int64) ([]*models.PageStats, error)
SelectAggregatedPageStats(int64, time.Time, time.Time, int64) ([]*models.PageStats, error)
GetAggregatedPageStatsPageviews(int64, time.Time, time.Time) (int64, error)

// referrer stats
GetReferrerStats(int64, time.Time, int64, int64) (*models.ReferrerStats, error)
SaveReferrerStats(*models.ReferrerStats) error
GetAggregatedReferrerStats(int64, time.Time, time.Time, int64) ([]*models.ReferrerStats, error)
SelectAggregatedReferrerStats(int64, time.Time, time.Time, int64) ([]*models.ReferrerStats, error)
GetAggregatedReferrerStatsPageviews(int64, time.Time, time.Time) (int64, error)

// hostnames
Expand Down
4 changes: 2 additions & 2 deletions pkg/datastore/sqlstore/page_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (db *sqlstore) updatePageStats(s *models.PageStats) error {
return err
}

func (db *sqlstore) GetAggregatedPageStats(siteID int64, startDate time.Time, endDate time.Time, limit int64) ([]*models.PageStats, error) {
func (db *sqlstore) SelectAggregatedPageStats(siteID int64, startDate time.Time, endDate time.Time, limit int64) ([]*models.PageStats, error) {
var result []*models.PageStats
query := db.Rebind(`SELECT
h.name AS hostname,
Expand All @@ -60,7 +60,7 @@ func (db *sqlstore) GetAggregatedPageStats(siteID int64, startDate time.Time, en

func (db *sqlstore) GetAggregatedPageStatsPageviews(siteID int64, startDate time.Time, endDate time.Time) (int64, error) {
var result int64
query := db.Rebind(`SELECT COALESCE(SUM(pageviews), 0) FROM page_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
query := db.Rebind(`SELECT SUM(pageviews) FROM page_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
err := db.Get(&result, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))
return result, err
}
4 changes: 2 additions & 2 deletions pkg/datastore/sqlstore/referrer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (db *sqlstore) updateReferrerStats(s *models.ReferrerStats) error {
return err
}

func (db *sqlstore) GetAggregatedReferrerStats(siteID int64, startDate time.Time, endDate time.Time, limit int64) ([]*models.ReferrerStats, error) {
func (db *sqlstore) SelectAggregatedReferrerStats(siteID int64, startDate time.Time, endDate time.Time, limit int64) ([]*models.ReferrerStats, error) {
var result []*models.ReferrerStats

sql := `SELECT
Expand Down Expand Up @@ -69,7 +69,7 @@ func (db *sqlstore) GetAggregatedReferrerStats(siteID int64, startDate time.Time

func (db *sqlstore) GetAggregatedReferrerStatsPageviews(siteID int64, startDate time.Time, endDate time.Time) (int64, error) {
var result int64
query := db.Rebind(`SELECT COALESCE(SUM(pageviews), 0) FROM referrer_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
query := db.Rebind(`SELECT SUM(pageviews) FROM referrer_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
err := db.Get(&result, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))
return result, mapError(err)
}
2 changes: 1 addition & 1 deletion pkg/datastore/sqlstore/site_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (db *sqlstore) updateSiteStats(s *models.SiteStats) error {
return err
}

func (db *sqlstore) GetSiteStatsPerDay(siteID int64, startDate time.Time, endDate time.Time) ([]*models.SiteStats, error) {
func (db *sqlstore) SelectSiteStats(siteID int64, startDate time.Time, endDate time.Time) ([]*models.SiteStats, error) {
results := []*models.SiteStats{}
query := db.Rebind(`SELECT *
FROM site_stats
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/page_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type PageStats struct {
New bool `db:"-" json:"-"`
SiteID int64 `db:"site_id"`
SiteID int64 `db:"site_id" json:"-"`
HostnameID int64 `db:"hostname_id" json:"-"`
PathnameID int64 `db:"pathname_id" json:"-"`
Hostname string `db:"hostname"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/referrer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type ReferrerStats struct {
New bool `db:"-" json:"-"`
SiteID int64 `db:"site_id"`
SiteID int64 `db:"site_id" json:"-"`
HostnameID int64 `db:"hostname_id" json:"-"`
PathnameID int64 `db:"pathname_id" json:"-"`
Hostname string `db:"hostname"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/site_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type SiteStats struct {
New bool `db:"-" json:"-" `
SiteID int64 `db:"site_id"`
SiteID int64 `db:"site_id" json:"-"`
Visitors int64 `db:"visitors"`
Pageviews int64 `db:"pageviews"`
Sessions int64 `db:"sessions"`
Expand Down

0 comments on commit e92b4b9

Please sign in to comment.