<commit_before>package alien4cloud.paas.cloudify3.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.xml.bind.DatatypeConverter;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import alien4cloud.paas.cloudify3.model.CloudifyLifeCycle;
import alien4cloud.paas.cloudify3.model.Event;
import alien4cloud.paas.cloudify3.model.EventAlienPersistent;
import alien4cloud.paas.cloudify3.model.EventAlienWorkflow;
import alien4cloud.paas.cloudify3.model.EventAlienWorkflowStarted;
import alien4cloud.paas.cloudify3.model.EventType;
import alien4cloud.paas.cloudify3.model.NodeInstance;
import alien4cloud.paas.cloudify3.model.NodeInstanceStatus;
import alien4cloud.paas.cloudify3.model.Workflow;
import alien4cloud.paas.cloudify3.restclient.DeploymentEventClient;
import alien4cloud.paas.cloudify3.restclient.NodeInstanceClient;
import alien4cloud.paas.model.AbstractMonitorEvent;
import alien4cloud.paas.model.DeploymentStatus;
import alien4cloud.paas.model.PaaSDeploymentStatusMonitorEvent;
import alien4cloud.paas.model.PaaSInstancePersistentResourceMonitorEvent;
import alien4cloud.paas.model.PaaSInstanceStateMonitorEvent;
import alien4cloud.paas.model.PaaSTopologyDeploymentContext;
import alien4cloud.paas.model.PaaSWorkflowMonitorEvent;
import alien4cloud.paas.model.PaaSWorkflowStepMonitorEvent;
import alien4cloud.utils.MapUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

/**
 * Handle cloudify 3 events request
 */
@Component("cloudify-event-service")
@Slf4j
public class EventService {

    @Resource
    private DeploymentEventClient eventClient;
    @Resource
    private NodeInstanceClient nodeInstanceClient;
    /**
     * Hold last event ids
     */
    private Set<String> lastEvents;
    private long lastRequestedTimestamp;

    @Resource
    private ScalableComputeReplacementService scalableComputeReplacementService;

    // TODO : May manage in a better manner this kind of state
    private Map<String, String> paaSDeploymentIdToAlienDeploymentIdMapping = Maps.newConcurrentMap();

    private Map<String, String> alienDeploymentIdToPaaSDeploymentIdMapping = Maps.newConcurrentMap();

    public void init(Map<String, PaaSTopologyDeploymentContext> activeDeploymentContexts) {
        for (Map.Entry<String, PaaSTopologyDeploymentContext> activeDeploymentContextEntry : activeDeploymentContexts.entrySet()) {
            paaSDeploymentIdToAlienDeploymentIdMapping.put(activeDeploymentContextEntry.getKey(), activeDeploymentContextEntry.getValue().getDeploymentId());
            alienDeploymentIdToPaaSDeploymentIdMapping.put(activeDeploymentContextEntry.getValue().getDeploymentId(), activeDeploymentContextEntry.getKey());
        }
    }

    /**
     * This queue is used for internal events
     */
    private List<AbstractMonitorEvent> internalProviderEventsQueue = Lists.newLinkedList();

    private static final long delay = 30 * 1000L;

    public synchronized ListenableFuture<AbstractMonitorEvent[]> getEventsSince(final Date lastTimestamp, int batchSize) {
        // TODO Workaround as cloudify 3 seems do not respect appearance order of event based on timestamp
        Date requestTimestamp = new Date(lastTimestamp.getTime());
        if (lastEvents != null) {
            requestTimestamp.setTime(requestTimestamp.getTime() - delay);
        } else {
            lastEvents = Sets.newConcurrentHashSet();
        }
        // Process internal events
        final ListenableFuture<AbstractMonitorEvent[]> internalEvents = processInternalQueue(batchSize);
        if (internalEvents != null) {
            // Deliver internal events first, next time when Alien poll, we'll deliver cloudify events
            return internalEvents;
        }
        // Try to get events from cloudify
        ListenableFuture<Event[]> eventsFuture;
        // If the request is on the same timestamp then iterate from the last event size
        // TODO It's like a queue consumption and it's really ugly
        if (lastRequestedTimestamp == lastTimestamp.getTime()) {
            eventsFuture = eventClient.asyncGetBatch(null, requestTimestamp, lastEvents.size(), batchSize);
        } else {
            eventsFuture = eventClient.asyncGetBatch(null, requestTimestamp, 0, batchSize);
        }
        Function<Event[], AbstractMonitorEvent[]> cloudify3ToAlienEventsAdapter = new Function<Event[], AbstractMonitorEvent[]>() {
            @Override
            public AbstractMonitorEvent[] apply(Event[] cloudifyEvents) {
                // Convert cloudify events to alien events
                List<Event> eventsAfterFiltering = Lists.newArrayList();
                for (Event cloudifyEvent : cloudifyEvents) {
                    if (!lastEvents.contains(cloudifyEvent.getId())) {
                        eventsAfterFiltering.add(cloudifyEvent);
                    } else if (log.isDebugEnabled()) {
                        log.debug("Filtering event " + cloudifyEvent.getId() + ", last events size " + lastEvents.size());
                    }
                }
                if (lastRequestedTimestamp != lastTimestamp.getTime()) {
                    // Only clear last events if the last requested timestamp has changed
                    lastEvents.clear();
                }
                lastRequestedTimestamp = lastTimestamp.getTime();
                for (Event cloudifyEvent : cloudifyEvents) {
                    lastEvents.add(cloudifyEvent.getId());
                }
                List<AbstractMonitorEvent> alienEvents = toAlienEvents(eventsAfterFiltering);
                return alienEvents.toArray(new AbstractMonitorEvent[alienEvents.size()]);
            }
        };
        return Futures.transform(eventsFuture, cloudify3ToAlienEventsAdapter);
    }

