kaqing 2.0.115__py3-none-any.whl → 2.0.172__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kaqing might be problematic. Click here for more details.

Files changed (187) hide show
  1. adam/__init__.py +0 -2
  2. adam/app_session.py +8 -11
  3. adam/batch.py +3 -3
  4. adam/checks/check_utils.py +14 -46
  5. adam/checks/cpu.py +7 -1
  6. adam/checks/cpu_metrics.py +52 -0
  7. adam/checks/disk.py +2 -3
  8. adam/columns/columns.py +3 -1
  9. adam/columns/cpu.py +3 -1
  10. adam/columns/cpu_metrics.py +22 -0
  11. adam/columns/memory.py +3 -4
  12. adam/commands/__init__.py +18 -0
  13. adam/commands/alter_tables.py +43 -47
  14. adam/commands/audit/audit.py +24 -25
  15. adam/commands/audit/audit_repair_tables.py +14 -17
  16. adam/commands/audit/audit_run.py +15 -23
  17. adam/commands/audit/show_last10.py +10 -13
  18. adam/commands/audit/show_slow10.py +10 -13
  19. adam/commands/audit/show_top10.py +10 -14
  20. adam/commands/audit/utils_show_top10.py +2 -3
  21. adam/commands/bash/__init__.py +5 -0
  22. adam/commands/bash/bash.py +8 -96
  23. adam/commands/bash/utils_bash.py +16 -0
  24. adam/commands/cat.py +14 -19
  25. adam/commands/cd.py +12 -100
  26. adam/commands/check.py +20 -21
  27. adam/commands/cli_commands.py +2 -3
  28. adam/commands/code.py +20 -23
  29. adam/commands/command.py +123 -39
  30. adam/commands/commands_utils.py +8 -17
  31. adam/commands/cp.py +33 -39
  32. adam/commands/cql/cql_completions.py +28 -10
  33. adam/commands/cql/cqlsh.py +10 -30
  34. adam/commands/cql/utils_cql.py +343 -0
  35. adam/commands/deploy/code_start.py +7 -10
  36. adam/commands/deploy/code_stop.py +4 -21
  37. adam/commands/deploy/code_utils.py +3 -3
  38. adam/commands/deploy/deploy.py +4 -27
  39. adam/commands/deploy/deploy_frontend.py +14 -17
  40. adam/commands/deploy/deploy_pg_agent.py +2 -5
  41. adam/commands/deploy/deploy_pod.py +65 -73
  42. adam/commands/deploy/deploy_utils.py +14 -24
  43. adam/commands/deploy/undeploy.py +4 -27
  44. adam/commands/deploy/undeploy_frontend.py +4 -7
  45. adam/commands/deploy/undeploy_pg_agent.py +5 -7
  46. adam/commands/deploy/undeploy_pod.py +11 -12
  47. adam/commands/devices/__init__.py +0 -0
  48. adam/commands/devices/device.py +118 -0
  49. adam/commands/devices/device_app.py +173 -0
  50. adam/commands/devices/device_auit_log.py +49 -0
  51. adam/commands/devices/device_cass.py +185 -0
  52. adam/commands/devices/device_export.py +86 -0
  53. adam/commands/devices/device_postgres.py +144 -0
  54. adam/commands/devices/devices.py +25 -0
  55. adam/commands/exit.py +1 -4
  56. adam/commands/export/clean_up_all_export_sessions.py +37 -0
  57. adam/commands/export/clean_up_export_sessions.py +51 -0
  58. adam/commands/export/drop_export_database.py +55 -0
  59. adam/commands/export/drop_export_databases.py +43 -0
  60. adam/commands/export/export.py +19 -26
  61. adam/commands/export/export_databases.py +174 -0
  62. adam/commands/export/export_handlers.py +71 -0
  63. adam/commands/export/export_select.py +48 -22
  64. adam/commands/export/export_select_x.py +54 -0
  65. adam/commands/export/export_use.py +19 -23
  66. adam/commands/export/exporter.py +353 -0
  67. adam/commands/export/import_session.py +40 -0
  68. adam/commands/export/importer.py +67 -0
  69. adam/commands/export/importer_athena.py +77 -0
  70. adam/commands/export/importer_sqlite.py +39 -0
  71. adam/commands/export/show_column_counts.py +54 -0
  72. adam/commands/export/show_export_databases.py +36 -0
  73. adam/commands/export/show_export_session.py +48 -0
  74. adam/commands/export/show_export_sessions.py +44 -0
  75. adam/commands/export/utils_export.py +223 -162
  76. adam/commands/help.py +1 -1
  77. adam/commands/intermediate_command.py +49 -0
  78. adam/commands/issues.py +11 -43
  79. adam/commands/kubectl.py +3 -6
  80. adam/commands/login.py +22 -24
  81. adam/commands/logs.py +3 -6
  82. adam/commands/ls.py +11 -128
  83. adam/commands/medusa/medusa.py +4 -22
  84. adam/commands/medusa/medusa_backup.py +20 -24
  85. adam/commands/medusa/medusa_restore.py +29 -33
  86. adam/commands/medusa/medusa_show_backupjobs.py +14 -18
  87. adam/commands/medusa/medusa_show_restorejobs.py +11 -18
  88. adam/commands/nodetool.py +6 -15
  89. adam/commands/param_get.py +11 -12
  90. adam/commands/param_set.py +9 -10
  91. adam/commands/postgres/postgres.py +41 -34
  92. adam/commands/postgres/postgres_context.py +57 -24
  93. adam/commands/postgres/postgres_ls.py +4 -8
  94. adam/commands/postgres/postgres_preview.py +5 -9
  95. adam/commands/postgres/psql_completions.py +1 -1
  96. adam/commands/postgres/utils_postgres.py +66 -0
  97. adam/commands/preview_table.py +5 -44
  98. adam/commands/pwd.py +14 -47
  99. adam/commands/reaper/reaper.py +4 -27
  100. adam/commands/reaper/reaper_forward.py +48 -55
  101. adam/commands/reaper/reaper_forward_session.py +6 -0
  102. adam/commands/reaper/reaper_forward_stop.py +10 -16
  103. adam/commands/reaper/reaper_restart.py +7 -14
  104. adam/commands/reaper/reaper_run_abort.py +11 -30
  105. adam/commands/reaper/reaper_runs.py +42 -57
  106. adam/commands/reaper/reaper_runs_abort.py +29 -49
  107. adam/commands/reaper/reaper_schedule_activate.py +11 -30
  108. adam/commands/reaper/reaper_schedule_start.py +10 -29
  109. adam/commands/reaper/reaper_schedule_stop.py +10 -29
  110. adam/commands/reaper/reaper_schedules.py +4 -14
  111. adam/commands/reaper/reaper_status.py +8 -16
  112. adam/commands/reaper/utils_reaper.py +196 -0
  113. adam/commands/repair/repair.py +4 -22
  114. adam/commands/repair/repair_log.py +5 -11
  115. adam/commands/repair/repair_run.py +27 -34
  116. adam/commands/repair/repair_scan.py +32 -38
  117. adam/commands/repair/repair_stop.py +5 -11
  118. adam/commands/report.py +27 -29
  119. adam/commands/restart.py +25 -26
  120. adam/commands/rollout.py +19 -24
  121. adam/commands/shell.py +10 -4
  122. adam/commands/show/show.py +10 -25
  123. adam/commands/show/show_cassandra_repairs.py +35 -0
  124. adam/commands/show/show_cassandra_status.py +32 -43
  125. adam/commands/show/show_cassandra_version.py +5 -18
  126. adam/commands/show/show_commands.py +19 -24
  127. adam/commands/show/show_host.py +1 -1
  128. adam/commands/show/show_login.py +20 -27
  129. adam/commands/show/show_processes.py +15 -19
  130. adam/commands/show/show_storage.py +10 -20
  131. adam/commands/watch.py +26 -29
  132. adam/config.py +5 -14
  133. adam/embedded_params.py +1 -1
  134. adam/log.py +4 -4
  135. adam/pod_exec_result.py +3 -3
  136. adam/repl.py +40 -103
  137. adam/repl_commands.py +32 -16
  138. adam/repl_state.py +57 -28
  139. adam/sql/sql_completer.py +44 -28
  140. adam/sql/sql_state_machine.py +89 -28
  141. adam/sso/authn_ad.py +6 -8
  142. adam/sso/authn_okta.py +4 -6
  143. adam/sso/cred_cache.py +3 -5
  144. adam/sso/idp.py +9 -12
  145. adam/utils.py +435 -6
  146. adam/utils_athena.py +57 -37
  147. adam/utils_audits.py +12 -14
  148. adam/utils_issues.py +32 -0
  149. adam/utils_k8s/app_clusters.py +13 -18
  150. adam/utils_k8s/app_pods.py +2 -0
  151. adam/utils_k8s/cassandra_clusters.py +22 -19
  152. adam/utils_k8s/cassandra_nodes.py +2 -2
  153. adam/utils_k8s/custom_resources.py +16 -17
  154. adam/utils_k8s/ingresses.py +2 -2
  155. adam/utils_k8s/jobs.py +7 -11
  156. adam/utils_k8s/k8s.py +87 -0
  157. adam/utils_k8s/pods.py +40 -77
  158. adam/utils_k8s/secrets.py +4 -4
  159. adam/utils_k8s/service_accounts.py +5 -4
  160. adam/utils_k8s/services.py +2 -2
  161. adam/utils_k8s/statefulsets.py +1 -12
  162. adam/utils_net.py +4 -4
  163. adam/utils_repl/__init__.py +0 -0
  164. adam/utils_repl/automata_completer.py +48 -0
  165. adam/utils_repl/repl_completer.py +46 -0
  166. adam/utils_repl/state_machine.py +173 -0
  167. adam/utils_sqlite.py +137 -0
  168. adam/version.py +1 -1
  169. {kaqing-2.0.115.dist-info → kaqing-2.0.172.dist-info}/METADATA +1 -1
  170. kaqing-2.0.172.dist-info/RECORD +230 -0
  171. adam/commands/app.py +0 -67
  172. adam/commands/app_ping.py +0 -44
  173. adam/commands/cql/cql_utils.py +0 -204
  174. adam/commands/devices.py +0 -147
  175. adam/commands/export/export_on_x.py +0 -76
  176. adam/commands/export/export_rmdbs.py +0 -65
  177. adam/commands/postgres/postgres_utils.py +0 -31
  178. adam/commands/reaper/reaper_session.py +0 -159
  179. adam/commands/show/show_app_actions.py +0 -56
  180. adam/commands/show/show_app_id.py +0 -47
  181. adam/commands/show/show_app_queues.py +0 -45
  182. adam/commands/show/show_repairs.py +0 -47
  183. adam/utils_export.py +0 -42
  184. kaqing-2.0.115.dist-info/RECORD +0 -203
  185. {kaqing-2.0.115.dist-info → kaqing-2.0.172.dist-info}/WHEEL +0 -0
  186. {kaqing-2.0.115.dist-info → kaqing-2.0.172.dist-info}/entry_points.txt +0 -0
  187. {kaqing-2.0.115.dist-info → kaqing-2.0.172.dist-info}/top_level.txt +0 -0
