<commit_before>package net.hawkengine.core.pipelinescheduler;

import net.hawkengine.core.utilities.constants.LoggerMessages;
import net.hawkengine.model.*;
import net.hawkengine.model.enums.*;
import net.hawkengine.services.AgentService;
import net.hawkengine.services.PipelineService;
import net.hawkengine.services.interfaces.IAgentService;
import net.hawkengine.services.interfaces.IPipelineService;
import net.hawkengine.ws.EndpointConnector;
import org.apache.log4j.Logger;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class StatusUpdaterService {
    private static final Logger LOGGER = Logger.getLogger(StatusUpdaterService.class.getName());
    private IAgentService agentService;
    private IPipelineService pipelineService;

    public StatusUpdaterService() {
        this.agentService = new AgentService();
        this.pipelineService = new PipelineService();
    }

    public StatusUpdaterService(IAgentService agentService, IPipelineService pipelineService) {
        this.agentService = agentService;
        this.pipelineService = pipelineService;
    }

    public void updateStatuses() {
        List<Agent> agents = (List<Agent>) this.agentService.getAll().getObject();
        for (Agent agent : agents) {
            this.updateAgentStatuses(agent);
        }

        List<Pipeline> pipelinesInProgress = (List<Pipeline>) this.pipelineService.getAllPreparedPipelinesInProgress().getObject();
        for (Pipeline pipeline : pipelinesInProgress) {
            if (pipeline.shouldBeCanceled()) {
                this.cancelPipeline(pipeline);
                LOGGER.info(String.format(LoggerMessages.PIPELINE_CANCELED, pipeline.getExecutionId(), pipeline.getPipelineDefinitionName()));
                ServiceResult result = new ServiceResult(null, NotificationType.WARNING, "Pipeline " + pipeline.getPipelineDefinitionName() + " was successfully canceled");
                EndpointConnector.passResultToEndpoint("NotificationService", "sendMessage", result);
            } else if (pipeline.getStatus() == PipelineStatus.PAUSED) {
                this.pausePipeline(pipeline);
            } else {
                this.updateAllStatuses(pipeline);
            }

            this.pipelineService.update(pipeline);
        }
    }

    public void updateAgentStatuses(Agent agent) {
        LocalDateTime lastReportedTime = agent.getLastReportedTime();
        LocalDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC).toLocalDateTime();
        long timeBetweenReports = ChronoUnit.SECONDS.between(lastReportedTime, currentTime);

        if ((timeBetweenReports > 12) && agent.isConnected()) {
            agent.setConnected(false);
            this.agentService.update(agent);
        }
    }
<commit_msg>Slight optimization of logic #234<commit_after>package net.hawkengine.core.pipelinescheduler;

import net.hawkengine.core.utilities.constants.LoggerMessages;
import net.hawkengine.model.*;
import net.hawkengine.model.enums.*;
import net.hawkengine.services.AgentService;
import net.hawkengine.services.PipelineService;
import net.hawkengine.services.interfaces.IAgentService;
import net.hawkengine.services.interfaces.IPipelineService;
import net.hawkengine.ws.EndpointConnector;
import org.apache.log4j.Logger;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class StatusUpdaterService {
    private static final Logger LOGGER = Logger.getLogger(StatusUpdaterService.class.getName());
    private IAgentService agentService;
    private IPipelineService pipelineService;

    public StatusUpdaterService() {
        this.agentService = new AgentService();
        this.pipelineService = new PipelineService();
    }

    public StatusUpdaterService(IAgentService agentService, IPipelineService pipelineService) {
        this.agentService = agentService;
        this.pipelineService = pipelineService;
    }

    public void updateStatuses() {
        List<Agent> agents = (List<Agent>) this.agentService.getAll().getObject();
        for (Agent agent : agents) {
            this.updateAgentStatus(agent);
        }

        List<Pipeline> pipelinesInProgress = (List<Pipeline>) this.pipelineService.getAllPreparedPipelinesInProgress().getObject();
        for (Pipeline pipeline : pipelinesInProgress) {
            if (pipeline.shouldBeCanceled()) {
                this.cancelPipeline(pipeline);
                LOGGER.info(String.format(LoggerMessages.PIPELINE_CANCELED, pipeline.getExecutionId(), pipeline.getPipelineDefinitionName()));
                ServiceResult result = new ServiceResult(null, NotificationType.WARNING, "Pipeline " + pipeline.getPipelineDefinitionName() + " was successfully canceled");
                EndpointConnector.passResultToEndpoint("NotificationService", "sendMessage", result);
            } else if (pipeline.getStatus() == PipelineStatus.PAUSED) {
                this.pausePipeline(pipeline);
            } else {
                this.updateAllStatuses(pipeline);
            }

            this.pipelineService.update(pipeline);
        }
    }

    public Agent updateAgentStatus(Agent agent) {
        if (agent != null) {
            LocalDateTime lastReportedTime = agent.getLastReportedTime();
            LocalDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC).toLocalDateTime();
            long timeBetweenReports = ChronoUnit.SECONDS.between(lastReportedTime, currentTime);

            if (agent.isConnected() && (timeBetweenReports > 12)) {
                agent.setConnected(false);
                this.agentService.update(agent);
            }
        }

        return agent;
    }

{'fraction_non_alphanumeric': 0.09627277285642655, 'fraction_numerical': 0.0015042620758816648, 'mean_word_length': 3.4857571214392804, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '537624', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1574, 'n_words': 279}