Help us shape the future of Go! Take the 2026 Go Developer Survey

crawlgraph

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package crawlgraph implements a directed graph of URLs discovered during crawling, with support for SQLite persistence, multiple export formats, and replay strategies for re-crawling changed or failed pages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentHash

func ContentHash(content []byte) string

ContentHash computes a SHA256 hash of content for change detection.

func GetReplayURLs

func GetReplayURLs(graph *CrawlGraph, cfg ReplayConfig, logger *slog.Logger) ([]string, error)

GetReplayURLs returns the list of URLs to re-crawl based on the strategy.

Types

type CrawlGraph

type CrawlGraph struct {
	// contains filtered or unexported fields
}

CrawlGraph is a thread-safe directed graph with SQLite persistence.

func New

func New(dbPath string, logger *slog.Logger) (*CrawlGraph, error)

New creates or opens a crawl graph database.

func (*CrawlGraph) AddEdge

func (g *CrawlGraph) AddEdge(edge Edge) error

AddEdge adds a link between two URLs.

func (*CrawlGraph) AddNode

func (g *CrawlGraph) AddNode(node Node) error

AddNode adds or updates a node in the graph.

func (*CrawlGraph) Close

func (g *CrawlGraph) Close() error

Close closes the graph database.

func (*CrawlGraph) ExportCSV

func (g *CrawlGraph) ExportCSV() (string, error)

ExportCSV exports nodes as CSV.

func (*CrawlGraph) ExportDOT

func (g *CrawlGraph) ExportDOT() (string, error)

ExportDOT exports the graph in GraphViz DOT format.

func (*CrawlGraph) ExportJSON

func (g *CrawlGraph) ExportJSON() ([]byte, error)

ExportJSON exports the graph as JSON.

func (*CrawlGraph) ExportMermaid

func (g *CrawlGraph) ExportMermaid() (string, error)

ExportMermaid exports the graph in Mermaid format.

func (*CrawlGraph) GetEdges

func (g *CrawlGraph) GetEdges() ([]Edge, error)

GetEdges retrieves all edges for the current session.

func (*CrawlGraph) GetFailedNodes

func (g *CrawlGraph) GetFailedNodes() ([]Node, error)

GetFailedNodes retrieves nodes with errors or non-2xx status.

func (*CrawlGraph) GetNodes

func (g *CrawlGraph) GetNodes() ([]Node, error)

GetNodes retrieves all nodes for the current session.

func (*CrawlGraph) GetNodesByDepth

func (g *CrawlGraph) GetNodesByDepth(depth int) ([]Node, error)

GetNodesByDepth retrieves nodes at a specific depth.

func (*CrawlGraph) SessionID

func (g *CrawlGraph) SessionID() string

SessionID returns the current session ID.

func (*CrawlGraph) StartSession

func (g *CrawlGraph) StartSession(seedURL string) error

StartSession records a new crawl session.

func (*CrawlGraph) Stats

func (g *CrawlGraph) Stats() (GraphStats, error)

Stats returns summary statistics for the current session.

type Edge

type Edge struct {
	FromURL  string `json:"from_url"`
	ToURL    string `json:"to_url"`
	LinkText string `json:"link_text"`
}

Edge represents a link between two nodes.

type GraphStats

type GraphStats struct {
	NodeCount    int   `json:"node_count"`
	EdgeCount    int   `json:"edge_count"`
	SuccessCount int   `json:"success_count"`
	ErrorCount   int   `json:"error_count"`
	MaxDepth     int   `json:"max_depth"`
	TotalTimeMs  int64 `json:"total_time_ms"`
	TotalItems   int   `json:"total_items"`
}

GraphStats holds summary statistics for a crawl graph.

type Node

type Node struct {
	ID           string    `json:"id"`
	URL          string    `json:"url"`
	StatusCode   int       `json:"status_code"`
	ContentType  string    `json:"content_type"`
	Depth        int       `json:"depth"`
	CrawlTimeMs  int64     `json:"crawl_time_ms"`
	ContentHash  string    `json:"content_hash"`
	ItemCount    int       `json:"item_count"`
	Error        string    `json:"error,omitempty"`
	ParentURL    string    `json:"parent_url,omitempty"`
	DiscoveredAt time.Time `json:"discovered_at"`
}

Node represents a single URL in the crawl graph.

type ReplayConfig

type ReplayConfig struct {
	Strategy    ReplayStrategy
	ReferenceDB string // Path to reference crawl graph DB.
	MaxDepth    int    // For depth strategy.
	URLPattern  string // For pattern strategy.
}

ReplayConfig configures a replay run.

type ReplayStrategy

type ReplayStrategy string

ReplayStrategy determines which URLs to re-crawl.

const (
	// ReplayErrors re-crawls nodes that had errors or non-2xx status codes.
	ReplayErrors ReplayStrategy = "errors"

	// ReplayChanged re-crawls nodes whose content hash differs from a reference session.
	ReplayChanged ReplayStrategy = "changed"

	// ReplayAll re-crawls all nodes from a previous session.
	ReplayAll ReplayStrategy = "all"

	// ReplayDepth re-crawls nodes at or below a specific depth.
	ReplayDepth ReplayStrategy = "depth"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL