1 package org.kuali.common.devops.jenkins.upgrade; 2 3 import static com.google.common.collect.Lists.newArrayList; 4 import static com.google.common.collect.Ordering.natural; 5 import static java.lang.Integer.parseInt; 6 import static java.lang.String.format; 7 import static java.lang.System.getProperty; 8 import static java.util.Arrays.asList; 9 import static org.kuali.common.core.io.Files.getDiskUsage; 10 import static org.kuali.common.util.FormatUtils.getCount; 11 import static org.kuali.common.util.FormatUtils.getSize; 12 import static org.kuali.common.util.log.LoggerUtils.logTable; 13 import static org.kuali.common.util.log.Loggers.newLogger; 14 15 import java.text.SimpleDateFormat; 16 import java.util.Date; 17 import java.util.List; 18 19 import org.kuali.common.core.io.DiskUsage; 20 import org.kuali.common.core.io.UnixFile; 21 import org.kuali.common.devops.jenkins.upgrade.model.BuildSummary; 22 import org.kuali.common.devops.jenkins.upgrade.model.JenkinsSummary; 23 import org.kuali.common.devops.jenkins.upgrade.model.JobSummary; 24 import org.slf4j.Logger; 25 26 import com.google.common.primitives.Doubles; 27 28 public final class JenkinsReporting { 29 30 private static final Logger logger = newLogger(); 31 32 public static void doReporting(JenkinsSummary jenkins) { 33 int limit = parseInt(getProperty("limit", "10")); 34 diskUsageReport(getJobDiskUsage(jenkins), getBuildDiskUsage(jenkins), limit); 35 } 36 37 public static void diskUsageReport(List<LabeledDiskUsage> jobs, List<LabeledDiskUsage> builds, int limit) { 38 List<String> columns = asList("", "files", "size"); 39 List<Object[]> rows = newArrayList(); 40 long totalFiles = 0; 41 long totalSize = 0; 42 int i = 0; 43 rows.add(new Object[] { "job", "", "" }); 44 for (LabeledDiskUsage element : jobs) { 45 if (i++ >= limit) { 46 break; 47 } 48 DiskUsage du = element.getDu(); 49 totalFiles += du.getCount(); 50 totalSize += du.getSize(); 51 String count = getCount(du.getCount()); 52 String size = getSize(du.getSize()); 53 Object[] row = { element.getLabel(), count, size }; 54 rows.add(row); 55 } 56 rows.add(new Object[] { "", "", "" }); 57 rows.add(new Object[] { "totals", getCount(totalFiles), getSize(totalSize) }); 58 rows.add(new Object[] { "", "", "" }); 59 rows.add(new Object[] { "build", "", "" }); 60 totalFiles = 0; 61 totalSize = 0; 62 i = 0; 63 for (LabeledDiskUsage element : builds) { 64 if (i++ >= limit) { 65 break; 66 } 67 DiskUsage du = element.getDu(); 68 totalFiles += du.getCount(); 69 totalSize += du.getSize(); 70 String count = getCount(du.getCount()); 71 String size = getSize(du.getSize()); 72 Object[] row = { element.getLabel(), count, size }; 73 rows.add(row); 74 } 75 rows.add(new Object[] { "", "", "" }); 76 rows.add(new Object[] { "totals", getCount(totalFiles), getSize(totalSize) }); 77 logTable(columns, rows); 78 } 79 80 public static List<LabeledDiskUsage> getBuildDiskUsage(JenkinsSummary jenkins) { 81 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 82 List<LabeledDiskUsage> list = newArrayList(); 83 for (JobSummary job : jenkins.getJobs()) { 84 for (BuildSummary build : job.getBuilds()) { 85 DiskUsage du = getDiskUsage(build.getFiles()); 86 String label = job.getName() + "-" + sdf.format(new Date(build.getStartTime())); 87 LabeledDiskUsage ldu = new LabeledDiskUsage(label, du); 88 list.add(ldu); 89 } 90 } 91 return natural().reverse().immutableSortedCopy(list); 92 } 93 94 public static List<LabeledDiskUsage> getJobDiskUsage(JenkinsSummary jenkins) { 95 List<LabeledDiskUsage> list = newArrayList(); 96 for (JobSummary job : jenkins.getJobs()) { 97 List<UnixFile> files = getFiles(job); 98 DiskUsage du = getDiskUsage(files); 99 LabeledDiskUsage ldu = new LabeledDiskUsage(job.getName(), du); 100 list.add(ldu); 101 } 102 return natural().reverse().immutableSortedCopy(list); 103 } 104 105 public static class LabeledDiskUsage implements Comparable<LabeledDiskUsage> { 106 107 public LabeledDiskUsage(String label, DiskUsage du) { 108 this.label = label; 109 this.du = du; 110 } 111 112 @Override 113 public int compareTo(LabeledDiskUsage other) { 114 return Doubles.compare(du.getSize(), other.getDu().getSize()); 115 } 116 117 private final String label; 118 private final DiskUsage du; 119 120 public String getLabel() { 121 return label; 122 } 123 124 public DiskUsage getDu() { 125 return du; 126 } 127 128 } 129 130 public static List<UnixFile> getFiles(JobSummary job) { 131 List<UnixFile> files = newArrayList(); 132 for (BuildSummary build : job.getBuilds()) { 133 files.addAll(build.getFiles()); 134 } 135 return files; 136 } 137 138 public static List<UnixFile> getFiles(JenkinsSummary jenkins) { 139 List<UnixFile> files = newArrayList(); 140 for (JobSummary job : jenkins.getJobs()) { 141 files.addAll(getFiles(job)); 142 } 143 return files; 144 } 145 146 protected static void info(String msg, Object... args) { 147 logger.info((args == null || args.length == 0) ? msg : format(msg, args)); 148 } 149 150 }