|
7 | 7 | import logging |
8 | 8 | import mimetypes |
9 | 9 | import os |
| 10 | +import time |
10 | 11 |
|
11 | 12 | from google.api_core import retry |
12 | 13 | import requests |
|
31 | 32 | from labelbox.schema.user import User |
32 | 33 | from labelbox.schema.project import Project |
33 | 34 | from labelbox.schema.role import Role |
| 35 | +from labelbox.schema.global_key import AssignGlobalKeyToDataRowInput |
34 | 36 |
|
35 | 37 | from labelbox.schema.media_type import MediaType |
36 | 38 |
|
@@ -939,3 +941,127 @@ def get_model_run(self, model_run_id: str) -> ModelRun: |
939 | 941 | A ModelRun object. |
940 | 942 | """ |
941 | 943 | return self._get_single(Entity.ModelRun, model_run_id) |
| 944 | + |
| 945 | + def assign_global_keys_to_data_rows( |
| 946 | + self, |
| 947 | + global_key_to_data_row_inputs: List[AssignGlobalKeyToDataRowInput], |
| 948 | + timeout=30) -> List[Dict[str, str]]: |
| 949 | + """ |
| 950 | + Assigns global keys to the related data rows. |
| 951 | + |
| 952 | + >>> global_key_data_row_inputs = [ |
| 953 | + AssignGlobalKeyToDataRowInput(data_row_id = "cl7asgri20yvo075b4vtfedjb", global_key = "key1"), |
| 954 | + AssignGlobalKeyToDataRowInput(data_row_id = "cl7asgri10yvg075b4pz176ht", global_key = "key2") |
| 955 | + ] |
| 956 | + >>> client.assign_global_keys_to_data_rows(global_key_data_row_inputs) |
| 957 | +
|
| 958 | + Args: |
| 959 | + A list of AssignGlobalKeyToDataRowInput objects. |
| 960 | + Returns: |
| 961 | + Returns successful assigned global keys and data rows |
| 962 | + """ |
| 963 | + |
| 964 | + mutation_str = """mutation assignGlobalKeysToDataRowsPyApi($globalKeyDataRowLinks: [AssignGlobalKeyToDataRowInput!]!) { |
| 965 | + assignGlobalKeysToDataRows(data: {assignInputs: $globalKeyDataRowLinks}) { |
| 966 | + jobId |
| 967 | + } |
| 968 | + } |
| 969 | + """ |
| 970 | + mutation_params = { |
| 971 | + 'globalKeyDataRowLinks': [ |
| 972 | + input.dict(by_alias=True) |
| 973 | + for input in global_key_to_data_row_inputs |
| 974 | + ] |
| 975 | + } |
| 976 | + assign_global_keys_to_data_rows_job = self.execute( |
| 977 | + mutation_str, mutation_params) |
| 978 | + |
| 979 | + get_failed_assignments_str = """query getDataRowsForGlobalKeysPyApi($jobId: ID!) { |
| 980 | + assignGlobalKeysToDataRowsResult(jobId: {id: $jobId}) { |
| 981 | + sanitizedAssignments { |
| 982 | + dataRowId |
| 983 | + globalKey |
| 984 | + } |
| 985 | + invalidGlobalKeyAssignments { |
| 986 | + dataRowId |
| 987 | + globalKey |
| 988 | + } |
| 989 | + unmodifiedAssignments { |
| 990 | + dataRowId |
| 991 | + globalKey |
| 992 | + } |
| 993 | + accessDeniedAssignments { |
| 994 | + dataRowId |
| 995 | + globalKey |
| 996 | + }}} |
| 997 | + """ |
| 998 | + get_failed_assignments_params = { |
| 999 | + "jobId": |
| 1000 | + assign_global_keys_to_data_rows_job["assignGlobalKeysToDataRows" |
| 1001 | + ]["jobId"] |
| 1002 | + } |
| 1003 | + |
| 1004 | + # TODO: Add a timeout to this based on success or not. output looks different than expected. |
| 1005 | + # TODO: Current output of sanitized rows is not showing any output. all outputs are empty lists. |
| 1006 | + # expected output {data, jobStatus} but seeing the assignment outputs above |
| 1007 | + |
| 1008 | + res = self.execute(get_failed_assignments_str, |
| 1009 | + get_failed_assignments_params) |
| 1010 | + errors = [] |
| 1011 | + if res['assignGlobalKeysToDataRowsResult'][ |
| 1012 | + 'invalidGlobalKeyAssignments'] is not None: |
| 1013 | + errors.append("Invalid Global Keys: " + |
| 1014 | + str(res['invalidGlobalKeyAssignments'])) |
| 1015 | + if res['assignGlobalKeysToDataRowsResult'][ |
| 1016 | + 'unmodifiedAssignments'] is not None: |
| 1017 | + errors.append("Unmodified Assignments: " + |
| 1018 | + str(res['unmodifiedAssignments'])) |
| 1019 | + if res['assignGlobalKeysToDataRowsResult'][ |
| 1020 | + 'accessDeniedAssignments'] is not None: |
| 1021 | + errors.append("Access Denied Assignments: " + |
| 1022 | + str(res['accessDeniedAssignments'])) |
| 1023 | + if len(errors) > 0: |
| 1024 | + raise Exception("Failed to assign global keys to data rows: " + |
| 1025 | + str(errors)) |
| 1026 | + return res['assignGlobalKeysToDataRowsResult']['sanitizedAssignments'] |
| 1027 | + |
| 1028 | + def get_data_row_ids_for_global_keys(self, |
| 1029 | + global_keys: List[str], |
| 1030 | + timeout=30) -> List[Dict[str, str]]: |
| 1031 | + """ |
| 1032 | + Gets data row ids for a list of global keys. |
| 1033 | +
|
| 1034 | + >>> data_row_ids = client.get_data_row_ids_for_global_keys(["key1",]) |
| 1035 | +
|
| 1036 | + Args: |
| 1037 | + A list of global keys |
| 1038 | + Returns: |
| 1039 | + A list of data row ids. Returns empty if the global keys are not found. |
| 1040 | + """ |
| 1041 | + |
| 1042 | + get_job_query_str = """query getDataRowsForGlobalKeysJobPyApi($globalKeys: [ID!]!) { |
| 1043 | + dataRowsForGlobalKeys(where: {ids: $globalKeys}) { jobId}} |
| 1044 | + """ |
| 1045 | + get_job_params = {"globalKeys": global_keys} |
| 1046 | + |
| 1047 | + data_rows_for_global_keys_job = self.execute(get_job_query_str, |
| 1048 | + get_job_params) |
| 1049 | + |
| 1050 | + get_data_rows_str = """query getDataRowsForGlobalKeysPyApi($jobId: ID!) { |
| 1051 | + dataRowsForGlobalKeysResult(jobId: {id: $jobId}) { data { fetchedDataRows {id}} jobStatus}} |
| 1052 | + """ |
| 1053 | + get_data_rows_params = { |
| 1054 | + "jobId": |
| 1055 | + data_rows_for_global_keys_job["dataRowsForGlobalKeys"]["jobId"] |
| 1056 | + } |
| 1057 | + |
| 1058 | + while timeout >= 0: |
| 1059 | + res = self.execute(get_data_rows_str, get_data_rows_params) |
| 1060 | + if res["dataRowsForGlobalKeysResult"]['jobStatus'] == "COMPLETE": |
| 1061 | + return res["dataRowsForGlobalKeysResult"]['data'][ |
| 1062 | + 'fetchedDataRows'] |
| 1063 | + time.sleep(2) |
| 1064 | + timeout -= 2 |
| 1065 | + |
| 1066 | + raise TimeoutError( |
| 1067 | + "Timed out waiting for data rows for global keys job to complete.") |
0 commit comments