1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
import com.google.common.collect.Lists; import com.google.common.collect.Maps; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.util.Strings; import org.springframework.util.ObjectUtils;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.NumberFormat; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; import java.util.stream.Stream;
@Slf4j public class TestCrudLine {
@org.junit.Test public void Test1() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(8); Stream<Path> pathStream = Files.find(Paths.get("D:", "dev", "xxx"), Integer.MAX_VALUE, (path, attr) -> path.getFileName().toString().endsWith(".xml") &&!path.toString().contains("target") &&!path.toString().contains(".idea") ); List<List<CrudVo>> sum = Lists.newArrayList(); pathStream.forEach(e->{ Future<List<CrudVo>> submit = executorService.submit(new GetCrudLines(e.toString())); try { Optional.of(submit.get()).ifPresent(sum::add); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (ExecutionException e1) { e1.printStackTrace(); } }); List<CrudVo> res = sum.parallelStream().flatMap(Collection::stream).collect(Collectors.toList()); System.out.println("汇总的sql对象:"+res); int size = res.size(); Map<String, LongSummaryStatistics> collect1 = res.stream().collect(Collectors.groupingBy(CrudVo::getType, Collectors.summarizingLong(CrudVo::getLines))); for(Map.Entry<String, LongSummaryStatistics> entries : collect1.entrySet()){ LongSummaryStatistics value = entries.getValue(); System.out.println(); System.out.println("SQL类型:"+entries.getKey()); System.out.println("求和:"+value.getSum()); System.out.println("求平均值:"+value.getAverage()); System.out.println("求最大值:"+value.getMax()); System.out.println("求最小值:"+value.getMin()); System.out.println("求总数:"+value.getCount()); } NumberFormat instance = NumberFormat.getInstance(); instance.setMaximumFractionDigits(2); Map<String, LongSummaryStatistics> collect2 = res.stream().collect(Collectors.groupingBy(CrudVo::getComplexity, Collectors.summarizingLong(CrudVo::getLines))); for(Map.Entry<String, LongSummaryStatistics> entries : collect2.entrySet()){ LongSummaryStatistics value = entries.getValue(); System.out.println(); System.out.println("复杂度:"+entries.getKey()); System.out.println("求总数:"+value.getCount()); System.out.println("占比:"+instance.format((float)value.getCount()/(float)size*100)+"%"); }
}
class GetCrudLines implements Callable<List<CrudVo>>{ String path; public GetCrudLines(String xmlPath) { this.path = xmlPath; } @Override public List<CrudVo> call() throws Exception { return getCrudLinesDo(this.path); } public String getId(String key,String s){ try { int i = s.indexOf(key + "=\"") + key.length() + 2; String substring = s.substring(i); int i1 = substring.indexOf("\""); String id = substring.substring(0, i1); return id; }catch (Exception e){ return ""; } }
private List<CrudVo> getCrudLinesDo(String path) throws IOException { List<CrudVo> res = Lists.newArrayList(); BufferedReader bufferedReader = new BufferedReader(new FileReader(path)); String str = null,sqlIdStr=null; int lineStart = 0,sqlIdStart=0; List<String> filterStr = Arrays.asList("insert", "delete", "update", "select"); Map<String,Integer> sqlFeild = Maps.newHashMap(); CrudVo crudVo=null; while((str = bufferedReader.readLine()) != null){ String s = Strings.trimToNull(str); if(ObjectUtils.isEmpty(s)){continue;} if(s.startsWith("<sql")){ sqlIdStr =getId("id",s); sqlFeild.put(sqlIdStr,0); sqlIdStart=0; } sqlIdStart++; if(s.endsWith("</sql>")){ sqlFeild.put(sqlIdStr,sqlIdStart-2); } String s1 = filterStr.stream().filter(e -> s.contains("<" + e)).findAny().orElse(""); if(!ObjectUtils.isEmpty(s1)){ crudVo = new CrudVo(); crudVo.setType(s1); crudVo.setId(getId("id",s)); lineStart = 0; } lineStart++; if(s.startsWith("<include")){ try { lineStart += sqlFeild.get(getId("refid", s)); }catch (Exception e){
} } String s2 = filterStr.stream().filter(e -> s.contains("</" + e)).findAny().orElse(""); if(!ObjectUtils.isEmpty(s2)){ int lines = lineStart - 2; crudVo.setLines(lines); if(lines<30){ crudVo.setComplexity("简单"); }else if(lines<60&&lines>=30){ crudVo.setComplexity("一般"); }else if(lines<100&&lines>=60){ crudVo.setComplexity("较难"); }else{ crudVo.setComplexity("难"); } res.add(crudVo); } }
return res; } } @Data @AllArgsConstructor @NoArgsConstructor class CrudVo{ String id; String type; int lines; String complexity;
@Override public String toString() { return "{" + "'id':'" + id + '\'' + ", 'type':'" + type + '\'' + ", 'lines':" + lines + ", 'complexity':" + complexity + '}'; } }
}
|