Skip to content

Commit

Permalink
Put date into index name
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Oct 19, 2024
1 parent 2389e1f commit b72a937
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
40 changes: 39 additions & 1 deletion scripts/create.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import re
import random
import string
from datetime import datetime
from pinecone import Pinecone


Expand All @@ -20,9 +22,45 @@ def write_gh_output(name, value):
print(f"{name}={value}", file=fh)


def generate_index_name(test_name: str) -> str:
github_actor = os.getenv("GITHUB_ACTOR", None)
user = os.getenv("USER", None)
index_owner = github_actor or user

current_date = datetime.now()
formatted_date = current_date.strftime("%Y%m%d-%f")

github_job = os.getenv("GITHUB_JOB", None)

if test_name.startswith("test_"):
test_name = test_name[5:]

# Remove trailing underscore, if any
if test_name.endswith("_"):
test_name = test_name[:-1]

name_parts = [index_owner, formatted_date, github_job, test_name]
index_name = "-".join([x for x in name_parts if x is not None])

# Remove invalid characters
replace_with_hyphen = re.compile(r"[\[\(_,\s]")
index_name = re.sub(replace_with_hyphen, "-", index_name)
replace_with_empty = re.compile(r"[\]\)\.]")
index_name = re.sub(replace_with_empty, "", index_name)

max_length = 45
index_name = index_name[:max_length]

# Trim final character if it is not alphanumeric
if index_name.endswith("_") or index_name.endswith("-"):
index_name = index_name[:-1]

return index_name.lower()


def main():
pc = Pinecone(api_key=read_env_var("PINECONE_API_KEY"))
index_name = read_env_var("NAME_PREFIX") + random_string(20)
index_name = generate_index_name(read_env_var("NAME_PREFIX") + random_string(20))
pc.create_index(
name=index_name,
metric=read_env_var("METRIC"),
Expand Down
19 changes: 12 additions & 7 deletions tests/integration/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@
import random
import string
from typing import Any
from datetime import datetime


def random_string(length):
return "".join(random.choice(string.ascii_lowercase) for i in range(length))


def generate_index_name(test_name: str) -> str:
buildNumber = os.getenv("GITHUB_BUILD_NUMBER", None)
github_actor = os.getenv("GITHUB_ACTOR", None)
user = os.getenv("USER", None)
index_owner = github_actor or user

current_date = datetime.now()
formatted_date = current_date.strftime("%Y%m%d-%f")

github_job = os.getenv("GITHUB_JOB", None)

if test_name.startswith("test_"):
test_name = test_name[5:]

# Trim name length to save space for other info in name
test_name = test_name[:20]

# Remove trailing underscore, if any
if test_name.endswith("_"):
test_name = test_name[:-1]

name_parts = [buildNumber, test_name, random_string(45)]
name_parts = [index_owner, formatted_date, github_job, test_name]
index_name = "-".join([x for x in name_parts if x is not None])

# Remove invalid characters
Expand All @@ -36,8 +41,8 @@ def generate_index_name(test_name: str) -> str:
index_name = index_name[:max_length]

# Trim final character if it is not alphanumeric
if test_name.endswith("_") or test_name.endswith("-"):
test_name = test_name[:-1]
if index_name.endswith("_") or index_name.endswith("-"):
index_name = index_name[:-1]

return index_name.lower()

Expand Down

0 comments on commit b72a937

Please sign in to comment.