    public synchronized void registerDeployment(String deploymentPaaSId, String deploymentId) {
        paaSDeploymentIdToAlienDeploymentIdMapping.put(deploymentPaaSId, deploymentId);
        alienDeploymentIdToPaaSDeploymentIdMapping.put(deploymentId, deploymentPaaSId);
    }

    public synchronized void registerDeploymentEvent(String deploymentPaaSId, DeploymentStatus deploymentStatus) {
        if (paaSDeploymentIdToAlienDeploymentIdMapping.containsKey(deploymentPaaSId)) {
            PaaSDeploymentStatusMonitorEvent deploymentStatusMonitorEvent = new PaaSDeploymentStatusMonitorEvent();
            deploymentStatusMonitorEvent.setDeploymentStatus(deploymentStatus);
            deploymentStatusMonitorEvent.setDeploymentId(paaSDeploymentIdToAlienDeploymentIdMapping.get(deploymentPaaSId));
            internalProviderEventsQueue.add(deploymentStatusMonitorEvent);
        } else {
            log.warn("Notify new status {} for the deployment {} which is not registered by event service", deploymentStatus, deploymentPaaSId);
        }
    }

    /**
     * Register an event to be added to the queue to dispatch it to Alien 4 Cloud.
     *
     * @param event The event to be dispatched.
     */
    public synchronized void registerEvent(AbstractMonitorEvent event) {<commit_msg>ALIEN-1665 [1.2.0] As a USER I want to view only my deployment logs in the runtime view.<commit_after>package alien4cloud.paas.cloudify3.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.xml.bind.DatatypeConverter;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import alien4cloud.paas.cloudify3.model.*;
import alien4cloud.paas.cloudify3.restclient.DeploymentEventClient;
import alien4cloud.paas.cloudify3.restclient.NodeInstanceClient;
import alien4cloud.paas.model.*;
import alien4cloud.utils.MapUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Handle cloudify 3 events request
 */
@Component("cloudify-event-service")
@Slf4j
public class EventService {

    @Resource
    private DeploymentEventClient eventClient;
    @Resource
    private NodeInstanceClient nodeInstanceClient;
    /**
     * Hold last event ids
     */
    private Set<String> lastEvents;
    private long lastRequestedTimestamp;

    @Resource
    private ScalableComputeReplacementService scalableComputeReplacementService;

    // TODO : May manage in a better manner this kind of state
    private Map<String, String> paaSDeploymentIdToAlienDeploymentIdMapping = Maps.newConcurrentMap();

    private Map<String, String> alienDeploymentIdToPaaSDeploymentIdMapping = Maps.newConcurrentMap();

    public void init(Map<String, PaaSTopologyDeploymentContext> activeDeploymentContexts) {
        for (Map.Entry<String, PaaSTopologyDeploymentContext> activeDeploymentContextEntry : activeDeploymentContexts.entrySet()) {
            paaSDeploymentIdToAlienDeploymentIdMapping.put(activeDeploymentContextEntry.getKey(), activeDeploymentContextEntry.getValue().getDeploymentId());
            alienDeploymentIdToPaaSDeploymentIdMapping.put(activeDeploymentContextEntry.getValue().getDeploymentId(), activeDeploymentContextEntry.getKey());
        }
    }

    /**
     * This queue is used for internal events
     */
    private List<AbstractMonitorEvent> internalProviderEventsQueue = Lists.newLinkedList();

    private static final long delay = 30 * 1000L;

    public synchronized ListenableFuture<AbstractMonitorEvent[]> getEventsSince(final Date lastTimestamp, int batchSize) {
        // TODO Workaround as cloudify 3 seems do not respect appearance order of event based on timestamp
        Date requestTimestamp = new Date(lastTimestamp.getTime());
        if (lastEvents != null) {
            requestTimestamp.setTime(requestTimestamp.getTime() - delay);
        } else {
            lastEvents = Sets.newConcurrentHashSet();
        }
        // Process internal events
        final ListenableFuture<AbstractMonitorEvent[]> internalEvents = processInternalQueue(batchSize);
        if (internalEvents != null) {
            // Deliver internal events first, next time when Alien poll, we'll deliver cloudify events
            return internalEvents;
        }
        // Try to get events from cloudify
        ListenableFuture<Event[]> eventsFuture;
        // If the request is on the same timestamp then iterate from the last event size
        // TODO It's like a queue consumption and it's really ugly
        if (lastRequestedTimestamp == lastTimestamp.getTime()) {
            eventsFuture = eventClient.asyncGetBatch(null, requestTimestamp, lastEvents.size(), batchSize);
        } else {
            eventsFuture = eventClient.asyncGetBatch(null, requestTimestamp, 0, batchSize);
        }
        Function<Event[], AbstractMonitorEvent[]> cloudify3ToAlienEventsAdapter = new Function<Event[], AbstractMonitorEvent[]>() {
            @Override
            public AbstractMonitorEvent[] apply(Event[] cloudifyEvents) {
                // Convert cloudify events to alien events
                List<Event> eventsAfterFiltering = Lists.newArrayList();
                for (Event cloudifyEvent : cloudifyEvents) {
                    if (!lastEvents.contains(cloudifyEvent.getId())) {
                        eventsAfterFiltering.add(cloudifyEvent);
                    } else if (log.isDebugEnabled()) {
                        log.debug("Filtering event " + cloudifyEvent.getId() + ", last events size " + lastEvents.size());
                    }
                }
                if (lastRequestedTimestamp != lastTimestamp.getTime()) {
                    // Only clear last events if the last requested timestamp has changed
                    lastEvents.clear();
                }
                lastRequestedTimestamp = lastTimestamp.getTime();
                for (Event cloudifyEvent : cloudifyEvents) {
                    lastEvents.add(cloudifyEvent.getId());
                }
                List<AbstractMonitorEvent> alienEvents = toAlienEvents(eventsAfterFiltering);
                return alienEvents.toArray(new AbstractMonitorEvent[alienEvents.size()]);
            }
        };
        return Futures.transform(eventsFuture, cloudify3ToAlienEventsAdapter);
    }

    public synchronized void registerDeployment(String deploymentPaaSId, String deploymentId) {
        paaSDeploymentIdToAlienDeploymentIdMapping.put(deploymentPaaSId, deploymentId);
        alienDeploymentIdToPaaSDeploymentIdMapping.put(deploymentId, deploymentPaaSId);
    }

    public synchronized void registerDeploymentEvent(String deploymentPaaSId, DeploymentStatus deploymentStatus) {
        if (paaSDeploymentIdToAlienDeploymentIdMapping.containsKey(deploymentPaaSId)) {
            PaaSDeploymentStatusMonitorEvent deploymentStatusMonitorEvent = new PaaSDeploymentStatusMonitorEvent();
            deploymentStatusMonitorEvent.setDeploymentStatus(deploymentStatus);
            deploymentStatusMonitorEvent.setDeploymentId(paaSDeploymentIdToAlienDeploymentIdMapping.get(deploymentPaaSId));
            internalProviderEventsQueue.add(deploymentStatusMonitorEvent);
        } else {
            log.warn("Notify new status {} for the deployment {} which is not registered by event service", deploymentStatus, deploymentPaaSId);
        }
    }

    public synchronized String getDeploymentIdFromDeploymentPaaSId(String deploymentPaaSId) {
        return paaSDeploymentIdToAlienDeploymentIdMapping.get(deploymentPaaSId);
    }

    /**
     * Register an event to be added to the queue to dispatch it to Alien 4 Cloud.
     *
     * @param event The event to be dispatched.
     */
    public synchronized void registerEvent(AbstractMonitorEvent event) {
{'fraction_non_alphanumeric': 0.07923687508703524, 'fraction_numerical': 0.005570254839158892, 'mean_word_length': 3.510992462311558, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5217566', 'n_tokens_mistral': 3782, 'n_tokens_neox': 3677, 'n_words': 809}