76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type HostRow struct {
|
|
ID int64
|
|
Hostname string
|
|
Protocol string
|
|
HtmlTitle string
|
|
IframeAllowed bool
|
|
BestIconHash string
|
|
RandomOrder float64
|
|
IconDownloadedAt *time.Time
|
|
}
|
|
|
|
// fetchHostsWithIcons gets hosts with icons, ordered by icon_downloaded_at for disk locality.
|
|
func fetchHostsWithIcons(ctx context.Context, pool *pgxpool.Pool, lastDownloaded *time.Time, lastID int64, limit int) ([]HostRow, error) {
|
|
var query string
|
|
var args []any
|
|
if lastDownloaded == nil && lastID == 0 {
|
|
query = `SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), best_icon_hash, random_order, icon_downloaded_at
|
|
FROM hosts WHERE html_title IS NOT NULL AND icon_downloaded_at IS NOT NULL
|
|
ORDER BY icon_downloaded_at, id LIMIT $1`
|
|
args = []any{limit}
|
|
} else {
|
|
query = `SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), best_icon_hash, random_order, icon_downloaded_at
|
|
FROM hosts WHERE html_title IS NOT NULL AND icon_downloaded_at IS NOT NULL
|
|
AND (icon_downloaded_at > $1 OR (icon_downloaded_at = $1 AND id > $2))
|
|
ORDER BY icon_downloaded_at, id LIMIT $3`
|
|
args = []any{lastDownloaded, lastID, limit}
|
|
}
|
|
rows, err := pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var hosts []HostRow
|
|
for rows.Next() {
|
|
var h HostRow
|
|
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash, &h.RandomOrder, &h.IconDownloadedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
hosts = append(hosts, h)
|
|
}
|
|
return hosts, rows.Err()
|
|
}
|
|
|
|
// fetchHostsWithoutIcons gets hosts without icons, ordered by id.
|
|
func fetchHostsWithoutIcons(ctx context.Context, pool *pgxpool.Pool, lastID int64, limit int) ([]HostRow, error) {
|
|
rows, err := pool.Query(ctx, `
|
|
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), '', random_order, NULL::timestamptz
|
|
FROM hosts
|
|
WHERE html_title IS NOT NULL AND icon_downloaded_at IS NULL AND id > $1
|
|
ORDER BY id LIMIT $2
|
|
`, lastID, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var hosts []HostRow
|
|
for rows.Next() {
|
|
var h HostRow
|
|
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash, &h.RandomOrder, &h.IconDownloadedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
hosts = append(hosts, h)
|
|
}
|
|
return hosts, rows.Err()
|
|
}
|