File size: 1,259 Bytes
a08376f efbe1ad a08376f efbe1ad db0740c c107a24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import os
from pymilvus import connections
from pymilvus import Collection
from datetime import date
import logging
def get_min_age_to_delete():
BASE_DATE = date(2024, 1, 1)
today = date.today()
timedelta = today - BASE_DATE
return timedelta.days - 30 # 30 days means deleting articles older than a month
def get_secrets():
uri = os.environ.get("URI")
token = os.environ.get("TOKEN")
collection_name = os.environ.get("COLLECTION_NAME")
return uri, token, collection_name
def get_collection(uri: str, token: str, collection_name: str):
connections.connect("default", uri=uri, token=token)
collection = Collection(name=collection_name)
print("Loaded existing collection")
return collection
def delete_from_db():
uri, token, collection_name = get_secrets()
collection = get_collection(uri, token, collection_name)
min_age_to_delete = get_min_age_to_delete()
expr = f"article_age <= {min_age_to_delete}"
collection.delete(expr)
logging.warning('Successfully deleted older articles')
res = collection.query(expr="article_age > -1000", output_fields = ["article_age"])
# dates = []
# for x in res:
# dates.append(x['article_age'])
# return dates |