<commit_before>package marathon

import (
	"net"
	"net/url"
	"strings"

	"github.com/x-cray/marathon-service-registrator/types"

	log "github.com/Sirupsen/logrus"
	marathon "github.com/x-cray/go-marathon"
)

type MarathonAdapter struct {
	client marathon.Marathon
}

func New(marathonUri string) (*MarathonAdapter, error) {
	config := marathon.NewDefaultConfig()
	config.URL = marathonUri
	config.EventsTransport = marathon.EVENTS_TRANSPORT_SSE

	log.WithField("prefix", "marathon").Infof("Connecting to Marathon at %v", marathonUri)
	client, err := marathon.NewClient(config)
	if err != nil {
		return nil, err
	}

	return &MarathonAdapter{client: client}, nil
}

func (m *MarathonAdapter) ListenForEvents() (types.EventsChannel, error) {
	update := make(marathon.EventsChannel, 5)
	result := make(types.EventsChannel, 5)
	if err := m.client.AddEventsListener(update, marathon.EVENTS_APPLICATIONS | marathon.EVENTS_SUBSCRIPTIONS); err != nil {
		return nil, err
	}

	// Convert Marathon events to abstract events and write to output channel.
	go func() {
		for {
			event := <-update
			log.WithFields(log.Fields{
				"prefix": "marathon",
				"event": event.Event,
			}).Debug("Received scheduler event")
			result <- &types.Event{
				ID:    event.ID,
				Name:  event.Name,
				Event: event.Event,
			}
		}
	}()

	return result, nil
}

func serviceFromTask(task *marathon.Task, port int, app *marathon.Application) (*types.Service, error) {
	taskIP, err := net.ResolveIPAddr("ip", task.Host)
	if err != nil {
		return nil, err
	}

	idTokens := strings.Split(app.ID, "/")
	name := idTokens[len(idTokens)-1]

	return &types.Service{
		ID:   task.ID,
		Name: name,
		IP:   taskIP.String(),
		Port: port,
	}, nil
}

func (m *MarathonAdapter) Services() ([]*types.Service, error) {
	params := make(url.Values)
	params.Add("embed", "apps.tasks")
	applications, err := m.client.Applications(params)
	if err != nil {
		return nil, err
	}

	result := make([]*types.Service, 0)
	for _, app := range applications.Apps {
		for _, task := range app.Tasks {
			for _, port := range task.Ports {
				service, err := serviceFromTask(task, port, &app)
				if err != nil {
					return nil, err
				}

				log.WithFields(log.Fields{
					"prefix": "marathon",
					"name":   service.Name,
					"ip":     service.IP,
					"port":   service.Port,
				}).Debugf("Service")
				result = append(result, service)
			}
		}
	}

	return result, nil
}
<commit_msg>Remove unnecessary log message<commit_after>package marathon

import (
	"net"
	"net/url"
	"strings"

	"github.com/x-cray/marathon-service-registrator/types"

	log "github.com/Sirupsen/logrus"
	marathon "github.com/x-cray/go-marathon"
)

type MarathonAdapter struct {
	client marathon.Marathon
}

func New(marathonUri string) (*MarathonAdapter, error) {
	config := marathon.NewDefaultConfig()
	config.URL = marathonUri
	config.EventsTransport = marathon.EVENTS_TRANSPORT_SSE

	log.WithField("prefix", "marathon").Infof("Connecting to Marathon at %v", marathonUri)
	client, err := marathon.NewClient(config)
	if err != nil {
		return nil, err
	}

	return &MarathonAdapter{client: client}, nil
}

func (m *MarathonAdapter) ListenForEvents() (types.EventsChannel, error) {
	update := make(marathon.EventsChannel, 5)
	result := make(types.EventsChannel, 5)
	if err := m.client.AddEventsListener(update, marathon.EVENTS_APPLICATIONS | marathon.EVENTS_SUBSCRIPTIONS); err != nil {
		return nil, err
	}

	// Convert Marathon events to abstract events and write to output channel.
	go func() {
		for {
			event := <-update
			result <- &types.Event{
				ID:    event.ID,
				Name:  event.Name,
				Event: event.Event,
			}
		}
	}()

	return result, nil
}

func serviceFromTask(task *marathon.Task, port int, app *marathon.Application) (*types.Service, error) {
	taskIP, err := net.ResolveIPAddr("ip", task.Host)
	if err != nil {
		return nil, err
	}

	idTokens := strings.Split(app.ID, "/")
	name := idTokens[len(idTokens)-1]

	return &types.Service{
		ID:   task.ID,
		Name: name,
		IP:   taskIP.String(),
		Port: port,
	}, nil
}

func (m *MarathonAdapter) Services() ([]*types.Service, error) {
	params := make(url.Values)
	params.Add("embed", "apps.tasks")
	applications, err := m.client.Applications(params)
	if err != nil {
		return nil, err
	}

	result := make([]*types.Service, 0)
	for _, app := range applications.Apps {
		for _, task := range app.Tasks {
			for _, port := range task.Ports {
				service, err := serviceFromTask(task, port, &app)
				if err != nil {
					return nil, err
				}

				log.WithFields(log.Fields{
					"prefix": "marathon",
					"name":   service.Name,
					"ip":     service.IP,
					"port":   service.Port,
				}).Debugf("Service")
				result = append(result, service)
			}
		}
	}

	return result, nil
}

{'fraction_non_alphanumeric': 0.14540121516865703, 'fraction_numerical': 0.0016760946993505132, 'mean_word_length': 3.671232876712329, 'pattern_counts': {'":': 10, '<': 7, '<?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': '5132885', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1640, 'n_words': 393}