@@ -1,8 +1,11 @@
1
- from adam.commands.export.utils_export import export_tables
1
+ from adam.commands import extract_options
2
2
  from adam.commands.command import Command
3
- from adam.commands.cql.cql_utils import cassandra_table_names
3
+ from adam.commands.cql.utils_cql import cassandra_keyspaces, cassandra_table_names
4
+ from adam.commands.export.export_databases import ExportDatabases
5
+ from adam.commands.export.export_handlers import export
4
6
  from adam.repl_state import ReplState, RequiredState
5
- from adam.sql.sql_completer import SqlCompleter
7
+ from adam.sql.sql_completer import SqlCompleter, SqlVariant
8
+ from adam.utils import log
6
9
  from adam.utils_k8s.statefulsets import StatefulSets
7
10
 
8
11
  class ExportTables(Command):
@@ -27,34 +30,24 @@ class ExportTables(Command):
27
30
  if not(args := self.args(cmd)):
28
31
  return super().run(cmd, state)
29
32
 
30
- state, args = self.apply_state(args, state)
31
- if not self.validate_state(state):
32
- return state
33
-
34
- if not state.pod:
35
- state.push()
36
- state.pod = StatefulSets.pod_names(state.sts, state.namespace)[0]
37
- try:
38
- export_tables(args, state)
39
- finally:
40
- state.pop()
41
-
42
- return state
33
+ with self.validate(args, state) as (args, state):
34
+ with extract_options(args, '--export-only') as (args, export_only):
35
+ with export(state) as exporter:
36
+ return exporter.export(args, export_only=export_only)
43
37
 
