Skip to content

Commit 2ab540f

Browse files
committed
Optimize the submission process
1 parent 93e3ea8 commit 2ab540f

File tree

6 files changed

+61
-43
lines changed

6 files changed

+61
-43
lines changed

src/main/java/com/shuzijun/leetcode/plugin/manager/CodeManager.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.intellij.openapi.application.ApplicationManager;
77
import com.intellij.openapi.fileEditor.FileEditorManager;
88
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
9+
import com.intellij.openapi.progress.ProgressIndicator;
10+
import com.intellij.openapi.progress.ProgressManager;
11+
import com.intellij.openapi.progress.Task;
912
import com.intellij.openapi.project.Project;
1013
import com.intellij.openapi.vfs.LocalFileSystem;
1114
import com.intellij.openapi.vfs.VirtualFile;
@@ -14,19 +17,16 @@
1417
import com.shuzijun.leetcode.plugin.setting.ProjectConfig;
1518
import com.shuzijun.leetcode.plugin.utils.*;
1619
import org.apache.commons.lang.StringUtils;
20+
import org.jetbrains.annotations.NotNull;
1721

1822
import java.io.File;
1923
import java.math.BigDecimal;
20-
import java.util.concurrent.ExecutorService;
21-
import java.util.concurrent.Executors;
2224

