from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection,MilvusClient
from config import MILVUS_CLIENT
import time
from datetime import datetime, timedelta

def create_partition_by_hour(current_hour):
    # 连接到 Milvus 服务器
    connections.connect("default",uri=MILVUS_CLIENT)
    # 获取集合
    collection_name = "pdf_measure_v4"
    collection = Collection(collection_name)

    # 创建当前小时的分区
    partition_name = f"partition_{current_hour}"
    if not collection.has_partition(partition_name):
        collection.create_partition(partition_name)
        print(f"Created partition: {partition_name}")
        partition = collection.partition(partition_name)
        partition.load()

    # 获取所有分区
    partitions = collection.partitions
    # 删除所有分区（除了默认分区和当前分区）
    for partition in partitions:
        name = partition.name
        if name not in ["_default", partition_name]:  # 保留默认分区
            pre_partition = collection.partition(name)
            pre_partition.release()
            collection.drop_partition(name)
            print(f"Partition '{name}' deleted.")