mirror of
https://github.com/NotoChen/Jetbrains-Help.git
synced 2025-11-01 09:48:32 +08:00
Merge 7f00919324 into 30b860c437
This commit is contained in:
commit
0175401bd9
|
|
@ -6,6 +6,7 @@ import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import cn.hutool.json.JSON;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.jetbrains.help.util.FileTools;
|
import com.jetbrains.help.util.FileTools;
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
|
|
@ -19,14 +20,16 @@ import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
@Slf4j(topic = "插件上下文")
|
@Slf4j(topic = "插件上下文")
|
||||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
public class PluginsContextHolder {
|
public class PluginsContextHolder {
|
||||||
|
private static final int PAGE_SIZE = 20;
|
||||||
|
|
||||||
private static final String PLUGIN_BASIC_URL = "https://plugins.jetbrains.com";
|
private static final String PLUGIN_BASIC_URL = "https://plugins.jetbrains.com";
|
||||||
|
|
||||||
private static final String PLUGIN_LIST_URL = PLUGIN_BASIC_URL + "/api/searchPlugins?max=10000&offset=0&orderBy=name";
|
private static final String PLUGIN_LIST_URL = PLUGIN_BASIC_URL + "/api/searchPlugins?max="+PAGE_SIZE+"&orderBy=name";
|
||||||
|
|
||||||
private static final String PLUGIN_INFO_URL = PLUGIN_BASIC_URL + "/api/plugins/";
|
private static final String PLUGIN_INFO_URL = PLUGIN_BASIC_URL + "/api/plugins/";
|
||||||
|
|
||||||
|
|
@ -88,19 +91,55 @@ public class PluginsContextHolder {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PluginList pluginList() {
|
public static PluginList pluginList() {
|
||||||
return HttpUtil.createGet(PLUGIN_LIST_URL)
|
// 初始化一个空的 PluginList 用于汇总结果
|
||||||
.thenFunction(response -> {
|
PluginList resultPluginList = new PluginList();
|
||||||
try (InputStream is = response.bodyStream()) {
|
resultPluginList.setPlugins(new ArrayList<>());
|
||||||
if (!response.isOk()) {
|
resultPluginList.setTotal(0L);
|
||||||
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", PLUGIN_LIST_URL, response));
|
int pluginPageSize = getPluginTotleSize(resultPluginList);
|
||||||
}
|
// 配置线程池,核心线程数
|
||||||
PluginList pluginList = JSONUtil.toBean(IoUtil.readUtf8(is), PluginList.class);
|
java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(3);
|
||||||
log.info("获取大小 => [{}]", pluginList.getTotal());
|
// 创建一个线程池,使用并行流处理请求
|
||||||
return pluginList;
|
List<CompletableFuture<PluginList>> futures = new ArrayList<>();
|
||||||
} catch (IOException e) {
|
for (int i = 1; i < pluginPageSize; i++) {
|
||||||
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求IO读取失败!", PLUGIN_LIST_URL), e);
|
int offset = i;
|
||||||
|
String url = PLUGIN_LIST_URL + "&offset=" + offset;
|
||||||
|
CompletableFuture<PluginList> future = CompletableFuture.supplyAsync(() -> {
|
||||||
|
try (InputStream is = HttpUtil.createGet(url).execute().bodyStream()) {
|
||||||
|
cn.hutool.http.HttpResponse response = HttpUtil.createGet(url).execute();
|
||||||
|
if (!response.isOk()) {
|
||||||
|
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", url, response));
|
||||||
}
|
}
|
||||||
});
|
PluginList currentPluginList = JSONUtil.toBean(IoUtil.readUtf8(is), PluginList.class);
|
||||||
|
log.info("获取大小 => [{}], 当前第 => [{}] 页",
|
||||||
|
currentPluginList.getPlugins() != null ? currentPluginList.getPlugins().size() : 0, offset);
|
||||||
|
return currentPluginList;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求IO读取失败!", url), e);
|
||||||
|
}
|
||||||
|
}, executor);
|
||||||
|
futures.add(future);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待所有请求完成
|
||||||
|
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
|
||||||
|
CompletableFuture<List<PluginList>> allResults = allFutures.thenApply(v ->
|
||||||
|
futures.stream()
|
||||||
|
.map(CompletableFuture::join)
|
||||||
|
.toList()
|
||||||
|
);
|
||||||
|
|
||||||
|
// 汇总结果
|
||||||
|
allResults.join().forEach(currentPluginList -> {
|
||||||
|
if (currentPluginList.getPlugins() != null) {
|
||||||
|
resultPluginList.getPlugins().addAll(currentPluginList.getPlugins());
|
||||||
|
resultPluginList.setTotal(resultPluginList.getTotal() + currentPluginList.getPlugins().size());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 关闭线程池
|
||||||
|
executor.shutdown();
|
||||||
|
|
||||||
|
return resultPluginList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<PluginList.Plugin> pluginListFilter(PluginList pluginList) {
|
public static List<PluginList.Plugin> pluginListFilter(PluginList pluginList) {
|
||||||
|
|
@ -148,6 +187,31 @@ public class PluginsContextHolder {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int getPluginTotleSize(PluginList resultPluginList) {
|
||||||
|
return HttpUtil.createGet(PLUGIN_LIST_URL + "&offset=0")
|
||||||
|
.thenFunction(response -> {
|
||||||
|
try (InputStream is = response.bodyStream()) {
|
||||||
|
if (!response.isOk()) {
|
||||||
|
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", PLUGIN_LIST_URL, response));
|
||||||
|
}
|
||||||
|
String readUtf8 = IoUtil.readUtf8(is);
|
||||||
|
PluginList currentPluginList = JSONUtil.toBean(readUtf8, PluginList.class);
|
||||||
|
resultPluginList.getPlugins().addAll(currentPluginList.getPlugins());
|
||||||
|
resultPluginList.setTotal(resultPluginList.getTotal() + currentPluginList.getPlugins().size());
|
||||||
|
JSON parse = JSONUtil.parse(readUtf8);
|
||||||
|
int pluginstotal = (int) parse.getByPath("total");
|
||||||
|
int total = pluginstotal / PAGE_SIZE;
|
||||||
|
if (pluginstotal % PAGE_SIZE > 0) {
|
||||||
|
total = total + 1;
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(CharSequenceUtil.format("{} 请求IO读取失败!", PLUGIN_LIST_URL + "&offset=0"), e);
|
||||||
|
return 500;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class PluginCache {
|
public static class PluginCache {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user