package knowledge import ( "fmt" "sort" "strings" "yetano.dev/yetano/core/solutions/catalog" "yetano.dev/yetano/core/solutions/ref" ) // Lint checks what catalog.Validate cannot: every yetano: reference // resolves (manual topics and sections included), and catalog-wide // advice (QS-03). It returns catalog.Validate's issues plus its own. func Lint(ix *Index) []catalog.Issue { c := ix.Cat issues := c.Validate() check := func(e *catalog.Entity, owner, r string) { res, err := ix.Resolve(r) switch { case err != nil: issues = append(issues, catalog.Issue{Severity: "error", File: e.File, Line: e.Line, ID: owner, Code: "reference", Message: err.Error()}) case !res.Exists: issues = append(issues, catalog.Issue{Severity: "error", File: e.File, Line: e.Line, ID: owner, Code: "reference", Message: fmt.Sprintf("%s does not resolve", r)}) case !res.SectionExists: issues = append(issues, catalog.Issue{Severity: "error", File: e.File, Line: e.Line, ID: owner, Code: "reference-section", Message: fmt.Sprintf("%s: the manual has no such section (renamed heading?)", r)}) } } for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} { for _, id := range catalog.SortedIDs(m) { s := m[id] for _, r := range RefsOf(s) { check(&s.Entity, id, r) } if s.Kind == catalog.KindSolution && len(c.Examples) > 0 { has := false for _, x := range c.Examples { has = has || x.Spec.Solution == id } if !has { issues = append(issues, catalog.Issue{Severity: "warning", File: s.File, ID: id, Code: "no-example", Message: "a solution should have at least one example (EX-01)"}) } } for _, q := range s.Spec.Questions { if q.Type == "select" && q.Decide == "" && q.Level != catalog.LevelAdvanced && len(q.Options) > 2 { issues = append(issues, catalog.Issue{Severity: "warning", File: s.File, ID: id + "." + q.ID, Code: "no-decide", Message: "a choice between several options should explain it (decide, HELP-01)"}) } } } } for _, id := range catalog.SortedIDs(c.Profiles) { p := c.Profiles[id] for _, r := range p.Spec.References { check(&p.Entity, id, r) } } for _, id := range ix.Missing { issues = append(issues, catalog.Issue{Severity: "warning", ID: id, Code: "source-missing", Message: "documents not checked out under sourcesRoot; its references are not checked"}) } return issues } // Coverage is the knowledge coverage report (COV-01), computed from data. type Coverage struct { Packages int `json:"packages"` Metrics []Metric `json:"metrics"` GoalsWithout []string `json:"goalsWithoutSolution"` ProgramsWithout []string `json:"bigPictureProgramsWithoutPackage"` ByPackage map[string]PackageCoverage `json:"byPackage"` } // Metric is one percentage. type Metric struct { Name string `json:"name"` Have int `json:"have"` Total int `json:"total"` Pct float64 `json:"percent"` } // PackageCoverage is what exists for one package. type PackageCoverage struct { Manual bool `json:"manual"` Microsite bool `json:"microsite"` Provides int `json:"capabilities"` Profiles int `json:"profiles"` Solutions int `json:"solutions"` Tasks int `json:"tasks"` } // ComputeCoverage builds the report. func ComputeCoverage(ix *Index) Coverage { c := ix.Cat cov := Coverage{ByPackage: map[string]PackageCoverage{}} for _, id := range catalog.SortedIDs(c.Packages) { p := c.Packages[id] pc := PackageCoverage{Microsite: p.Spec.Microsite != ""} for _, m := range p.Spec.Modules { pc.Provides += len(m.Provides) } for _, k := range c.Sources { pc.Manual = pc.Manual || k.Spec.Package == id } for _, pr := range c.Profiles { if pr.Meta.Package == id { pc.Profiles++ } } for _, s := range c.Solutions { for _, pu := range s.Spec.Packages { if pu.ID == id { pc.Solutions++ break } } } for _, s := range c.Tasks { for _, pu := range s.Spec.Packages { if pu.ID == id { pc.Tasks++ break } } } cov.ByPackage[id] = pc } cov.Packages = len(c.Packages) count := func(name string, f func(PackageCoverage) bool) { m := Metric{Name: name, Total: len(cov.ByPackage)} for _, pc := range cov.ByPackage { if f(pc) { m.Have++ } } cov.Metrics = append(cov.Metrics, pct(m)) } count("Packages with a manual source", func(p PackageCoverage) bool { return p.Manual }) count("Packages with a microsite", func(p PackageCoverage) bool { return p.Microsite }) count("Packages with capabilities", func(p PackageCoverage) bool { return p.Provides > 0 }) count("Packages with profiles", func(p PackageCoverage) bool { return p.Profiles > 0 }) count("Packages used by a solution", func(p PackageCoverage) bool { return p.Solutions > 0 }) count("Packages with a task", func(p PackageCoverage) bool { return p.Tasks > 0 }) // references refs, ok := 0, 0 for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} { for _, s := range m { for _, r := range RefsOf(s) { refs++ if res, err := ix.Resolve(r); err == nil && res.Exists && res.SectionExists { ok++ } } } } cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Valid references", Have: ok, Total: refs})) tested := 0 for _, x := range c.Examples { if x.Spec.Tested { tested++ } } cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Examples marked tested", Have: tested, Total: len(c.Examples)})) // translations of solution titles tr, total := 0, 0 for _, loc := range c.Spec.Locales { if loc == "en" { continue } for _, m := range []map[string]*catalog.Solution{c.Solutions, c.Tasks} { for _, s := range m { total++ if s.Meta.Translations[loc]["title"] != "" { tr++ } } } } cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Solution titles translated", Have: tr, Total: total})) if bp := c.BigPicture; bp != nil { used := map[string]bool{} for _, s := range c.Solutions { used[s.Spec.BigPicture] = true } goals := 0 for _, id := range catalog.SortedIDs(bp.Stories) { if used[id] { goals++ } else { cov.GoalsWithout = append(cov.GoalsWithout, id) } } cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Big Picture goals with a solution", Have: goals, Total: len(bp.Stories)})) pk := map[string]bool{} for _, p := range c.Packages { pk[p.Spec.BigPicture] = true } for _, id := range catalog.SortedIDs(bp.Products) { if !pk[id] { cov.ProgramsWithout = append(cov.ProgramsWithout, strings.TrimPrefix(id, "product.")) } } cov.Metrics = append(cov.Metrics, pct(Metric{Name: "Big Picture programs in the catalog", Have: len(bp.Products) - len(cov.ProgramsWithout), Total: len(bp.Products)})) } sort.SliceStable(cov.Metrics, func(i, j int) bool { return false }) return cov } func pct(m Metric) Metric { if m.Total > 0 { m.Pct = float64(int(float64(m.Have)*1000/float64(m.Total)+0.5)) / 10 } return m } var _ = ref.Solution