New constructs an immutable Program snapshot from precompiled IR items.
(version string, revision uint64, metrics []string, charts []Chart)
| 29 | |
| 30 | // New constructs an immutable Program snapshot from precompiled IR items. |
| 31 | func New(version string, revision uint64, metrics []string, charts []Chart) (*Program, error) { |
| 32 | if version == "" { |
| 33 | return nil, fmt.Errorf("program: version is required") |
| 34 | } |
| 35 | |
| 36 | p := &Program{ |
| 37 | version: version, |
| 38 | revision: revision, |
| 39 | metrics: make(map[string]struct{}, len(metrics)), |
| 40 | charts: make([]Chart, 0, len(charts)), |
| 41 | chartByTmpl: make(map[string]int, len(charts)), |
| 42 | } |
| 43 | |
| 44 | for _, name := range metrics { |
| 45 | if name == "" { |
| 46 | return nil, fmt.Errorf("program: metric name cannot be empty") |
| 47 | } |
| 48 | if _, ok := p.metrics[name]; ok { |
| 49 | continue |
| 50 | } |
| 51 | p.metrics[name] = struct{}{} |
| 52 | p.metricNames = append(p.metricNames, name) |
| 53 | } |
| 54 | slices.Sort(p.metricNames) |
| 55 | |
| 56 | for i, chart := range charts { |
| 57 | if err := validateChart(chart); err != nil { |
| 58 | return nil, fmt.Errorf("program: chart[%d]: %w", i, err) |
| 59 | } |
| 60 | if _, exists := p.chartByTmpl[chart.TemplateID]; exists { |
| 61 | return nil, fmt.Errorf("program: duplicate chart template_id %q", chart.TemplateID) |
| 62 | } |
| 63 | |
| 64 | clone := chart.clone() |
| 65 | p.chartByTmpl[clone.TemplateID] = len(p.charts) |
| 66 | p.chartOrderID = append(p.chartOrderID, clone.TemplateID) |
| 67 | p.charts = append(p.charts, clone) |
| 68 | } |
| 69 | |
| 70 | return p, nil |
| 71 | } |
| 72 | |
| 73 | // Version returns the program schema/version marker. |
| 74 | func (p *Program) Version() string { return p.version } |