<commit_before>"""
This will update node links on POPULAR_LINKS_NODE, POPULAR_LINKS_NODE_REGISTRATIONS and NEW_AND_NOTEWORTHY_LINKS_NODE.
"""
import sys
import logging
from modularodm import Q
from website.app import init_app
from website import models
from framework.auth.core import Auth
from scripts import utils as script_utils
from framework.celery_tasks import app as celery_app
from framework.transactions.context import TokuTransaction
from website.project.utils import activity
from website.settings import POPULAR_LINKS_NODE, POPULAR_LINKS_NODE_REGISTRATIONS

logger = logging.getLogger(__name__)

def popular_activity_json():
    """ Return popular_public_projects node_ids """

    activity_json = activity()
    popular_projects = activity_json['popular_public_projects']
    popular_registrations = activity_json['popular_public_registrations']

    return {
        'popular_public_projects': [proj._id for proj in popular_projects],
        'popular_public_registrations': [reg._id for reg in popular_registrations]
    }

def update_node_links(designated_node, target_node_ids, description):
    """ Takes designated node, removes current node links and replaces them with node links to target nodes """
    logger.info('Repopulating {} with latest {} nodes.'.format(designated_node._id, description))
    user = designated_node.creator
    auth = Auth(user)

    for pointer in designated_node.nodes_pointer:
        designated_node.rm_pointer(pointer, auth)

    for n_id in target_node_ids:
        n = models.Node.load(n_id)
        if is_eligible_node(n):
            designated_node.add_pointer(n, auth, save=True)
            logger.info('Added node link {} to {}'.format(n, designated_node))

def main(dry_run=True):
    init_app(routes=False)
    popular_activity = popular_activity_json()

    popular_node_ids = popular_activity['popular_node_ids']
    popular_links_node = models.Node.find_one(Q('_id', 'eq', POPULAR_LINKS_NODE))
    popular_registration_ids = popular_activity_json()['popular_registration_ids']
    popular_links_node_registrations = models.Node.find_one(Q('_id', 'eq', POPULAR_LINKS_NODE_REGISTRATIONS))

    update_node_links(popular_links_node, popular_node_ids, 'popular')
    update_node_links(popular_links_node_registrations, popular_registration_ids, 'popular registrations')

    try:
        popular_links_node.save()
        logger.info('Node links on {} updated.'.format(popular_links_node._id))
    except (KeyError, RuntimeError) as error:
        logger.error('Could not migrate popular nodes due to error')
        logger.exception(error)

    try:<commit_msg>Simplify script a bit y just passing nodes<commit_after>"""
This will update node links on POPULAR_LINKS_NODE, POPULAR_LINKS_NODE_REGISTRATIONS and NEW_AND_NOTEWORTHY_LINKS_NODE.
"""
import sys
import logging
from modularodm import Q
from website.app import init_app
from website import models
from framework.auth.core import Auth
from scripts import utils as script_utils
from framework.celery_tasks import app as celery_app
from framework.transactions.context import TokuTransaction
from website.project.utils import activity
from website.settings import POPULAR_LINKS_NODE, POPULAR_LINKS_NODE_REGISTRATIONS

logger = logging.getLogger(__name__)


def update_node_links(designated_node, target_nodes, description):
    """ Takes designated node, removes current node links and replaces them with node links to target nodes """
    logger.info('Repopulating {} with latest {} nodes.'.format(designated_node._id, description))
    user = designated_node.creator
    auth = Auth(user)

    for pointer in designated_node.nodes_pointer:
        designated_node.rm_pointer(pointer, auth)

    for node in target_nodes:
        designated_node.add_pointer(node, auth, save=True)
        logger.info('Added node link {} to {}'.format(node, designated_node))

def main(dry_run=True):
    init_app(routes=False)

    popular_activity = activity()

    popular_nodes = popular_activity['popular_public_projects']
    popular_links_node = models.Node.find_one(Q('_id', 'eq', POPULAR_LINKS_NODE))
    popular_registrations = popular_activity['popular_public_registrations']
    popular_links_node_registrations = models.Node.find_one(Q('_id', 'eq', POPULAR_LINKS_NODE_REGISTRATIONS))

    update_node_links(popular_links_node, popular_nodes, 'popular')
    update_node_links(popular_links_node_registrations, popular_registrations, 'popular registrations')
    try:
        popular_links_node.save()
        logger.info('Node links on {} updated.'.format(popular_links_node._id))
    except (KeyError, RuntimeError) as error:
        logger.error('Could not migrate popular nodes due to error')
        logger.exception(error)

    try:
{'fraction_non_alphanumeric': 0.08149084017687934, 'fraction_numerical': 0.0, 'mean_word_length': 5.121134020618556, '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': '6267702', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1367, 'n_words': 336}