From d6ff754a96a36b035bc02911f1da58b3970feee1 Mon Sep 17 00:00:00 2001 From: wm Date: Tue, 7 Nov 2023 09:39:52 +0800 Subject: [PATCH 1/2] update docker apiInfo detail --- jcommon/docker/pom.xml | 2 +- .../com/xiaomi/youpin/docker/AppInfo.java | 4 ++ .../xiaomi/youpin/docker/YpDockerClient.java | 43 ++++++++++++------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/jcommon/docker/pom.xml b/jcommon/docker/pom.xml index 8251117ee..a42bf4ee0 100644 --- a/jcommon/docker/pom.xml +++ b/jcommon/docker/pom.xml @@ -10,7 +10,7 @@ 4.0.0 docker - 1.6-SNAPSHOT + 1.7-SNAPSHOT diff --git a/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/AppInfo.java b/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/AppInfo.java index 3027ff5d9..1c3b2c23d 100644 --- a/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/AppInfo.java +++ b/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/AppInfo.java @@ -31,4 +31,8 @@ public class AppInfo { private String appName; + private int cpuNum; + + private long mem; + } diff --git a/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/YpDockerClient.java b/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/YpDockerClient.java index d6bfbaf91..5551f7e93 100644 --- a/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/YpDockerClient.java +++ b/jcommon/docker/src/main/java/com/xiaomi/youpin/docker/YpDockerClient.java @@ -24,6 +24,7 @@ import com.github.dockerjava.core.command.*; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; +import org.springframework.util.CollectionUtils; import java.io.ByteArrayOutputStream; import java.io.File; @@ -472,6 +473,28 @@ private String getAppName(String imageName) { */ public UseInfo containerUseInfo(String ip) { final List list = this.listContainers(Lists.newArrayList(), false); + + Map cpuAndMem = new HashMap<>();//envId:cpu,mem + + List ids = list.stream().map(it -> it.getId()).collect(Collectors.toList()); + Optional res = ids.stream().map(it -> { + try { + InspectContainerResponse info = this.inspectContainer(it); + UseInfo ui = UseInfo.builder().useCpuNum(getCpuNum(info.getHostConfig().getCpusetCpus())) + .useMemNum(info.getHostConfig().getMemory()) + .build(); + Map labels = info.getConfig().getLabels(); + if (!CollectionUtils.isEmpty(labels) && labels.containsKey("ENV_ID")){ + cpuAndMem.put(labels.get("ENV_ID"), ui.getUseCpuNum()+","+ui.getUseMemNum()); + } + return ui; + } catch (Throwable ex) { + log.warn("error:{}", ex.getMessage()); + return new UseInfo(0, 0, new HashSet<>(), Lists.newArrayList()); + } + } + ).reduce((a, b) -> UseInfo.builder().useCpuNum(a.getUseCpuNum() + b.getUseCpuNum()).useMemNum(a.getUseMemNum() + b.getUseMemNum()).build()); + List appList = Lists.newLinkedList(); Safe.run(() -> appList.addAll(list.stream().filter(it -> { Map labels = it.getLabels(); @@ -486,26 +509,16 @@ public UseInfo containerUseInfo(String ip) { info.setEnvId(labels.get("ENV_ID")); info.setAppName(getAppName(it.getImage())); info.setIp(ip); + if (cpuAndMem.containsKey(info.getEnvId())){ + String[] cpuMem = cpuAndMem.get(info.getEnvId()).split(","); + info.setCpuNum(Integer.parseInt(cpuMem[0])); + info.setMem(Long.parseLong(cpuMem[1])); + } return info; }).collect(Collectors.toList()))); log.info("docker app list size:{}", appList.size()); - List ids = list.stream().map(it -> it.getId()).collect(Collectors.toList()); - Optional res = ids.stream().map(it -> { - try { - InspectContainerResponse info = this.inspectContainer(it); - UseInfo ui = UseInfo.builder().useCpuNum(getCpuNum(info.getHostConfig().getCpusetCpus())) - .useMemNum(info.getHostConfig().getMemory()) - .build(); - return ui; - } catch (Throwable ex) { - log.warn("error:{}", ex.getMessage()); - return new UseInfo(0, 0, new HashSet<>(), Lists.newArrayList()); - } - } - ).reduce((a, b) -> UseInfo.builder().useCpuNum(a.getUseCpuNum() + b.getUseCpuNum()).useMemNum(a.getUseMemNum() + b.getUseMemNum()).build()); - Set appNames = list.stream().map(it -> getAppName(it.getImage())).collect(Collectors.toSet()); UseInfo info = null; if (res.isPresent()) { From c8dfae9295e731c5bd642baae23203894eaae1b4 Mon Sep 17 00:00:00 2001 From: wm Date: Mon, 8 Jan 2024 10:33:03 +0800 Subject: [PATCH 2/2] feat: CsvExcelConverter --- jcommon/excel/pom.xml | 6 ++ .../run/mone/excel/CsvExcelConverter.java | 93 +++++++++++++++++++ jcommon/excel/src/test/java/MyTest.java | 40 ++++++++ 3 files changed, 139 insertions(+) create mode 100644 jcommon/excel/src/main/java/run/mone/excel/CsvExcelConverter.java create mode 100644 jcommon/excel/src/test/java/MyTest.java diff --git a/jcommon/excel/pom.xml b/jcommon/excel/pom.xml index ed22aa09b..78aef4d8a 100644 --- a/jcommon/excel/pom.xml +++ b/jcommon/excel/pom.xml @@ -42,6 +42,12 @@ poi-scratchpad 3.17 + + org.apache.commons + commons-csv + 1.8 + + \ No newline at end of file diff --git a/jcommon/excel/src/main/java/run/mone/excel/CsvExcelConverter.java b/jcommon/excel/src/main/java/run/mone/excel/CsvExcelConverter.java new file mode 100644 index 000000000..2b38606fa --- /dev/null +++ b/jcommon/excel/src/main/java/run/mone/excel/CsvExcelConverter.java @@ -0,0 +1,93 @@ +package run.mone.excel; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.csv.CSVRecord; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author wmin + * @date 2024/1/8 + */ +public class CsvExcelConverter { + //csv转excel + public static void convertCsvToExcel(String csvFilePath, String excelFilePath) throws IOException { + try ( + InputStream csvInputStream = new FileInputStream(csvFilePath); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(csvInputStream)); + Workbook workbook = new XSSFWorkbook(); + FileOutputStream excelOutputStream = new FileOutputStream(excelFilePath) + ) { + String line; + Sheet sheet = workbook.createSheet("Sheet1"); + int rowNumber = 0; + while ((line = bufferedReader.readLine()) != null) { + String[] values = line.split(","); + Row row = sheet.createRow(rowNumber++); + for (int i = 0; i < values.length; i++) { + row.createCell(i).setCellValue(values[i]); + } + } + workbook.write(excelOutputStream); + } + } + + //excel转csv + public static void convertExcelToCsv(String excelFilePath, String csvFilePath) throws IOException, InvalidFormatException { + try (Workbook workbook = WorkbookFactory.create(new File(excelFilePath)); + CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(csvFilePath), CSVFormat.DEFAULT)) { + Sheet sheet = workbook.getSheetAt(0); + for (Row row : sheet) { + List csvValues = new ArrayList<>(); + row.forEach(cell -> { + String text = new DataFormatter().formatCellValue(cell); + csvValues.add(text); + }); + csvPrinter.printRecord(csvValues); + } + } + } + + //给定一个csv文件,将所有所有列名a=x的行的列名b的值修改为y + public static void updateCsvColumnWhereAnotherColumnEquals(String csvFilePath, String updatedCsvFilePath, String targetColumnName, String conditionColumnName, String conditionValue, String newValue) throws IOException { + try ( + Reader reader = Files.newBufferedReader(Paths.get(csvFilePath)); + Writer writer = Files.newBufferedWriter(Paths.get(updatedCsvFilePath)); + CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader()); + CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(parser.getHeaderMap().keySet().toArray(new String[0]))) + ) { + Map headerMap = parser.getHeaderMap(); + Integer targetColumnIndex = headerMap.get(targetColumnName); + Integer conditionColumnIndex = headerMap.get(conditionColumnName); + + if (targetColumnIndex == null || conditionColumnIndex == null) { + throw new IllegalArgumentException("Column name not found in the CSV file"); + } + + // Iterate through records and update the target column where condition matches + for (CSVRecord record : parser) { + List updatedRecord = new ArrayList<>(); + for (String value : record) { + updatedRecord.add(value); + } + + if (record.get(conditionColumnIndex).equals(conditionValue)) { + updatedRecord.set(targetColumnIndex, newValue); + } + + printer.printRecord(updatedRecord); + } + } + } + +} diff --git a/jcommon/excel/src/test/java/MyTest.java b/jcommon/excel/src/test/java/MyTest.java new file mode 100644 index 000000000..a56adde16 --- /dev/null +++ b/jcommon/excel/src/test/java/MyTest.java @@ -0,0 +1,40 @@ +import org.junit.Test; +import run.mone.excel.CsvExcelConverter; + +import java.io.IOException; + +/** + * @author wmin + * @date 2024/1/8 + */ +public class MyTest { + + @Test + public void testConvertCsvToExcel() { + try { + CsvExcelConverter.convertCsvToExcel("/Users/wmin/Downloads/miline_scaleOrder.csv","/Users/wmin/Downloads/miline_scaleOrder_0.xlsx"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConvertExcelToCsv() { + try { + CsvExcelConverter.convertExcelToCsv("/Users/wmin/Downloads/miline_scaleOrder.xlsx","/Users/wmin/Downloads/miline_scaleOrder_0.csv"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testUpdateCsvColumnWhereAnotherColumnEquals() { + try { + CsvExcelConverter.updateCsvColumnWhereAnotherColumnEquals("/Users/wmin/Downloads/miline_scaleOrder.csv", + "/Users/wmin/Downloads/miline_scaleOrder1.csv", + "env", "id","60267","online"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +}