6a12196bf3
build / build (push) Successful in 1m51s
- 页面 9 个出口统一走 reconciliationMessages:四个内部码不再进入 DOM,未知码回退中文;批次行/阶段码同样翻译。 - 绑定页加载与点击前读 /api/v1/kb-bindings 预检:测试环境未配置当场提示且不发 POST;未启用优先于绑定缺失。 - 运行时读按上游 401/403 与连接失败拆成 runtime_key_forbidden / runtime_unreachable,保留 404 与未配置语义。 - EnvironmentLabel 与前端环境标签改「测试/生产知识库」,不再出现 090。 - 验:make verify 17 项全绿;go test 三包、web 656 用例、typecheck 通过。
143 lines
4.5 KiB
Go
143 lines
4.5 KiB
Go
package reconciliation
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
type RuntimeConfig struct {
|
|
BaseURL func(context.Context) string
|
|
APIKey string
|
|
Timeout time.Duration
|
|
AllowDockerHTTP bool
|
|
AllowPrivateHTTP bool
|
|
Now func() time.Time
|
|
}
|
|
|
|
type RuntimeProvider struct {
|
|
baseURL func(context.Context) string
|
|
apiKey string
|
|
timeout time.Duration
|
|
allowDockerHTTP bool
|
|
allowPrivateHTTP bool
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewRuntimeProvider(config RuntimeConfig) *RuntimeProvider {
|
|
if config.Now == nil {
|
|
config.Now = time.Now
|
|
}
|
|
return &RuntimeProvider{
|
|
baseURL: config.BaseURL, apiKey: strings.TrimSpace(config.APIKey), timeout: config.Timeout,
|
|
allowDockerHTTP: config.AllowDockerHTTP, allowPrivateHTTP: config.AllowPrivateHTTP, now: config.Now,
|
|
}
|
|
}
|
|
|
|
func (p *RuntimeProvider) Available() bool {
|
|
return p != nil && p.apiKey != "" && p.baseURL != nil
|
|
}
|
|
|
|
func (p *RuntimeProvider) client(ctx context.Context) (*weknora.RuntimeClient, error) {
|
|
if !p.Available() {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
baseURL := strings.TrimSpace(p.baseURL(ctx))
|
|
if baseURL == "" {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
client, err := weknora.NewRuntimeClient(weknora.RuntimeClientConfig{
|
|
BaseURL: baseURL,
|
|
APIKey: p.apiKey,
|
|
Timeout: p.timeout,
|
|
AllowDockerHTTP: p.allowDockerHTTP,
|
|
AllowPrivateHTTP: p.allowPrivateHTTP,
|
|
})
|
|
if err != nil {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
return client, nil
|
|
}
|
|
|
|
func (p *RuntimeProvider) Queues(ctx context.Context) (*governance.WeKnoraQueueSummary, error) {
|
|
client, err := p.client(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// The overview must not hang for the full business timeout just because the
|
|
// queue endpoint is slow; a short probe is enough to decide null vs data.
|
|
queueCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
queues, err := client.Queues(queueCtx)
|
|
if err != nil {
|
|
return nil, ErrRuntimeUnavailable
|
|
}
|
|
summary := &governance.WeKnoraQueueSummary{
|
|
Available: queues.Available, QueuesError: queues.QueuesError, WorkersError: queues.WorkersError,
|
|
Queues: make([]governance.WeKnoraQueue, 0, len(queues.Queues)), Timestamp: queues.Timestamp,
|
|
}
|
|
for _, queue := range queues.Queues {
|
|
summary.Queues = append(summary.Queues, governance.WeKnoraQueue{
|
|
Name: queue.Name, Pool: queue.Pool, Pending: queue.Pending, Active: queue.Active,
|
|
Scheduled: queue.Scheduled, Retry: queue.Retry, Archived: queue.Archived,
|
|
Processed: queue.Processed, Failed: queue.Failed,
|
|
})
|
|
}
|
|
return summary, nil
|
|
}
|
|
|
|
func (p *RuntimeProvider) DocumentProgress(ctx context.Context, documentID string) (governance.ReconciliationProgress, error) {
|
|
client, err := p.client(ctx)
|
|
if err != nil {
|
|
return governance.ReconciliationProgress{}, err
|
|
}
|
|
detail, err := client.DocumentProgress(ctx, documentID)
|
|
if err != nil {
|
|
switch {
|
|
case weknora.IsNotFound(err):
|
|
return governance.ReconciliationProgress{}, err
|
|
case weknora.StatusCode(err) == http.StatusUnauthorized || weknora.StatusCode(err) == http.StatusForbidden:
|
|
return governance.ReconciliationProgress{}, ErrRuntimeForbidden
|
|
default:
|
|
return governance.ReconciliationProgress{}, ErrRuntimeUnreachable
|
|
}
|
|
}
|
|
progress := governance.ReconciliationProgress{
|
|
DocumentID: detail.Document.ID, ParseStatus: detail.Document.ParseStatus,
|
|
IngestionStatus: detail.Document.IngestionStatus, WikiStatus: detail.Document.WikiStatus,
|
|
HasMoreFailures: detail.HasMoreFailures, ObservedAt: p.now().UTC(),
|
|
Stages: make([]governance.ReconciliationStage, 0, len(detail.Document.Stages)),
|
|
Failures: make([]governance.ReconciliationFailure, 0, len(detail.Failures)),
|
|
}
|
|
if progress.DocumentID == "" {
|
|
progress.DocumentID = documentID
|
|
}
|
|
for _, stage := range detail.Document.Stages {
|
|
progress.Stages = append(progress.Stages, governance.ReconciliationStage{
|
|
Name: stage.Name, Status: stage.Status, ErrorCode: stage.ErrorCode, FinishedAt: stage.FinishedAt,
|
|
})
|
|
}
|
|
for _, failure := range detail.Failures {
|
|
progress.Failures = append(progress.Failures, governance.ReconciliationFailure{
|
|
TaskType: failure.TaskType, FailCount: failure.FailCount, FailedAt: failure.FailedAt,
|
|
LastError: truncateRunes(failure.LastError, 500),
|
|
})
|
|
}
|
|
return progress, nil
|
|
}
|
|
|
|
func truncateRunes(value string, limit int) string {
|
|
if limit <= 0 {
|
|
return ""
|
|
}
|
|
runes := []rune(value)
|
|
if len(runes) <= limit {
|
|
return value
|
|
}
|
|
return string(runes[:limit])
|
|
}
|