Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update docker apiInfo detail #751

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jcommon/docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>docker</artifactId>
<version>1.6-SNAPSHOT</version>
<version>1.7-SNAPSHOT</version>

<dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public class AppInfo {

private String appName;

private int cpuNum;

private long mem;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -472,6 +473,28 @@ private String getAppName(String imageName) {
*/
public UseInfo containerUseInfo(String ip) {
final List<Container> list = this.listContainers(Lists.newArrayList(), false);

Map<String, String> cpuAndMem = new HashMap<>();//envId:cpu,mem

List<String> ids = list.stream().map(it -> it.getId()).collect(Collectors.toList());
Optional<UseInfo> 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<String, String> 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<AppInfo> appList = Lists.newLinkedList();
Safe.run(() -> appList.addAll(list.stream().filter(it -> {
Map<String, String> labels = it.getLabels();
Expand All @@ -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<String> ids = list.stream().map(it -> it.getId()).collect(Collectors.toList());
Optional<UseInfo> 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<String> appNames = list.stream().map(it -> getAppName(it.getImage())).collect(Collectors.toSet());
UseInfo info = null;
if (res.isPresent()) {
Expand Down
6 changes: 6 additions & 0 deletions jcommon/excel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>

</dependencies>

</project>
93 changes: 93 additions & 0 deletions jcommon/excel/src/main/java/run/mone/excel/CsvExcelConverter.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String, Integer> 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<String> updatedRecord = new ArrayList<>();
for (String value : record) {
updatedRecord.add(value);
}

if (record.get(conditionColumnIndex).equals(conditionValue)) {
updatedRecord.set(targetColumnIndex, newValue);
}

printer.printRecord(updatedRecord);
}
}
}

}
40 changes: 40 additions & 0 deletions jcommon/excel/src/test/java/MyTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}