@@ -816,101 +816,193 @@ def query_programs_list(
816
816
raise typer .Exit (1 )
817
817
818
818
819
- @query_app .command (name = "run" )
820
- def query_run (query_id : str ):
821
- """Run a query program."""
819
+ @query_app .command (name = "get" )
820
+ def query_get (
821
+ query_id : str ,
822
+ publish : bool = typer .Option (False , "--publish" , help = "Publish the query program" ),
823
+ unpublish : bool = typer .Option (
824
+ False , "--unpublish" , help = "Unpublish the query program"
825
+ ),
826
+ run : bool = typer .Option (False , "--run" , help = "Run the query program" ),
827
+ analyze : bool = typer .Option (False , "--analyze" , help = "Analyze the query program" ),
828
+ ):
829
+ """Get a query program by ID and optionally perform actions on it."""
822
830
client = get_client ()
823
831
824
832
try :
825
- result = client .query_programs .evaluate (query_id )
833
+ query_program = client .query_programs .get (query_id )
834
+
835
+ if publish and unpublish :
836
+ typer .echo ("Error: Cannot use --publish and --unpublish together" , err = True )
837
+ raise typer .Exit (1 )
838
+
839
+ if publish :
840
+ query_program = client .query_programs .publish (query_id )
841
+ typer .echo (f"Query program { query_id } published successfully" )
826
842
827
- typer .echo ("Query executed successfully!" )
843
+ if unpublish :
844
+ query_program = client .query_programs .unpublish (query_id )
845
+ typer .echo (f"Query program { query_id } unpublished successfully" )
828
846
829
- if isinstance (result , dict ) and "data" in result :
830
- data = result ["data" ]
831
- if isinstance (data , list ) and data :
832
- table = Table ()
833
- headers = list (data [0 ].keys ())
834
- for header in headers :
835
- table .add_column (header )
847
+ if run :
848
+ result = client .query_programs .evaluate (query_id )
849
+ if isinstance (result , dict ) and "data" in result :
850
+ data = result ["data" ]
851
+ if isinstance (data , list ) and data :
852
+ table = Table ()
853
+ headers = list (data [0 ].keys ())
854
+ for header in headers :
855
+ table .add_column (header )
836
856
837
- for row in data :
838
- table .add_row (* [str (row .get (h , "" )) for h in headers ])
857
+ for row in data :
858
+ table .add_row (* [str (row .get (h , "" )) for h in headers ])
839
859
840
- console .print (table )
860
+ console .print (table )
861
+ else :
862
+ print (json .dumps (data , indent = 2 ))
841
863
else :
842
- print (json .dumps (data , indent = 2 ))
843
- else :
844
- print (json .dumps (result , indent = 2 ))
864
+ print (json .dumps (result , indent = 2 ))
865
+
866
+ if analyze :
867
+ # This is a placeholder for the analyze functionality
868
+ # Implement according to your API's analyze endpoint
869
+ typer .echo ("Analyzing query program..." )
870
+ analysis = client .query_programs .analyze (query_id )
871
+ print (json .dumps (analysis , indent = 2 ))
872
+
873
+ if not any ([publish , unpublish , run , analyze ]):
874
+ # Display query program details
875
+ table = Table ()
876
+ table .add_column ("Field" )
877
+ table .add_column ("Value" )
878
+
879
+ table .add_row ("ID" , query_program .id )
880
+ table .add_row ("Name" , query_program .name or "" )
881
+ table .add_row ("Published" , "Yes" if query_program .published else "No" )
882
+ table .add_row ("Public" , "Yes" if query_program .public else "No" )
883
+ table .add_row ("Query" , query_program .query or "" )
884
+ if query_program .created_at :
885
+ table .add_row ("Created At" , str (query_program .created_at ))
886
+ if query_program .updated_at :
887
+ table .add_row ("Updated At" , str (query_program .updated_at ))
888
+
889
+ console .print (table )
845
890
846
891
except Exception as e :
847
- typer .echo (f"Failed to run query: { e } " , err = True )
892
+ typer .echo (f"Failed to get query program : { e } " , err = True )
848
893
raise typer .Exit (1 )
849
894
850
895
851
- @query_app .command (name = "publish" )
852
- def query_publish (
853
- query_id : str ,
854
- group_slots : int | None = typer .Option (None , help = "Number of group slots" ),
896
+ @query_app .command (name = "select" )
897
+ def query_select (
898
+ publish : bool = typer .Option (
899
+ False , "--publish" , help = "Publish the selected query program"
900
+ ),
901
+ unpublish : bool = typer .Option (
902
+ False , "--unpublish" , help = "Unpublish the selected query program"
903
+ ),
904
+ run : bool = typer .Option (False , "--run" , help = "Run the selected query program" ),
905
+ analyze : bool = typer .Option (
906
+ False , "--analyze" , help = "Analyze the selected query program"
907
+ ),
855
908
):
856
- """Publish a query program."""
909
+ """Interactively select a query program and optionally perform actions on it ."""
857
910
client = get_client ()
858
911
859
- try :
860
- query_program = client .query_programs .publish (query_id , group_slots = group_slots )
861
-
862
- typer .echo ("Query program published successfully!" )
863
- typer .echo (f"ID: { query_program .id } " )
864
- typer .echo (f"Name: { query_program .name } " )
865
- typer .echo (f"Published: { query_program .published } " )
866
- typer .echo (f"Public: { query_program .public } " )
912
+ if publish and unpublish :
913
+ typer .echo ("Error: Cannot use --publish and --unpublish together" , err = True )
914
+ raise typer .Exit (1 )
867
915
868
- except Exception as e :
869
- typer .echo (f"Failed to publish query program: { e } " , err = True )
916
+ if not client .state .project_id :
917
+ typer .echo (
918
+ "No project selected. Please select a project first with 'nf projects select'" ,
919
+ err = True ,
920
+ )
870
921
raise typer .Exit (1 )
871
922
923
+ try :
924
+ query_programs = client .query_programs .list (project_id = client .state .project_id )
872
925
873
- @query_app .command (name = "unpublish" )
874
- def query_unpublish (query_id : str ):
875
- """Unpublish a query program."""
876
- client = get_client ()
926
+ if not query_programs :
927
+ typer .echo ("No query programs found" )
928
+ return
877
929
878
- try :
879
- query_program = client . query_programs . unpublish ( query_id )
930
+ # Create a list of choices
931
+ choices = { str ( i ): qp for i , qp in enumerate ( query_programs , 1 )}
880
932
881
- typer .echo ("Query program unpublished successfully!" )
882
- typer .echo (f"ID: { query_program .id } " )
883
- typer .echo (f"Name: { query_program .name } " )
884
- typer .echo (f"Published: { query_program .published } " )
933
+ # Display query programs with numbers
934
+ table = Table ()
935
+ table .add_column ("#" )
936
+ table .add_column ("ID" )
937
+ table .add_column ("Name" )
938
+ table .add_column ("Published" )
939
+ table .add_column ("Public" )
940
+ table .add_column ("Question" )
885
941
886
- except Exception as e :
887
- typer .echo (f"Failed to unpublish query program: { e } " , err = True )
888
- raise typer .Exit (1 )
942
+ for num , qp in choices .items ():
943
+ question = qp .query or ""
944
+ if len (question ) > 47 :
945
+ question = question [:47 ] + "..."
946
+ table .add_row (
947
+ num ,
948
+ qp .id ,
949
+ qp .name or "" ,
950
+ "Yes" if qp .published else "No" ,
951
+ "Yes" if qp .public else "No" ,
952
+ question ,
953
+ )
889
954
955
+ console .print (table )
890
956
891
- @query_app .command (name = "generate" )
892
- def query_generate (
893
- dataline_id : str ,
894
- name : str | None = typer .Option (None , help = "Name for the generated query program" ),
895
- ):
896
- """Generate a query program."""
897
- get_client ()
957
+ # Prompt for selection
958
+ choice = Prompt .ask (
959
+ "\n Select query program number" ,
960
+ choices = list (choices .keys ()),
961
+ show_choices = False ,
962
+ )
898
963
899
- try :
900
- # This is a placeholder as the actual API call would depend on the specific implementation
901
- typer .echo ("Query program generation started..." )
902
- typer .echo ("Analyzing data structure..." )
903
- typer .echo ("Generating query program..." )
964
+ selected_query = choices [choice ]
965
+ typer .echo (
966
+ f"\n Selected query program: { selected_query .name } (ID: { selected_query .id } )"
967
+ )
904
968
905
- # Mock data for example
906
- query_id = "qp-789ghi"
969
+ # Handle actions based on flags
970
+ if publish :
971
+ result = client .query_programs .publish (selected_query .id )
972
+ typer .echo (f"Query program { selected_query .id } published successfully" )
973
+
974
+ if unpublish :
975
+ result = client .query_programs .unpublish (selected_query .id )
976
+ typer .echo (f"Query program { selected_query .id } unpublished successfully" )
977
+
978
+ if run :
979
+ result = client .query_programs .evaluate (selected_query .id )
980
+ if isinstance (result , dict ) and "data" in result :
981
+ data = result ["data" ]
982
+ if isinstance (data , list ) and data :
983
+ table = Table ()
984
+ headers = list (data [0 ].keys ())
985
+ for header in headers :
986
+ table .add_column (header )
987
+
988
+ for row in data :
989
+ table .add_row (* [str (row .get (h , "" )) for h in headers ])
990
+
991
+ console .print (table )
992
+ else :
993
+ print (json .dumps (data , indent = 2 ))
994
+ else :
995
+ print (json .dumps (result , indent = 2 ))
907
996
908
- typer .echo ("Query program created successfully!" )
909
- typer .echo (f"ID: { query_id } " )
910
- typer .echo (f"Name: { name } " )
997
+ if analyze :
998
+ # This is a placeholder for the analyze functionality
999
+ # Implement according to your API's analyze endpoint
1000
+ typer .echo ("Analyzing query program..." )
1001
+ analysis = client .query_programs .analyze (selected_query .id )
1002
+ print (json .dumps (analysis , indent = 2 ))
911
1003
912
1004
except Exception as e :
913
- typer .echo (f"Failed to generate query program: { e } " , err = True )
1005
+ typer .echo (f"Failed to select query program: { e } " , err = True )
914
1006
raise typer .Exit (1 )
915
1007
916
1008
0 commit comments