mirror of
				https://github.com/NotoChen/Jetbrains-Help.git
				synced 2025-11-01 01:38:31 +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.util.StrUtil; | ||||
| import cn.hutool.http.HttpUtil; | ||||
| import cn.hutool.json.JSON; | ||||
| import cn.hutool.json.JSONUtil; | ||||
| import com.jetbrains.help.util.FileTools; | ||||
| import lombok.AccessLevel; | ||||
|  | @ -19,14 +20,16 @@ import java.io.InputStream; | |||
| import java.nio.charset.StandardCharsets; | ||||
| import java.util.*; | ||||
| import java.util.concurrent.CompletableFuture; | ||||
| import java.util.concurrent.atomic.AtomicInteger; | ||||
| 
 | ||||
| @Slf4j(topic = "插件上下文") | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| 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_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/"; | ||||
| 
 | ||||
|  | @ -88,19 +91,55 @@ public class PluginsContextHolder { | |||
|     } | ||||
| 
 | ||||
|     public static PluginList pluginList() { | ||||
|         return HttpUtil.createGet(PLUGIN_LIST_URL) | ||||
|                 .thenFunction(response -> { | ||||
|                     try (InputStream is = response.bodyStream()) { | ||||
|                         if (!response.isOk()) { | ||||
|                             throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", PLUGIN_LIST_URL, response)); | ||||
|                         } | ||||
|                         PluginList pluginList = JSONUtil.toBean(IoUtil.readUtf8(is), PluginList.class); | ||||
|                         log.info("获取大小 => [{}]", pluginList.getTotal()); | ||||
|                         return pluginList; | ||||
|                     } catch (IOException e) { | ||||
|                         throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求IO读取失败!", PLUGIN_LIST_URL), e); | ||||
|         // 初始化一个空的 PluginList 用于汇总结果 | ||||
|         PluginList resultPluginList = new PluginList(); | ||||
|         resultPluginList.setPlugins(new ArrayList<>()); | ||||
|         resultPluginList.setTotal(0L); | ||||
|         int pluginPageSize = getPluginTotleSize(resultPluginList); | ||||
|         // 配置线程池,核心线程数 | ||||
|         java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(3); | ||||
|         // 创建一个线程池,使用并行流处理请求 | ||||
|         List<CompletableFuture<PluginList>> futures = new ArrayList<>(); | ||||
|         for (int i = 1; i < pluginPageSize; i++) { | ||||
|             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) { | ||||
|  | @ -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 | ||||
|     public static class PluginCache { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 gift95
						gift95