Skip to content
Permalink
Browse files

clean-up route & func names

  • Loading branch information...
dannyvankooten committed Nov 14, 2018
1 parent 9be2f9d commit e92b4b9a29281bb58be8b12ef0fd39c2dcb51b9e
@@ -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)) {
@@ -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)) {
@@ -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
@@ -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>

@@ -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 {
@@ -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 {
@@ -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)

@@ -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
}
@@ -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
@@ -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
}
@@ -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)
}
@@ -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
@@ -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"`
@@ -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"`
@@ -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"`

0 comments on commit e92b4b9

Please sign in to comment.
You can’t perform that action at this time.