<commit_before># Copyright (c) 2012 Samsung SDS Co., LTD
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import json
import os
import traceback
import time
from uuid import uuid4

from synaps.cep.storm import Spout, emit, log
from synaps.db import Cassandra
from synaps import flags
from synaps import log as logging
from synaps.rpc import CHECK_METRIC_ALARM_MSG_ID


LOG = logging.getLogger(__name__)
FLAGS = flags.FLAGS


class CheckSpout(Spout):
    SPOUT_NAME = "CheckSpout"
    lastchecked = 0
    
    def initialize(self, conf, context):
        self.pid = os.getpid()
        self.cass = Cassandra()
        self.nextTuple()
        self.delivery_tags = {}
        self.lastchecked = time.time()
    
    def log(self, msg):
        log("[%s:%d] %s" % (self.SPOUT_NAME, self.pid, msg))
        
    def tracelog(self, e):
        msg = traceback.format_exc(e)
        for line in msg.splitlines():
            self.log("TRACE: " + line)
    
    def nextTuple(self):
        now = time.time()
        if self.lastchecked == 0:
            self.lastchecked = now
        elif now - self.lastchecked >= 60: 
            self.lastchecked = now
            id = "periodic_%s" % str(uuid4())
            body = json.dumps({'message_id': CHECK_METRIC_ALARM_MSG_ID})
            message = "Periodic monitoring message sent [%s] %s"
            self.log(message % (id, body))
            emit([None, body], id=id)
        else:
            time.sleep(1)<commit_msg>Changed to evaluate alarms near the start of the period.<commit_after># Copyright (c) 2012 Samsung SDS Co., LTD
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from datetime import datetime
import json
import os
import traceback
import time
from uuid import uuid4

from synaps.cep.storm import Spout, emit, log
from synaps.db import Cassandra
from synaps import flags
from synaps import log as logging
from synaps.rpc import CHECK_METRIC_ALARM_MSG_ID


LOG = logging.getLogger(__name__)
FLAGS = flags.FLAGS


class CheckSpout(Spout):
    SPOUT_NAME = "CheckSpout"
    lastchecked = 0
    
    def initialize(self, conf, context):
        self.pid = os.getpid()
        self.cass = Cassandra()
        self.nextTuple()
        self.delivery_tags = {}
        self.lastchecked = self.get_now()
    
    def log(self, msg):
        log("[%s:%d] %s" % (self.SPOUT_NAME, self.pid, msg))
        
    def tracelog(self, e):
        msg = traceback.format_exc(e)
        for line in msg.splitlines():
            self.log("TRACE: " + line)

    def get_now(self):
        return datetime.utcnow().replace(second=0, microsecond=0)
    
    def nextTuple(self):
        now = self.get_now()
        
        if self.lastchecked != now:
            self.lastchecked = now
            id = "periodic_%s" % str(uuid4())
            body = json.dumps({'message_id': CHECK_METRIC_ALARM_MSG_ID})
            message = "Periodic monitoring message sent [%s] %s"
            self.log(message % (id, body))
            emit([None, body], id=id)
        else:
            time.sleep(1)
{'fraction_non_alphanumeric': 0.09537642217380779, 'fraction_numerical': 0.007020091987412249, 'mean_word_length': 2.58992180712424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5698694', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1218, 'n_words': 415}