Skip to content

Commit

Permalink
feat: support rebuild index (#312)
Browse files Browse the repository at this point in the history
* feat: support rebuild index
  • Loading branch information
crimson-gao authored Jan 7, 2025
1 parent 251c47c commit d7a8189
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 1 deletion.
1 change: 1 addition & 0 deletions aliyun/log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 68 additions & 0 deletions aliyun/log/logclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
60 changes: 60 additions & 0 deletions aliyun/log/rebuild_index_response.py
Original file line number Diff line number Diff line change
@@ -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())
2 changes: 1 addition & 1 deletion aliyun/log/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.9.13rc1'
__version__ = '0.9.13'

import sys
OS_VERSION = str(sys.platform)
Expand Down
39 changes: 39 additions & 0 deletions tests/rebuild_index_examples/rebuild_index_example.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d7a8189

Please sign in to comment.