You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: dashboard.go
    path of file: ./repos/goatcounter/handlers
    
    the code of the file until where you have to start completion: // Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com

package handlers

import (
	"context"
	"html/template"
	"math"
	"net/http"
	"strconv"
	"strings"
	"sync"
	"time"

	"zgo.at/errors"
	"zgo.at/goatcounter/v2"
	"zgo.at/goatcounter/v2/metrics"
	"zgo.at/goatcounter/v2/widgets"
	"zgo.at/guru"
	"zgo.at/z18n"
	"zgo.at/zhttp"
	"zgo.at/zlog"
	"zgo.at/zstd/zint"
	"zgo.at/zstd/zsync"
	"zgo.at/zstd/ztime"
	"zgo.at/ztpl"
	"zgo.at/zvalidate"
)

// DailyView forces the "view by day" if the number of selected days is larger than this.
const DailyView = 90

func (h backend) dashboard(w http.ResponseWriter, r *http.Request) error {
	m := metrics.Start("dashboard")
	m.AddTag(r.Host)
	defer m.Done()

	site := Site(r.Context())
	user := User(r.Context())

	// Cache much more aggressively for public displays. Don't care so much if
	// it's outdated by an hour.
	if site.Settings.IsPublic() && User(r.Context()).ID == 0 {
		w.Header().Set("Cache-Control", "public,max-age=3600")
		w.Header().Set("Vary", "Cookie")
	}

	q := r.URL.Query()

	// Load view, but override this from query.
	view, _ := user.Settings.Views.Get("default")

	rng, err := getPeriod(w, r, site, user)

    
func (h backend) loadWidget(w http.ResponseWriter, r *http.Request) error {
	user := User(r.Context())
	rng, err := getPeriod(w, r, Site(r.Context()), user)
	if err != nil {
		return err
	}

	v := goatcounter.NewValidate(r.Context())
	var (
		widget     = int(v.Integer("widget", r.URL.Query().Get("widget")))
		key        = r.URL.Query().Get("key")
		total      = int(v.Integer("total", r.URL.Query().Get("total")))
		offset     = int(v.Integer("offset", r.URL.Query().Get("offset")))
		pathFilter = getPathFilter(&v, r)
	)
	if v.HasErrors() {
		return v
	}

	args := widgets.SharedData{
		Site:     Site(r.Context()),
		User:     User(r.Context()),
		TotalUTC: total,
		Total:    total,
		RowsOnly: key != "" || offset > 0,
		Args: widgets.Args{
			Rng:        rng,
			PathFilter: pathFilter,
			Offset:     offset,
		},
	}

	wid := widgets.FromSiteWidget(r.Context(), user.Settings.Widgets[widget])
	if key != "" {
		s := wid.Settings()
		s.Set("key", key)
		wid.SetSettings(s)
	}

	ret := make(map[string]any)
	switch wid.Name() {
	case "pages":
		p := wid.(*widgets.Pages)

		args.RowsOnly = true
		args.Args.Daily, args.Args.ForcedDaily = getDaily(r, rng)

		if key != "" {
			p.RefsForPath, _ = strconv.ParseInt(key, 10, 64)
		} else {
			p.Max, err = strconv.Atoi(r.URL.Query().Get("max"))
			if err != nil {
				return err
			}
			p.Exclude, err = zint.Split(r.URL.Query().Get("exclude"), ",")
			if err != nil {
				return err
			}
		}
	}

	ret["more"], err = wid.GetData(r.Context(), args.Args)
	if err != nil {
		return err
	}
	ret["html"], err = ztpl.ExecuteString(wid.RenderHTML(r.Context(), args))
	if err != nil {
		return err
	}
	switch wid.Name() {
	case "pages":
		p := wid.(*widgets.Pages)
		ret["total_display"] = p.Display
		ret["max"] = p.Max
	}

	return zhttp.JSON(w, ret)
}