diff --git a/aliyun/log/__init__.py b/aliyun/log/__init__.py index df7a5d6..1b09001 100755 --- a/aliyun/log/__init__.py +++ b/aliyun/log/__init__.py @@ -34,6 +34,7 @@ from .common_response import * from .external_store_config_response import * from .proto import LogGroupRaw as LogGroup +from .rebuild_index_response import * # logging handler from .logger_hanlder import SimpleLogHandler, QueuedLogHandler, LogFields, UwsgiQueuedLogHandler diff --git a/aliyun/log/logclient.py b/aliyun/log/logclient.py index e7c9ee3..8fbbba7 100644 --- a/aliyun/log/logclient.py +++ b/aliyun/log/logclient.py @@ -37,6 +37,7 @@ from .substore_config_response import * from .logtail_config_response import * from .machinegroup_response import * +from .rebuild_index_response import * from .project_response import * from .pulllog_response import PullLogResponse from .putlogsresponse import PutLogsResponse @@ -5797,6 +5798,73 @@ def create_shipper(self, project, logstore, detail): (resp, header) = self._send("POST", project, body_str, resource, params, headers) return CreateEntityResponse(header, resp) + def create_rebuild_index(self, project, logstore, job_name, display_name, from_time, to_time): + """Create a job that rebuild index for a logstore + type: (string, string, string, string, int, int) -> CreateRebuildIndexResponse + + :type project: string + :param project: the project name + + :type logstore: string + :param logstore: the logstore name + + :type job_name: string + :param job_name: job name + + :type display_name: string + :param display_name: display name for the job + + :type from_time: int + :param from_time: from time, in unix timestamp format, eg 1736156803 + + + :type to_time: int + :param to_time: to time, in unix timestamp format, eg 1736156803, to_time should not large than the unix_timestamp before 900 seconds ago + + :return: CreateRebuildIndexResponse + + :raise: LogException + """ + + params = {} + headers = {"Content-Type": "application/json"} + body = { + "configuration": + { + "fromTime": from_time, + "toTime": to_time, + "logstore": logstore, + }, + "displayName": display_name, + "name": job_name, + "recyclable": False, + "type": "RebuildIndex" + } + data = six.b(json.dumps(body)) + + resource = "/jobs" + (resp, header) = self._send("POST", project, data, resource, params, headers) + return CreateRebuildIndexResponse(header, resp) + + def get_rebuild_index(self, project, job_name): + """get rebuild index by the given job_name + + :type project: string + :param project: the project name + + :type job_name: string + :param job_name: the job name + + :return: GetRebuildIndexResponse + + :raise: LogException + """ + headers = {} + params = {} + resource = "/jobs/" + job_name + (resp, header) = self._send("GET", project, None, resource, params, headers) + return GetRebuildIndexResponse(header, resp) + # make_lcrud_methods(LogClient, 'job', name_field='name', root_resource='/jobs', entities_key='results') # make_lcrud_methods(LogClient, 'dashboard', name_field='dashboardName') # make_lcrud_methods(LogClient, 'alert', name_field='name', root_resource='/jobs', entities_key='results', job_type="Alert") diff --git a/aliyun/log/rebuild_index_response.py b/aliyun/log/rebuild_index_response.py new file mode 100644 index 0000000..b7f9424 --- /dev/null +++ b/aliyun/log/rebuild_index_response.py @@ -0,0 +1,60 @@ +from .logresponse import LogResponse + +__all__ = ['CreateRebuildIndexResponse', 'GetRebuildIndexResponse'] + + +class CreateRebuildIndexResponse(LogResponse): + """ Response of create_rebuild_index + + :type resp: dict + :param resp: CreateRebuildIndexResponse HTTP response body + + :type header: dict + :param header: CreateRebuildIndexResponse HTTP response header + """ + + def __init__(self, header, resp): + LogResponse.__init__(self, header, resp) + + def log_print(self): + print('CreateRebuildIndexResponse:') + print('headers:', self.get_all_headers()) + + +class GetRebuildIndexResponse(LogResponse): + """ The response of the get_rebuild_index. + + :type resp: dict + :param resp: GetRebuildIndexResponse HTTP response body + + :type header: dict + :param header: GetRebuildIndexResponse HTTP response header + """ + + def __init__(self, header, resp): + LogResponse.__init__(self, header, resp) + self._status = resp['status'] + self._execution_details = resp.get('executionDetails') + self._configuration = resp['configuration'] + + def get_status(self): + """job status + """ + return self._status + + def get_execution_details(self): + """job execution details + """ + return self._execution_details + + def get_configuration(self): + """job configuration + """ + return self._configuration + + def log_print(self): + print('GetRebuildIndexResponse:') + print('headers:', self.get_all_headers()) + print('status: ', self.get_status()) + print('execution_details: ', self.get_execution_details()) + print('configuration: ', self.get_configuration()) diff --git a/aliyun/log/version.py b/aliyun/log/version.py index f174f93..583a3ea 100644 --- a/aliyun/log/version.py +++ b/aliyun/log/version.py @@ -1,4 +1,4 @@ -__version__ = '0.9.13rc1' +__version__ = '0.9.13' import sys OS_VERSION = str(sys.platform) diff --git a/tests/rebuild_index_examples/rebuild_index_example.py b/tests/rebuild_index_examples/rebuild_index_example.py new file mode 100644 index 0000000..5d32bd8 --- /dev/null +++ b/tests/rebuild_index_examples/rebuild_index_example.py @@ -0,0 +1,39 @@ +import time +from aliyun.log import LogClient + + +def main(): + client = LogClient("cn-hangzhou.log.aliyuncs.com", + "your_access_key_id", "your_access_key_secret") + + # replace with an unique job_name here + job_name = 'test-rebuild-index-test-1' + + project = 'your_project' + logstore = 'your_logstore' + + # to_time must <= now - 900 + to_time = int(time.time()) - 900 + from_time = to_time - 3600 + display_name = "this is create rebuild index test job" + client.create_rebuild_index(project, + logstore, + job_name, + display_name, + from_time, + to_time) + + # wait a while util job complete + while True: + print('wait rebuild index done...') + time.sleep(10) + resp = client.get_rebuild_index(project, job_name) + resp.log_print() + + if resp.get_status() == 'SUCCEEDED': + print('rebuild index succeed') + break + + +if __name__ == "__main__": + main()