Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 1 addition & 63 deletions business_objects/information_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,54 +131,6 @@ def get_payloads_by_project_id(project_id: str) -> List[Any]:
return general.execute_all(query)


def get_zero_shot_is_data(project_id: str, information_source_id: str) -> Any:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
information_source_id = prevent_sql_injection(
information_source_id, isinstance(information_source_id, str)
)
sql: str = f"""
SELECT base.*, a.name attribute_name
FROM (
SELECT
_is.id::TEXT,
_is.type is_type,
_is.source_code::JSON ->>'config' config,
(_is.source_code::JSON ->>'min_confidence')::FLOAT min_confidence,
(_is.source_code::JSON ->>'run_individually')::BOOLEAN run_individually,
lt.task_target,
COALESCE(lt.attribute_id,(_is.source_code::JSON ->>'attribute_id')::UUID) attribute_id,
ltl.labels
FROM information_source _is
INNER JOIN labeling_task lt
ON _is.labeling_task_id = lt.id
INNER JOIN (
SELECT ltl.labeling_task_id,array_agg(ltl.name) labels
FROM labeling_task_label ltl
WHERE ltl.project_id = '{project_id}'
GROUP BY ltl.labeling_task_id
) ltl
ON _is.labeling_task_id = ltl.labeling_task_id
WHERE _is.id = '{information_source_id}' AND _is.project_id = '{project_id}' )base
INNER JOIN attribute a
ON a.id = base.attribute_id AND a.project_id = '{project_id}'
"""
return general.execute_first(sql)


def get_first_crowd_is_for_annotator(project_id: str, annotator_id: str) -> str:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
annotator_id = prevent_sql_injection(annotator_id, isinstance(annotator_id, str))
query = f"""
SELECT _is.id::TEXT
FROM information_source _is
WHERE _is.project_id = '{project_id}' AND _is."type" = '{enums.InformationSourceType.CROWD_LABELER.value}'
AND _is.source_code::JSON ->>'annotator_id' = '{annotator_id}'
"""
v = general.execute_first(query)
if v and v[0]:
return v[0]


def get_exclusion_record_ids(source_id: str) -> List[str]:
exclusions = (
session.query(InformationSourceStatisticsExclusion.record_id).filter(
Expand Down Expand Up @@ -223,15 +175,8 @@ def get_all_states(project_id: str, source_id: Optional[str] = None) -> Dict[str
return {r[0]: r[1] for r in general.execute_all(query)}


def get_overview_data(
project_id: str, is_model_callback: bool = False
) -> List[Dict[str, Any]]:
def get_overview_data(project_id: str) -> List[Dict[str, Any]]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))

if is_model_callback:
type_selection = " = 'MODEL_CALLBACK'"
else:
type_selection = " != 'MODEL_CALLBACK'"
query = f"""
SELECT array_agg(row_to_json(data_select))
FROM (
Expand Down Expand Up @@ -274,7 +219,6 @@ def get_overview_data(
GROUP BY source_id) stats
ON _is.id = stats.source_id
WHERE _is.project_id = '{project_id}'
AND _is.type {type_selection}
ORDER BY "createdAt" DESC,name
)data_select """
values = general.execute_first(query)
Expand Down Expand Up @@ -523,15 +467,10 @@ def update_is_selected_for_project(
project_id: str,
update_value: bool,
with_commit: bool = False,
is_model_callback: bool = False,
only_with_state: Optional[enums.PayloadState] = None,
) -> None:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))

