Skip to content
quarry Artifact [8fb89ae70a]
Login

Artifact [8fb89ae70a]

Artifact 8fb89ae70aafb5fab9e1bfe62463870f387bb441e4e60af3e89138173e1aec3b:


//go:build !js && !wasip1

package main

import (
	"fmt"
	"log"
	"path/filepath"
	"time"

	"craftdesign.group/quarry/internal/obs"
	"craftdesign.group/quarry/internal/obsduck"
)

// openAnalytics wires the durable observation store, returning a stop
// function. Failure is NOT fatal: analytics is derived data about a
// forge that works without it, and a forge that refuses to start
// because its metrics store is unhappy has the priority backwards.
//
// Two files provide this — one behind !js, one behind js — so that the
// wasm build the CGO-free rule exists for never reaches DuckDB. It is
// not enough to leave the import out of the request path: go-duckdb's
// own build constraints exclude wasm, so any reachable import fails the
// build with "build constraints exclude all Go files".
func openAnalytics(dir string) func() {
	if dir == "" {
		return func() {}
	}
	path := filepath.Join(dir, "ops.duckdb")
	sink, err := obsduck.Open(path, 0)
	if err != nil {
		log.Printf("analytics: %v; the live panels still work", err)
		return func() {}
	}
	obs.Default.SetSink(sink)
	stop := make(chan struct{})
	go sink.FlushEvery(30*time.Second, stop)
	fmt.Println("quarry: analytics ->", path)
	return func() {
		close(stop)
		if err := sink.Close(); err != nil {
			log.Printf("analytics: close: %v", err)
		}
	}
}