From 3b8a8849917759ff4bdc0b56e2882a084e065a74 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Mon, 21 May 2018 11:54:01 +0200 Subject: [PATCH] godoc improvements --- pkg/aggregator/aggregator.go | 7 +++++-- pkg/aggregator/result.go | 6 +++--- pkg/aggregator/store.go | 6 +++--- pkg/config/config.go | 5 +++-- pkg/datastore/datastore.go | 3 +++ 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/aggregator/aggregator.go b/pkg/aggregator/aggregator.go index 8e72d2a..ebdd0d6 100644 --- a/pkg/aggregator/aggregator.go +++ b/pkg/aggregator/aggregator.go @@ -11,12 +11,14 @@ type aggregator struct { database datastore.Datastore } +// New returns a new aggregator instance with the database dependency injected. func New(db datastore.Datastore) *aggregator { return &aggregator{ database: db, } } +// Run processes the pageviews which are ready to be processed and adds them to daily aggregation func (agg *aggregator) Run() { // Get unprocessed pageviews pageviews, err := agg.database.GetProcessablePageviews() @@ -61,9 +63,10 @@ func (agg *aggregator) Run() { } } -func (agg *aggregator) Process(pageviews []*models.Pageview) *Results { +// Process processes the given pageviews and returns the (aggregated) results per metric per day +func (agg *aggregator) Process(pageviews []*models.Pageview) *results { log.Debugf("processing %d pageviews", len(pageviews)) - results := NewResults() + results := newResults() for _, p := range pageviews { site, err := agg.getSiteStats(results, p.Timestamp) diff --git a/pkg/aggregator/result.go b/pkg/aggregator/result.go index 376fc84..171082b 100644 --- a/pkg/aggregator/result.go +++ b/pkg/aggregator/result.go @@ -4,14 +4,14 @@ import ( "github.com/usefathom/fathom/pkg/models" ) -type Results struct { +type results struct { Sites map[string]*models.SiteStats Pages map[string]*models.PageStats Referrers map[string]*models.ReferrerStats } -func NewResults() *Results { - return &Results{ +func newResults() *results { + return &results{ Sites: map[string]*models.SiteStats{}, Pages: map[string]*models.PageStats{}, Referrers: map[string]*models.ReferrerStats{}, diff --git a/pkg/aggregator/store.go b/pkg/aggregator/store.go index 6c4742e..b7b0631 100644 --- a/pkg/aggregator/store.go +++ b/pkg/aggregator/store.go @@ -7,7 +7,7 @@ import ( "github.com/usefathom/fathom/pkg/models" ) -func (agg *aggregator) getSiteStats(r *Results, t time.Time) (*models.SiteStats, error) { +func (agg *aggregator) getSiteStats(r *results, t time.Time) (*models.SiteStats, error) { // get from map date := t.Format("2006-01-02") if stats, ok := r.Sites[date]; ok { @@ -37,7 +37,7 @@ func (agg *aggregator) getSiteStats(r *Results, t time.Time) (*models.SiteStats, return stats, nil } -func (agg *aggregator) getPageStats(r *Results, t time.Time, hostname string, pathname string) (*models.PageStats, error) { +func (agg *aggregator) getPageStats(r *results, t time.Time, hostname string, pathname string) (*models.PageStats, error) { date := t.Format("2006-01-02") if stats, ok := r.Pages[date+hostname+pathname]; ok { return stats, nil @@ -64,7 +64,7 @@ func (agg *aggregator) getPageStats(r *Results, t time.Time, hostname string, pa return stats, nil } -func (agg *aggregator) getReferrerStats(r *Results, t time.Time, url string) (*models.ReferrerStats, error) { +func (agg *aggregator) getReferrerStats(r *results, t time.Time, url string) (*models.ReferrerStats, error) { date := t.Format("2006-01-02") if stats, ok := r.Referrers[date+url]; ok { return stats, nil diff --git a/pkg/config/config.go b/pkg/config/config.go index 1bb97ee..f8f1dee 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,12 +9,13 @@ import ( "github.com/usefathom/fathom/pkg/datastore/sqlstore" ) +// Config wraps the configuration structs for the various application parts type Config struct { Database *sqlstore.Config - - Secret string + Secret string } +// Parses the supplied file + environment into a Config struct func Parse(file string) *Config { var cfg Config var err error diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index bc63539..7a6f6d6 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -7,6 +7,7 @@ import ( "github.com/usefathom/fathom/pkg/models" ) +// ErrNoResults is returned whenever a single-item query returns 0 results var ErrNoResults = sqlstore.ErrNoResults // ??? type Datastore interface { @@ -47,9 +48,11 @@ type Datastore interface { GetAggregatedReferrerStats(time.Time, time.Time, int) ([]*models.ReferrerStats, error) GetAggregatedReferrerStatsPageviews(time.Time, time.Time) (int, error) + // misc Close() } +// New instantiates a new datastore from the given configuration struct func New(c *sqlstore.Config) Datastore { return sqlstore.New(c) }