meerschaum.connectors.api
Interact with the Meerschaum API (send commands, pull data, etc.)
23class APIConnector(InstanceConnector): 24 """ 25 Connect to a Meerschaum API instance. 26 27 The requests are made by the Rust client (`meerschaum._core.ApiConnector`, one per connector 28 and process): the same routes, query parameters and bodies as v4. Nothing here imports 29 `requests` or `httpx`. 30 """ 31 32 IS_THREAD_SAFE: bool = False 33 OPTIONAL_ATTRIBUTES: List[str] = ['port', 'client_secret', 'client_id', 'api_key'] 34 35 from ._request import ( 36 make_request, 37 get, 38 post, 39 put, 40 patch, 41 delete, 42 wget, 43 ) 44 from ._actions import ( 45 get_actions, 46 do_action, 47 do_action_async, 48 do_action_legacy, 49 ) 50 from ._misc import get_mrsm_version, get_chaining_status 51 from ._metrics import get_engine_metrics, initialize_engine_metrics 52 from ._pipes import ( 53 get_pipe_instance_keys, 54 register_pipe, 55 fetch_pipes_keys, 56 edit_pipe, 57 sync_pipe, 58 delete_pipe, 59 rename_pipe, 60 delete_pipe_cache, 61 get_pipe_data, 62 get_pipe_id, 63 get_pipe_attributes, 64 get_sync_time, 65 pipe_exists, 66 create_metadata, 67 get_pipe_rowcount, 68 drop_pipe, 69 clear_pipe, 70 get_pipe_columns_types, 71 get_pipe_columns_indices, 72 get_pipe_index_names, 73 get_pipe_docs, 74 get_pipe_size, 75 compress_pipe, 76 decompress_pipe, 77 vacuum_pipe, 78 analyze_pipe, 79 partition_pipe, 80 ) 81 from ._fetch import fetch 82 from ._plugins import ( 83 register_plugin, 84 install_plugin, 85 delete_plugin, 86 get_plugins, 87 get_plugin_attributes, 88 get_plugin_version, 89 ) 90 from ._login import login, test_connection 91 from ._users import ( 92 register_user, 93 get_user_id, 94 get_users, 95 edit_user, 96 delete_user, 97 get_user_password_hash, 98 get_user_type, 99 get_user_attributes, 100 ) 101 from ._tokens import ( 102 register_token, 103 get_token_model, 104 get_tokens, 105 edit_token, 106 invalidate_token, 107 get_token_scopes, 108 token_exists, 109 delete_token, 110 ) 111 from ._uri import from_uri 112 from ._jobs import ( 113 get_jobs, 114 get_job, 115 get_job_metadata, 116 get_job_properties, 117 get_job_exists, 118 delete_job, 119 start_job, 120 create_job, 121 stop_job, 122 pause_job, 123 get_logs, 124 get_job_stop_time, 125 monitor_logs, 126 monitor_logs_async, 127 get_job_is_blocking_on_stdin, 128 get_job_prompt_kwargs, 129 get_job_began, 130 get_job_ended, 131 get_job_paused, 132 get_job_status, 133 ) 134 ### The access model: teams, permissions, access and locks are the server's. The 135 ### `InstanceConnector` methods reach its routes through the Rust client; these answer in 136 ### one request. 137 from ._access import ( 138 get_team_pipes_count, 139 get_team_jobs_count, 140 get_users_permissions, 141 get_user_permissions_record, 142 get_pipe_access, 143 ) 144 145 def __init__( 146 self, 147 label: Optional[str] = None, 148 wait: bool = False, 149 debug: bool = False, 150 **kw 151 ): 152 if 'uri' in kw: 153 from_uri_params = self.from_uri(kw['uri'], as_dict=True) 154 label = label or from_uri_params.get('label', None) 155 _ = from_uri_params.pop('label', None) 156 kw.update(from_uri_params) 157 158 super().__init__('api', label=label, **kw) 159 if 'protocol' not in self.__dict__: 160 self.protocol = ( 161 'https' if self.__dict__.get('uri', '').startswith('https') 162 else 'http' 163 ) 164 165 if 'uri' not in self.__dict__: 166 self.verify_attributes(required_attributes) 167 else: 168 conn_attrs = self.from_uri(self.__dict__['uri'], as_dict=True) 169 self.__dict__.update({ 170 key: val 171 for key, val in conn_attrs.items() 172 if key not in ('label', 'protocol') 173 }) 174 175 self.url = ( 176 self.protocol + '://' + 177 self.host 178 + ( 179 (':' + str(self.port)) 180 if self.__dict__.get('port', None) 181 else '' 182 ) 183 ) 184 self._rust_client_obj = None 185 self._instance_keys = self.__dict__.get('instance_keys', None) 186 187 def _rust_client(self): 188 """ 189 Return the `meerschaum._core.ApiConnector` of this connector (built once per process: 190 a forked child builds its own, which holds its own HTTP connections and token). 191 """ 192 import os 193 attributes = { 194 key: value 195 for key, value in self.__dict__.items() 196 if not str(key).startswith('_') 197 and isinstance(value, (str, int, float, bool)) 198 } 199 client = self.__dict__.get('_rust_client_obj', None) 200 if ( 201 client is not None 202 and self.__dict__.get('_rust_client_pid', None) == os.getpid() 203 and self.__dict__.get('_rust_client_attributes', None) == attributes 204 ): 205 return client 206 207 ### A new process, or credentials / address changed (`conn.password = ...`): build a client 208 ### with the current attributes (it logs in again; the native flows find it by them). 209 from meerschaum import _core 210 client = _core.ApiConnector(self.label, attributes) 211 self.__dict__['_rust_client_obj'] = client 212 self.__dict__['_rust_client_pid'] = os.getpid() 213 self.__dict__['_rust_client_attributes'] = attributes 214 return client 215 216 def __getstate__(self): 217 state = self.__dict__.copy() 218 for key in ('_rust_client_obj', '_rust_client_pid', '_rust_client_attributes', '_session'): 219 state.pop(key, None) 220 state['_rust_client_obj'] = None 221 return state 222 223 def __setstate__(self, state): 224 self.__dict__.update(state) 225 226 @property 227 def URI(self) -> str: 228 """ 229 Return the fully qualified URI. 230 """ 231 return self._rust_client().uri 232 233 @property 234 def session(self): 235 """ 236 A `requests.Session` (not used by the connector, which makes its requests in Rust). 237 It exists for plugins which still reach for it and needs `requests` installed. 238 """ 239 if self.__dict__.get('_session', None) is None: 240 from meerschaum.utils.packages import attempt_import 241 requests = attempt_import('requests', lazy=False) 242 if requests is None: 243 raise ImportError("Failed to import requests. Is requests installed?") 244 self._session = requests.Session() 245 return self._session 246 247 @property 248 def token(self): 249 """ 250 Return the API key, or the session token (logging in when it is missing or expiring). 251 """ 252 return self._rust_client().token 253 254 @property 255 def _token(self) -> Union[str, None]: 256 """ 257 The stored session token (v4's attribute): `None` before a login, and it never logs in. 258 Assign `None` to force a new login. 259 """ 260 return self._rust_client().stored_token 261 262 @_token.setter 263 def _token(self, value: Union[str, None]) -> None: 264 self._rust_client().stored_token = value 265 266 @property 267 def _expires(self): 268 """ 269 When the stored session token expires (v4's attribute, a naive `datetime`), or `None`. 270 """ 271 import datetime 272 value = self._rust_client().stored_expires 273 return None if value is None else datetime.datetime.fromisoformat(value) 274 275 @_expires.setter 276 def _expires(self, value) -> None: 277 import datetime 278 if value is not None: 279 if isinstance(value, str): 280 value = datetime.datetime.fromisoformat(value) 281 if not isinstance(value, datetime.datetime): 282 raise TypeError( 283 f"`_expires` must be a datetime, an ISO string or None, not {type(value).__name__}." 284 ) 285 ### Stored as naive UTC, like v4's: an aware value is converted, not just stripped. 286 if value.tzinfo is not None: 287 value = value.astimezone(datetime.timezone.utc).replace(tzinfo=None) 288 value = value.isoformat() 289 self._rust_client().stored_expires = value 290 291 @property 292 def instance_keys(self) -> Union[str, None]: 293 """ 294 Return the instance keys to be sent alongside pipe requests. 295 """ 296 return self._instance_keys 297 298 @property 299 def login_scheme(self) -> str: 300 """ 301 Return the login scheme to use based on the configured credentials. 302 """ 303 return self._rust_client().login_scheme
Connect to a Meerschaum API instance.
The requests are made by the Rust client (meerschaum._core.ApiConnector, one per connector
and process): the same routes, query parameters and bodies as v4. Nothing here imports
requests or httpx.
145 def __init__( 146 self, 147 label: Optional[str] = None, 148 wait: bool = False, 149 debug: bool = False, 150 **kw 151 ): 152 if 'uri' in kw: 153 from_uri_params = self.from_uri(kw['uri'], as_dict=True) 154 label = label or from_uri_params.get('label', None) 155 _ = from_uri_params.pop('label', None) 156 kw.update(from_uri_params) 157 158 super().__init__('api', label=label, **kw) 159 if 'protocol' not in self.__dict__: 160 self.protocol = ( 161 'https' if self.__dict__.get('uri', '').startswith('https') 162 else 'http' 163 ) 164 165 if 'uri' not in self.__dict__: 166 self.verify_attributes(required_attributes) 167 else: 168 conn_attrs = self.from_uri(self.__dict__['uri'], as_dict=True) 169 self.__dict__.update({ 170 key: val 171 for key, val in conn_attrs.items() 172 if key not in ('label', 'protocol') 173 }) 174 175 self.url = ( 176 self.protocol + '://' + 177 self.host 178 + ( 179 (':' + str(self.port)) 180 if self.__dict__.get('port', None) 181 else '' 182 ) 183 ) 184 self._rust_client_obj = None 185 self._instance_keys = self.__dict__.get('instance_keys', None)
226 @property 227 def URI(self) -> str: 228 """ 229 Return the fully qualified URI. 230 """ 231 return self._rust_client().uri
Return the fully qualified URI.
233 @property 234 def session(self): 235 """ 236 A `requests.Session` (not used by the connector, which makes its requests in Rust). 237 It exists for plugins which still reach for it and needs `requests` installed. 238 """ 239 if self.__dict__.get('_session', None) is None: 240 from meerschaum.utils.packages import attempt_import 241 requests = attempt_import('requests', lazy=False) 242 if requests is None: 243 raise ImportError("Failed to import requests. Is requests installed?") 244 self._session = requests.Session() 245 return self._session
A requests.Session (not used by the connector, which makes its requests in Rust).
It exists for plugins which still reach for it and needs requests installed.
247 @property 248 def token(self): 249 """ 250 Return the API key, or the session token (logging in when it is missing or expiring). 251 """ 252 return self._rust_client().token
Return the API key, or the session token (logging in when it is missing or expiring).
291 @property 292 def instance_keys(self) -> Union[str, None]: 293 """ 294 Return the instance keys to be sent alongside pipe requests. 295 """ 296 return self._instance_keys
Return the instance keys to be sent alongside pipe requests.
298 @property 299 def login_scheme(self) -> str: 300 """ 301 Return the login scheme to use based on the configured credentials. 302 """ 303 return self._rust_client().login_scheme
Return the login scheme to use based on the configured credentials.
21def make_request( 22 self, 23 method: str, 24 r_url: str, 25 headers: Optional[Dict[str, Any]] = None, 26 use_token: bool = True, 27 debug: bool = False, 28 **kwargs: Any 29) -> 'meerschaum._core.ApiResponse': 30 """ 31 Make a request to the API and return a response object 32 (`status_code`, `ok`, `text`, `content`, `headers`, `json()`, `raise_for_status()`). 33 34 Parameters 35 ---------- 36 method: str 37 The name of the request method (e.g. `'GET'`, `'POST'`). 38 39 r_url: str 40 The relative URL for the request (e.g. `'/pipes'`). 41 42 headers: Optional[Dict[str, Any]], default None 43 Headers to override the default headers. 44 45 use_token: bool, default True 46 If `True`, add the authorization token to the headers. 47 48 debug: bool, default False 49 Verbosity toggle. 50 51 kwargs: Any 52 `params` (the query), `json`, `data`, `files` and `timeout` are sent as `requests` would. 53 """ 54 if method.upper() not in METHODS: 55 raise ValueError(f"Method '{method}' is not supported.") 56 57 if debug: 58 dprint(f"[{self}] Sending a '{method.upper()}' request to {self.url}{r_url}") 59 60 return self._rust_client().make_request( 61 method, 62 r_url, 63 headers=headers, 64 use_token=use_token, 65 **kwargs 66 )
Make a request to the API and return a response object
(status_code, ok, text, content, headers, json(), raise_for_status()).
Parameters
- method (str):
The name of the request method (e.g.
'GET','POST'). - r_url (str):
The relative URL for the request (e.g.
'/pipes'). - headers (Optional[Dict[str, Any]], default None): Headers to override the default headers.
- use_token (bool, default True):
If
True, add the authorization token to the headers. - debug (bool, default False): Verbosity toggle.
- kwargs (Any):
params(the query),json,data,filesandtimeoutare sent asrequestswould.
69def get(self, r_url: str, **kwargs: Any) -> 'meerschaum._core.ApiResponse': 70 """ 71 Send a `GET` request (see `make_request()`). 72 """ 73 return self.make_request('GET', r_url, **kwargs)
Send a GET request (see make_request()).
76def post(self, r_url: str, **kwargs: Any) -> 'meerschaum._core.ApiResponse': 77 """ 78 Send a `POST` request (see `make_request()`). 79 """ 80 return self.make_request('POST', r_url, **kwargs)
Send a POST request (see make_request()).
90def put(self, r_url: str, **kwargs: Any) -> 'meerschaum._core.ApiResponse': 91 """ 92 Send a `PUT` request (see `make_request()`). 93 """ 94 return self.make_request('PUT', r_url, **kwargs)
Send a PUT request (see make_request()).
83def patch(self, r_url: str, **kwargs: Any) -> 'meerschaum._core.ApiResponse': 84 """ 85 Send a `PATCH` request (see `make_request()`). 86 """ 87 return self.make_request('PATCH', r_url, **kwargs)
Send a PATCH request (see make_request()).
97def delete(self, r_url: str, **kwargs: Any) -> 'meerschaum._core.ApiResponse': 98 """ 99 Send a `DELETE` request (see `make_request()`). 100 """ 101 return self.make_request('DELETE', r_url, **kwargs)
Send a DELETE request (see make_request()).
104def wget( 105 self, 106 r_url: str, 107 dest: Optional[Union[str, pathlib.Path]] = None, 108 headers: Optional[Dict[str, Any]] = None, 109 use_token: bool = True, 110 debug: bool = False, 111 **kw: Any 112) -> pathlib.Path: 113 """Mimic wget: download a resource of the API (with the token) to `dest`.""" 114 from meerschaum.utils.misc import wget 115 if headers is None: 116 headers = {} 117 if use_token: 118 headers.update({'Authorization': f'Bearer {self.token}'}) 119 request_url = urllib.parse.urljoin(self.url, r_url) 120 if debug: 121 dprint( 122 f"[{self}] Downloading {request_url}" 123 + (f' to {dest}' if dest is not None else '') 124 + "..." 125 ) 126 return wget(request_url, dest=dest, headers=headers, **kw)
Mimic wget: download a resource of the API (with the token) to dest.
18def get_actions(self): 19 """ 20 Get available actions from the API instance. 21 Returns the response (v4 does: `.json()` is the list of action names). 22 """ 23 from meerschaum._internal.static import STATIC_CONFIG 24 return self.get(STATIC_CONFIG['api']['endpoints']['actions'])
Get available actions from the API instance.
Returns the response (v4 does: .json() is the list of action names).
27def do_action(self, sysargs: List[str]) -> SuccessTuple: 28 """ 29 Execute a Meerschaum action remotely. 30 """ 31 return self._rust_client().do_action(sysargs)
Execute a Meerschaum action remotely.
34async def do_action_async( 35 self, 36 sysargs: List[str], 37 callback_function: Callable[[str], None] = partial(print, end=''), 38) -> SuccessTuple: 39 """ 40 Execute an action as a temporary remote job. 41 """ 42 from meerschaum.connectors.api._jobs import _LoopCallback 43 loop = asyncio.get_running_loop() 44 client = self._rust_client() 45 return await loop.run_in_executor( 46 None, 47 lambda: client.do_action(sysargs, _LoopCallback(loop, callback_function)), 48 )
Execute an action as a temporary remote job.
51def do_action_legacy( 52 self, 53 action: Optional[List[str]] = None, 54 sysargs: Optional[List[str]] = None, 55 debug: bool = False, 56 **kw 57) -> SuccessTuple: 58 """ 59 NOTE: This method is deprecated. 60 Please use `do_action()` or `do_action_async()`. 61 62 Execute a Meerschaum action remotely. 63 64 If `sysargs` are provided, parse those instead. 65 Otherwise infer everything from keyword arguments. 66 67 Examples 68 -------- 69 >>> conn = mrsm.get_connector('api:main') 70 >>> conn.do_action(['show', 'pipes']) 71 (True, "Success") 72 >>> conn.do_action(['show', 'arguments'], name='test') 73 (True, "Success") 74 """ 75 if action is None: 76 action = [] 77 78 if sysargs is not None and action and action[0] == '': 79 from meerschaum._internal.arguments import parse_arguments 80 json_dict = parse_arguments(sysargs) 81 action = list(json_dict.pop('action', None) or []) 82 kw = json_dict 83 else: 84 action = list(action) 85 if debug: 86 kw['debug'] = debug 87 88 return self._rust_client().do_action_legacy(action, kw)
NOTE: This method is deprecated.
Please use do_action() or do_action_async().
Execute a Meerschaum action remotely.
If sysargs are provided, parse those instead.
Otherwise infer everything from keyword arguments.
Examples
>>> conn = mrsm.get_connector('api:main')
>>> conn.do_action(['show', 'pipes'])
(True, "Success")
>>> conn.do_action(['show', 'arguments'], name='test')
(True, "Success")
15def get_mrsm_version(self, **kw) -> Optional[str]: 16 """ 17 Return the Meerschaum version of the API instance. 18 """ 19 return self._rust_client().get_mrsm_version()
Return the Meerschaum version of the API instance.
22def get_chaining_status(self, **kw) -> Optional[bool]: 23 """ 24 Fetch the chaining status of the API instance. 25 """ 26 return self._rust_client().get_chaining_status()
Fetch the chaining status of the API instance.
5def get_engine_metrics(self, **kwargs): 6 """Read bounded sync and Pipeline measurements visible to this credential.""" 7 from meerschaum.core.Pipeline._metrics import get_engine_metrics as read 8 return read(instance=self, **kwargs)
Read bounded sync and Pipeline measurements visible to this credential.
11def initialize_engine_metrics(self): 12 """Initialize metrics storage on the served instance (full administrator only).""" 13 from meerschaum.core.Pipeline._metrics import initialize_engine_metrics as initialize 14 return initialize(instance=self)
Initialize metrics storage on the served instance (full administrator only).
34def get_pipe_instance_keys(self, pipe: mrsm.Pipe) -> Union[str, None]: 35 """ 36 Return the configured instance keys for a pipe if set, 37 else fall back to the default `instance_keys` for this `APIConnector`. 38 """ 39 return self._rust_client().get_pipe_instance_keys(pipe)
Return the configured instance keys for a pipe if set,
else fall back to the default instance_keys for this APIConnector.
42def register_pipe( 43 self, 44 pipe: mrsm.Pipe, 45 debug: bool = False, 46) -> SuccessTuple: 47 """ 48 Submit a POST to the API to register a new Pipe object. 49 Returns a tuple of (success_bool, response_dict). 50 """ 51 return self._rust_client().register_pipe(pipe)
Submit a POST to the API to register a new Pipe object. Returns a tuple of (success_bool, response_dict).
67def fetch_pipes_keys( 68 self, 69 connector_keys: Optional[List[str]] = None, 70 metric_keys: Optional[List[str]] = None, 71 location_keys: Optional[List[str]] = None, 72 tags: Optional[List[str]] = None, 73 params: Optional[Dict[str, Any]] = None, 74 debug: bool = False 75) -> Union[ 76 Dict[Union[int, str], Tuple[str, str, Union[str, None], Dict[str, Any]]], 77 List[Tuple[str, str, Union[str, None]]], 78]: 79 """ 80 Fetch registered Pipes' keys from the API. 81 82 Parameters 83 ---------- 84 connector_keys: Optional[List[str]], default None 85 The connector keys for the query. 86 87 metric_keys: Optional[List[str]], default None 88 The metric keys for the query. 89 90 location_keys: Optional[List[str]], default None 91 The location keys for the query. 92 93 tags: Optional[List[str]], default None 94 A list of tags for the query. 95 96 params: Optional[Dict[str, Any]], default None 97 A parameters dictionary for filtering against the `pipes` table 98 (e.g. `{'connector_keys': 'plugin:foo'}`). 99 Not recommeded to be used. 100 101 Returns 102 ------- 103 A dictionary mapping pipe IDs to key tuples, or a list of key tuples for older servers. 104 """ 105 return self._rust_client().fetch_pipes_keys( 106 connector_keys, 107 metric_keys, 108 location_keys, 109 tags, 110 params, 111 )
Fetch registered Pipes' keys from the API.
Parameters
- connector_keys (Optional[List[str]], default None): The connector keys for the query.
- metric_keys (Optional[List[str]], default None): The metric keys for the query.
- location_keys (Optional[List[str]], default None): The location keys for the query.
- tags (Optional[List[str]], default None): A list of tags for the query.
- params (Optional[Dict[str, Any]], default None):
A parameters dictionary for filtering against the
pipestable (e.g.{'connector_keys': 'plugin:foo'}). Not recommeded to be used.
Returns
- A dictionary mapping pipe IDs to key tuples, or a list of key tuples for older servers.
54def edit_pipe( 55 self, 56 pipe: mrsm.Pipe, 57 patch: bool = False, 58 debug: bool = False, 59) -> SuccessTuple: 60 """ 61 Submit a PATCH to the API to edit an existing Pipe object. 62 Returns a tuple of (success_bool, response_dict). 63 """ 64 return self._rust_client().edit_pipe(pipe, patch=patch)
Submit a PATCH to the API to edit an existing Pipe object. Returns a tuple of (success_bool, response_dict).
114def sync_pipe( 115 self, 116 pipe: mrsm.Pipe, 117 df: Optional[Union['pd.DataFrame', Dict[Any, Any], str]] = None, 118 chunksize: Optional[int] = -1, 119 debug: bool = False, 120 **kw: Any 121) -> SuccessTuple: 122 """ 123 Sync a DataFrame (or documents, or a dictionary of lists) into a Pipe. 124 Keyword arguments are sent to the API as query parameters. 125 """ 126 if isinstance(df, str): 127 df = json.loads(df) 128 return self._rust_client().sync_pipe(pipe, df, chunksize=chunksize, **kw)
Sync a DataFrame (or documents, or a dictionary of lists) into a Pipe. Keyword arguments are sent to the API as query parameters.
141def delete_pipe( 142 self, 143 pipe: Optional[mrsm.Pipe] = None, 144 debug: bool = False, 145) -> SuccessTuple: 146 """Delete a Pipe and drop its table.""" 147 if pipe is None: 148 raise ValueError("Pipe cannot be None.") 149 return self._rust_client().delete_pipe(pipe)
Delete a Pipe and drop its table.
152def rename_pipe( 153 self, 154 pipe: mrsm.Pipe, 155 connector_keys: Optional[str] = None, 156 metric_key: Optional[str] = None, 157 location_key: Optional[str] = None, 158 clear_location: bool = False, 159 target: Optional[str] = None, 160 dry_run: bool = False, 161 debug: bool = False, 162) -> Dict[str, Any]: 163 """ 164 Atomically rename a registered Pipe over the API (`POST /pipes/{keys}/rename`), 165 returning the structured `RenamePipeReport` dictionary. 166 """ 167 if pipe is None: 168 raise ValueError("Pipe cannot be None.") 169 return self._rust_client().rename_pipe( 170 pipe, 171 connector_keys=connector_keys, 172 metric_key=metric_key, 173 location_key=location_key, 174 clear_location=clear_location, 175 target=target, 176 dry_run=dry_run, 177 )
Atomically rename a registered Pipe over the API (POST /pipes/{keys}/rename),
returning the structured RenamePipeReport dictionary.
131def delete_pipe_cache( 132 self, 133 pipe: mrsm.Pipe, 134 debug: bool = False, 135 **kw: Any 136) -> SuccessTuple: 137 """Invalidate the server-side cache for a pipe.""" 138 return self._rust_client().delete_pipe_cache(pipe)
Invalidate the server-side cache for a pipe.
180def get_pipe_data( 181 self, 182 pipe: mrsm.Pipe, 183 select_columns: Optional[List[str]] = None, 184 omit_columns: Optional[List[str]] = None, 185 begin: Union[str, datetime, int, None] = None, 186 end: Union[str, datetime, int, None] = None, 187 params: Optional[Dict[str, Any]] = None, 188 as_chunks: bool = False, 189 debug: bool = False, 190 as_polars: Optional[bool] = None, 191 as_pandas: Optional[bool] = None, 192 **kw: Any 193) -> Union['pd.DataFrame', None]: 194 """ 195 Fetch data from the API, as a DataFrame of the `meerschaum:dataframe` library 196 (`as_polars=True` / `as_pandas=True` choose one). 197 """ 198 from meerschaum.utils.dataframe import frame_from_arrow, frame_library_of 199 df = self._rust_client().get_pipe_data( 200 pipe, 201 select_columns=select_columns, 202 omit_columns=omit_columns, 203 begin=begin, 204 end=end, 205 params=params, 206 as_chunks=as_chunks, 207 ) 208 if frame_library_of(df) is None: 209 return df 210 return frame_from_arrow(df, as_polars=as_polars, as_pandas=as_pandas)
Fetch data from the API, as a DataFrame of the meerschaum:dataframe library
(as_polars=True / as_pandas=True choose one).
213def get_pipe_id( 214 self, 215 pipe: mrsm.Pipe, 216 debug: bool = False, 217) -> Union[int, str, None]: 218 """Get a Pipe's ID from the API.""" 219 return self._rust_client().get_pipe_id(pipe)
Get a Pipe's ID from the API.
222def get_pipe_attributes( 223 self, 224 pipe: mrsm.Pipe, 225 debug: bool = False, 226) -> Dict[str, Any]: 227 """ 228 Get a Pipe's attributes from the API. 229 230 Parameters 231 ---------- 232 pipe: meerschaum.Pipe 233 The pipe whose attributes we are fetching. 234 235 Returns 236 ------- 237 A dictionary of a pipe's attributes. 238 If the pipe does not exist, return an empty dictionary. 239 """ 240 return self._rust_client().get_pipe_attributes(pipe)
Get a Pipe's attributes from the API.
Parameters
- pipe (meerschaum.Pipe): The pipe whose attributes we are fetching.
Returns
- A dictionary of a pipe's attributes.
- If the pipe does not exist, return an empty dictionary.
243def get_sync_time( 244 self, 245 pipe: mrsm.Pipe, 246 params: Optional[Dict[str, Any]] = None, 247 newest: bool = True, 248 debug: bool = False, 249) -> Union[datetime, int, None]: 250 """ 251 Get a Pipe's most recent datetime value from the API. 252 253 Parameters 254 ---------- 255 pipe: meerschaum.Pipe 256 The pipe to select from. 257 258 params: Optional[Dict[str, Any]], default None 259 Optional params dictionary to build the WHERE clause. 260 261 newest: bool, default True 262 If `True`, get the most recent datetime (honoring `params`). 263 If `False`, get the oldest datetime (ASC instead of DESC). 264 265 Returns 266 ------- 267 The most recent (or oldest if `newest` is `False`) datetime of a pipe, 268 rounded down to the closest minute. 269 """ 270 return self._rust_client().get_sync_time(pipe, params=params, newest=newest)
Get a Pipe's most recent datetime value from the API.
Parameters
- pipe (meerschaum.Pipe): The pipe to select from.
- params (Optional[Dict[str, Any]], default None): Optional params dictionary to build the WHERE clause.
- newest (bool, default True):
If
True, get the most recent datetime (honoringparams). IfFalse, get the oldest datetime (ASC instead of DESC).
Returns
- The most recent (or oldest if
newestisFalse) datetime of a pipe, - rounded down to the closest minute.
273def pipe_exists( 274 self, 275 pipe: mrsm.Pipe, 276 debug: bool = False 277) -> bool: 278 """ 279 Check the API to see if a Pipe exists. 280 281 Parameters 282 ---------- 283 pipe: 'meerschaum.Pipe' 284 The pipe which were are querying. 285 286 Returns 287 ------- 288 A bool indicating whether a pipe's underlying table exists. 289 """ 290 return self._rust_client().pipe_exists(pipe)
Check the API to see if a Pipe exists.
Parameters
- pipe ('meerschaum.Pipe'): The pipe which were are querying.
Returns
- A bool indicating whether a pipe's underlying table exists.
293def create_metadata( 294 self, 295 debug: bool = False 296) -> bool: 297 """ 298 Create metadata tables. 299 300 Returns 301 ------- 302 A bool indicating success. 303 """ 304 return self._rust_client().create_metadata()
Create metadata tables.
Returns
- A bool indicating success.
307def get_pipe_rowcount( 308 self, 309 pipe: mrsm.Pipe, 310 begin: Union[str, datetime, int, None] = None, 311 end: Union[str, datetime, int, None] = None, 312 params: Optional[Dict[str, Any]] = None, 313 remote: bool = False, 314 debug: bool = False, 315) -> int: 316 """ 317 Get a pipe's row count from the API. 318 319 Parameters 320 ---------- 321 pipe: 'meerschaum.Pipe': 322 The pipe whose row count we are counting. 323 324 begin: Union[str, datetime, int, None], default None 325 If provided, bound the count by this datetime. 326 327 end: Union[str, datetime, int, None], default None 328 If provided, bound the count by this datetime. 329 330 params: Optional[Dict[str, Any]], default None 331 If provided, bound the count by these parameters. 332 333 remote: bool, default False 334 If `True`, return the rowcount for the fetch definition. 335 336 Returns 337 ------- 338 The number of rows in the pipe's table, bound the given parameters. 339 If the table does not exist, return 0. 340 """ 341 return self._rust_client().get_pipe_rowcount( 342 pipe, 343 begin=begin, 344 end=end, 345 params=params, 346 remote=remote, 347 )
Get a pipe's row count from the API.
Parameters
- pipe ('meerschaum.Pipe':): The pipe whose row count we are counting.
- begin (Union[str, datetime, int, None], default None): If provided, bound the count by this datetime.
- end (Union[str, datetime, int, None], default None): If provided, bound the count by this datetime.
- params (Optional[Dict[str, Any]], default None): If provided, bound the count by these parameters.
- remote (bool, default False):
If
True, return the rowcount for the fetch definition.
Returns
- The number of rows in the pipe's table, bound the given parameters.
- If the table does not exist, return 0.
350def drop_pipe( 351 self, 352 pipe: mrsm.Pipe, 353 debug: bool = False 354) -> SuccessTuple: 355 """ 356 Drop a pipe's table but maintain its registration. 357 358 Parameters 359 ---------- 360 pipe: meerschaum.Pipe: 361 The pipe to be dropped. 362 363 Returns 364 ------- 365 A success tuple (bool, str). 366 """ 367 if pipe is None: 368 raise ValueError("Pipe cannot be None.") 369 return self._rust_client().drop_pipe(pipe)
Drop a pipe's table but maintain its registration.
Parameters
- pipe (meerschaum.Pipe:): The pipe to be dropped.
Returns
- A success tuple (bool, str).
372def clear_pipe( 373 self, 374 pipe: mrsm.Pipe, 375 begin: Union[str, datetime, int, None] = None, 376 end: Union[str, datetime, int, None] = None, 377 params: Optional[Dict[str, Any]] = None, 378 debug: bool = False, 379 **kw 380) -> SuccessTuple: 381 """ 382 Delete rows in a pipe's table. 383 384 Parameters 385 ---------- 386 pipe: meerschaum.Pipe 387 The pipe with rows to be deleted. 388 389 begin: Union[str, datetime, int, None], default None 390 Delete rows at or after this datetime. 391 392 end: Union[str, datetime, int, None], default None 393 Delete rows before this datetime. 394 395 params: Optional[Dict[str, Any]], default None 396 Delete rows matching these parameters. 397 398 Returns 399 ------- 400 A success tuple. 401 """ 402 return self._rust_client().clear_pipe(pipe, begin=begin, end=end, params=params)
Delete rows in a pipe's table.
Parameters
- pipe (meerschaum.Pipe): The pipe with rows to be deleted.
- begin (Union[str, datetime, int, None], default None): Delete rows at or after this datetime.
- end (Union[str, datetime, int, None], default None): Delete rows before this datetime.
- params (Optional[Dict[str, Any]], default None): Delete rows matching these parameters.
Returns
- A success tuple.
405def get_pipe_columns_types( 406 self, 407 pipe: mrsm.Pipe, 408 debug: bool = False, 409) -> Union[Dict[str, str], None]: 410 """ 411 Fetch the columns and types of the pipe's table. 412 413 Parameters 414 ---------- 415 pipe: meerschaum.Pipe 416 The pipe whose columns to be queried. 417 418 Returns 419 ------- 420 A dictionary mapping column names to their database types, or `None` on failure. 421 """ 422 return self._rust_client().get_pipe_columns_types(pipe)
Fetch the columns and types of the pipe's table.
Parameters
- pipe (meerschaum.Pipe): The pipe whose columns to be queried.
Returns
- A dictionary mapping column names to their database types, or
Noneon failure.
425def get_pipe_columns_indices( 426 self, 427 pipe: mrsm.Pipe, 428 debug: bool = False, 429) -> Union[Dict[str, List[Dict[str, str]]], None]: 430 """ 431 Fetch the index information for a pipe. 432 433 Parameters 434 ---------- 435 pipe: mrsm.Pipe 436 The pipe whose columns to be queried. 437 438 Returns 439 ------- 440 A dictionary mapping column names to lists of dictionaries with index `name` and `type`, 441 or `None` on failure. 442 """ 443 return self._rust_client().get_pipe_columns_indices(pipe)
Fetch the index information for a pipe.
Parameters
- pipe (mrsm.Pipe): The pipe whose columns to be queried.
Returns
- A dictionary mapping column names to lists of dictionaries with index
nameandtype, - or
Noneon failure.
537def get_pipe_index_names( 538 self, 539 pipe: mrsm.Pipe, 540 debug: bool = False, 541) -> Dict[str, str]: 542 """Return the index names of a pipe's table.""" 543 return self._rust_client().get_pipe_index_names(pipe)
Return the index names of a pipe's table.
446def get_pipe_docs( 447 self, 448 pipe: mrsm.Pipe, 449 select_columns: Optional[List[str]] = None, 450 omit_columns: Optional[List[str]] = None, 451 begin: Union[str, datetime, int, None] = None, 452 end: Union[str, datetime, int, None] = None, 453 params: Optional[Dict[str, Any]] = None, 454 order: str = 'asc', 455 limit: Optional[int] = None, 456 debug: bool = False, 457 **kw: Any 458) -> List[Dict[str, Any]]: 459 """Fetch a pipe's data as a list of documents from the API.""" 460 return self._rust_client().get_pipe_docs( 461 pipe, 462 select_columns=select_columns, 463 omit_columns=omit_columns, 464 begin=begin, 465 end=end, 466 params=params, 467 order=order, 468 limit=limit, 469 )
Fetch a pipe's data as a list of documents from the API.
472def get_pipe_size( 473 self, 474 pipe: mrsm.Pipe, 475 debug: bool = False, 476 **kw: Any 477) -> Union[int, None]: 478 """ 479 Return the size of the pipe's table in bytes, or `None` if it is not available. 480 """ 481 return self._rust_client().get_pipe_size(pipe)
Return the size of the pipe's table in bytes, or None if it is not available.
484def compress_pipe( 485 self, 486 pipe: mrsm.Pipe, 487 debug: bool = False, 488 **kw: Any 489) -> SuccessTuple: 490 """Compress a pipe's table on the API instance.""" 491 return self._rust_client().compress_pipe(pipe)
Compress a pipe's table on the API instance.
494def decompress_pipe( 495 self, 496 pipe: mrsm.Pipe, 497 no_policy: bool = False, 498 debug: bool = False, 499 **kw: Any 500) -> SuccessTuple: 501 """Decompress a pipe's table on the API instance.""" 502 return self._rust_client().decompress_pipe(pipe, no_policy=no_policy)
Decompress a pipe's table on the API instance.
505def vacuum_pipe( 506 self, 507 pipe: mrsm.Pipe, 508 full: bool = False, 509 debug: bool = False, 510 **kw: Any 511) -> SuccessTuple: 512 """Vacuum a pipe's table on the API instance.""" 513 return self._rust_client().vacuum_pipe(pipe, full=full)
Vacuum a pipe's table on the API instance.
527def analyze_pipe( 528 self, 529 pipe: mrsm.Pipe, 530 debug: bool = False, 531 **kw: Any 532) -> SuccessTuple: 533 """Analyze a pipe's table on the API instance.""" 534 return self._rust_client().analyze_pipe(pipe)
Analyze a pipe's table on the API instance.
516def partition_pipe( 517 self, 518 pipe: mrsm.Pipe, 519 chunk_minutes: Optional[int] = None, 520 debug: bool = False, 521 **kw: Any 522) -> SuccessTuple: 523 """Repartition a pipe's table on the API instance.""" 524 return self._rust_client().partition_pipe(pipe, chunk_minutes=chunk_minutes)
Repartition a pipe's table on the API instance.
19def fetch( 20 self, 21 pipe: mrsm.Pipe, 22 begin: Union[datetime, str, int] = '', 23 end: Union[datetime, int] = None, 24 params: Optional[Dict[str, Any]] = None, 25 debug: bool = False, 26 **kw: Any 27) -> Iterator['pd.DataFrame']: 28 """Get the Pipe data from the remote Pipe (`parameters['fetch']['pipe']`).""" 29 from meerschaum.config._patch import apply_patch_to_config 30 import copy 31 32 fetch_params = pipe.parameters.get('fetch', {}) 33 if not fetch_params: 34 warn(f"Missing 'fetch' parameters for {pipe}.", stack=False) 35 return None 36 37 pipe_meta = dict(fetch_params.get('pipe', {})) 38 ### Legacy: check for `connector_keys`, etc. at the root. 39 if not pipe_meta: 40 ck, mk, lk = ( 41 fetch_params.get('connector_keys', None), 42 fetch_params.get('metric_key', None), 43 fetch_params.get('location_key', None), 44 ) 45 if not ck or not mk: 46 warn(f"Missing `fetch:pipe` keys for {pipe}.", stack=False) 47 return None 48 pipe_meta.update({ 49 'connector': ck, 50 'metric': mk, 51 'location': lk, 52 }) 53 54 pipe_meta['instance'] = self 55 source_pipe = mrsm.Pipe(**pipe_meta) 56 _params = copy.deepcopy(params) if params is not None else {} 57 _params = apply_patch_to_config(_params, fetch_params.get('params', {})) 58 select_columns = fetch_params.get('select_columns', []) 59 omit_columns = fetch_params.get('omit_columns', []) 60 return source_pipe.get_data( 61 select_columns=select_columns, 62 omit_columns=omit_columns, 63 begin=begin, 64 end=end, 65 params=_params, 66 debug=debug, 67 as_iterator=True, 68 )
Get the Pipe data from the remote Pipe (parameters['fetch']['pipe']).
24def register_plugin( 25 self, 26 plugin: mrsm.core.Plugin, 27 make_archive: bool = True, 28 debug: bool = False, 29) -> SuccessTuple: 30 """Register a plugin and upload its archive.""" 31 import os 32 archive_path = plugin.make_tar(debug=debug) if make_archive else plugin.archive_path 33 try: 34 with open(archive_path, 'rb') as f: 35 archive = f.read() 36 except Exception: 37 return False, f"Failed to register plugin '{plugin}'." 38 return self._rust_client().register_plugin( 39 plugin, 40 archive, 41 os.path.basename(str(archive_path)), 42 )
Register a plugin and upload its archive.
45def install_plugin( 46 self, 47 name: str, 48 skip_deps: bool = False, 49 force: bool = False, 50 debug: bool = False 51) -> SuccessTuple: 52 """Download and attempt to install a plugin from the API.""" 53 from meerschaum.plugins import invalidate_plugins_cache 54 55 result = self._rust_client().install_plugin(name, skip_deps, force, debug) 56 if result[0]: 57 invalidate_plugins_cache() 58 return result
Download and attempt to install a plugin from the API.
102def delete_plugin( 103 self, 104 plugin: mrsm.core.Plugin, 105 debug: bool = False 106) -> SuccessTuple: 107 """Delete a plugin from an API repository.""" 108 return self._rust_client().delete_plugin(plugin)
Delete a plugin from an API repository.
61def get_plugins( 62 self, 63 user_id: Optional[int] = None, 64 search_term: Optional[str] = None, 65 debug: bool = False 66) -> List[str]: 67 """Return a list of registered plugin names. 68 69 Parameters 70 ---------- 71 user_id: Optional[int], default None 72 If specified, return all plugins from a certain user. 73 74 search_term: Optional[str], default None 75 If specified, return plugins beginning with this string. 76 77 Returns 78 ------- 79 A list of plugin names. 80 """ 81 return self._rust_client().get_plugins(user_id=user_id, search_term=search_term)
Return a list of registered plugin names.
Parameters
- user_id (Optional[int], default None): If specified, return all plugins from a certain user.
- search_term (Optional[str], default None): If specified, return plugins beginning with this string.
Returns
- A list of plugin names.
84def get_plugin_attributes( 85 self, 86 plugin: mrsm.core.Plugin, 87 debug: bool = False 88) -> Dict[str, Any]: 89 """Return a plugin's attributes.""" 90 return self._rust_client().get_plugin_attributes(plugin)
Return a plugin's attributes.
93def get_plugin_version( 94 self, 95 plugin: mrsm.core.Plugin, 96 debug: bool = False 97) -> Union[str, None]: 98 """Return a plugin's version.""" 99 return self._rust_client().get_plugin_version(plugin)
Return a plugin's version.
15def login( 16 self, 17 debug: bool = False, 18 warn: bool = True, 19 **kw: Any 20) -> SuccessTuple: 21 """Log in and set the session token.""" 22 if warn and self.__dict__.get('_warn', None) is False: 23 warn = False 24 return self._rust_client().login(warn=warn)
Log in and set the session token.
27def test_connection( 28 self, 29 **kw: Any 30) -> Union[bool, None]: 31 """ 32 Test if a successful connection to the API may be made. 33 34 The keyword arguments are those of `meerschaum.connectors.poll.retry_connect()`: 35 `max_retries` (default 1), `retry_wait` (default 0), `warn` (default `False`), 36 `enforce_chaining` and `enforce_login` (default `False`). With `warn`, a failed attempt 37 says why (`cannot connect to localhost:8000 (connection refused)`). 38 """ 39 from meerschaum.connectors.poll import retry_connect 40 from meerschaum.utils.misc import filter_keywords 41 _default_kw = { 42 'max_retries': 1, 'retry_wait': 0, 'warn': False, 43 'connector': self, 'enforce_chaining': False, 44 'enforce_login': False, 45 } 46 _default_kw.update(kw) 47 try: 48 return retry_connect(**filter_keywords(retry_connect, **_default_kw)) 49 except Exception: 50 return False
Test if a successful connection to the API may be made.
The keyword arguments are those of meerschaum.connectors.poll.retry_connect():
max_retries (default 1), retry_wait (default 0), warn (default False),
enforce_chaining and enforce_login (default False). With warn, a failed attempt
says why (cannot connect to localhost:8000 (connection refused)).
37def register_user( 38 self, 39 user: mrsm.core.User, 40 debug: bool = False, 41 **kw: Any 42) -> SuccessTuple: 43 """Register a new user.""" 44 return self._rust_client().register_user(user)
Register a new user.
47def get_user_id( 48 self, 49 user: mrsm.core.User, 50 debug: bool = False, 51 **kw: Any 52) -> Union[int, str, UUID, None]: 53 """Get a user's ID.""" 54 user_id = self._rust_client().get_user_id(user) 55 if isinstance(user_id, str): 56 try: 57 return UUID(user_id) if len(user_id) == 36 else user_id 58 except ValueError: 59 return user_id 60 return user_id
Get a user's ID.
18def get_users( 19 self, 20 debug: bool = False, 21 **kw: Any 22) -> List[str]: 23 """Return a list of registered usernames.""" 24 return self._rust_client().get_users()
Return a list of registered usernames.
27def edit_user( 28 self, 29 user: mrsm.core.User, 30 debug: bool = False, 31 **kw: Any 32) -> SuccessTuple: 33 """Edit an existing user.""" 34 return self._rust_client().edit_user(user)
Edit an existing user.
63def delete_user( 64 self, 65 user: mrsm.core.User, 66 debug: bool = False, 67 **kw: Any 68) -> SuccessTuple: 69 """Delete a user.""" 70 return self._rust_client().delete_user(user)
Delete a user.
87def get_user_password_hash( 88 self, 89 user: mrsm.core.User, 90 debug: bool = False, 91 **kw: Any 92) -> Optional[str]: 93 """If configured, get a user's password hash.""" 94 return self._rust_client().get_user_password_hash(user)
If configured, get a user's password hash.
97def get_user_type( 98 self, 99 user: mrsm.core.User, 100 debug: bool = False, 101 **kw: Any 102) -> Optional[str]: 103 """If configured, get a user's type.""" 104 return self._rust_client().get_user_type(user)
If configured, get a user's type.
73def get_user_attributes( 74 self, 75 user: mrsm.core.User, 76 debug: bool = False, 77 **kw 78) -> Any: 79 """Get a user's attributes.""" 80 return self._rust_client().get_user_attributes(user)
Get a user's attributes.
20def register_token(self, token: 'Token', debug: bool = False) -> mrsm.SuccessTuple: 21 """ 22 Register the provided token to the API. 23 """ 24 return self._rust_client().register_token(token)
Register the provided token to the API.
27def get_token_model(self, token_id: uuid.UUID, debug: bool = False) -> 'Union[TokenModel, None]': 28 """ 29 Return a token's model from the API instance. 30 """ 31 from meerschaum.models import TokenModel 32 data = self._rust_client().get_token_model(_token_id(token_id)) 33 if data is None: 34 return None 35 return TokenModel(**data)
Return a token's model from the API instance.
38def get_tokens(self, labels: Optional[List[str]] = None, debug: bool = False) -> List['Token']: 39 """ 40 Return the tokens registered to the current user. 41 """ 42 from meerschaum.core import Token 43 return [ 44 Token(instance=self, **payload) 45 for payload in self._rust_client().get_tokens(labels) 46 ]
Return the tokens registered to the current user.
49def edit_token(self, token: 'Token', debug: bool = False) -> mrsm.SuccessTuple: 50 """ 51 Persist the token's in-memory state to the API. 52 """ 53 return self._rust_client().edit_token(token)
Persist the token's in-memory state to the API.
56def invalidate_token(self, token: 'Token', debug: bool = False) -> mrsm.SuccessTuple: 57 """ 58 Invalidate the token, disabling it for future requests. 59 """ 60 return self._rust_client().invalidate_token(token)
Invalidate the token, disabling it for future requests.
63def get_token_scopes(self, token_id: Union[uuid.UUID, 'Token'], debug: bool = False) -> List[str]: 64 """ 65 Return the scopes for a token. 66 """ 67 return self._rust_client().get_token_scopes(_token_id(token_id))
Return the scopes for a token.
70def token_exists(self, token_id: Union[uuid.UUID, 'Token'], debug: bool = False) -> bool: 71 """ 72 Return `True` if a token exists. 73 """ 74 return self._rust_client().token_exists(_token_id(token_id))
Return True if a token exists.
77def delete_token(self, token: 'Token', debug: bool = False) -> mrsm.SuccessTuple: 78 """ 79 Delete the token from the API. 80 """ 81 return self._rust_client().delete_token(token)
Delete the token from the API.
13@classmethod 14def from_uri( 15 cls, 16 uri: str, 17 label: Optional[str] = None, 18 as_dict: bool = False, 19) -> Union[ 20 'meerschaum.connectors.APIConnector', 21 Dict[str, Union[str, int]], 22 ]: 23 """ 24 Create a new APIConnector from a URI string. 25 26 Parameters 27 ---------- 28 uri: str 29 The URI connection string. 30 31 label: Optional[str], default None 32 If provided, use this as the connector label. 33 Otherwise use the determined database name. 34 35 as_dict: bool, default False 36 If `True`, return a dictionary of the keyword arguments 37 necessary to create a new `APIConnector`, otherwise create a new object. 38 39 Returns 40 ------- 41 A new APIConnector object or a dictionary of attributes (if `as_dict` is `True`). 42 """ 43 from meerschaum import _core 44 params = _core.api_uri_params(uri, label) 45 return cls(**params) if not as_dict else params
Create a new APIConnector from a URI string.
Parameters
- uri (str): The URI connection string.
- label (Optional[str], default None): If provided, use this as the connector label. Otherwise use the determined database name.
- as_dict (bool, default False):
If
True, return a dictionary of the keyword arguments necessary to create a newAPIConnector, otherwise create a new object.
Returns
- A new APIConnector object or a dictionary of attributes (if
as_dictisTrue).
17def get_jobs(self, debug: bool = False) -> Dict[str, 'Job']: 18 """ 19 Return a dictionary of remote jobs. 20 """ 21 from meerschaum.jobs import Job 22 return { 23 name: Job( 24 name, 25 job_meta['sysargs'], 26 executor_keys=str(self), 27 _properties=job_meta['daemon']['properties'], 28 ) 29 for name, job_meta in self._rust_client().get_jobs_metadata().items() 30 }
Return a dictionary of remote jobs.
33def get_job(self, name: str, debug: bool = False) -> 'Job': 34 """ 35 Return a single Job object. 36 """ 37 from meerschaum.jobs import Job 38 metadata = self.get_job_metadata(name, debug=debug) 39 if not metadata: 40 raise ValueError(f"Job '{name}' does not exist.") 41 42 return Job( 43 name, 44 metadata['sysargs'], 45 executor_keys=str(self), 46 _properties=metadata['daemon']['properties'], 47 )
Return a single Job object.
50def get_job_metadata(self, name: str, debug: bool = False) -> Dict[str, Any]: 51 """ 52 Return the metadata for a single job. 53 """ 54 return self._rust_client().get_job_metadata(name)
Return the metadata for a single job.
57def get_job_properties(self, name: str, debug: bool = False) -> Dict[str, Any]: 58 """ 59 Return the daemon properties for a single job. 60 """ 61 return self._rust_client().get_job_properties(name)
Return the daemon properties for a single job.
92def get_job_exists(self, name: str, debug: bool = False) -> bool: 93 """ 94 Return whether a job exists. 95 """ 96 return self._rust_client().get_job_exists(name)
Return whether a job exists.
99def delete_job(self, name: str, debug: bool = False) -> SuccessTuple: 100 """ 101 Delete a job. 102 """ 103 return self._rust_client().delete_job(name)
Delete a job.
106def start_job(self, name: str, debug: bool = False) -> SuccessTuple: 107 """ 108 Start a job. 109 """ 110 return self._rust_client().start_job(name)
Start a job.
113def create_job( 114 self, 115 name: str, 116 sysargs: List[str], 117 properties: Optional[Dict[str, str]] = None, 118 debug: bool = False, 119) -> SuccessTuple: 120 """ 121 Create a job. 122 """ 123 return self._rust_client().create_job(name, sysargs, properties)
Create a job.
126def stop_job(self, name: str, debug: bool = False) -> SuccessTuple: 127 """ 128 Stop a job. 129 """ 130 return self._rust_client().stop_job(name)
Stop a job.
133def pause_job(self, name: str, debug: bool = False) -> SuccessTuple: 134 """ 135 Pause a job. 136 """ 137 return self._rust_client().pause_job(name)
Pause a job.
140def get_logs(self, name: str, debug: bool = False) -> str: 141 """ 142 Return the logs for a job. 143 """ 144 return self._rust_client().get_logs(name)
Return the logs for a job.
147def get_job_stop_time(self, name: str, debug: bool = False) -> Union[datetime, None]: 148 """ 149 Return the job's manual stop time. 150 """ 151 data = self._rust_client().get_job_stop_time(name) 152 if data is None: 153 return None 154 155 return datetime.fromisoformat(data)
Return the job's manual stop time.
223def monitor_logs( 224 self, 225 name: str, 226 callback_function: Callable[[Any], Any], 227 input_callback_function: Callable[[None], str], 228 stop_callback_function: Callable[[None], str], 229 stop_on_exit: bool = False, 230 strip_timestamps: bool = False, 231 accept_input: bool = True, 232 debug: bool = False, 233): 234 """ 235 Monitor a job's log files and execute a callback with the changes. 236 """ 237 return asyncio.run( 238 self.monitor_logs_async( 239 name, 240 callback_function, 241 input_callback_function=input_callback_function, 242 stop_callback_function=stop_callback_function, 243 stop_on_exit=stop_on_exit, 244 strip_timestamps=strip_timestamps, 245 accept_input=accept_input, 246 debug=debug 247 ) 248 )
Monitor a job's log files and execute a callback with the changes.
176async def monitor_logs_async( 177 self, 178 name: str, 179 callback_function: Callable[[Any], Any], 180 input_callback_function: Callable[[], str], 181 stop_callback_function: Callable[[SuccessTuple], str], 182 stop_on_exit: bool = False, 183 strip_timestamps: bool = False, 184 accept_input: bool = True, 185 debug: bool = False, 186): 187 """ 188 Monitor a job's log files and await a callback with the changes. 189 190 The stream runs in the Rust client; coroutine callbacks are awaited on this event loop. 191 Raise `meerschaum.jobs.StopMonitoringLogs` from a callback to stop monitoring. 192 """ 193 import threading 194 loop = asyncio.get_running_loop() 195 client = self._rust_client() 196 cancelled = threading.Event() 197 try: 198 await loop.run_in_executor( 199 None, 200 lambda: client.monitor_logs( 201 name, 202 _LoopCallback(loop, callback_function), 203 input_callback_function=( 204 _LoopCallback(loop, input_callback_function) 205 if input_callback_function is not None 206 else None 207 ), 208 stop_callback_function=( 209 _LoopCallback(loop, stop_callback_function) 210 if stop_callback_function is not None 211 else None 212 ), 213 stop_on_exit=stop_on_exit, 214 strip_timestamps=strip_timestamps, 215 stop_event=cancelled, 216 ), 217 ) 218 finally: 219 ### A cancelled task ends the stream (the worker thread checks every 100 ms). 220 cancelled.set()
Monitor a job's log files and await a callback with the changes.
The stream runs in the Rust client; coroutine callbacks are awaited on this event loop.
Raise meerschaum.jobs.StopMonitoringLogs from a callback to stop monitoring.
251def get_job_is_blocking_on_stdin(self, name: str, debug: bool = False) -> bool: 252 """ 253 Return whether a remote job is blocking on stdin. 254 """ 255 return self._rust_client().get_job_is_blocking_on_stdin(name)
Return whether a remote job is blocking on stdin.
258def get_job_prompt_kwargs(self, name: str, debug: bool = False) -> Dict[str, Any]: 259 """ 260 Return the kwargs to the blocking `prompt()`, if available. 261 """ 262 return self._rust_client().get_job_prompt_kwargs(name)
Return the kwargs to the blocking prompt(), if available.
71def get_job_began(self, name: str, debug: bool = False) -> Union[str, None]: 72 """ 73 Return a job's `began` timestamp, if it exists. 74 """ 75 return self._rust_client().get_job_began(name)
Return a job's began timestamp, if it exists.
78def get_job_ended(self, name: str, debug: bool = False) -> Union[str, None]: 79 """ 80 Return a job's `ended` timestamp, if it exists. 81 """ 82 return self._rust_client().get_job_ended(name)
Return a job's ended timestamp, if it exists.
85def get_job_paused(self, name: str, debug: bool = False) -> Union[str, None]: 86 """ 87 Return a job's `paused` timestamp, if it exists. 88 """ 89 return self._rust_client().get_job_paused(name)
Return a job's paused timestamp, if it exists.
64def get_job_status(self, name: str, debug: bool = False) -> str: 65 """ 66 Return the job's status. 67 """ 68 return self._rust_client().get_job_status(name)
Return the job's status.
42def get_team_pipes_count(self, name: str, debug: bool = False, **kwargs: Any) -> int: 43 """ 44 Return how many pipes (and saved Pipelines) name a team, as far as you can see them (what 45 deleting the team gives to `Everyone`): `GET /teams/{name}?counts=true`. 46 """ 47 response = self.get(f"/teams/{_segment(name)}", params={'counts': 'true'}, debug=debug) 48 return int(_body(response).get('pipes', 0) or 0)
Return how many pipes (and saved Pipelines) name a team, as far as you can see them (what
deleting the team gives to Everyone): GET /teams/{name}?counts=true.
51def get_team_jobs_count(self, name: str, debug: bool = False, **kwargs: Any) -> int: 52 """ 53 Return how many of the server's jobs belong to a team: `GET /teams/{name}?counts=true`. 54 """ 55 response = self.get(f"/teams/{_segment(name)}", params={'counts': 'true'}, debug=debug) 56 return int(_body(response).get('jobs', 0) or 0)
Return how many of the server's jobs belong to a team: GET /teams/{name}?counts=true.
59def get_users_permissions(self, debug: bool = False, **kwargs: Any) -> List[Dict[str, Any]]: 60 """ 61 Return every user as the server describes them: `username`, `user_type`, `role`, 62 `permissions`, `teams` (`[{'name', 'role'}]`), `primary_team` and `email` (`None` unless it 63 is yours, you are an administrator, or you manage a team the user is in): 64 `GET /users/permissions`. 65 """ 66 response = self.get('/users/permissions', debug=debug) 67 return list(_body(response).get('users', None) or [])
Return every user as the server describes them: username, user_type, role,
permissions, teams ([{'name', 'role'}]), primary_team and email (None unless it
is yours, you are an administrator, or you manage a team the user is in):
GET /users/permissions.
70def get_user_permissions_record( 71 self, 72 user: Any, 73 debug: bool = False, 74 **kwargs: Any 75) -> Dict[str, Any]: 76 """ 77 Return one user as `get_users_permissions()` describes them (`{}` for an unknown user): 78 `GET /users/{username}/permissions`. 79 """ 80 username = str(getattr(user, 'username', user)) 81 response = self.get(f"/users/{_segment(username)}/permissions", debug=debug) 82 if response.status_code == 404: 83 return {} 84 return dict(_body(response).get('user', None) or {})
Return one user as get_users_permissions() describes them ({} for an unknown user):
GET /users/{username}/permissions.
87def get_pipe_access(self, pipe: mrsm.Pipe, debug: bool = False, **kwargs: Any) -> Dict[str, Any]: 88 """ 89 Return a pipe's `access` (`owner`, `team`, `visibility`, `edit`), its `locks` and what you 90 `may` do with it (`edit`, `manage_access`, `lock`): `GET /pipes/{keys}/access`. 91 """ 92 from meerschaum.connectors.api._pipes import pipe_r_url 93 if pipe.connector_keys == 'mrsm' and pipe.metric_key == 'pipeline': 94 r_url = f"/pipelines/{_segment(pipe.location_key)}/access" 95 else: 96 r_url = pipe_r_url(pipe) + '/access' 97 response = self.get( 98 r_url, 99 params={'instance_keys': self.get_pipe_instance_keys(pipe)}, 100 debug=debug, 101 ) 102 body = _body(response) 103 return {key: body.get(key, None) for key in ('access', 'locks', 'may')}
Return a pipe's access (owner, team, visibility, edit), its locks and what you
may do with it (edit, manage_access, lock): GET /pipes/{keys}/access.