44
38
  def completion(self, state: ReplState):
45
39
  def sc():
46
- return SqlCompleter(
47
- lambda: cassandra_table_names(state),
48
- dml='export',
49
- columns = lambda table: ['id', '*'],
50
- variant='cql'
51
- )
40
+ return SqlCompleter(lambda: cassandra_table_names(state), expandables={
41
+ 'dml':'export',
42
+ 'columns': lambda table: ['id', '*'],
43
+ 'keyspaces': lambda: cassandra_keyspaces(state),
44
+ 'export-dbs': lambda: ExportDatabases.database_names(),
45
+ }, variant=SqlVariant.CQL)
52
46
 
53
- if state.sts:
54
- return {ExportTables.COMMAND: sc()} | \
55
- {f'@{p}': {ExportTables.COMMAND: sc()} for p in StatefulSets.pod_names(state.sts, state.namespace)}
47
+ if super().completion(state):
48
+ return {f'@{p}': {ExportTables.COMMAND: sc()} for p in StatefulSets.pod_names(state.sts, state.namespace)}
56
49
 
57
50
  return {}
58
51
 
59
52
  def help(self, _: ReplState):
60
- return f'{ExportTables.COMMAND} [TABLE] [as target-name] [with consistency <level>]\t export table'
53
+ return f'{ExportTables.COMMAND} [* [in KEYSPACE]] | [TABLE] [as target-name] [with consistency <level>]\t export tables to Sqlite, Athena or CSV file'
@@ -0,0 +1,174 @@
1
+ from collections.abc import Callable
2
+ import os
3
+ import boto3
4
+
5
+ from adam.commands.export.utils_export import ExportTableStatus
6
+ from adam.config import Config
7
+ from adam.repl_session import ReplSession
8
+ from adam.utils import debug, lines_to_tabular, log, log2, ing, log_exc
9
+ from adam.utils_athena import Athena
10
+ from adam.utils_k8s.statefulsets import StatefulSets
11
+ from adam.utils_sqlite import SQLite
12
+
13
+ LIKE = 'e%_%'
14
+
15
+ class ExportDatabases:
16
+ def run_query(query: str, database: str, show_query=False, output: Callable[[str], str] = None) -> int:
17
+ cnt: int = 0
18
+
19
+ if show_query:
20
+ log2(query)
21
+
22
+ log_file = None
23
+ if database.startswith('s'):
24
+ cnt0, log_file = SQLite.run_query(query, database=database, output=output)
25
+ cnt += cnt0
26
+ else:
27
+ cnt0, log_file = Athena.run_query(query, database=database, output=output)
28
+ cnt += cnt0
29
+
30
+ if Config().get('repl.history.push-cat-log-file', True):
31
+ if log_file and ReplSession().prompt_session:
32
+ ReplSession().prompt_session.history.append_string(f':sh cat {log_file}')
33
+
34
+ return cnt
35
+
36
+ def sessions_from_dbs(dbs: list[str]):
37
+
38
+ sessions = set()
39
+
40
+ for db in dbs:
41
+ sessions.add(db.split('_')[0])
42
+
43
+ return list(sessions)
44
+
45
+ def drop_export_dbs(db: str = None):
46
+ dbs: list[str] = []
47
+
48
+ if not db or db.startswith('s'):
49
+ dbs.extend(ExportDatabases.drop_sqlite_dbs(db))
50
+ if not db or db.startswith('e'):
51
+ dbs.extend(ExportDatabases.drop_athena_dbs(db))
52
+
53
+ return dbs
54
+
55
+ def drop_sqlite_dbs(db: str = None):
56
+ dbs = SQLite.database_names(db)
57
+ if dbs:
58
+ with ing(f'Droping {len(dbs)} SQLite databases'):
59
+ with log_exc():
60
+ for db in dbs:
61
+ file_path = f'{SQLite.local_db_dir()}/{db}'
62
+ try:
63
+ os.remove(file_path)
64
+ except OSError as e:
65
+ pass
66
+
67
+ return dbs
68
+
69
+ def drop_athena_dbs(db: str = None):
70
+ dbs = Athena.database_names(f'{db}_%' if db else LIKE)
71
+ if dbs:
72
+ with ing(f'Droping {len(dbs)} Athena databases'):
73
+ for db in dbs:
74
+ query = f'DROP DATABASE {db} CASCADE'
75
+ debug(query)
76
+ Athena.query(query)
77
+
78
+ with ing(f'Deleting s3 folder: export'):
79
+ with log_exc():
80
+ if not db:
81
+ db = ''
82
+
83
+ s3 = boto3.resource('s3')
84
+ bucket = s3.Bucket(Config().get('export.bucket', 'c3.ops--qing'))
85
+ bucket.objects.filter(Prefix=f'export/{db}').delete()
86
+
87
+ return dbs
88
+
89
+ def display_export_db(export_session: str):
90
+ if not export_session:
91
+ return
92
+
93
+ Athena.clear_cache()
94
+
95
+ keyspaces = {}
96
+ for table in ExportDatabases.table_names(export_session):
97
+ keyspace = table.split('.')[0]
98
+ if keyspace in keyspaces:
99
+ keyspaces[keyspace] += 1
100
+ else:
101
+ keyspaces[keyspace] = 1
102
+
103
+ log(lines_to_tabular([f'{k},{v}' for k, v in keyspaces.items()], header='SCHEMA,# of TABLES', separator=','))
104
+
105
+ def disply_export_session(sts: str, pod: str, namespace: str, session: str):
106
+ if not pod:
107
+ pod = StatefulSets.pod_names(sts, namespace)[0]
108
+
109
+ if not pod:
110
+ return
111
+
112
+ tables, _ = ExportTableStatus.from_session(sts, pod, namespace, session)
113
+ log()
114
+ log(lines_to_tabular([f'{table.keyspace}\t{table.table}\t{table.target_table}\t{"export_completed_pending_import" if table.status == "pending_import" else table.status}' for table in tables], header='KEYSPACE\tTABLE\tTARGET_TABLE\tSTATUS', separator='\t'))
115
+
116
+ def database_names():
117
+ return ExportDatabases.copy_database_names() + ExportDatabases.export_database_names()
118
+
119
+ def copy_database_names():
120
+ return list({n.split('_')[0] for n in SQLite.database_names()})
121
+
122
+ def export_database_names():
123
+ return list({n.split('_')[0] for n in Athena.database_names(LIKE)})
124
+
125
+ def database_names_with_keyspace_cnt(importer: str = None):
126
+ r = {}
127
+
128
+ names = []
129
+ if not importer:
130
+ names = SQLite.database_names() + Athena.database_names(LIKE)
131
+ elif importer == 'athena':
132
+ names = Athena.database_names(LIKE)
133
+ else:
134
+ names = SQLite.database_names()
135
+
136
+ for n in names:
137
+ tokens = n.split('_')
138
+ name = tokens[0]
139
+ keyspace = None
140
+ if len(tokens) > 1:
141
+ keyspace = tokens[1].replace('.db', '')
142
+
143
+ if keyspace == 'root':
144
+ continue
145
+
146
+ if name in r:
147
+ r[name] += 1
148
+ else:
149
+ r[name] = 1
150
+
151
+ return r
152
+
153
+ def table_names(session: str):
154
+ tables = []
155
+
156
+ for session in ExportDatabases._session_database_names(session):
157
+ if session.startswith('s'):
158
+ for table in SQLite.table_names(database=session):
159
+ tables.append(f'{SQLite.keyspace(session)}.{table}')
160
+ else:
161
+ for table in Athena.table_names(database=session, function='export'):
162
+ tables.append(f'{session}.{table}')
163
+
164
+ return tables
165
+
166
+ def _session_database_names(db: str):
167
+ eprefix = db
168
+ if '_' in db:
169
+ eprefix = db.split('_')[0]
170
+
171
+ if db.startswith('s'):
172
+ return SQLite.database_names(prefix=f'{eprefix}_')
173
+ else:
174
+ return Athena.database_names(like=f'{eprefix}_%')
@@ -0,0 +1,71 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.commands.export.exporter import Exporter
4
+ from adam.commands.export.utils_export import ExportSpec, state_with_pod
5
+ from adam.repl_state import ReplState
6
+ from adam.utils import log, log2
7
+
8
+ class ExportService:
9
+ def __init__(self, handler: 'ExporterHandler'):
10
+ self.handler = handler
11
+
12
+ def export(self, args: list[str], export_only=False):
13
+ state = self.handler.state
14
+ export_session = state.export_session
15
+ spec: ExportSpec = None
16
+ try:
17
+ with state_with_pod(state) as state:
18
+ # --export-only for testing only
19
+ statuses, spec = Exporter.export_tables(args, state, export_only=export_only)
20
+ if not statuses:
21
+ return state
22
+
23
+ Exporter.clear_export_session_cache()
24
+
25
+ if spec.importer == 'csv' or export_only:
26
+ ExportDatabases.disply_export_session(state.sts, state.pod, state.namespace, spec.session)
27
+ else:
28
+ log()
29
+ ExportDatabases.display_export_db(state.export_session)
30
+ finally:
31
+ # if exporting to csv, do not bind the new session id to repl state
32
+ if spec and spec.importer == 'csv':
33
+ state.export_session = export_session
34
+
35
+ return state
36
+
37
+ def import_sesion(self, args: list[str]):
38
+ state = self.handler.state
39
+
40
+ if not args:
41
+ if state.in_repl:
42
+ log2('Specify export session name.')
43
+ else:
44
+ log2('* Export session name is missing.')
45
+
46
+ Command.display_help()
47
+
48
+ return 'command-missing'
49
+
50
+ with state_with_pod(state) as state:
51
+ tables, _ = Exporter.import_session(args, state)
52
+ if tables:
53
+ Exporter.clear_export_session_cache()
54
+
55
+ log()
56
+ ExportDatabases.display_export_db(state.export_session)
57
+
58
+ return state
59
+
60
+ class ExporterHandler:
61
+ def __init__(self, state: ReplState):
62
+ self.state = state
63
+
64
+ def __enter__(self):
65
+ return ExportService(self)
66
+
67
+ def __exit__(self, exc_type, exc_val, exc_tb):
68
+ return False
69
+
70
+ def export(state: ReplState):
71
+ return ExporterHandler(state)
@@ -1,11 +1,15 @@
1
+ from datetime import datetime
2
+ from adam.commands import extract_trailing_options
1
3
  from adam.commands.command import Command
