Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/en/seatunnel-engine/rest-api-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ Please refer [security](security.md)
### Returns An Overview And State Of All Jobs

<details>
<summary><code>GET</code> <code><b>/running-jobs</b></code> <code>(Returns an overview over all jobs and their current state.)</code></summary>
<summary><code>GET</code> <code><b>/running-jobs?page=1&rows=10</b></code> <code>(Returns an overview over all jobs and their current state.)</code></summary>

#### Parameters

> | name | type | data type | description |
> |-------|----------|-----------|-----------------------------------------------------------------------------------|
> | page | optional | int | page number. |
> | rows | optional | int | page size. |

#### Responses

```json
Expand Down Expand Up @@ -286,13 +291,15 @@ When we can't get the job info, the response will be:
### Return All Finished Jobs Info

<details>
<summary><code>GET</code> <code><b>/finished-jobs/:state</b></code> <code>(Return all finished Jobs Info.)</code></summary>
<summary><code>GET</code> <code><b>/finished-jobs/:state?page=1&rows=10</b></code> <code>(Return all finished Jobs Info.)</code></summary>

#### Parameters

> | name | type | data type | description |
> |-------|----------|-----------|-----------------------------------------------------------------------------------|
> | state | optional | string | finished job status. `FINISHED`,`CANCELED`,`FAILED`,`SAVEPOINT_DONE`,`UNKNOWABLE` |
> | page | optional | int | page number. |
> | rows | optional | int | page size. |

#### Responses

Expand Down
11 changes: 9 additions & 2 deletions docs/zh/seatunnel-engine/rest-api-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ seatunnel:
### 返回所有作业及其当前状态的概览

<details>
<summary><code>GET</code> <code><b>/running-jobs</b></code> <code>(返回所有作业及其当前状态的概览。)</code></summary>
<summary><code>GET</code> <code><b>/running-jobs?page=1&rows=10</b></code> <code>(返回所有作业及其当前状态的概览。)</code></summary>

#### 参数

> | 参数名称 | 是否必传 | 参数类型 | 参数描述 |
> |------|------|------|------|
> | page | 否 | int | 页号 |
> | rows | 否 | int | 每页行数 |

#### 响应

```json
Expand Down Expand Up @@ -267,13 +272,15 @@ seatunnel:
### 返回所有已完成的作业信息

<details>
<summary><code>GET</code> <code><b>/finished-jobs/:state</b></code> <code>(返回所有已完成的作业信息。)</code></summary>
<summary><code>GET</code> <code><b>/finished-jobs/:state?page=1&rows=10</b></code> <code>(返回所有已完成的作业信息。)</code></summary>

#### 参数

> | 参数名称 | 是否必传 | 参数类型 | 参数描述 |
> |-------|----------|--------|-----------------------------------------------------------------------------------|
> | state | optional | string | finished job status. `FINISHED`,`CANCELED`,`FAILED`,`SAVEPOINT_DONE`,`UNKNOWABLE` |
> | page | 否 | int | 页号 |
> | rows | 否 | int | 每页行数 |

#### 响应

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public JsonArray getRunningJobsJson() {
IMap<Long, JobInfo> values =
nodeEngine.getHazelcastInstance().getMap(Constant.IMAP_RUNNING_JOB_INFO);
return values.entrySet().stream()
.sorted(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JobId is ordered, I think it can be sorted by jobId, compared to higher efficiency.

return values.entrySet().stream()
          .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
          .map(jobInfoEntry -> convertToJson(jobInfoEntry.getValue(), jobInfoEntry.getKey()))
          .collect(JsonArray::new, JsonArray::add, JsonArray::add);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JobId is manually passed in and may not guarantee order, so it has been changed to use the InitializationTimestamp of the JobInfo for sorting.

Comparator.comparing(
entry -> entry.getValue().getInitializationTimestamp(),
Comparator.reverseOrder()))
.map(jobInfoEntry -> convertToJson(jobInfoEntry.getValue(), jobInfoEntry.getKey()))
.collect(JsonArray::new, JsonArray::add, JsonArray::add);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import java.io.IOException;

public class FinishedJobsServlet extends BaseServlet {
public class FinishedJobsServlet extends PageBaseServlet {

private static final long serialVersionUID = 1L;

Expand All @@ -50,6 +50,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
state = "";
}

writeJson(resp, jobInfoService.getJobsByStateJson(state));
writeJsonWithPagination(req, resp, jobInfoService.getJobsByStateJson(state));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.engine.server.rest.servlet;

import com.hazelcast.internal.json.JsonArray;
import com.hazelcast.internal.json.JsonObject;
import com.hazelcast.spi.impl.NodeEngineImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Map;

public class PageBaseServlet extends BaseServlet {
private final String pageParam = "page";
private final String rowsParam = "rows";
Comment on lines +31 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API parameters in the docs rest-api-v2.md have been updated.


public PageBaseServlet(NodeEngineImpl nodeEngine) {
super(nodeEngine);
}

protected void writeJsonWithPagination(
HttpServletRequest req, HttpServletResponse resp, JsonArray jsonArray)
throws IOException {
int total = jsonArray.size();

// fetch pagination params, if page exist, then paginate data,pagination data format like:
// {"data": [], "total": 10}
Map<String, String> parameterMap = getParameterMap(req);
if (parameterMap != null && parameterMap.containsKey(pageParam)) {
int page = Integer.parseInt(parameterMap.get(pageParam));
int rows =
parameterMap.get(rowsParam) != null
? Integer.parseInt(parameterMap.get(rowsParam))
: 10;
int start = (page - 1) * rows;
if (start > total || page < 1) {
throw new IllegalArgumentException(
page < 1
? "Page number must be greater than 0"
: "Page number exceeds total pages");
}
JsonArray paginatedArray = new JsonArray();
jsonArray
.values()
.subList(start, Math.min(start + rows, total))
.forEach(
t -> {
paginatedArray.add(t);
});
JsonObject paginatedObj = new JsonObject();
paginatedObj.add("data", paginatedArray);
paginatedObj.add("total", total);
writeJson(resp, paginatedObj);
} else {
writeJson(resp, jsonArray);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.io.IOException;

@Slf4j
public class RunningJobsServlet extends BaseServlet {
public class RunningJobsServlet extends PageBaseServlet {

private final JobInfoService jobInfoService;

Expand All @@ -42,6 +42,6 @@ public RunningJobsServlet(NodeEngineImpl nodeEngine) {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

writeJson(resp, jobInfoService.getRunningJobsJson());
writeJsonWithPagination(req, resp, jobInfoService.getRunningJobsJson());
}
}
Loading