Files
llm-intelligence/internal/collectors/collector.go
phamnazage-jpg 77e6610fd2
Some checks failed
CI / test (push) Has been cancelled
chore: prepare repository for publishing
2026-05-13 14:42:45 +08:00

130 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// internal/collectors/collector.go
// Collector 接口定义:所有数据源采集器的统一抽象
package collectors
import (
"context"
"time"
)
// Result 采集结果
type Result struct {
Models []ModelInfo
Meta CollectionMeta
}
// CollectionMeta 采集元信息
type CollectionMeta struct {
Source string
Count int
Duration time.Duration
Timestamp time.Time
BatchID string
CollectorVersion string
}
// ModelInfo 标准模型信息(与 fetch_openrouter.go 兼容)
type ModelInfo struct {
ID string
Name string
Provider string
ProviderID string
Version string
Modality string
ContextLength int
Capabilities []string
Pricing ModelPricing
Description string
IsFree bool
SourceURL string
}
// ModelPricing 标准定价信息
type ModelPricing struct {
Input float64
Output float64
}
// Collector 采集器接口
type Collector interface {
// Name 返回采集器名称
Name() string
// Collect 执行采集,返回标准模型列表
Collect(ctx context.Context) (Result, error)
// Schedule 返回推荐调度周期(如 "0 8 * * *"
Schedule() string
// Timeout 返回单次采集超时时间
Timeout() time.Duration
// RetryCount 返回最大重试次数
RetryCount() int
}
// BaseCollector 提供默认实现的嵌入类型
type BaseCollector struct {
name string
schedule string
timeout time.Duration
retryCount int
version string
}
func (b *BaseCollector) Name() string { return b.name }
func (b *BaseCollector) Schedule() string { return b.schedule }
func (b *BaseCollector) Timeout() time.Duration { return b.timeout }
func (b *BaseCollector) RetryCount() int { return b.retryCount }
func (b *BaseCollector) Version() string { return b.version }
// NewBaseCollector 创建基础采集器配置
func NewBaseCollector(name, schedule string, timeout time.Duration, retry int, version string) BaseCollector {
return BaseCollector{
name: name,
schedule: schedule,
timeout: timeout,
retryCount: retry,
version: version,
}
}
// CollectorRegistry 采集器注册表
type CollectorRegistry struct {
collectors map[string]Collector
}
// NewRegistry 创建采集器注册表
func NewRegistry() *CollectorRegistry {
return &CollectorRegistry{collectors: make(map[string]Collector)}
}
// Register 注册采集器
func (r *CollectorRegistry) Register(c Collector) {
r.collectors[c.Name()] = c
}
// Get 获取采集器
func (r *CollectorRegistry) Get(name string) (Collector, bool) {
c, ok := r.collectors[name]
return c, ok
}
// All 返回所有已注册采集器
func (r *CollectorRegistry) All() []Collector {
cs := make([]Collector, 0, len(r.collectors))
for _, c := range r.collectors {
cs = append(cs, c)
}
return cs
}
// Names 返回所有已注册采集器名称
func (r *CollectorRegistry) Names() []string {
names := make([]string, 0, len(r.collectors))
for n := range r.collectors {
names = append(names, n)
}
return names
}