<commit_before>package empire

import (
	"database/sql"
	"fmt"
	"strings"
	"time"

	"github.com/coreos/go-etcd/etcd"
	"gopkg.in/gorp.v1"
)

type Domain struct {
	ID        string    `db:"id"`
	AppName   string    `db:"app_id"`
	Hostname  string    `db:"hostname"`
	CreatedAt time.Time `db:"created_at"`
}

// PreInsert implements a pre insert hook for the db interface
func (d *Domain) PreInsert(s gorp.SqlExecutor) error {
	d.CreatedAt = Now()
	return nil
}

type domainRegistry interface {
	Register(*Domain) error
	Unregister(*Domain) error
}

func newDomainRegistry(urls string) domainRegistry {
	if urls == "fake" {
		return &fakeDomainRegistry{}
	}

	return &etcdDomainRegistry{client: etcd.NewClient(strings.Split(urls, ","))}
}

type fakeDomainRegistry struct{}

func (r *fakeDomainRegistry) Register(domain *Domain) error {
	return nil
}

func (r *fakeDomainRegistry) Unregister(domain *Domain) error {
	return nil
}

type etcdDomainRegistry struct {
	client *etcd.Client
}

func (r *etcdDomainRegistry) Register(domain *Domain) error {
	_, err := r.client.Set(r.key(domain.AppName, domain.Hostname), domain.Hostname, 0)
	return err
}

func (r *etcdDomainRegistry) Unregister(domain *Domain) error {
	_, err := r.client.Delete(r.key(domain.AppName, domain.Hostname), false)
	return err
}

func (r *etcdDomainRegistry) key(app, host string) string {
	return fmt.Sprintf("/empire/domains/%s/%s", app, host)
}

type domainsService struct {
	store    *store
	registry domainRegistry
}

func (s *domainsService) DomainsCreate(domain *Domain) (*Domain, error) {<commit_msg>Add some extra logging when unable to connect to etcd<commit_after>package empire

import (
	"database/sql"
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/coreos/go-etcd/etcd"
	"gopkg.in/gorp.v1"
)

type Domain struct {
	ID        string    `db:"id"`
	AppName   string    `db:"app_id"`
	Hostname  string    `db:"hostname"`
	CreatedAt time.Time `db:"created_at"`
}

// PreInsert implements a pre insert hook for the db interface
func (d *Domain) PreInsert(s gorp.SqlExecutor) error {
	d.CreatedAt = Now()
	return nil
}

type domainRegistry interface {
	Register(*Domain) error
	Unregister(*Domain) error
}

func newDomainRegistry(urls string) domainRegistry {
	if urls == "fake" {
		return &fakeDomainRegistry{}
	}

	return &etcdDomainRegistry{client: etcd.NewClient(strings.Split(urls, ","))}
}

type fakeDomainRegistry struct{}

func (r *fakeDomainRegistry) Register(domain *Domain) error {
	return nil
}

func (r *fakeDomainRegistry) Unregister(domain *Domain) error {
	return nil
}

type etcdDomainRegistry struct {
	client *etcd.Client
}

func (r *etcdDomainRegistry) Register(domain *Domain) error {
	_, err := r.client.Set(r.key(domain.AppName, domain.Hostname), domain.Hostname, 0)
	return r.handleError(err)
}

func (r *etcdDomainRegistry) Unregister(domain *Domain) error {
	_, err := r.client.Delete(r.key(domain.AppName, domain.Hostname), false)
	return r.handleError(err)
}

func (r *etcdDomainRegistry) key(app, host string) string {
	return fmt.Sprintf("/empire/domains/%s/%s", app, host)
}

func (r *etcdDomainRegistry) handleError(err error) error {
	if e, ok := err.(etcd.EtcdError); ok && e.ErrorCode == etcd.ErrCodeEtcdNotReachable {
		log.Printf("Unable to connect to etcd. Cluster: %s", strings.Join(r.client.GetCluster(), ","))
	}
	return err
}

type domainsService struct {
	store    *store
	registry domainRegistry
}

func (s *domainsService) DomainsCreate(domain *Domain) (*Domain, error) {
{'fraction_non_alphanumeric': 0.1297622457748496, 'fraction_numerical': 0.0011458034947006588, 'mean_word_length': 4.8006644518272426, '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': '4846805', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1198, 'n_words': 307}