add tag_list fix
Browse files- fix_tags.py +59 -0
 
    	
        fix_tags.py
    CHANGED
    
    | 
         @@ -25,5 +25,64 @@ def fix_tags_with_Counter(): 
     | 
|
| 25 | 
         
             
                         .where(Tag.id == tag)
         
     | 
| 26 | 
         
             
                         .execute())
         
     | 
| 27 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 28 | 
         
             
            if __name__ == "__main__":
         
     | 
| 
         | 
|
| 29 | 
         
             
                fix_tags_with_Counter()
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 25 | 
         
             
                         .where(Tag.id == tag)
         
     | 
| 26 | 
         
             
                         .execute())
         
     | 
| 27 | 
         | 
| 28 | 
         
            +
            # bulk update tag_list from sum of tag_list_*
         
     | 
| 29 | 
         
            +
            def fix_tag_list_batch(batch_size=300, limit=1000000, simulate=False):
         
     | 
| 30 | 
         
            +
                with db.atomic() as transaction:
         
     | 
| 31 | 
         
            +
                    # Process in batches to reduce memory usage and improve performance
         
     | 
| 32 | 
         
            +
                    query = Post.select()
         
     | 
| 33 | 
         
            +
                    if limit > 0:
         
     | 
| 34 | 
         
            +
                        query = query.limit(limit)
         
     | 
| 35 | 
         
            +
                    # sample id
         
     | 
| 36 | 
         
            +
                    sample_id = query[0].id
         
     | 
| 37 | 
         
            +
                    # print sample's tag_list
         
     | 
| 38 | 
         
            +
                    print("Sample tag_list: {}".format(query[0].tag_list))
         
     | 
| 39 | 
         
            +
                    print("Updating tag_list for {} posts".format(query.count()))
         
     | 
| 40 | 
         
            +
                    for i in tqdm(range(0, query.count(), batch_size)):
         
     | 
| 41 | 
         
            +
                        batch = query[i:i + batch_size]
         
     | 
| 42 | 
         
            +
                        # Update each post in the batch
         
     | 
| 43 | 
         
            +
                        for post in batch:
         
     | 
| 44 | 
         
            +
                            post.tag_list = post.tag_list_general + post.tag_list_artist + post.tag_list_character + post.tag_list_meta + post.tag_list_copyright
         
     | 
| 45 | 
         
            +
                        # Bulk update the batch
         
     | 
| 46 | 
         
            +
                        Post.bulk_update(batch, fields=[Post.tag_list])
         
     | 
| 47 | 
         
            +
                    # return sample id
         
     | 
| 48 | 
         
            +
                    print("Sample tag_list: {}".format(Post.get_by_id(sample_id).tag_list))
         
     | 
| 49 | 
         
            +
                    if simulate:
         
     | 
| 50 | 
         
            +
                        transaction.rollback()
         
     | 
| 51 | 
         
            +
                    return sample_id
         
     | 
| 52 | 
         
            +
             
     | 
| 53 | 
         
            +
            def pretty_print(post_id:int) -> str:
         
     | 
| 54 | 
         
            +
                """
         
     | 
| 55 | 
         
            +
                Returns a pretty printed post
         
     | 
| 56 | 
         
            +
                """
         
     | 
| 57 | 
         
            +
                post = Post.get_by_id(post_id)
         
     | 
| 58 | 
         
            +
                def pretty_print_tag(tag):
         
     | 
| 59 | 
         
            +
                    """
         
     | 
| 60 | 
         
            +
                    Returns a pretty printed tag
         
     | 
| 61 | 
         
            +
                    """
         
     | 
| 62 | 
         
            +
                    return f"{tag.name}({tag.id}), (popularity: {tag.popularity}))"
         
     | 
| 63 | 
         
            +
                
         
     | 
| 64 | 
         
            +
                def pretty_print_tags(tags):
         
     | 
| 65 | 
         
            +
                    """
         
     | 
| 66 | 
         
            +
                    Returns a pretty printed list of tags
         
     | 
| 67 | 
         
            +
                    """
         
     | 
| 68 | 
         
            +
                    return ", ".join([pretty_print_tag(tag) for tag in tags])
         
     | 
| 69 | 
         
            +
                
         
     | 
| 70 | 
         
            +
                fields = {"tag_list_general", "tag_list_artist", "tag_list_character", "tag_list_meta", "tag_list_copyright"}
         
     | 
| 71 | 
         
            +
                string = ""
         
     | 
| 72 | 
         
            +
                # start printing
         
     | 
| 73 | 
         
            +
                string += f"Post {post_id}\n"
         
     | 
| 74 | 
         
            +
                # created_at
         
     | 
| 75 | 
         
            +
                string += f"\tcreated_at: {post.created_at}\n"
         
     | 
| 76 | 
         
            +
                for field in fields:
         
     | 
| 77 | 
         
            +
                    string += f"\t{field}: {pretty_print_tags(post.__getattribute__(field))}\n"
         
     | 
| 78 | 
         
            +
                return string
         
     | 
| 79 | 
         
            +
             
     | 
| 80 | 
         
             
            if __name__ == "__main__":
         
     | 
| 81 | 
         
            +
                fix_tag_list_batch(1000, -1, simulate=False)
         
     | 
| 82 | 
         
             
                fix_tags_with_Counter()
         
     | 
| 83 | 
         
            +
                # get popularity of tag 684816
         
     | 
| 84 | 
         
            +
                print(Tag.get_by_id(684816).popularity)
         
     | 
| 85 | 
         
            +
                # get random 20 post to test
         
     | 
| 86 | 
         
            +
                posts = Post.select().order_by(fn.Random()).limit(20)
         
     | 
| 87 | 
         
            +
                for post in posts:
         
     | 
| 88 | 
         
            +
                    print(pretty_print(post.id))
         
     |