<commit_before>from typing import Optional

import logging

from .hook_sender import hook_sender
from .pipeline_execution import PipelineExecution


class PipelineStatus(object):

    def __init__(self, backend,
                 pipeline_id):
        self.backend = backend
        self.pipeline_id = pipeline_id
        self.__load()

    def init(self,
             pipeline_details,
             source_spec,
             validation_errors,
             cache_hash):
        self.pipeline_details = pipeline_details
        self.source_spec = source_spec
        self.validation_errors = validation_errors
        self.cache_hash = cache_hash
        self.backend.register_pipeline_id(self.pipeline_id)
        self.__save()

    def __load(self):
        data = self.backend.get_status('PipelineStatus:' + self.pipeline_id)
        if data is None:
            data = {}
        self.pipeline_details = data.get('pipeline_details', {})
        self.source_spec = data.get('source_spec', {})
        self.validation_errors = data.get('validation_errors', [])
        self.cache_hash = data.get('cache_hash', '')
        self.executions = [PipelineExecution.from_execution_id(self.backend, ex)
                           for ex in data.get('executions', [])]

    def __iter__(self):
        yield 'pipeline_details', self.pipeline_details,
        yield 'source_spec', self.source_spec,
        yield 'validation_errors', self.validation_errors
        yield 'cache_hash', self.cache_hash
        yield 'executions', [ex.execution_id for ex in self.executions]

    def __save(self):
        # logging.debug('SAVING PipelineStatus %s -> %r' % (self.pipeline_id, self.executions))
        self.backend.set_status('PipelineStatus:' + self.pipeline_id, dict(self))

    def dirty(self):
        return len(self.executions) == 0 or self.cache_hash != self.executions[0].cache_hash

    def errors(self):
        if not self.runnable():
            return ['%s :%s' % tuple(err)
                    for err in self.validation_errors]
        else:
            ex = self.get_last_execution()
            if ex is not None:
                return ex.error_log
        return []

    def runnable(self):
        return len(self.validation_errors) == 0

    def get_last_execution(self) -> Optional[PipelineExecution]:
        if len(self.executions) == 0:
            return None
        return self.executions[0]

    def get_last_successful_execution(self) -> Optional[PipelineExecution]:
        for ex in self.executions:
            if ex.success:
                return ex
        return None

    def queue_execution(self, execution_id, trigger):
        last_exec = self.get_last_execution()
        if last_exec.finish_time is None:<commit_msg>Fix test for status in queue_execution<commit_after>from typing import Optional

import logging

from .hook_sender import hook_sender
from .pipeline_execution import PipelineExecution


class PipelineStatus(object):

    def __init__(self, backend,
                 pipeline_id):
        self.backend = backend
        self.pipeline_id = pipeline_id
        self.__load()

    def init(self,
             pipeline_details,
             source_spec,
             validation_errors,
             cache_hash):
        self.pipeline_details = pipeline_details
        self.source_spec = source_spec
        self.validation_errors = validation_errors
        self.cache_hash = cache_hash
        self.backend.register_pipeline_id(self.pipeline_id)
        self.__save()

    def __load(self):
        data = self.backend.get_status('PipelineStatus:' + self.pipeline_id)
        if data is None:
            data = {}
        self.pipeline_details = data.get('pipeline_details', {})
        self.source_spec = data.get('source_spec', {})
        self.validation_errors = data.get('validation_errors', [])
        self.cache_hash = data.get('cache_hash', '')
        self.executions = [PipelineExecution.from_execution_id(self.backend, ex)
                           for ex in data.get('executions', [])]

    def __iter__(self):
        yield 'pipeline_details', self.pipeline_details,
        yield 'source_spec', self.source_spec,
        yield 'validation_errors', self.validation_errors
        yield 'cache_hash', self.cache_hash
        yield 'executions', [ex.execution_id for ex in self.executions]

    def __save(self):
        # logging.debug('SAVING PipelineStatus %s -> %r' % (self.pipeline_id, self.executions))
        self.backend.set_status('PipelineStatus:' + self.pipeline_id, dict(self))

    def dirty(self):
        return len(self.executions) == 0 or self.cache_hash != self.executions[0].cache_hash

    def errors(self):
        if not self.runnable():
            return ['%s :%s' % tuple(err)
                    for err in self.validation_errors]
        else:
            ex = self.get_last_execution()
            if ex is not None:
                return ex.error_log
        return []

    def runnable(self):
        return len(self.validation_errors) == 0

    def get_last_execution(self) -> Optional[PipelineExecution]:
        if len(self.executions) == 0:
            return None
        return self.executions[0]

    def get_last_successful_execution(self) -> Optional[PipelineExecution]:
        for ex in self.executions:
            if ex.success:
                return ex
        return None

    def queue_execution(self, execution_id, trigger):
        last_exec = self.get_last_execution()
        if last_exec is not None and last_exec.finish_time is None:
{'fraction_non_alphanumeric': 0.09518658734451055, 'fraction_numerical': 0.0018027762754642149, 'mean_word_length': 2.401594114040466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6952496', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1544, 'n_words': 345}