<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Find nodes derived from other nodes that share a piwik_site_id with a parent.

"""

import json
import logging
import sys

from modularodm import Q
from nose.tools import *
import requests
import responses

from framework.auth import Auth
from tests.base import OsfTestCase
from tests.factories import NodeFactory, ProjectFactory, RegistrationFactory, UserFactory
from website.app import init_app
from website.models import Node
from website.settings import PIWIK_ADMIN_TOKEN, PIWIK_HOST


logger = logging.getLogger('root')


class PiwikSiteCache(object):
    def __init__(self):
        self._cache = None

    def update_cache(self):
        self._cache = requests.get('{host}index.php?module=API&method=SitesManager.getAllSites&format=JSON&token_auth={auth_token}'.format(
            host=PIWIK_HOST,
            auth_token=PIWIK_ADMIN_TOKEN,
        )).json()

    def node_has_duplicate_piwik_id(self, node):
        if not self._cache:
            self.update_cache()

        return not self._cache[str(node.piwik_site_id)]['name'][6:] == node._id


piwik_cache = PiwikSiteCache()<commit_msg>Allow Piwik node migration script to handle site ids not in Piwik.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Find nodes derived from other nodes that share a piwik_site_id with a parent.

"""

import json
import logging
import sys

from modularodm import Q
from nose.tools import *
import requests
import responses

from framework.auth import Auth
from tests.base import OsfTestCase
from tests.factories import NodeFactory, ProjectFactory, RegistrationFactory, UserFactory
from website.app import init_app
from website.models import Node
from website.settings import PIWIK_ADMIN_TOKEN, PIWIK_HOST


logger = logging.getLogger('root')


class PiwikSiteCache(object):
    def __init__(self):
        self._cache = None

    def update_cache(self):
        self._cache = requests.get('{host}index.php?module=API&method=SitesManager.getAllSites&format=JSON&token_auth={auth_token}'.format(
            host=PIWIK_HOST,
            auth_token=PIWIK_ADMIN_TOKEN,
        )).json()

    def node_has_duplicate_piwik_id(self, node):
        if not self._cache:
            self.update_cache()

        try:
            piwik_alias = self._cache[str(node.piwik_site_id)].get('name')
        except KeyError:
            return False

        if not piwik_alias:
            return False
        return not piwik_alias[6:] == node._id


piwik_cache = PiwikSiteCache()
{'fraction_non_alphanumeric': 0.0904799370574351, 'fraction_numerical': 0.0015735641227380016, 'mean_word_length': 3.6575091575091574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2477869', 'n_tokens_mistral': 780, 'n_tokens_neox': 754, 'n_words': 202}