-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutil.py
78 lines (59 loc) · 1.93 KB
/
util.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Utility functions for database operations."""
import hashlib
import json
from pathlib import Path
import pyarrow as pa
from fastapi import HTTPException
from pyarrow import DataType
from zeno_backend.classes.base import MetadataType
def resolve_metadata_type(d: DataType) -> MetadataType:
"""Get the Zeno MetadataType of a PyArrow type.
Args:
d (DataType): PyArrow datatype.
Returns:
MetadataType: MetadataType of the column.
"""
if pa.types.is_integer(d) or pa.types.is_floating(d) or pa.types.is_decimal(d):
return MetadataType.CONTINUOUS
elif pa.types.is_boolean(d):
return MetadataType.BOOLEAN
elif pa.types.is_temporal(d):
return MetadataType.DATETIME
elif pa.types.is_string(d):
return MetadataType.NOMINAL
elif pa.types.is_list(d) and (
pa.types.is_floating(d.value_type) or pa.types.is_integer(d.value_type)
):
return MetadataType.EMBEDDING
return MetadataType.OTHER
def hash_api_key(api_key: str) -> str:
"""Hash an API key.
Args:
api_key (str): API key to hash.
Returns:
str: hashed API key.
"""
hasher = hashlib.sha256()
hasher.update(api_key.encode("utf-8"))
return hasher.hexdigest()
def match_instance_view(view: str) -> str:
"""Get the according view specification for an instance view.
Args:
view (str): the view of the project.
Returns:
str: the view specification for the project.
"""
if view == "":
return ""
if view.startswith("{"):
return view
views = [x.stem for x in Path("zeno_backend/instance_views").glob("*.json")]
if view in views:
return json.dumps(
json.load(Path(f"zeno_backend/instance_views/{view}.json").open("r"))
)
else:
raise HTTPException(
status_code=400,
detail=f"Instance view {view} not found. Available views: {str(views)}",
)