4
+ from adam.commands.export.export_databases import ExportDatabases
5
+ from adam.config import Config
2
6
  from adam.repl_state import ReplState, RequiredState
3
- from adam.sql.sql_completer import SqlCompleter
7
+ from adam.sql.sql_completer import SqlCompleter, SqlVariant
4
8
  from adam.utils import log2
5
9
  from adam.utils_athena import Athena
6
10
 
7
11
  class ExportSelect(Command):
8
- COMMAND = '&select'
12
+ COMMAND = '.select'
9
13
 
10
14
  # the singleton pattern
11
15
  def __new__(cls, *args, **kwargs):
@@ -20,49 +24,71 @@ class ExportSelect(Command):
20
24
  return ExportSelect.COMMAND
21
25
 
22
26
  def required(self):
23
- return [RequiredState.CLUSTER_OR_POD, ReplState.X]
27
+ return RequiredState.EXPORT_DB
24
28
 
25
29
  def run(self, cmd: str, state: ReplState):
26
30
  if not(args := self.args(cmd)):
27
31
  return super().run(cmd, state)
28
32
 
29
- state, args = self.apply_state(args, state)
30
- if not self.validate_state(state):
31
- return state
33
+ with self.validate(args, state) as (args, state):
34
+ with extract_trailing_options(args, '&') as (args, backgrounded):
35
+ if not state.export_session:
36
+ if state.in_repl:
37
+ if state.device == ReplState.C:
38
+ log2("Select an export database first with 'use' command.")
39
+ else:
40
+ log2('cd to an export database first.')
41
+ else:
42
+ log2('* export database is missing.')
32
43
 
