From e92b4b9a29281bb58be8b12ef0fd39c2dcb51b9e Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Wed, 14 Nov 2018 11:47:47 +0100 Subject: [PATCH] clean-up route & func names --- assets/src/js/components/Chart.js | 2 +- assets/src/js/components/Sidebar.js | 2 +- assets/src/js/components/Table.js | 4 ++-- assets/src/js/pages/dashboard.js | 4 ++-- pkg/api/page_stats.go | 8 ++++---- pkg/api/referrer_stats.go | 8 +++----- pkg/api/routes.go | 10 +++++----- pkg/api/site_stats.go | 12 ++++++------ pkg/datastore/datastore.go | 8 ++++---- pkg/datastore/sqlstore/page_stats.go | 4 ++-- pkg/datastore/sqlstore/referrer_stats.go | 4 ++-- pkg/datastore/sqlstore/site_stats.go | 2 +- pkg/models/page_stats.go | 2 +- pkg/models/referrer_stats.go | 2 +- pkg/models/site_stats.go | 2 +- 15 files changed, 36 insertions(+), 38 deletions(-) diff --git a/assets/src/js/components/Chart.js b/assets/src/js/components/Chart.js index 6c5fece5..12b40448 100644 --- a/assets/src/js/components/Chart.js +++ b/assets/src/js/components/Chart.js @@ -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)) { diff --git a/assets/src/js/components/Sidebar.js b/assets/src/js/components/Sidebar.js index 30127b95..f6fd5de9 100644 --- a/assets/src/js/components/Sidebar.js +++ b/assets/src/js/components/Sidebar.js @@ -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)) { diff --git a/assets/src/js/components/Table.js b/assets/src/js/components/Table.js index c1601b66..7c3fbf3f 100644 --- a/assets/src/js/components/Table.js +++ b/assets/src/js/components/Table.js @@ -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) ) { @@ -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 diff --git a/assets/src/js/pages/dashboard.js b/assets/src/js/pages/dashboard.js index 4a15f5f5..d42144f3 100644 --- a/assets/src/js/pages/dashboard.js +++ b/assets/src/js/pages/dashboard.js @@ -172,10 +172,10 @@ class Dashboard extends Component {
- +
-
+
diff --git a/pkg/api/page_stats.go b/pkg/api/page_stats.go index 0176acfb..9712b89b 100644 --- a/pkg/api/page_stats.go +++ b/pkg/api/page_stats.go @@ -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 { diff --git a/pkg/api/referrer_stats.go b/pkg/api/referrer_stats.go index c32feca2..694ad186 100644 --- a/pkg/api/referrer_stats.go +++ b/pkg/api/referrer_stats.go @@ -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 { diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 9629274b..5e1fe4f7 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -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) diff --git a/pkg/api/site_stats.go b/pkg/api/site_stats.go index d6275cd1..8e05ac05 100644 --- a/pkg/api/site_stats.go +++ b/pkg/api/site_stats.go @@ -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 { @@ -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) @@ -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 } diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index 530dff8f..42d50b8c 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -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 @@ -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 diff --git a/pkg/datastore/sqlstore/page_stats.go b/pkg/datastore/sqlstore/page_stats.go index edbd944d..cb682cb3 100644 --- a/pkg/datastore/sqlstore/page_stats.go +++ b/pkg/datastore/sqlstore/page_stats.go @@ -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, @@ -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 } diff --git a/pkg/datastore/sqlstore/referrer_stats.go b/pkg/datastore/sqlstore/referrer_stats.go index 68aefebd..8bef554c 100644 --- a/pkg/datastore/sqlstore/referrer_stats.go +++ b/pkg/datastore/sqlstore/referrer_stats.go @@ -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 @@ -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) } diff --git a/pkg/datastore/sqlstore/site_stats.go b/pkg/datastore/sqlstore/site_stats.go index 120e98d2..310b58bb 100644 --- a/pkg/datastore/sqlstore/site_stats.go +++ b/pkg/datastore/sqlstore/site_stats.go @@ -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 diff --git a/pkg/models/page_stats.go b/pkg/models/page_stats.go index ddbb70af..ef59e9d9 100644 --- a/pkg/models/page_stats.go +++ b/pkg/models/page_stats.go @@ -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"` diff --git a/pkg/models/referrer_stats.go b/pkg/models/referrer_stats.go index cccb763a..c4a90957 100644 --- a/pkg/models/referrer_stats.go +++ b/pkg/models/referrer_stats.go @@ -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"` diff --git a/pkg/models/site_stats.go b/pkg/models/site_stats.go index ad2cbe48..2df12e17 100644 --- a/pkg/models/site_stats.go +++ b/pkg/models/site_stats.go @@ -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"`