if is_model_callback:
type_selection = " = 'MODEL_CALLBACK'"
else:
type_selection = " != 'MODEL_CALLBACK'"
if update_value:
str_value = "TRUE"
else:
Expand All @@ -549,7 +488,6 @@ def update_is_selected_for_project(
UPDATE information_source
SET is_selected = {str_value}
WHERE project_id = '{project_id}'
AND type {type_selection}
{id_selection}
"""
general.execute(query)
Expand Down
5 changes: 2 additions & 3 deletions business_objects/labeling_access_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_all_by_type_and_external_id(


def __get_add_ids_data_slice(project_id: str, slice_id: str) -> List[str]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
slice_id = prevent_sql_injection(slice_id, isinstance(slice_id, str))
query = f"""
SELECT array_agg(lal.id::TEXT)
Expand All @@ -82,7 +82,7 @@ def __get_add_ids_data_slice(project_id: str, slice_id: str) -> List[str]:
ON lal.project_id = _is.project_id AND lal.heuristic_id = _is.id
WHERE lal.project_id = '{project_id}'
AND _is.source_code::JSON->>'data_slice_id' = '{slice_id}'
AND _is.type = '{enums.InformationSourceType.CROWD_LABELER.value}' """
"""
add_ids = general.execute_first(query)
if add_ids:
return add_ids[0]
Expand All @@ -108,7 +108,6 @@ def get_by_all_by_user_id(
INNER JOIN information_source _is
ON lal.project_id = _is.project_id AND lal.heuristic_id = _is.id
WHERE _is.source_code::JSON->>'annotator_id' = '{user_id}'
AND _is.type = '{enums.InformationSourceType.CROWD_LABELER.value}'
AND NOT lal.is_locked
"""
ids = [r[0] for r in general.execute_all(query)]
Expand Down
95 changes: 33 additions & 62 deletions business_objects/personal_access_token.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,40 @@
import datetime
from typing import List, Optional, Union
from typing import List
from ..session import session
from submodules.model.business_objects import general
from submodules.model.models import PersonalAccessToken, CognitionPersonalAccessToken


def __get_token_type(
in_cognition_scope: bool,
) -> Union[PersonalAccessToken, CognitionPersonalAccessToken]:
if in_cognition_scope:
return CognitionPersonalAccessToken
else:
return PersonalAccessToken
from submodules.model.models import CognitionPersonalAccessToken


def get_by_user_and_name(
project_id: str,
created_by: str,
name: str,
in_cognition_scope: Optional[bool] = False,
) -> Union[PersonalAccessToken, CognitionPersonalAccessToken]:
token_table = __get_token_type(in_cognition_scope)
query = session.query(token_table).filter(
token_table.project_id == project_id,
token_table.name == name,
) -> CognitionPersonalAccessToken:
return (
session.query(CognitionPersonalAccessToken)
.filter(
CognitionPersonalAccessToken.project_id == project_id,
CognitionPersonalAccessToken.name == name,
CognitionPersonalAccessToken.created_by == created_by,
)
.first()
)

if in_cognition_scope:
query.filter(token_table.created_by == created_by)
else:
query.filter(token_table.user_id == created_by)

return query.first()


def get_all(
project_id: str, in_cognition_scope: Optional[bool] = False
) -> List[Union[PersonalAccessToken, CognitionPersonalAccessToken]]:
token_table = __get_token_type(in_cognition_scope)

return session.query(token_table).filter(token_table.project_id == project_id).all()
def get_all(project_id: str) -> List[CognitionPersonalAccessToken]:
return (
session.query(CognitionPersonalAccessToken)
.filter(CognitionPersonalAccessToken.project_id == project_id)
.all()
)


def get_by_token(
project_id: str, token: str, in_cognition_scope: Optional[bool] = False
) -> Union[PersonalAccessToken, CognitionPersonalAccessToken]:
token_table = __get_token_type(in_cognition_scope)
def get_by_token(project_id: str, token: str) -> CognitionPersonalAccessToken:
return (
session.query(token_table)
session.query(CognitionPersonalAccessToken)
.filter(
token_table.project_id == project_id,
token_table.token == token,
CognitionPersonalAccessToken.project_id == project_id,
CognitionPersonalAccessToken.token == token,
)
.first()
)
Expand All @@ -63,51 +47,40 @@ def create(
scope: str,
expires_at: datetime,
token: str,
in_cognition_scope: Optional[bool] = False,
with_commit: bool = False,
) -> None:
token_table = __get_token_type(in_cognition_scope)
personal_access_token: Union[
PersonalAccessToken, CognitionPersonalAccessToken
] = token_table(
) -> CognitionPersonalAccessToken:
personal_access_token = CognitionPersonalAccessToken(
project_id=project_id,
name=name,
scope=scope,
token=token,
expires_at=expires_at,
created_by=created_by,
)
if in_cognition_scope:
personal_access_token.created_by = created_by
else:
personal_access_token.user_id = created_by
general.add(personal_access_token, with_commit)
return personal_access_token


def delete(
project_id: str,
token_id: str,
in_cognition_scope: Optional[bool] = False,
with_commit: bool = False,
) -> None:
token_table = __get_token_type(in_cognition_scope)
session.query(token_table).filter(
token_table.project_id == project_id,
token_table.id == token_id,
session.query(CognitionPersonalAccessToken).filter(
CognitionPersonalAccessToken.project_id == project_id,
CognitionPersonalAccessToken.id == token_id,
).delete()
general.flush_or_commit(with_commit)


def delete_token_by_ids(
project_id: str,
token_ids: List[str],
in_cognition_scope: Optional[bool] = False,
with_commit: bool = False,
) -> None:
token_table = __get_token_type(in_cognition_scope)
session.query(token_table).filter(
token_table.project_id == project_id,
token_table.id.in_(token_ids),
session.query(CognitionPersonalAccessToken).filter(
CognitionPersonalAccessToken.project_id == project_id,
CognitionPersonalAccessToken.id.in_(token_ids),
).delete()
general.flush_or_commit(with_commit)

Expand All @@ -116,14 +89,12 @@ def update_last_used(
project_id: str,
token_id: str,
with_commit: bool = False,
in_cognition_scope: Optional[bool] = False,
) -> None:
token_table = __get_token_type(in_cognition_scope)
token_item = (
session.query(token_table)
session.query(CognitionPersonalAccessToken)
.filter(
token_table.project_id == project_id,
token_table.id == token_id,
CognitionPersonalAccessToken.project_id == project_id,
CognitionPersonalAccessToken.id == token_id,
)
.first()
)
Expand Down
43 changes: 0 additions & 43 deletions business_objects/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,49 +314,6 @@ def get_confusion_matrix(
return values[0]


def get_zero_shot_project_config(project_id: str, payload_id: str) -> Any:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
payload_id = prevent_sql_injection(payload_id, isinstance(payload_id, str))
query = f"""
SELECT base.*, a.name attribute_name
FROM (
SELECT
isp.source_id,
isp.created_by,
isp.source_code::JSON ->>'config' config,
(isp.source_code::JSON ->>'min_confidence')::FLOAT min_confidence,
(isp.source_code::JSON ->>'run_individually')::BOOLEAN run_individually,
COALESCE(lt.attribute_id,(isp.source_code::JSON ->>'attribute_id')::UUID) attribute_id,
ltl.label_names,
ltl.label_ids
FROM information_source_payload isp
INNER JOIN information_source _is
ON isp.project_id = _is.project_id AND isp.source_id = _is.id
INNER JOIN labeling_task lt
ON _is.labeling_task_id = lt.id
INNER JOIN (
SELECT ltl.labeling_task_id,array_agg(ltl.name ORDER BY ltl.id) label_names,array_agg(ltl.id ORDER BY ltl.id) label_ids
FROM labeling_task_label ltl
INNER JOIN (
SELECT _is.labeling_task_id, TRANSLATE((isp.source_code::JSON ->>'excluded_labels'), '[]','{{}}')::UUID[] excluded_labels
FROM information_source _is
INNER JOIN information_source_payload isp
ON _is.project_id = isp.project_id AND _is.id =isp.source_id
WHERE isp.id = '{payload_id}' AND isp.project_id = '{project_id}'
) isp
ON ltl.labeling_task_id = isp.labeling_task_id
WHERE ltl.project_id = '{project_id}'
AND NOT (ltl.id = ANY (isp.excluded_labels))
GROUP BY ltl.labeling_task_id
) ltl
ON _is.labeling_task_id = ltl.labeling_task_id
WHERE isp.id = '{payload_id}' AND isp.project_id = '{project_id}' )base
INNER JOIN attribute a
ON a.id = base.attribute_id AND a.project_id = '{project_id}'
"""
return general.execute_first(query)


def get_or_create_queue_project(
org_id: str, user_id: str, with_commit: bool = False
) -> Project:
Expand Down
18 changes: 0 additions & 18 deletions business_objects/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,24 +394,6 @@ def get_missing_columns_str(project_id: str) -> str:
return ",\n".join([f"'{k[0]}',r.data->'{k[0]}'" for k in missing_columns])


def get_zero_shot_n_random_records(
project_id: str, attribute_name: str, n: int = 10
) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
n = prevent_sql_injection(n, isinstance(n, int))
sql = f"""
SELECT r.id, r."data", r."data" ->> '{attribute_name}' "text"
FROM record r
WHERE project_id = '{project_id}' AND r.category = '{enums.RecordCategory.SCALE.value}'
ORDER BY RANDOM()
LIMIT {n}
"""
return general.execute_all(sql)


def get_record_id_groups(project_id: str, group_size: int = 20) -> List[List[str]]:
if group_size <= 0:
return None
Expand Down
Loading