<commit_before>import copy
import logging

from modularodm import Q
from modularodm.exceptions import NoResultsFound
from modularodm.storage.base import KeyExistsException

from framework.mongo import database
from framework.transactions.context import TokuTransaction

from scripts import utils as scripts_utils

from website.app import init_app
from website.project.model import NodeLog
from website.addons.osfstorage import model, oldels

logger = logging.getLogger(__name__)


def migrate_download_counts(node, old, new, dry=True):
    collection = database['pagecounters']

    if dry:
        new_id = ':'.join(['download', node._id, 'new id'])
    else:
        new_id = ':'.join(['download', node._id, new._id])

    old_id = ':'.join(['download', node._id, old.path])

    for doc in collection.find({'_id': {'$regex': '^{}(:\d)?'.format(old_id)}}):
        new_doc = copy.deep(doc)
        new_doc['_id'] = doc['_id'].replace(old_id, new_id)

        logger.info('{} -> {}'.format(doc, new_doc))
        if not dry:
            collection.update(doc, new_doc)


def migrate_node_settings(node_settings, dry=True):
    logger.info('Running `on add` for node settings of {}'.format(node_settings.owner._id))

    if not dry:
        node_settings.on_add()

def migrate_file(node, old, parent, dry=True):
    assert isinstance(old, oldels.OsfStorageFileRecord)
    logger.info('Creating new child {}'.format(old.name))
    if not dry:
        try:
            new = parent.append_file(old.name)
        except KeyExistsException:
            logger.info('{!r} has already been migrated'.format(old))
        new.versions = old.versions
        new.is_deleted = old.is_deleted
        new.save()
    else:
        new = None

    migrate_guid(node, old, new, dry=dry)
    migrate_log(node, old, new, dry=dry)
    migrate_download_counts(node, old, new, dry=dry)<commit_msg>Fix download count migration<commit_after>import copy
import logging

from modularodm import Q
from modularodm.exceptions import NoResultsFound
from modularodm.storage.base import KeyExistsException

from framework.mongo import database
from framework.transactions.context import TokuTransaction

from scripts import utils as scripts_utils

from website.app import init_app
from website.project.model import NodeLog
from website.addons.osfstorage import model, oldels

logger = logging.getLogger(__name__)


def migrate_download_counts(node, old, new, dry=True):
    if dry:
        new_id = ':'.join(['download', node._id, 'new id'])
    else:
        new_id = ':'.join(['download', node._id, new._id])

    old_id = ':'.join(['download', node._id, old.path.replace('.', '_').replace('$', '_')])

    for doc in database.pagecounters.find({'_id': {'$regex': '^{}(:\d)?'.format(old_id)}}):
        new_doc = copy.deepcopy(doc)
        assert old_id in doc['_id']
        new_doc['_id'] = doc['_id'].replace(old_id, new_id)

        logger.info('{} -> {}'.format(doc, new_doc))
        if not dry:
            database.pagecounters.insert(new_doc)
            database.pagecounters.remove(doc)


def migrate_node_settings(node_settings, dry=True):
    logger.info('Running `on add` for node settings of {}'.format(node_settings.owner._id))

    if not dry:
        node_settings.on_add()

def migrate_file(node, old, parent, dry=True):
    assert isinstance(old, oldels.OsfStorageFileRecord)
    logger.info('Creating new child {}'.format(old.name))
    if not dry:
        try:
            new = parent.append_file(old.name)
        except KeyExistsException:
            logger.info('{!r} has already been migrated'.format(old))
        new.versions = old.versions
        new.is_deleted = old.is_deleted
        new.save()
    else:
        new = None

    migrate_guid(node, old, new, dry=dry)
    migrate_log(node, old, new, dry=dry)
    migrate_download_counts(node, old, new, dry=dry)
{'fraction_non_alphanumeric': 0.1281985009046265, 'fraction_numerical': 0.0, 'mean_word_length': 3.4843568945538816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7379504', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1201, 'n_words': 299}