<commit_before>"""Sync external ressources with elastic search."""

import re
import requests
import logging

from pyramid.traversal import find_interface

from adhocracy_core.interfaces import IResourceCreatedAndAdded
from adhocracy_core.interfaces import IResourceSheetModified
from adhocracy_core.resources.comment import IComment
from adhocracy_core.resources.external_resource import IExternalResource
from adhocracy_core.sheets.name import IName
from adhocracy_core.utils import get_sheet_field

log = logging.getLogger(__name__)


def notify_policycompass(event):
    """Push comments of IExternalResource ressources to elastic search."""
    comment = event.object
    external_resource = find_interface(comment, IExternalResource)

    if external_resource is None:  # pragma: no cover
        return

    resource_name = get_sheet_field(external_resource, IName, 'name')
    match = re.match(
        '(?P<type>visualization|event|dataset|metric|fuzzymap|indicator)'
        '_(?P<id>[0-9]+)',
        resource_name)

    # this is not a known policycompass external resource
    if match is None:  # pragma: no cover
        return

    resource_type = match.group('type')
    resource_id = match.group('id')

    settings = event.registry.settings
    pcompass_endpoint = settings.get('adhocracy_pcompass.pcompass_endpoint',
                                     'http://localhost:8000')
    url = '{base}/api/v1/searchmanager/updateindexitem/{res_type}/{res_id}' \
        .format(base=pcompass_endpoint,
                res_type=resource_type,
                res_id=resource_id)

    r = requests.post(url)
    if r.status_code != 200:  # indexing error on pcompass
        msg = 'Notifying policy compass about "{}_{}" failed with "{}"'.format(
            resource_type, resource_type, r.text)
        raise ValueError(msg)


def includeme(config):
    """Register subscribers."""
    config.add_subscriber(notify_policycompass,
                          IResourceCreatedAndAdded,
                          object_iface=IComment)
    config.add_subscriber(notify_policycompass,
                          IResourceSheetModified,
                          object_iface=IComment)<commit_msg>Fix: external resources are not reindexd when comments change<commit_after>"""Sync external ressources with elastic search."""

import re
import requests
import logging

from adhocracy_core.interfaces import ISheetBackReferenceAdded
from adhocracy_core.interfaces import ISheetBackReferenceModified
from adhocracy_core.resources.external_resource import IExternalResource
from adhocracy_core.sheets.name import IName
from adhocracy_core.sheets.comment import ICommentable
from adhocracy_core.utils import get_sheet_field

log = logging.getLogger(__name__)


def notify_policy_compass(event):
    """Send notification to policy compass to reindex an external resource."""
    external_resource = event.object

    if external_resource is None:  # pragma: no cover
        return

    resource_name = get_sheet_field(external_resource, IName, 'name')
    match = re.match(
        '(?P<type>visualization|event|dataset|metric|fuzzymap|indicator)'
        '_(?P<id>[0-9]+)',
        resource_name)

    # this is not a known policycompass external resource
    if match is None:  # pragma: no cover
        return

    resource_type = match.group('type')
    resource_id = match.group('id')

    settings = event.registry.settings
    pcompass_endpoint = settings.get('adhocracy_pcompass.pcompass_endpoint',
                                     'http://localhost:8000')
    url = '{base}/api/v1/searchmanager/updateindexitem/{res_type}/{res_id}' \
        .format(base=pcompass_endpoint,
                res_type=resource_type,
                res_id=resource_id)

    r = requests.post(url)
    if r.status_code != 200:  # indexing error on pcompass
        msg = 'Notifying policy compass about "{}_{}" failed with "{}"'.format(
            resource_type, resource_type, r.text)
        raise ValueError(msg)


def includeme(config):
    """Register subscribers."""
    config.add_subscriber(notify_policy_compass,
                          ISheetBackReferenceModified,
                          object_iface=IExternalResource,
                          event_isheet=ICommentable)
    config.add_subscriber(notify_policy_compass,
                          ISheetBackReferenceAdded,
                          object_iface=IExternalResource,
                          event_isheet=ICommentable)
{'fraction_non_alphanumeric': 0.0832035595105673, 'fraction_numerical': 0.004449388209121246, 'mean_word_length': 3.039532794249775, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059391', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1202, 'n_words': 276}