33
- if not args:
34
- if state.in_repl:
35
- log2('Use a SQL statement.')
36
- else:
37
- log2('* SQL statement is missing.')
44
+ Command.display_help()
38
45
 
39
- Command.display_help()
46
+ return 'command-missing'
40
47
 
41
- return 'command-missing'
48
+ if not args:
49
+ if state.in_repl:
50
+ log2('Use a SQL statement.')
51
+ else:
52
+ log2('* SQL statement is missing.')
42
53
 
43
- query = ' '.join(args)
54
+ Command.display_help()
44
55
 
45
- Athena.run_query(f'select {query}', database=f'export_{state.export_session}')
56
+ return 'command-missing'
46
57
 
47
- return state
58
+ query = ' '.join(args)
59
+
60
+ def output(out: str):
61
+ log_prefix = Config().get('export.log-prefix', '/tmp/qing')
62
+ log_file = f'{log_prefix}-{datetime.now().strftime("%d%H%M%S")}-sqlite.log'
63
+
64
+ with open(log_file, 'w') as f:
65
+ f.write(out)
66
+
67
+ return log_file
68
+
69
+ ExportDatabases.run_query(f'select {query}', database=state.export_session, output=output if backgrounded else None)
70
+
71
+ return state
48
72
 
49
73
  def completion(self, state: ReplState):