2325
/**
2426
* @author shuzijun
2527
*/
2628
public class CodeManager {
2729

28-
private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
29-
3030
public static void openCode(Question question, Project project) {
3131
Config config = PersistentConfig.getInstance().getInitConfig();
3232
String codeType = config.getCodeType();
@@ -173,7 +173,7 @@ public static void SubmitCode(Question question, Project project) {
173173
if (response != null && response.getStatusCode() == 200) {
174174
String body = response.getBody();
175175
JSONObject returnObj = JSONObject.parseObject(body);
176-
cachedThreadPool.execute(new SubmitCheckTask(returnObj, codeTypeEnum, question, project));
176+
ProgressManager.getInstance().run(new SubmitCheckTask(returnObj, codeTypeEnum, question, project));
177177
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending"));
178178
} else if (response != null && response.getStatusCode() == 429) {
179179
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending"));
@@ -218,7 +218,7 @@ public static void RunCodeCode(Question question, Project project) {
218218

219219
String body = response.getBody();
220220
JSONObject returnObj = JSONObject.parseObject(body);
221-
cachedThreadPool.execute(new RunCodeCheckTask(returnObj, project));
221+
ProgressManager.getInstance().run(new RunCodeCheckTask(returnObj, project));
222222
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending"));
223223
} else {
224224
LogUtils.LOG.error("RuncodeCode failure " + response.getBody());
@@ -336,24 +336,29 @@ private static String getContent(JSONObject jsonObject) {
336336
return sb.toString();
337337
}
338338

339-
private static class SubmitCheckTask implements Runnable {
339+
private static class SubmitCheckTask extends Task.Backgroundable {
340340

341341
private Question question;
342342
private JSONObject returnObj;
343343
private CodeTypeEnum codeTypeEnum;
344344
private Project project;
345345

346346
public SubmitCheckTask(JSONObject returnObj, CodeTypeEnum codeTypeEnum, Question question, Project project) {
347+
super(project,"leetcode.editor.submitCheckTask",true);
347348
this.returnObj = returnObj;
348349
this.codeTypeEnum = codeTypeEnum;
349350
this.question = question;
350351
this.project = project;
351352
}
352353

353354
@Override
354-
public void run() {
355+
public void run(@NotNull ProgressIndicator progressIndicator) {
355356
String key = returnObj.getString("submission_id");
356357
for (int i = 0; i < 50; i++) {
358+
if(progressIndicator.isCanceled()){
359+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.cancel"));
360+
return;
361+
}
357362
try {
358363
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeSubmissions() + key + "/check/");
359364
HttpResponse response = HttpRequestUtils.executeGet(httpRequest);
@@ -424,22 +429,27 @@ private static String buildErrorMsg(JSONObject errorBody) {
424429
}
425430

426431

427-
private static class RunCodeCheckTask implements Runnable {
432+
private static class RunCodeCheckTask extends Task.Backgroundable {
428433
private JSONObject returnObj;
429434
private Project project;
430435

431436
public RunCodeCheckTask(JSONObject returnObj, Project project) {
437+
super(project,"leetcode.editor.runCodeCheckTask",true);
432438
this.returnObj = returnObj;
433439
this.project = project;
434440
}
435441

436442
@Override
437-
public void run() {
443+
public void run(@NotNull ProgressIndicator progressIndicator) {
438444
String key = returnObj.getString("interpret_expected_id");
439445
if (StringUtils.isBlank(key)) {
440446
key = returnObj.getString("interpret_id");
441447
}
442448
for (int i = 0; i < 50; i++) {
449+
if(progressIndicator.isCanceled()){
450+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.cancel"));
451+
return;
452+
}
443453
String body = null;
444454
try {
445455
HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeSubmissions() + key + "/check/");

src/main/java/com/shuzijun/leetcode/plugin/setting/SettingUI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.shuzijun.leetcode.plugin.model.Config;
2121
import com.shuzijun.leetcode.plugin.model.Constant;
2222
import com.shuzijun.leetcode.plugin.renderer.CustomTreeCellRenderer;
23+
import com.shuzijun.leetcode.plugin.timer.TimerBarWidget;
2324
import com.shuzijun.leetcode.plugin.utils.MTAUtils;
2425
import com.shuzijun.leetcode.plugin.utils.PropertiesUtils;
2526
import com.shuzijun.leetcode.plugin.utils.URLUtils;
@@ -223,6 +224,7 @@ public void apply() {
223224
PersistentConfig.getInstance().setInitConfig(config);
224225
PersistentConfig.getInstance().savePassword(passwordField.getText());
225226
CustomTreeCellRenderer.loaColor();
227+
TimerBarWidget.loaColor();
226228
}
227229

228230
public void process(Config config) {

src/main/java/com/shuzijun/leetcode/plugin/timer/TimerBarWidget.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ public class TimerBarWidget implements CustomStatusBarWidget {
2929
private Project project;
3030

3131

32-
private Color Level1 = new Color(92, 184, 92);
33-
private Color Level2 = new Color(240, 173, 78);
34-
private Color Level3 = new Color(217, 83, 79);
32+
private static Color Level1 = new Color(92, 184, 92);
33+
private static Color Level2 = new Color(240, 173, 78);
34+
private static Color Level3 = new Color(217, 83, 79);
3535

3636
public TimerBarWidget(Project project) {
3737
this.project = project;
38+
loaColor();
39+
}
40+
public static void loaColor(){
41+
Config config = PersistentConfig.getInstance().getInitConfig();
42+
if (config != null) {
43+
Color[] colors = config.getFormatLevelColour();
44+
Level1 = colors[0];
45+
Level2 = colors[1];
46+
Level3 = colors[2];
47+
}
3848
}
3949

4050
private JLabel label = new JLabel(time());

src/main/java/com/shuzijun/leetcode/plugin/utils/MTAUtils.java

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.shuzijun.leetcode.plugin.utils;
22

3+
import com.intellij.ide.plugins.PluginManager;
4+
import com.intellij.openapi.extensions.PluginId;
5+
import com.intellij.openapi.util.SystemInfo;
36
import com.shuzijun.leetcode.plugin.model.Config;
4-
import org.apache.http.HttpResponse;
5-
import org.apache.http.client.config.RequestConfig;
6-
import org.apache.http.client.methods.HttpGet;
7+
import com.shuzijun.leetcode.plugin.model.Constant;
8+
import com.shuzijun.leetcode.plugin.utils.io.HttpRequests;
79
import org.apache.http.client.utils.URIBuilder;
8-
import org.apache.http.impl.client.CloseableHttpClient;
9-
import org.apache.http.impl.client.HttpClients;
10-
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
11-
import org.apache.http.util.EntityUtils;
1210

1311
import java.awt.*;
1412
import java.net.URI;
@@ -25,23 +23,10 @@ public class MTAUtils {
2523
private static String URL = "http://pingtcss.qq.com/pingd";
2624
private static String SID = "500676642";
2725
private static String SI = getI("s");
26+
private static String version = null;
27+
private static String userAgent = null;
2828

2929
private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
30-
private static final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
31-
32-
public static CloseableHttpClient getHttpClient() {
33-
34-
RequestConfig requestConfig = RequestConfig.custom()
35-
.setConnectionRequestTimeout(1000)
36-
.setConnectTimeout(1000)
37-
.setSocketTimeout(1000)
38-
.build();
39-
40-
return HttpClients.custom()
41-
.setDefaultRequestConfig(requestConfig)
42-
.setConnectionManager(connManager)
43-
.build();
44-
}
4530

4631
public static String getI(String prefix) {
4732
int[] b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
@@ -59,24 +44,35 @@ public static String getI(String prefix) {
5944
}
6045

6146
public static void click(String actionsId, Config config) {
62-
cachedThreadPool.execute(new ClickTask(getHttpClient(), config, actionsId));
47+
cachedThreadPool.execute(new ClickTask(config, actionsId));
6348
}
6449

6550
private static class ClickTask implements Runnable {
6651

67-
private CloseableHttpClient client;
6852
private Config config;
6953
private String actionsId;
7054

71-
public ClickTask(CloseableHttpClient client, Config config, String actionsId) {
72-
this.client = client;
55+
public ClickTask(Config config, String actionsId) {
7356
this.config = config;
7457
this.actionsId = actionsId;
7558
}
7659

7760
@Override
7861
public void run() {
7962
try {
63+
if (version == null) {
64+
version = PluginManager.getPlugin(PluginId.getId(Constant.PLUGIN_ID)).getVersion()
65+
.replace("v", "").replaceAll("-|_", ".");
66+
}
67+
if (userAgent == null) {
68+
if (SystemInfo.OS_NAME.toUpperCase().contains("MAC")) {
69+
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36";
70+
} else if (SystemInfo.OS_NAME.toUpperCase().contains("LINUX")) {
71+
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36";
72+
} else {
73+
userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36";
74+
}
75+
}
8076
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
8177
Calendar calendar = Calendar.getInstance();
8278
URI uri = new URIBuilder(URL)
@@ -89,7 +85,7 @@ public void run() {
8985
.setParameter("rdm", "")
9086
.setParameter("rurl", "")
9187
.setParameter("rarg", "")
92-
.setParameter("adt", "")
88+
.setParameter("adt", version)
9389
.setParameter("r2", SID)
9490
.setParameter("scr", (int)screensize.getWidth() + "x" + (int)screensize.getHeight())
9591
.setParameter("scl", Toolkit.getDefaultToolkit().getScreenResolution() + "-bit")
@@ -99,10 +95,8 @@ public void run() {
9995
.setParameter("random", System.currentTimeMillis() + "")
10096
.build();
10197

102-
HttpGet get = new HttpGet(uri);
98+
HttpRequests.request(uri.toURL().toString()).userAgent(userAgent).tryConnect();
10399

104-
HttpResponse response = client.execute(get);
105-
EntityUtils.consume(response.getEntity());
106100
} catch (Exception e) {
107101
}
108102
}

src/main/resources/i18n/info.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ response.timeout=Timed out while waiting for result
1919
request.code=Code does not exist
2020
request.empty=Empty code(not including comment)
2121
request.pending=Code submitted. Please wait...
22+
request.cancel=Request cancelled
2223
request.failed=Failed to send request
2324
submit.success=Success:\n\tRuntime:{0}, faster than {1}% of {2} online submissions.\n\tMemory Usage:{3}, less than {4}% of {5} online submissions.\n
2425
submit.failed=Wrong Answer:\n\tinput:{0}\n\tOutput:{1}\n\tExpected:{2}\n\tstdout:\n\t\t{3}\n

src/main/resources/i18n/info_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ response.timeout=\u7B49\u5F85\u7ED3\u679C\u8D85\u65F6
1919
request.code=\u63D0\u4EA4\u7684\u4EE3\u7801\u4E0D\u5B58\u5728
2020
request.empty=\u63D0\u4EA4\u7684\u4EE3\u7801\u4E3A\u7A7A(\u4E0D\u5305\u542B\u6CE8\u91CA)
2121
request.pending=\u5DF2\u63D0\u4EA4,\u8BF7\u7A0D\u7B49
22+
request.cancel=\u8BF7\u6C42\u5DF2\u53D6\u6D88
2223
request.failed=\u8BF7\u6C42\u5931\u8D25
2324
submit.success=\u89E3\u7B54\u6210\u529F:\n\t\u6267\u884C\u8017\u65F6:{0},\u51FB\u8D25\u4E86{1}% \u7684{2}\u7528\u6237\n\t\u5185\u5B58\u6D88\u8017:{3},\u51FB\u8D25\u4E86{4}% \u7684{5}\u7528\u6237\n
2425
submit.failed=\u89E3\u7B54\u5931\u8D25:\n\t\u6D4B\u8BD5\u7528\u4F8B:{0}\n\t\u6D4B\u8BD5\u7ED3\u679C:{1}\n\t\u671F\u671B\u7ED3\u679C:{2}\n\tstdout:\n\t\t{3}\n

0 commit comments

Comments
 (0)