Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various bits and pieces; very much WIP.
- Loading branch information
Showing
7 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright © 2019 Martin Tournoij <martin@arp242.net> | ||
// This file is part of GoatCounter and published under the terms of the EUPL | ||
// v1.2, which can be found in the LICENSE file or at http://eupl12.zgo.at | ||
|
||
package cron | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"zgo.at/blackmail" | ||
"zgo.at/goatcounter" | ||
"zgo.at/goatcounter/cfg" | ||
"zgo.at/zdb" | ||
"zgo.at/zhttp" | ||
"zgo.at/zlog" | ||
) | ||
|
||
func emailReports(ctx context.Context) error { | ||
db := zdb.MustGet(ctx) | ||
|
||
var ids []int64 | ||
err := db.SelectContext(ctx, &ids, | ||
`select id from sites where settings->>'email_reports'::varchar != '0'`) | ||
if err != nil { | ||
return fmt.Errorf("cron.emailReports get sites: %w", err) | ||
} | ||
|
||
var sites goatcounter.Sites | ||
err = sites.ListIDs(ctx, ids...) | ||
if err != nil { | ||
return fmt.Errorf("cron.emailReports: %w", err) | ||
} | ||
|
||
// Note: maybe pool subsites in one email? | ||
for _, s := range sites { | ||
text, html, err := report(ctx, s) | ||
if err != nil { | ||
zlog.Field("site", s.ID).Errorf("cron.emailReports: %w", err) | ||
continue | ||
} | ||
|
||
blackmail.Send("Report", | ||
blackmail.From("GoatCounter report", cfg.EmailFrom), | ||
blackmail.To("TODO@TODO.TODO"), | ||
blackmail.BodyText(text), | ||
blackmail.BodyHTML(html)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type templateArgs struct { | ||
Site goatcounter.Site | ||
PeriodName string | ||
Pages goatcounter.HitStats | ||
} | ||
|
||
func report(ctx context.Context, s goatcounter.Site) ([]byte, []byte, error) { | ||
ctx = goatcounter.WithSite(ctx, &s) | ||
|
||
pn := map[int]string{ | ||
-1: "first-time", | ||
1: "weekly", | ||
2: "biweekly", | ||
3: "monthly", | ||
}[s.Settings.EmailReports.Int()] | ||
|
||
start := goatcounter.Now().Add(-7 * 24 * time.Hour) | ||
end := goatcounter.Now().Add(-14 * 24 * time.Hour) | ||
|
||
var pages goatcounter.HitStats | ||
_, _, _, _, _, _, err := pages.List(ctx, start, end, "", nil, true) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("cron.report: %w", err) | ||
} | ||
|
||
args := templateArgs{ | ||
Site: s, | ||
PeriodName: pn, | ||
Pages: pages, | ||
} | ||
|
||
text, err := zhttp.ExecuteTpl("email_report.gotxt", args) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("cron.report text: %w", err) | ||
} | ||
html, err := zhttp.ExecuteTpl("email_report.gohtml", args) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("cron.report html: %w", err) | ||
} | ||
return text, html, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
HTML Version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Hi there! | ||
|
||
This is your {{.PeriodName}} GoatCounter report for {{.Site.Code}} | ||
|
||
Note: this is the text version of the email and best viewed with a monospaced | ||
typeface. | ||
|
||
Path Visitors Pageviews Difference | ||
|
||
Totals 13,645 14,143 +5% | ||
{{range $p := .Pages}} | ||
{{$p.Path}} {{$p.CountUnique}} {{$p.PageViews}} | ||
{{end}} | ||
{{/* | ||
Top referrers this week: | ||
|
||
reddit.com 1,531 | ||
Hacker News 1,531 | ||
|
||
|
||
See https://foo.goatcounter.com for the full details. | ||
|
||
if .Once | ||
This email is sent once for new installations; if you wish to keep receiving it | ||
then please enable the setting. | ||
else | ||
This is email is sent because you set it. Please change the setting if you no | ||
longer want to. | ||
end | ||
*/}} | ||
{{template "_email_bottom.gotxt" .}} |