66from labelbox .orm .model import Field , Relationship , Entity
77
88
9- # Maps comparison operations to the suffixes appended to the field
10- # name when generating a GraphQL query.
11- COMPARISON_TO_SUFFIX = {
12- Comparison .Op .EQ : "" ,
13- Comparison .Op .NE : "_not" ,
14- Comparison .Op .LT : "_lt" ,
15- Comparison .Op .GT : "_gt" ,
16- Comparison .Op .LE : "_lte" ,
17- Comparison .Op .GE : "_gte" ,
18- }
9+ """ Common query creation functionality. """
1910
2011
2112def format_param_declaration (params ):
@@ -98,6 +89,14 @@ def format_clauses(self, params):
9889 def format_where (node ):
9990 """ Helper that resursively constructs a where clause from a
10091 LogicalExpression tree (leaf nodes are Comparisons). """
92+ COMPARISON_TO_SUFFIX = {
93+ Comparison .Op .EQ : "" ,
94+ Comparison .Op .NE : "_not" ,
95+ Comparison .Op .LT : "_lt" ,
96+ Comparison .Op .GT : "_gt" ,
97+ Comparison .Op .LE : "_lte" ,
98+ Comparison .Op .GE : "_gte" ,
99+ }
101100 assert isinstance (node , (Comparison , LogicalExpression ))
102101 if isinstance (node , Comparison ):
103102 param_name = "param_%d" % len (params )
@@ -331,81 +330,6 @@ def format_param_value(attribute, param):
331330 return query_str , {name : value for name , (value , _ ) in params .items ()}
332331
333332
334- def create_data_rows (dataset_id , json_file_url ):
335- """ Generates the query and parameters dictionary for creating multiple
336- DataRows for a Dataset.
337-
338- Args:
339- dataset_id (str): ID of the Dataset object to create DataRows for.
340- json_file_url (str): URL of the file containing row data.
341- Return:
342- (query_string, parameters_dict)
343- """
344- dataset_param = "dataSetId"
345- url_param = "jsonURL"
346- query_str = """mutation AppendRowsToDatasetPyApi(
347- $%s: ID!, $%s: String!){
348- appendRowsToDataset(data:{datasetId: $%s, jsonFileUrl: $%s}
349- ){ taskId accepted } } """ % (dataset_param , url_param , dataset_param ,
350- url_param )
351-
352- return query_str , {dataset_param : dataset_id , url_param : json_file_url }
353-
354-
355- def set_labeling_parameter_overrides (project , data ):
356- """ Constructs a query for setting labeling parameter overrides.
357- Args:
358- project (Project): The project to set param overrides for.
359- data (iterable): An iterable of tuples. Each tuple must contain
360- (DataRow, priority, numberOfLabels) for the new override.
361- Return:
362- (query_string, query_parameters)
363- """
364- data_str = ",\n " .join (
365- "{dataRow: {id: \" %s\" }, priority: %d, numLabels: %d }" % (
366- data_row .uid , priority , num_labels )
367- for data_row , priority , num_labels in data )
368- query_str = """mutation setLabelingParameterOverridesPyApi {
369- project(where: { id: "%s" }) {
370- setLabelingParameterOverrides(data: [%s]) { success } } } """ % (
371- project .uid , data_str )
372- return query_str , {}
373-
374-
375- def unset_labeling_parameter_overrides (project , data_rows ):
376- """ Constructs a query for unsetting labeling parameter overrides.
377- Args:
378- project (Project): The project to set param overrides for.
379- data_rows (iterable): An iterable of DataRow objects
380- for which the to set as parameter overrides.
381- Return:
382- (query_string, query_parameters)
383- """
384- data_str = ",\n " .join ("{dataRowId: \" %s\" }" % data_row .uid
385- for data_row in data_rows )
386- query_str = """mutation unsetLabelingParameterOverridesPyApi {
387- project(where: { id: "%s" }) {
388- unsetLabelingParameterOverrides(data: [%s]) { success } } } """ % (
389- project .uid , data_str )
390- return query_str , {}
391-
392-
393- def create_metadata (meta_type , meta_value , data_row_id ):
394- meta_type_param = "meta_type"
395- meta_value_param = "meta_value"
396- data_row_id_param = "data_row_id"
397- query_str = """mutation CreateAssetMetadataPyApi(
398- $%s: MetadataType!, $%s: String!, $%s: ID!) {
399- createAssetMetadata(data: {
400- metaType: $%s metaValue: $%s dataRowId: $%s}) {%s}} """ % (
401- meta_type_param , meta_value_param , data_row_id_param ,
402- meta_type_param , meta_value_param , data_row_id_param ,
403- results_query_part (Entity .named ("AssetMetadata" )))
404- return query_str , {meta_type_param : meta_type ,
405- meta_value_param : meta_value ,
406- data_row_id_param : data_row_id }
407-
408-
409333def update_relationship (a , b , relationship , update ):
410334 """ Updates the relationship in DB object `a` to connect or disconnect
411335 DB object `b`.
@@ -500,51 +424,6 @@ def delete(db_object):
500424 return query_str , {id_param : db_object .uid }
501425
502426
503- def project_labels (project , datasets , order_by ):
504- """ Returns the query and params for getting a Project's labels
505- relationship. A non-standard relationship query is used to support
506- filtering on Datasets.
507- Args:
508- datasets (list or None): The datasets filter. If None it's
509- ignored.
510- Return:
511- (query_string, params)
512- """
513- label_entity = Entity .named ("Label" )
514-
515- if datasets is not None :
516- where = " where:{dataRow: {dataset: {id_in: [%s]}}}" % ", " .join (
517- '"%s"' % dataset .uid for dataset in datasets )
518- else :
519- where = ""
520-
521- if order_by is not None :
522- check_order_by_clause (label_entity , order_by )
523- order_by_str = "orderBy: %s_%s" % (
524- order_by [0 ].graphql_name , order_by [1 ].name .upper ())
525- else :
526- order_by_str = ""
527-
528- query_str = """query GetProjectLabelsPyApi($project_id: ID!)
529- {project (where: {id: $project_id})
530- {labels (skip: %%d first: %%d%s%s) {%s}}}""" % (
531- where , order_by_str , results_query_part (label_entity ))
532- return query_str , {"project_id" : project .uid }
533-
534-
535- def export_labels ():
536- """ Returns the query and ID param for exporting a Project's
537- labels.
538- Return:
539- (query_string, id_param_name)
540- """
541- id_param = "projectId"
542- query_str = """mutation GetLabelExportUrlPyApi($%s: ID!) {exportLabels(data:{
543- projectId: $%s } ) {
544- downloadUrl createdAt shouldPoll } }
545- """ % (id_param , id_param )
546- return (query_str , id_param )
547-
548427
549428def bulk_delete (db_objects , use_where_clause ):
550429 """ Generates a query that bulk-deletes the given `db_objects` from the
@@ -567,70 +446,3 @@ def bulk_delete(db_objects, use_where_clause):
567446 ", " .join ('"%s"' % db_object .uid for db_object in db_objects )
568447 )
569448 return query_str , {}
570-
571-
572- def create_webhook (topics , url , secret , project ):
573- project_str = "" if project is None else ("project:{id:\" %s\" }," % project .uid )
574-
575- query_str = """mutation CreateWebhookPyApi {
576- createWebhook(data:{%s topics:{set:[%s]}, url:"%s", secret:"%s" }){%s}
577- } """ % (project_str , " " .join (topics ), url , secret ,
578- results_query_part (Entity .named ("Webhook" )))
579-
580- return query_str , {}
581-
582-
583- def update_webhook (webhook , topics , url , status ):
584- topics_str = "" if topics is None else "topics: {set: [%s]}" % " " .join (topics )
585- url_str = "" if url is None else "url: \" %s\" " % url
586- status_str = "" if status is None else "status: %s" % status
587-
588- query_str = """mutation UpdateWebhookPyApi {
589- updateWebhook(where: {id: "%s"} data:{%s}){%s}} """ % (
590- webhook .uid ,
591- ", " .join (filter (None , (topics_str , url_str , status_str ))),
592- results_query_part (type (webhook )))
593-
594- return query_str , {}
595-
596-
597- def project_review_metrics (project , net_score ):
598- project_id_param = "project_id"
599- net_score_literal = "None" if net_score is None else net_score .name
600- query_str = """query ProjectReviewMetricsPyApi($%s: ID!){
601- project(where: {id:$%s})
602- {reviewMetrics {labelAggregate(netScore: %s) {count}}}
603- }""" % (project_id_param , project_id_param , net_score_literal )
604-
605- return query_str , {project_id_param : project .uid }
606-
607-
608- def create_benchmark (label ):
609- label_id_param = "labelId"
610- query_str = """mutation CreateBenchmarkPyApi($%s: ID!) {
611- createBenchmark(data: {labelId: $%s}) {%s}} """ % (
612- label_id_param , label_id_param ,
613- results_query_part (Entity .named ("Benchmark" )))
614- return query_str , {label_id_param : label .uid }
615-
616-
617- def delete_benchmark (label ):
618- label_id_param = "labelId"
619- query_str = """mutation DeleteBenchmarkPyApi($%s: ID!) {
620- deleteBenchmark(where: {labelId: $%s}) {id}} """ % (
621- label_id_param , label_id_param )
622- return query_str , {label_id_param : label .uid }
623-
624-
625- def labeler_performance (project ):
626- project_id_param = "projectId"
627- query_str = """query LabelerPerformancePyApi($%s: ID!) {
628- project(where: {id: $%s}) {
629- labelerPerformance(skip: %%d first: %%d) {
630- count user {%s} secondsPerLabel totalTimeLabeling consensus
631- averageBenchmarkAgreement lastActivityTime}
632- }
633- }""" % (project_id_param , project_id_param ,
634- results_query_part (Entity .named ("User" )))
635-
636- return query_str , {project_id_param : project .uid }
0 commit comments