50
74
  if not state.export_session:
51
75
  return {}
52
76
 
53
- db = f'export_{state.export_session}'
77
+ db = state.export_session
54
78
 
55
79
  # warm up the caches first time when x: drive is accessed
56
- Athena.table_names(database=db, function='export')
80
+ ExportDatabases.table_names(db)
57
81
  Athena.column_names(database=db, function='export')
58
82
  Athena.column_names(partition_cols_only=True, database=db, function='export')
59
83
 
60
84
  return {ExportSelect.COMMAND: SqlCompleter(
61
- lambda: Athena.table_names(database=db, function='export'),
85
+ lambda: ExportDatabases.table_names(db),
62
86
  dml='select',
63
- columns=lambda table: Athena.column_names(database=db, function='export'),
64
- variant='athena'
87
+ expandables={
88
+ 'columns':lambda table: Athena.column_names(database=db, function='export'),
89
+ },
90
+ variant=SqlVariant.ATHENA
65
91
  )}
66
92
 
67
93
  def help(self, _: ReplState):
68
- return f'<sql-select-statements>\t run queries on export database'
94
+ return f'.<sql-select-statements>\t run queries on export database'
@@ -0,0 +1,54 @@
1
+ from adam.commands.command import Command
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.repl_state import ReplState, RequiredState
4
+ from adam.sql.sql_completer import SqlCompleter, SqlVariant
5
+ from adam.utils_athena import Athena
6
+
7
+ # No action body, only for a help entry and auto-completion
8
+ class ExportSelectX(Command):
9
+ COMMAND = 'select_on_x'
10
+
11
+ # the singleton pattern
12
+ def __new__(cls, *args, **kwargs):
13
+ if not hasattr(cls, 'instance'): cls.instance = super(ExportSelectX, cls).__new__(cls)
14
+
15
+ return cls.instance
16
+
17
+ def __init__(self, successor: Command=None):
18
+ super().__init__(successor)
19
+
20
+ def command(self):
21
+ return ExportSelectX.COMMAND
22
+
23
+ def required(self):
24
+ return RequiredState.EXPORT_DB
25
+
26
+ def completion(self, state: ReplState):
27
+ completions = {}
28
+
29
+ if state.device == ReplState.X:
30
+ completions = {'drop': SqlCompleter(
31
+ lambda: ExportDatabases.table_names(state.export_session),
32
+ dml='drop',
33
+ expandables={
34
+ 'export-dbs': lambda: ExportDatabases.database_names(),
35
+ 'columns':lambda _: Athena.column_names(database=state.export_session, function='export'),
36
+ },
37
+ variant=SqlVariant.ATHENA
38
+ )}
39
+
40
+ if state.export_session:
41
+ completions |= {'select': SqlCompleter(
42
+ lambda: ExportDatabases.table_names(state.export_session),
43
+ dml='select',
44
+ expandables={
45
+ 'export-dbs': lambda: ExportDatabases.database_names(),
46
+ 'columns':lambda _: Athena.column_names(database=state.export_session, function='export'),
47
+ },
48
+ variant=SqlVariant.ATHENA
49
+ )}
50
+
51
+ return completions
52
+
53
+ def help(self, _: ReplState):
54
+ return f'<sql-select-statements>\t run queries on export database'
@@ -1,12 +1,12 @@
1
1
  from adam.commands.command import Command
