Phase 3 part 2: the morning check and the Monday report
The morning check compares the last 7 days of final Search Console data with the 7 before, by page and by search, and lists only moves past the thresholds (editable). Each move is explained from the data: the search whose clicks changed, and whether a position change is real or a change in the mix of searches. Missing rows are "no data", not zero. The Monday report has three numbers with where the change came from, the 3 pages that moved most, and "Waiting for your yes"; it's written each Monday, kept, and optionally emailed through the outreach mailbox. Schema 13. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+159
-24
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"supermarket/internal/api/gsc"
|
||||
"supermarket/internal/oauth"
|
||||
"supermarket/internal/outreach"
|
||||
"supermarket/internal/profile"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/seo"
|
||||
@@ -116,6 +117,7 @@ func (s *Server) renderSearchSettings(w http.ResponseWriter, r *http.Request, er
|
||||
"Sites": s.seoSites(),
|
||||
"Connected": s.Secrets.Has(secrets.GoogleRefreshToken),
|
||||
"Account": s.connections()["google"].Account,
|
||||
"SEO": seo.LoadSettings(s.DB),
|
||||
}
|
||||
if c := s.gsc(); c != nil {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 20*time.Second)
|
||||
@@ -147,6 +149,16 @@ func (s *Server) searchSettingsSave(w http.ResponseWriter, r *http.Request) {
|
||||
gauth.token = ""
|
||||
gauth.mu.Unlock()
|
||||
}
|
||||
if _, has := r.PostForm["min_clicks"]; has {
|
||||
f := r.PostFormValue
|
||||
clicks, _ := strconv.Atoi(f("min_clicks"))
|
||||
impr, _ := strconv.Atoi(f("min_impressions"))
|
||||
pos, _ := strconv.ParseFloat(f("min_positions"), 64)
|
||||
if err := seo.SaveSettings(s.DB, seo.Settings{MinClicks: clicks, MinPositions: pos, MinImpressions: impr, EmailReport: f("email_report") == "on"}); err != nil {
|
||||
s.renderSearchSettings(w, r, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if props, has := r.PostForm["property"]; has {
|
||||
p, err := profile.Load(s.DB)
|
||||
if err != nil {
|
||||
@@ -225,6 +237,7 @@ func (s *Server) RunSEO(ctx context.Context) {
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
s.weeklyReports(ctx, time.Now())
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -237,30 +250,8 @@ func (s *Server) RunSEO(ctx context.Context) {
|
||||
// --- The SEO page ---
|
||||
|
||||
func (s *Server) seoPage(w http.ResponseWriter, r *http.Request) {
|
||||
sites := s.seoSites()
|
||||
var mapped []seoSite
|
||||
for _, st := range sites {
|
||||
if st.Property != "" {
|
||||
mapped = append(mapped, st)
|
||||
}
|
||||
}
|
||||
var cur *seoSite
|
||||
if i, err := strconv.Atoi(r.URL.Query().Get("site")); err == nil {
|
||||
for j := range mapped {
|
||||
if mapped[j].Index == i {
|
||||
cur = &mapped[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
if cur == nil && len(mapped) > 0 {
|
||||
cur = &mapped[0]
|
||||
}
|
||||
data := map[string]any{
|
||||
"Connected": s.Secrets.Has(secrets.GoogleRefreshToken),
|
||||
"Sites": mapped,
|
||||
"Current": cur,
|
||||
"Days": seo.RanksDays,
|
||||
}
|
||||
data, cur := s.seoBase(r)
|
||||
data["Path"], data["Days"] = "/seo", seo.RanksDays
|
||||
if cur != nil {
|
||||
ranks, err := seo.WhatRanks(s.DB, cur.Property)
|
||||
if err != nil {
|
||||
@@ -283,3 +274,147 @@ func pagePath(u string) string {
|
||||
}
|
||||
return p.Path
|
||||
}
|
||||
|
||||
// seoCurrent is the site the request asks for (?site=), or the first.
|
||||
func (s *Server) seoCurrent(r *http.Request) ([]seoSite, *seoSite) {
|
||||
var mapped []seoSite
|
||||
for _, st := range s.seoSites() {
|
||||
if st.Property != "" {
|
||||
mapped = append(mapped, st)
|
||||
}
|
||||
}
|
||||
if i, err := strconv.Atoi(r.URL.Query().Get("site")); err == nil {
|
||||
for j := range mapped {
|
||||
if mapped[j].Index == i {
|
||||
return mapped, &mapped[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(mapped) > 0 {
|
||||
return mapped, &mapped[0]
|
||||
}
|
||||
return mapped, nil
|
||||
}
|
||||
|
||||
func (s *Server) seoBase(r *http.Request) (map[string]any, *seoSite) {
|
||||
sites, cur := s.seoCurrent(r)
|
||||
return map[string]any{
|
||||
"Connected": s.Secrets.Has(secrets.GoogleRefreshToken),
|
||||
"Sites": sites,
|
||||
"Current": cur,
|
||||
}, cur
|
||||
}
|
||||
|
||||
// seoCheckPage is the morning check.
|
||||
func (s *Server) seoCheckPage(w http.ResponseWriter, r *http.Request) {
|
||||
data, cur := s.seoBase(r)
|
||||
data["Path"] = "/seo/check"
|
||||
if cur != nil {
|
||||
c, err := seo.MorningCheck(s.DB, cur.Property, seo.LoadSettings(s.DB))
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
data["Check"] = c
|
||||
if c != nil && len(c.Moves) > 25 {
|
||||
data["More"] = len(c.Moves) - 25
|
||||
c.Moves = c.Moves[:25]
|
||||
}
|
||||
}
|
||||
s.render(w, r, "seo-check", page{Title: "Morning check", Nav: "seo/check", Data: data})
|
||||
}
|
||||
|
||||
// seoReportPage shows a stored Monday report (the latest, or ?week=).
|
||||
func (s *Server) seoReportPage(w http.ResponseWriter, r *http.Request) {
|
||||
data, cur := s.seoBase(r)
|
||||
data["Path"] = "/seo/report"
|
||||
if cur != nil {
|
||||
list, err := seo.Reports(s.DB, cur.Property, 12)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
data["Reports"] = list
|
||||
var rep *seo.Report
|
||||
if wk := r.URL.Query().Get("week"); wk != "" {
|
||||
rep, _ = seo.GetReport(s.DB, cur.Property, wk)
|
||||
} else if len(list) > 0 {
|
||||
rep = &list[0]
|
||||
}
|
||||
data["Report"] = rep
|
||||
if rep != nil {
|
||||
for i := range list {
|
||||
if list[i].WeekTo < rep.WeekTo {
|
||||
data["Previous"] = &list[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s.render(w, r, "seo-report", page{Title: "Monday report", Nav: "seo/report", Data: data})
|
||||
}
|
||||
|
||||
// seoReportWrite writes this week's report now.
|
||||
func (s *Server) seoReportWrite(w http.ResponseWriter, r *http.Request) {
|
||||
_, cur := s.seoCurrent(r)
|
||||
if cur == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
rep, err := s.writeReport(r.Context(), *cur, false)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
if rep == nil {
|
||||
redirect(w, r, "/seo/report?site="+strconv.Itoa(cur.Index), "no-data")
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/seo/report?site="+strconv.Itoa(cur.Index)+"&week="+rep.WeekTo, "report-written")
|
||||
}
|
||||
|
||||
// writeReport builds and stores a site's report, and emails it if the
|
||||
// settings say so (and email is set up).
|
||||
func (s *Server) writeReport(ctx context.Context, site seoSite, email bool) (*seo.Report, error) {
|
||||
rep, err := seo.Weekly(s.DB, site.Property, time.Now())
|
||||
if err != nil || rep == nil {
|
||||
return rep, err
|
||||
}
|
||||
if err := seo.Save(s.DB, rep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if email && seo.LoadSettings(s.DB).EmailReport {
|
||||
if set, mb, err := s.mailbox(); err == nil {
|
||||
ectx, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
||||
err = outreach.SendToSelf(ectx, set, mb, "SEO report: "+site.URL+", week to "+rep.WeekTo, rep.Text(), time.Now())
|
||||
cancel()
|
||||
if err != nil {
|
||||
s.Log.Warn("seo: emailing the report", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
|
||||
// weeklyReports writes each site's Monday report once its week's data is
|
||||
// in: on Mondays, or when the last report is more than a week old.
|
||||
func (s *Server) weeklyReports(ctx context.Context, now time.Time) {
|
||||
for _, site := range s.seoSites() {
|
||||
if site.Property == "" || seo.Through(s.DB, site.Property) == "" {
|
||||
continue
|
||||
}
|
||||
list, err := seo.Reports(s.DB, site.Property, 1)
|
||||
if err != nil {
|
||||
s.Log.Warn("seo: reports", "err", err)
|
||||
continue
|
||||
}
|
||||
stale := len(list) == 0 || now.Sub(list[0].CreatedAt) > 7*24*time.Hour
|
||||
monday := now.Weekday() == time.Monday && now.Hour() >= 6 && (len(list) == 0 || now.Sub(list[0].CreatedAt) > 24*time.Hour)
|
||||
if !stale && !monday {
|
||||
continue
|
||||
}
|
||||
if _, err := s.writeReport(ctx, site, true); err != nil {
|
||||
s.Log.Warn("seo: writing the report", "site", site.Property, "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user