Exemplo n.º 1
0
    def import_comment_counts(self, prefix=None, automatic_flush=True, start=None, stop=None):
        from rockpack.mainsite.services.video.models import VideoInstanceComment, VideoInstance, Video
        from rockpack.mainsite.core.dbapi import db

        counts = db.session.query(VideoInstance.id, func.count(VideoInstance.id)).join(
            VideoInstanceComment,
            VideoInstanceComment.video_instance == VideoInstance.id
        ).join(
            Video,
            (Video.id == VideoInstance.video) &
            (Video.visible == True)
        ).filter(VideoInstance.deleted == False)

        if prefix:
            counts = counts.filter(VideoInstance.id.like(prefix.replace('_', '\\_') + '%'))

        if start:
            counts = counts.filter(VideoInstanceComment.date_added.between(start, stop))

        counts = counts.group_by(
            VideoInstance.id
        )

        app.logger.debug('%d video%s with comments' % (counts.count(), 's' if counts.count() > 1 else ''))
        app.logger.debug('Processing ...')

        ev = ESVideo.updater(bulk=True)
        for videoid, count in counts:
            ev.set_document_id(videoid)
            ev.add_field('comments.count', count)
            ev.update()
            ev.reset()

        if automatic_flush:
            ESVideo.flush()
Exemplo n.º 2
0
    def import_dolly_repin_counts(self, prefix=None, start=None, stop=None, automatic_flush=True):
        from rockpack.mainsite.services.video.models import VideoInstance, Video

        with app.test_request_context():
            child = aliased(VideoInstance, name='child')
            query = readonly_session.query(
                VideoInstance.id,
                VideoInstance.video,
                child.source_channel,
                func.count(VideoInstance.id)
            ).outerjoin(
                child,
                (VideoInstance.video == child.video) &
                (VideoInstance.channel == child.source_channel)
            ).join(
                Video,
                (Video.id == VideoInstance.video) &
                (Video.visible == True) &
                (VideoInstance.deleted == False))

            if prefix:
                query = query.filter(VideoInstance.id.like(prefix.replace('_', '\\_') + '%'))

            if start:
                video_ids = VideoInstance.query.filter(
                    VideoInstance.date_updated.between(start, stop)
                ).with_entities(VideoInstance.video).subquery()

                query = query.filter(Video.id.in_(video_ids))

            query = query.group_by(VideoInstance.id, VideoInstance.video, child.source_channel)

            instance_counts = {}
            influential_index = {}

            total = query.count()
            done = 1

            for _id, video, source_channel, count in query.yield_per(6000):
                # Set the count for the video instance
                instance_counts[(_id, video)] = count
                # If the count is higher for the same video that
                # the previous instance, mark this instance as the
                # influential one for this video
                i_id, i_count = influential_index.get(video, [None, 0])

                # Count will always be at least 1
                # but should really be zero if no children
                if not source_channel and count == 1:
                    count = 0
                if (count > i_count) or\
                        (count == i_count) and not source_channel:
                    influential_index.update({video: (_id, count,)})

                if app.logger.isEnabledFor(logging.DEBUG):
                    self.print_percent_complete(done, total)
                done += 1

            total = len(instance_counts)
            done = 1

            ec = ESVideo.updater(bulk=True)
            for (_id, video), count in instance_counts.iteritems():
                ec.set_document_id(_id)
                ec.add_field('child_instance_count', count)
                ec.add_field('most_influential', True if influential_index.get(video, '')[0] == _id else False)
                ec.update()
                ec.reset()

                if app.logger.isEnabledFor(logging.DEBUG):
                    self.print_percent_complete(done, total)
                done += 1

            if automatic_flush:
                ESVideo.flush()