2
- from adam.commands.export.export_select import ExportSelect
3
- from adam.config import Config
4
- from adam.repl_state import ReplState, RequiredState
2
+ from adam.commands.export.export_databases import ExportDatabases
3
+ from adam.repl_state import ReplState
5
4
  from adam.utils import log2
6
5
  from adam.utils_athena import Athena
6
+ from adam.utils_sqlite import SQLite
7
7
 
8
8
  class ExportUse(Command):
9
- COMMAND = '&use'
9
+ COMMAND = 'use'
10
10
 
11
11
  # the singleton pattern
12
12
  def __new__(cls, *args, **kwargs):
@@ -21,36 +21,32 @@ class ExportUse(Command):
21
21
  return ExportUse.COMMAND
22
22
 
23
23
  def required(self):
24
- return RequiredState.CLUSTER_OR_POD
24
+ return [ReplState.C, ReplState.X]
25
25
 
26
26
  def run(self, cmd: str, state: ReplState):
27
27
  if not(args := self.args(cmd)):
28
28
  return super().run(cmd, state)
29
29
 
30
- state, args = self.apply_state(args, state)
31
- if not self.validate_state(state):
32
- return state
30
+ with self.validate(args, state) as (args, state):
31
+ if not args:
32
+ state.export_session = None
33
33
 
34
- if not args:
35
- if state.in_repl:
36
- log2('Specify database to use.')
37
- else:
38
- log2('* database is missing.')
34
+ log2('Export database is unset.')
39
35
 
40
- Command.display_help()
36
+ return state
41
37
 
42
- return 'command-missing'
38
+ state.export_session = args[0]
39
+ if state.export_session.startswith('e'):
40
+ Athena.clear_cache()
41
+ else:
42
+ SQLite.clear_cache()
43
43
 
44
- state.export_session = args[0].replace('export_', '') if args[0].startswith('export_') else args[0]
45
- Athena.clear_cache()
44
+ ExportDatabases.display_export_db(state.export_session)
46
45
 
47
- return state
46
+ return state
48
47
 
49
48
  def completion(self, state: ReplState):
50
- # warm up the caches first time when l: drive is accessed
51
- Athena.database_names()
52
-
53
- return super().completion(state, {n: None for n in Athena.database_names()})
49
+ return super().completion(state, {n: None for n in ExportDatabases.database_names()})
54
50
 
55
51
  def help(self, _: ReplState):
56
- return f'{ExportUse.COMMAND} <export db name>\t use Export Database'
52
+ return f'{ExportUse.COMMAND} <export-database-name>\t use export database'