# Path: screenchop/models.py
# class Post(Document):
#     uid = SequenceField()
#     submitter = StringField(required=True)
#     caption = StringField(max_length=200)
#     tags = ListField(StringField(max_length=30))
#     filename = StringField(max_length=30)
#     date = DateTimeField(default=datetime.datetime.now)
#     upvotes = IntField(default=1)
#     downvotes = IntField(default=0)
#     width = IntField(required=True)
#     height = IntField(required=True)
#     rank = FloatField(default=ranking.hot(1, 0, datetime.datetime.now()))
# 
#     meta = {
#         'indexes': ['uid', 'filename', 'rank'],
#         }
# 
# class Tag_freq(Document):
#     tag = StringField(required=True)
#     freq = IntField()
# 
#     meta = {
#         'indexes': ['tag']
#         }


from mongoengine import *
from screenchop.models import Post, Tag_freq
from operator import itemgetter
#!/usr/bin/env python

''' tag_freq.py - Queries a list of frequently used tags from posts/chops.
This will then update the tag_freq Mongo collection with data for less resource
heavy querying on the /tags view. '''



def tag_frequencies():
    ''' Finds frequently used tags in Posts and sorts them into tuples. '''
    
    tag_freqs = Post.objects.item_frequencies('tags', normalize=False)
    top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:30]
    
    # Print output and save results into Tag_freq collection
    for tag, freq in top_tags:
        print tag, int(freq)
        a, created = Tag_freq.objects.get_or_create(tag=tag)

# Path: screenchop/models.py
# class Post(Document):
#     uid = SequenceField()
#     submitter = StringField(required=True)
#     caption = StringField(max_length=200)
#     tags = ListField(StringField(max_length=30))
#     filename = StringField(max_length=30)
#     date = DateTimeField(default=datetime.datetime.now)
#     upvotes = IntField(default=1)
#     downvotes = IntField(default=0)
#     width = IntField(required=True)
#     height = IntField(required=True)
#     rank = FloatField(default=ranking.hot(1, 0, datetime.datetime.now()))
# 
#     meta = {
#         'indexes': ['uid', 'filename', 'rank'],
#         }
# 
# class Tag_freq(Document):
#     tag = StringField(required=True)
#     freq = IntField()
# 
#     meta = {
#         'indexes': ['tag']
#         }


# Path: scripts/tag_freq.py
from mongoengine import *
from screenchop.models import Post, Tag_freq
from operator import itemgetter

#!/usr/bin/env python

''' tag_freq.py - Queries a list of frequently used tags from posts/chops.
This will then update the tag_freq Mongo collection with data for less resource
heavy querying on the /tags view. '''



def tag_frequencies():
    ''' Finds frequently used tags in Posts and sorts them into tuples. '''
    
    tag_freqs = Post.objects.item_frequencies('tags', normalize=False)
    top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:30]
    
    # Print output and save results into Tag_freq collection
    for tag, freq in top_tags:
        print tag, int(freq)
        a, created = Tag_freq.objects.get_or_create(tag=tag)

        Tag_freq.objects(tag=tag).update_one(set__freq=int(freq))