101 lines
1.8 KiB
Go
101 lines
1.8 KiB
Go
package index
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/david/knox/internal/db"
|
|
)
|
|
|
|
type Reflector struct {
|
|
db *db.KnoxDB
|
|
}
|
|
|
|
func NewReflector(kdb *db.KnoxDB) *Reflector {
|
|
return &Reflector{db: kdb}
|
|
}
|
|
|
|
type ReflectionResult struct {
|
|
SessionID string
|
|
Project string
|
|
EntryCount int
|
|
Status string
|
|
}
|
|
|
|
func (r *Reflector) Reflect() ([]ReflectionResult, error) {
|
|
sessions, err := r.db.PendingSessions()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pending sessions: %w", err)
|
|
}
|
|
|
|
var results []ReflectionResult
|
|
for _, s := range sessions {
|
|
entries, err := r.db.EntriesByProject(s.Project, 100)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := ReflectionResult{
|
|
SessionID: s.SessionID,
|
|
Project: s.Project,
|
|
EntryCount: len(entries),
|
|
Status: "reflected",
|
|
}
|
|
|
|
if err := r.db.MarkSessionIndexed(s.SessionID); err != nil {
|
|
return nil, err
|
|
}
|
|
results = append(results, result)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
type GapReport struct {
|
|
Project string
|
|
TotalEntries int
|
|
}
|
|
|
|
func (r *Reflector) Gaps() ([]GapReport, error) {
|
|
entries, err := r.db.RecentEntries(500)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
projects := make(map[string]int)
|
|
for _, e := range entries {
|
|
if e.Project != "" {
|
|
projects[e.Project]++
|
|
}
|
|
}
|
|
|
|
var reports []GapReport
|
|
for proj, count := range projects {
|
|
reports = append(reports, GapReport{
|
|
Project: proj,
|
|
TotalEntries: count,
|
|
})
|
|
}
|
|
return reports, nil
|
|
}
|
|
|
|
func (r *Reflector) FormatResult(results []ReflectionResult) string {
|
|
var b strings.Builder
|
|
for _, res := range results {
|
|
b.WriteString(fmt.Sprintf(" %-12s %-20s %d entries [%s]\n",
|
|
truncateStr(res.SessionID, 12),
|
|
res.Project,
|
|
res.EntryCount,
|
|
res.Status,
|
|
))
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func truncateStr(s string, n int) string {
|
|
runes := []rune(s)
|
|
if len(runes) <= n {
|
|
return s
|
|
}
|
|
return string(runes[:n]) + "..."
|
|
}
|