<commit_before># Copyright 2016 - Alcatel-Lucent
# Copyright 2016 - Nokia
#
# 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 oslo_log import log
from oslo_service import service as os_service

from vitrage.common.constants import SyncMode

LOG = log.getLogger(__name__)


class SynchronizerService(os_service.Service):

    def __init__(self, conf, registered_plugins):
        super(SynchronizerService, self).__init__()
        self.conf = conf
        self.registered_plugins = registered_plugins

    def set_callback(self, callback_function):
        self.callback_function = callback_function


class SnapshotsService(SynchronizerService):

    def __init__(self, conf, registered_plugins):
        super(SnapshotsService, self).__init__(conf, registered_plugins)
        self.first_time = True

    def start(self):
        LOG.info("Start VitrageSnapshotsService")

        super(SnapshotsService, self).start()
        interval = self.conf.synchronizer.snapshots_interval
        self.tg.add_timer(interval, self._get_all)

        LOG.info("Finish start VitrageSnapshotsService")

    def stop(self, graceful=False):
        LOG.info("Stop VitrageSnapshotsService")

        super(SnapshotsService, self).stop()

        LOG.info("Finish stop VitrageSnapshotsService")

    def _get_all(self):
        sync_mode = SyncMode.INIT_SNAPSHOT \
            if self.first_time else SyncMode.SNAPSHOT
        LOG.debug("start get all with sync mode %s" % sync_mode)

        for plugin in self.registered_plugins.values():
            entities_dictionaries = plugin.get_all(sync_mode)
            for entity in entities_dictionaries:
                self.callback_function(entity)

        LOG.debug("end get all with sync mode %s" % sync_mode)
        self.first_time = False


class ChangesService(SynchronizerService):

    def __init__(self, conf, registered_plugins, changes_interval):
        super(ChangesService, self).__init__(conf, registered_plugins)
        self.changes_interval = changes_interval

    def start(self):
        LOG.info("Start VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

        super(ChangesService, self).start()
        self.tg.add_timer(self.changes_interval, self._get_changes)

        LOG.info("Finish start VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

    def stop(self, graceful=False):
        LOG.info("Stop VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

        super(ChangesService, self).stop()

        LOG.info("Finish stop VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

    def _get_changes(self):
        LOG.debug("start get changes")<commit_msg>change initial_delay for change service to start after changes_interval<commit_after># Copyright 2016 - Alcatel-Lucent
# Copyright 2016 - Nokia
#
# 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 oslo_log import log
from oslo_service import service as os_service

from vitrage.common.constants import SyncMode

LOG = log.getLogger(__name__)


class SynchronizerService(os_service.Service):

    def __init__(self, conf, registered_plugins):
        super(SynchronizerService, self).__init__()
        self.conf = conf
        self.registered_plugins = registered_plugins

    def set_callback(self, callback_function):
        self.callback_function = callback_function


class SnapshotsService(SynchronizerService):

    def __init__(self, conf, registered_plugins):
        super(SnapshotsService, self).__init__(conf, registered_plugins)
        self.first_time = True

    def start(self):
        LOG.info("Start VitrageSnapshotsService")

        super(SnapshotsService, self).start()
        interval = self.conf.synchronizer.snapshots_interval
        self.tg.add_timer(interval, self._get_all)

        LOG.info("Finish start VitrageSnapshotsService")

    def stop(self, graceful=False):
        LOG.info("Stop VitrageSnapshotsService")

        super(SnapshotsService, self).stop()

        LOG.info("Finish stop VitrageSnapshotsService")

    def _get_all(self):
        sync_mode = SyncMode.INIT_SNAPSHOT \
            if self.first_time else SyncMode.SNAPSHOT
        LOG.debug("start get all with sync mode %s" % sync_mode)

        for plugin in self.registered_plugins.values():
            entities_dictionaries = plugin.get_all(sync_mode)
            for entity in entities_dictionaries:
                self.callback_function(entity)

        LOG.debug("end get all with sync mode %s" % sync_mode)
        self.first_time = False


class ChangesService(SynchronizerService):

    def __init__(self, conf, registered_plugins, changes_interval):
        super(ChangesService, self).__init__(conf, registered_plugins)
        self.changes_interval = changes_interval

    def start(self):
        LOG.info("Start VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

        super(ChangesService, self).start()
        self.tg.add_timer(interval=self.changes_interval,
                          callback=self._get_changes,
                          initial_delay=self.changes_interval)

        LOG.info("Finish start VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

    def stop(self, graceful=False):
        LOG.info("Stop VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

        super(ChangesService, self).stop()

        LOG.info("Finish stop VitrageChangesService - %s",
                 self.registered_plugins[0].__class__.__name__)

    def _get_changes(self):
        LOG.debug("start get changes")
{'fraction_non_alphanumeric': 0.08490146688398281, 'fraction_numerical': 0.004741443176766928, 'mean_word_length': 3.216114928169894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7227433', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1810, 'n_words': 478}