<commit_before>package core

import (
	"github.com/af83/edwig/logger"
	"github.com/af83/edwig/model"
)

type StopVisitUpdateSubscriber func(model.StopVisitUpdateEvent)

type CollectManagerInterface interface {
	UpdateStopArea(request *StopAreaUpdateRequest)
}

type CollectManager struct {
	partners              Partners
	stopVisitUpdateEvents chan model.StopVisitUpdateEvent
}

type TestCollectManager struct {
	Done   chan bool
	Events []*model.StopAreaUpdateEvent
}

func NewTestCollectManager() CollectManagerInterface {
	return &TestCollectManager{
		Done: make(chan bool, 1),
	}
}

func (manager *TestCollectManager) UpdateStopArea(request *StopAreaUpdateRequest) {
	event := &model.StopAreaUpdateEvent{}
	manager.Events = append(manager.Events, event)

	manager.Done <- true
}

func NewCollectManager(partners Partners) CollectManagerInterface {
	return &CollectManager{
		partners:              partners,
		stopVisitUpdateEvents: make(chan model.StopVisitUpdateEvent),
	}
}

func (manager *CollectManager) HandleStopVisitUpdateEvent(stopVisitUpdateSubscriber StopVisitUpdateSubscriber) {
	for stopVisitUpdateEvent := range manager.stopVisitUpdateEvents {
		stopVisitUpdateSubscriber(stopVisitUpdateEvent)
	}
}

func (manager *CollectManager) UpdateStopArea(request *StopAreaUpdateRequest) {
	partner := manager.bestPartner(request)
	if partner == nil {
		logger.Log.Debugf("Can't find a partner for StopArea %v", request.StopAreaId())
		return
	}

	event, err := manager.requestStopAreaUpdate(partner, request)
	if err != nil {
		logger.Log.Printf("Can't request stop area update : %v", err)
		return
	}

	for _, stopVisitUpdateEvent := range event.StopVisitUpdateEvents {
		manager.stopVisitUpdateEvents <- *stopVisitUpdateEvent
	}
}

func (manager *CollectManager) bestPartner(request *StopAreaUpdateRequest) *Partner {
	var testPartner *Partner
	for _, partner := range manager.partners.FindAll() {
		if partner.isConnectorDefined(SIRI_STOP_MONITORING_REQUEST_COLLECTOR) {
			return partner
		} else if partner.isConnectorDefined(TEST_STOP_MONITORING_REQUEST_COLLECTOR) {
			testPartner = partner
		}
	}
	if testPartner != nil {
		return testPartner
	}
	return nil
}

func (manager *CollectManager) requestStopAreaUpdate(partner *Partner, request *StopAreaUpdateRequest) (*model.StopAreaUpdateEvent, error) {
	logger.Log.Debugf("RequestStopAreaUpdate %#v", request)

	event, err := partner.StopMonitoringRequestCollector().RequestStopAreaUpdate(request)
	if err != nil {
		return nil, err
	}
	return event, nil
}
<commit_msg>Use simple StopVisitUpdateSubscriber array to broadcast StopVisitUpdateEvent in CollectManager. Refs #2180<commit_after>package core

import (
	"github.com/af83/edwig/logger"
	"github.com/af83/edwig/model"
)

type StopVisitUpdateSubscriber func(*model.StopVisitUpdateEvent)

type CollectManagerInterface interface {
	UpdateStopArea(request *StopAreaUpdateRequest)
}

type CollectManager struct {
	partners                   Partners
	stopVisitUpdateSubscribers []StopVisitUpdateSubscriber
}

type TestCollectManager struct {
	Done   chan bool
	Events []*model.StopAreaUpdateEvent
}

func NewTestCollectManager() CollectManagerInterface {
	return &TestCollectManager{
		Done: make(chan bool, 1),
	}
}

func (manager *TestCollectManager) UpdateStopArea(request *StopAreaUpdateRequest) {
	event := &model.StopAreaUpdateEvent{}
	manager.Events = append(manager.Events, event)

	manager.Done <- true
}

func NewCollectManager(partners Partners) CollectManagerInterface {
	return &CollectManager{
		partners:                   partners,
		stopVisitUpdateSubscribers: make([]StopVisitUpdateSubscriber, 0),
	}
}

func (manager *CollectManager) HandleStopVisitUpdateEvent(stopVisitUpdateSubscriber StopVisitUpdateSubscriber) {
	manager.stopVisitUpdateSubscribers = append(manager.stopVisitUpdateSubscribers, stopVisitUpdateSubscriber)
}

func (manager *CollectManager) broadcastStopVisitUpdateEvent(event *model.StopVisitUpdateEvent) {
	for _, stopVisitUpdateSubscriber := range manager.stopVisitUpdateSubscribers {
		stopVisitUpdateSubscriber(event)
	}
}

func (manager *CollectManager) UpdateStopArea(request *StopAreaUpdateRequest) {
	partner := manager.bestPartner(request)
	if partner == nil {
		logger.Log.Debugf("Can't find a partner for StopArea %v", request.StopAreaId())
		return
	}

	event, err := manager.requestStopAreaUpdate(partner, request)
	if err != nil {
		logger.Log.Printf("Can't request stop area update : %v", err)
		return
	}

	for _, stopVisitUpdateEvent := range event.StopVisitUpdateEvents {
		manager.broadcastStopVisitUpdateEvent(stopVisitUpdateEvent)
	}
}

func (manager *CollectManager) bestPartner(request *StopAreaUpdateRequest) *Partner {
	var testPartner *Partner
	for _, partner := range manager.partners.FindAll() {
		if partner.isConnectorDefined(SIRI_STOP_MONITORING_REQUEST_COLLECTOR) {
			return partner
		} else if partner.isConnectorDefined(TEST_STOP_MONITORING_REQUEST_COLLECTOR) {
			testPartner = partner
		}
	}
	if testPartner != nil {
		return testPartner
	}
	return nil
}

func (manager *CollectManager) requestStopAreaUpdate(partner *Partner, request *StopAreaUpdateRequest) (*model.StopAreaUpdateEvent, error) {
	logger.Log.Debugf("RequestStopAreaUpdate %#v", request)

	event, err := partner.StopMonitoringRequestCollector().RequestStopAreaUpdate(request)
	if err != nil {
		return nil, err
	}
	return event, nil
}

{'fraction_non_alphanumeric': 0.0891566265060241, 'fraction_numerical': 0.0027803521779425394, 'mean_word_length': 5.909090909090909, 'pattern_counts': {'":': 0, '<': 6, '<?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': '1644844', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1606, 'n_words': 344}