Skip to content

Commit

Permalink
优化 统计数据发送功能,为Docker部署做相关准备。Optimise the statistics sending function, …
Browse files Browse the repository at this point in the history
…Make relevant preparations for Docker deployment.
  • Loading branch information
Winnie0408 committed Oct 25, 2023
1 parent 810e490 commit 75ed593
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
Expand All @@ -18,6 +23,7 @@
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
Expand All @@ -39,6 +45,7 @@
@RequestMapping("/converter")
public class ConverterController {
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterController.class);
private static final boolean IN_DOCKER = false;

@GetMapping("/init")
public Object init(@RequestParam String source, HttpServletRequest request, HttpServletResponse response) {
Expand Down Expand Up @@ -190,7 +197,7 @@ public Object databaseSummary(HttpServletRequest request, HttpServletResponse re

Database db = new Database();
try {
conn = db.getSQLiteConnection("sqliteUpload/" + database);
conn = db.getSQLiteConnection("sqliteUpload" + File.separator + database);

Statement stmt = conn.createStatement();
ResultSet rs;
Expand Down Expand Up @@ -251,6 +258,12 @@ public Object databaseSummary(HttpServletRequest request, HttpServletResponse re
// result.put("songNum", songNum);
response.setStatus(200);
db.closeConnection(conn);

if (IN_DOCKER)
saveUsageDocker(session.getId(), "Uploaded");
else
saveUsage(session.getId(), "Uploaded");

return new JSONObject(result);

} catch (SQLException e) {
Expand Down Expand Up @@ -301,7 +314,7 @@ public Object attemptConvert(@RequestParam(defaultValue = "85") String similarit
Connection conn = null;

try {
conn = db.getSQLiteConnection("sqliteUpload/" + database);
conn = db.getSQLiteConnection("sqliteUpload" + File.separator + database);

Statement stmt = conn.createStatement();
Statement stmt1 = conn.createStatement();
Expand Down Expand Up @@ -458,8 +471,26 @@ public Object attemptConvert(@RequestParam(defaultValue = "85") String similarit
result.put("total", num);
result.put("sourceChn", sourceChn);
if (session.getAttribute("allowStatistic").equals(true)) {
String startTime = saveStatistic1(sourceAttribute, enableParenthesesRemoval, enableArtistNameMatch, enableAlbumNameMatch, mode, num, autoSuccessCount, similarity, session.getId());
session.setAttribute("startTime", startTime);
if (IN_DOCKER) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String startTime = sdf.format(System.currentTimeMillis());

Map<String, Object> statisticRelated = new HashMap<>();
statisticRelated.put("sourceChn", sourceChn);
statisticRelated.put("sourceEng", sourceEng);
statisticRelated.put("enableParenthesesRemoval", enableParenthesesRemoval);
statisticRelated.put("enableArtistNameMatch", enableArtistNameMatch);
statisticRelated.put("enableAlbumNameMatch", enableAlbumNameMatch);
statisticRelated.put("mode", mode);
statisticRelated.put("totalCount", num);
statisticRelated.put("autoSuccessCount", autoSuccessCount);
statisticRelated.put("similarity", similarity);
statisticRelated.put("startTime", startTime);
session.setAttribute("statisticRelated", statisticRelated);
} else {
String startTime = saveStatistic1(sourceAttribute, enableParenthesesRemoval, enableArtistNameMatch, enableAlbumNameMatch, mode, num, autoSuccessCount, similarity, session.getId());
session.setAttribute("startTime", startTime);
}
}
return new JSONObject(result);
} catch (SQLException e) {
Expand Down Expand Up @@ -553,8 +584,18 @@ public Object saveCurrentMusicList(@RequestBody String frontEnd, HttpServletRequ
});
fileWriter.close();
response.setStatus(200);
if (session.getAttribute("allowStatistic").equals(true))
saveStatistic2((String) session.getAttribute("startTime"), session.getId(), map.size());
if (session.getAttribute("allowStatistic").equals(true)) {
if (IN_DOCKER)
saveStatisticDocker(session, map.size());
else
saveStatistic2((String) session.getAttribute("startTime"), session.getId(), map.size());
}

if (IN_DOCKER)
saveUsageDocker(session.getId(), "Save");
else
saveUsage(session.getId(), "Save");

return "{\"msg\":\"保存成功\"}";
} catch (Exception e) {
LOGGER.error(e.toString(), e);
Expand Down Expand Up @@ -610,6 +651,17 @@ public String test(@RequestParam(defaultValue = "") String ping, @RequestParam b
HttpSession session = request.getSession(true);
session.setMaxInactiveInterval(900);
session.setAttribute("allowStatistic", allowStatistic);

if (IN_DOCKER)
saveUsageDocker(session.getId(), "Access");
else {
try {
saveUsage(session.getId(), "Access");
} catch (SQLException e) {
LOGGER.error(e.toString(), e);
}
}

if (ping.equals("Ping!"))
return "Pong!";
return "";
Expand Down Expand Up @@ -686,6 +738,80 @@ private void saveStatistic2(String time, String sessionId, int saveCount) throws
db.closeConnection(conn);
}

private void saveStatisticDocker(HttpSession session, int saveCount) {
Map<String, Object> result = (Map<String, Object>) session.getAttribute("statisticRelated");
result.put("tool", "Docker Vue+SpringBoot");
result.put("uuid", session.getId());
result.put("successCount", saveCount);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String time = sdf.format(System.currentTimeMillis());
result.put("endTime", time);
JSONObject jsonObject = new JSONObject(result);

String url;
if (IN_DOCKER)
url = "http://localhost:8082/statistic/save";
else
url = "https://saltconv.hwinzniej.top:46000/statistic/save";
try {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);

StringEntity stringEntity = new StringEntity(jsonObject.toString(), StandardCharsets.UTF_8);
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
response.close();
httpClient.close();
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
}

private void saveUsage(String sessionId, String type) throws SQLException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String time = sdf.format(System.currentTimeMillis());
Database db = new Database();
Connection conn = db.getMySQLConnection();

Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO `usage` (time,sessionId,type,tool) VALUES ('" + time + "', '" + sessionId + "', '" + type + "', 'Vue+SpringBoot')");

stmt.close();
db.closeConnection(conn);
}

private void saveUsageDocker(String sessionId, String type) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String time = sdf.format(System.currentTimeMillis());

Map<String, Object> result = new HashMap<>();
result.put("time", time);
result.put("sessionId", sessionId);
result.put("tool", "Docker Vue+SpringBoot");
result.put("type", type);

JSONObject jsonObject = new JSONObject(result);
String url;
if (IN_DOCKER)
url = "http://localhost:8082/statistic/usage";
else
url = "https://saltconv.hwinzniej.top:46000/statistic/usage";
try {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);

StringEntity stringEntity = new StringEntity(jsonObject.toString(), StandardCharsets.UTF_8);
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
response.close();
httpClient.close();
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
}

private void deleteFilesInDir(File dir, String startWith) throws Exception {
if (dir.isDirectory()) {
File[] files = dir.listFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.alibaba.fastjson.JSONObject;
import com.hwinzniej.saltplayerconverter.backend.utils.Database;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -22,7 +24,9 @@
@RestController
@RequestMapping("/statistic")
public class StatisticController {
@PostMapping("save")
private static final Logger LOGGER = LoggerFactory.getLogger(StatisticController.class);

@PostMapping("/save")
public Object save(@RequestBody String frontEnd, HttpServletResponse response) {
JSONObject data = JSONObject.parseObject(frontEnd);
Map<String, Object> result = new HashMap<>();
Expand All @@ -41,17 +45,20 @@ public Object save(@RequestBody String frontEnd, HttpServletResponse response) {
int saveCount = data.getIntValue("successCount");
boolean enableParenthesesRemoval = data.getBooleanValue("enableParenthesesRemoval");
boolean enableAlbumNameMatch = data.getBooleanValue("enableAlbumNameMatch");
boolean enableArtistNameMatch = data.getBooleanValue("enableArtistNameMatch");
boolean mode = data.getBooleanValue("mode");
double similarity = data.getDoubleValue("similarity");
int autoSuccessCount = data.getIntValue("autoSuccessCount");
String time = data.getString("startTime");
String endTime = data.getString("endTime");
String tool = data.getString("tool");

try {
Database db = new Database();
Connection conn = db.getMySQLConnection();

Statement stmt = conn.createStatement();
int saveResult = stmt.executeUpdate("INSERT INTO info (sourceEng, sourceChn, enableParenthesesRemoval, enableArtistNameMatch, enableAlbumNameMatch, mode, totalCount, autoSuccessCount, similarity, startTime, sessionId, tool,saveCount,endTime) VALUES ('" + sourceEng + "', " + "'" + sourceChn + "', " + enableParenthesesRemoval + ", " + null + ", " + enableAlbumNameMatch + ", " + 1 + ", " + totalCount + ", " + autoSuccessCount + ", " + similarity + ", '" + time + "', '" + sessionId + "', 'JavaSE'" + " , " + saveCount + " , '" + endTime + "')");
int saveResult = stmt.executeUpdate("INSERT INTO info (sourceEng, sourceChn, enableParenthesesRemoval, enableArtistNameMatch, enableAlbumNameMatch, mode, totalCount, autoSuccessCount, similarity, startTime, sessionId, tool, saveCount, endTime) VALUES ('" + sourceEng + "', " + "'" + sourceChn + "', " + enableParenthesesRemoval + ", " + enableArtistNameMatch + ", " + enableAlbumNameMatch + ", " + mode + ", " + totalCount + ", " + autoSuccessCount + ", " + similarity + ", '" + time + "', '" + sessionId + "', '" + tool + "' , " + saveCount + " , '" + endTime + "')");
stmt.close();
db.closeConnection(conn);

Expand All @@ -69,8 +76,55 @@ public Object save(@RequestBody String frontEnd, HttpServletResponse response) {
response.setStatus(500);
result.put("result", 1);
result.put("msg", "上传失败" + e.getMessage());
LOGGER.error(e.toString(), e);
}

return result;
}

@PostMapping("/usage")
public Object usage(@RequestBody String frontEnd, HttpServletResponse response) {
JSONObject data = JSONObject.parseObject(frontEnd);
Map<String, Object> result = new HashMap<>();

if (data.isEmpty()) {
response.setStatus(400);
result.put("result", "0");
result.put("msg", "参数错误");
return new JSONObject(result);
}

String sessionId = data.getString("sessionId");
String time = data.getString("time");
String tool = data.getString("tool");
String type = data.getString("type");

try {
Database db = new Database();
Connection conn = db.getMySQLConnection();

Statement stmt = conn.createStatement();
int saveResult = stmt.executeUpdate("INSERT INTO `usage` (time,sessionId,type,tool) VALUES ('" + time + "', '" + sessionId + "', '" + type + "', '" + tool + "')");

stmt.close();
db.closeConnection(conn);

if (saveResult == 1) {
response.setStatus(200);
result.put("result", 0);
result.put("msg", "上传成功");
} else {
response.setStatus(500);
result.put("result", 1);
result.put("msg", "上传失败");
}

} catch (Exception e) {
response.setStatus(500);
result.put("result", 1);
result.put("msg", "上传失败" + e.getMessage());
LOGGER.error(e.toString(), e);
}
return result;
}
}

0 comments on commit 75ed593

Please sign in to comment.