<commit_before>// Package catalog implements the service catalog handling immutable data: origins, sources and metrics.
package catalog

import (
	"log"
	"sync"
	"time"

	"github.com/facette/facette/pkg/config"
)

// Catalog represents the main structure of a catalog instance.
type Catalog struct {
	Config     *config.Config
	Origins    map[string]*Origin
	Updated    time.Time
	debugLevel int
}

const (
	_ = iota
	// OriginCmdRefresh represents an origin refresh command
	OriginCmdRefresh
	// OriginCmdShutdown represents an origin shutdown command
	OriginCmdShutdown
)

// NewCatalog creates a new instance of catalog.
func NewCatalog(config *config.Config, debugLevel int) *Catalog {
	return &Catalog{
		Config:     config,
		Origins:    make(map[string]*Origin),
		debugLevel: debugLevel,
	}
}

// GetMetric returns an existing metric entry based on its origin, source and name.
func (catalog *Catalog) GetMetric(origin, source, name string) *Metric {
	if _, ok := catalog.Origins[origin]; !ok {
		return nil
	} else if _, ok := catalog.Origins[origin].Sources[source]; !ok {
		return nil
	} else if _, ok := catalog.Origins[origin].Sources[source].Metrics[name]; !ok {
		return nil
	}

	return catalog.Origins[origin].Sources[source].Metrics[name]
}

// Refresh updates the current catalog by refreshing its origins.
func (catalog *Catalog) Refresh() error {
	var (
		origin *Origin
		err    error
	)

	log.Println("INFO: catalog refresh started")

	// Get origins from configuration
	catalog.Origins = make(map[string]*Origin)

	for originName, originConfig := range catalog.Config.Origins {
		origin, err = NewOrigin(originName, originConfig)
		if err != nil {
			log.Printf("ERROR: %s, discarding origin\n", err.Error())
			continue
		}

		origin.Catalog = catalog

		catalog.Origins[originName] = origin
	}
<commit_msg>Improve error message on NewOrigin() failure<commit_after>// Package catalog implements the service catalog handling immutable data: origins, sources and metrics.
package catalog

import (
	"log"
	"sync"
	"time"

	"github.com/facette/facette/pkg/config"
)

// Catalog represents the main structure of a catalog instance.
type Catalog struct {
	Config     *config.Config
	Origins    map[string]*Origin
	Updated    time.Time
	debugLevel int
}

const (
	_ = iota
	// OriginCmdRefresh represents an origin refresh command
	OriginCmdRefresh
	// OriginCmdShutdown represents an origin shutdown command
	OriginCmdShutdown
)

// NewCatalog creates a new instance of catalog.
func NewCatalog(config *config.Config, debugLevel int) *Catalog {
	return &Catalog{
		Config:     config,
		Origins:    make(map[string]*Origin),
		debugLevel: debugLevel,
	}
}

// GetMetric returns an existing metric entry based on its origin, source and name.
func (catalog *Catalog) GetMetric(origin, source, name string) *Metric {
	if _, ok := catalog.Origins[origin]; !ok {
		return nil
	} else if _, ok := catalog.Origins[origin].Sources[source]; !ok {
		return nil
	} else if _, ok := catalog.Origins[origin].Sources[source].Metrics[name]; !ok {
		return nil
	}

	return catalog.Origins[origin].Sources[source].Metrics[name]
}

// Refresh updates the current catalog by refreshing its origins.
func (catalog *Catalog) Refresh() error {
	var (
		origin *Origin
		err    error
	)

	log.Println("INFO: catalog refresh started")

	// Get origins from configuration
	catalog.Origins = make(map[string]*Origin)

	for originName, originConfig := range catalog.Config.Origins {
		origin, err = NewOrigin(originName, originConfig)
		if err != nil {
			log.Printf("ERROR: in origin `%s', %s", originName, err.Error())
			log.Printf("WARNING: discarding origin `%s'", originName)
			continue
		}

		origin.Catalog = catalog

		catalog.Origins[originName] = origin
	}

{'fraction_non_alphanumeric': 0.10502525923956395, 'fraction_numerical': 0.0, 'mean_word_length': 4.167582417582418, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4516460', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1141, 'n_words': 356}