Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.svi.main.logic; import com.svi.main.frame.MainFrame; import com.svi.main.objects.Elements; import com.svi.main.objects.Nodes; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @author Daniel */ public class ExtractAndPrint implements Runnable { private final int MAX_MINUTES_ELEMENT; private final MainFrame mf; private final String logPath; private final String cluster; private final String selectedDates; private List<String> fileHolder = new ArrayList<>(); private final Set<String> nodeIds = new HashSet<>(); private final List<String> extraDetails = new ArrayList<>(); private final List<Elements> elementHolder = new ArrayList<>(); private AtomicInteger progress; private AtomicInteger total; ExtractAndPrint(MainFrame mf, List<String> fileHolder, String logPath, String cluster, String selectedDates, int minutesException) { this.mf = mf; this.fileHolder = fileHolder; this.logPath = logPath; this.cluster = cluster; this.selectedDates = selectedDates; this.MAX_MINUTES_ELEMENT = minutesException; } private void retrieveData(String inputStr) { Elements element; String line; String value = ""; long longValue = 0; try (BufferedReader br = new BufferedReader(new FileReader(inputStr))) { while ((line = br.readLine()) != null) { element = new Elements(); String[] splitter = line.split(","); for (int index = 0; index < splitter.length; index++) { setElementValues(longValue, splitter, index, value, element); } nodeIds.add(splitter[2]); elementHolder.add(element); } } catch (IOException e) { e.printStackTrace(); } } private void setElementValues(long longValue, String[] splitter, int index, String value, Elements element) { try { longValue = (long) Double.parseDouble(splitter[index]); } catch (NumberFormatException nfe) { value = splitter[index]; } switch (index) { case 0: element.setElementId(value); break; case 1: element.setElementName(value); break; case 2: element.setNodeId(value); break; case 3: element.setCluster(value); break; case 4: element.setWorkerId(value); break; case 5: element.setPriority(longValue); break; case 6: element.setElementType(value); case 7: element.setStatus(value); break; case 8: element.setStartWaitingTime(longValue); break; case 9: element.setStartProcessingTime(longValue); break; case 10: element.setEndProcessingTime(longValue); break; case 11: element.setWaitingDuration(longValue); break; case 12: element.setProcessingDuration(longValue); break; case 13: element.setInternalUnit(value); break; case 14: element.setInternalCost(longValue); break; case 15: element.setInternalOutputCount(longValue); break; case 16: element.setInternalErrorCount(longValue); break; case 17: element.setExternalUnit(value); break; case 18: element.setExternalCost(longValue); break; case 19: element.setExternalOutputCount(longValue); break; case 20: element.setExternalErrorCount(longValue); break; case 21: element.setApplicationUnit(value); break; case 22: element.setApplicationCost(longValue); break; case 23: element.setApplicationOutputCount(longValue); break; case 24: element.setApplicationErrorcount(longValue); break; case 25: element.setCompensationUnit(value); break; case 26: element.setCompensationCost(longValue); break; case 27: element.setCompensationOutputCount(longValue); break; case 28: element.setCompensationErrorCount(longValue); break; case 29: element.setOthersUnit(value); break; case 30: element.setOthersCount(longValue); break; case 31: element.setOthersOutputCount(longValue); break; case 32: element.setOthersErrorCount(longValue); break; case 33: extraDetails.add(value); element.setExtraDetails(fileHolder); break; } } private List<Nodes> setElementsToNodes() { List<Nodes> nodeHolder = new ArrayList<>(); List<Elements> completeElementsHolder = new ArrayList<>(); List<String> startWaitTimeHolder = new ArrayList<>(); List<String> startProcTimeHolder = new ArrayList<>(); List<String> endProcTimeHolder = new ArrayList<>(); List<Long> aveWaitDuration = new ArrayList<>(); List<Long> aveProcDuration = new ArrayList<>(); long currentTimeStamp = System.currentTimeMillis(); long tempAveWaitDur = 0; long tempAveProcDur = 0; long hoursElementsProcessed = 0; long minutesElementProcessing; long minutesElementComplete; Nodes node; int waitingCtr = 0; int processedCtr = 0; int numberOfImages = 0; double elementWithErrorCount = 0; // Assign unique node IDs for (String s : nodeIds) { node = new Nodes(); node.setNodeId(s); nodeHolder.add(node); } // Assign elements to respective node for (Elements e : elementHolder) { for (int i = 0; i < nodeHolder.size(); i++) { //For filtering by cluster. // if (nodeHolder.get(i).getNodeId().equals(e.getNodeId()) && e.getCluster().equalsIgnoreCase(cluster) ) { if (nodeHolder.get(i).getNodeId().equals(e.getNodeId())) { nodeHolder.get(i).add(e); } } } //Check for completed elements for (int i = 0; i < nodeHolder.size(); i++) { for (Elements e : nodeHolder.get(i)) { if (e.getStatus().equalsIgnoreCase("COMPLETE")) { completeElementsHolder.add(e); } } } for (Nodes n : nodeHolder) { for (Elements e : n) { for (Elements e2 : completeElementsHolder) { if (e.getElementId().equals(e2.getElementId()) && e.getNodeId().equals(e2.getNodeId())) { e.setCompleted(true); } } } } //Gather data for (int i = 0; i < nodeHolder.size(); i++) { for (Elements e : nodeHolder.get(i)) { //Get external unit (images) for OCR if (e.getExternalUnit() != null || !e.getExternalUnit().isEmpty()) { if (e.getExternalUnit().equalsIgnoreCase("image")) { numberOfImages += e.getExternalOutputCount(); } } if (e.getStartProcessingTime() == 0) { aveWaitDuration.add(currentTimeStamp - e.getStartWaitingTime()); } else if (e.getStartProcessingTime() != 0) { aveWaitDuration.add(e.getStartProcessingTime() - e.getStartWaitingTime()); } startWaitTimeHolder.add(longToDate(e.getStartWaitingTime())); if (e.getStatus().equalsIgnoreCase("waiting")) { waitingCtr++; } else if (e.getStatus().equalsIgnoreCase("processing") && !e.isCompleted()) { startProcTimeHolder.add(longToDate(e.getStartProcessingTime())); minutesElementProcessing = currentTimeStamp - e.getStartProcessingTime(); e.setConvertedStartProcTime(longToDate(e.getStartProcessingTime())); e.setConvertedEndProcTime(longToDate(currentTimeStamp)); e.setTotalProcTime(TimeUnit.MILLISECONDS.toMinutes(minutesElementProcessing)); if (e.getTotalProcTime() > MAX_MINUTES_ELEMENT) { elementWithErrorCount++; } } else if (e.getStatus().equalsIgnoreCase("COMPLETE")) { processedCtr++; hoursElementsProcessed += e.getEndProcessingTime() - e.getStartProcessingTime(); minutesElementComplete = e.getEndProcessingTime() - e.getStartProcessingTime(); e.setConvertedStartProcTime(longToDate(e.getStartProcessingTime())); e.setConvertedEndProcTime(longToDate(e.getEndProcessingTime())); e.setTotalProcTime(TimeUnit.MILLISECONDS.toMinutes(minutesElementComplete)); if (e.getTotalProcTime() > MAX_MINUTES_ELEMENT) { elementWithErrorCount++; } aveProcDuration.add(e.getProcessingDuration()); endProcTimeHolder.add(longToDate(e.getStartProcessingTime())); } nodeHolder.get(i).setCluster(e.getCluster()); } //Sort data Collections.sort(startWaitTimeHolder); Collections.sort(startProcTimeHolder); Collections.sort(endProcTimeHolder); /*Set data for node reports*/ //Set averages for (long l : aveWaitDuration) { tempAveWaitDur += l; } for (long l : aveProcDuration) { tempAveProcDur += l; } nodeHolder.get(i).setExternalOutputCount(numberOfImages); nodeHolder.get(i).setTotalProcessDuration(TimeUnit.MILLISECONDS.toMinutes(tempAveProcDur)); nodeHolder.get(i) .setSpeedPerNode((TimeUnit.MILLISECONDS.toMinutes(tempAveProcDur) / nodeHolder.get(i).size())); // Set averages if (!aveWaitDuration.isEmpty()) { tempAveWaitDur = (tempAveWaitDur / aveWaitDuration.size()); } if (!aveProcDuration.isEmpty()) { tempAveProcDur = (tempAveProcDur / aveProcDuration.size()); } nodeHolder.get(i).setAveWaitingDuration(TimeUnit.MILLISECONDS.toMinutes(tempAveWaitDur)); nodeHolder.get(i).setAveProcessDuration(TimeUnit.MILLISECONDS.toMinutes(tempAveProcDur)); nodeHolder.get(i).setNoOfElements(nodeHolder.get(i).size()); nodeHolder.get(i).setElementWithError(elementWithErrorCount); nodeHolder.get(i).setErrorPercentage((elementWithErrorCount / nodeHolder.get(i).size())); hoursElementsProcessed = TimeUnit.MILLISECONDS.toHours(hoursElementsProcessed); if (hoursElementsProcessed != 0) { nodeHolder.get(i) .setAveNumberOfElementsHour(Math.round(nodeHolder.get(i).size() / hoursElementsProcessed)); } nodeHolder.get(i).setNoOfWaitingElements(waitingCtr); nodeHolder.get(i).setNoOfElementsProcessed(processedCtr); //Check if empty before set earlist start proc time and latest proc time if (!startWaitTimeHolder.isEmpty()) { nodeHolder.get(i).setEarliestSWT(startWaitTimeHolder.get(0)); } else { nodeHolder.get(i).setEarliestSWT("N/A"); } if (!startProcTimeHolder.isEmpty()) { nodeHolder.get(i).setEarliestSPT(startProcTimeHolder.get(0)); } else { nodeHolder.get(i).setEarliestSPT("N/A"); } if (!endProcTimeHolder.isEmpty()) { nodeHolder.get(i).setLatestEPT(endProcTimeHolder.get(endProcTimeHolder.size() - 1)); } else { nodeHolder.get(i).setLatestEPT("N/A"); } //Reset all values for each node waitingCtr = 0; processedCtr = 0; tempAveWaitDur = 0; tempAveProcDur = 0; hoursElementsProcessed = 0; elementWithErrorCount = 0; numberOfImages = 0; startWaitTimeHolder = new ArrayList<>(); startProcTimeHolder = new ArrayList<>(); endProcTimeHolder = new ArrayList<>(); aveWaitDuration = new ArrayList<>(); aveProcDuration = new ArrayList<>(); } return nodeHolder; } private String longToDate(long longValue) { Date date = new Date(longValue); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm", Locale.US); return sdf.format(date); } private void writeLogFile(List<Nodes> nodesHolder) { DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmm"); Date date = new Date(); Sheet mainSheet; Sheet dataSheet; Sheet elementSheet; Row dataSheetRow; Row elementSheetRow; InputStream fis; XSSFWorkbook workbook; File outputPath = new File(logPath + "\\Logs"); File outputFile = new File(logPath + "\\Logs\\BPO KPI Report " + dateFormat.format(date) + ".xlsx"); // File name int dataSheetRowCount = 1; int elementSheetRowCount = 1; int totalElementException = 0; try { if (!Files.exists(outputPath.toPath())) { Files.createDirectories(outputPath.toPath()); } fis = ExtractAndPrint.class.getResourceAsStream("bpo_template.xlsx"); workbook = new XSSFWorkbook(fis); //Style for exception sheet XSSFCellStyle style = workbook.createCellStyle(); style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); //Get data Sheet mainSheet = workbook.getSheetAt(0); writeProjectDetails(mainSheet); dataSheet = workbook.getSheetAt(4); dataSheetRow = dataSheet.createRow(0); elementSheet = workbook.getSheetAt(1); elementSheetRow = elementSheet.createRow(0); //Write excel headers writeDataSheetHeaders(dataSheetRow); writeElementSheetHeaders(elementSheetRow); //Set progress bar values progress = new AtomicInteger(0); total = new AtomicInteger(nodesHolder.size()); mf.setJprogressValues(total, progress); // Sort the nodes per Node ID Collections.sort(nodesHolder, new Comparator<Nodes>() { public int compare(Nodes o1, Nodes o2) { return o1.getNodeId().compareTo(o2.getNodeId()); } }); //Write Data Sheet for (Nodes node : nodesHolder) { mf.loader(); dataSheetRow = dataSheet.createRow(dataSheetRowCount++); writeDataSheet(node, dataSheetRow); } for (Nodes node : nodesHolder) { for (Elements e : node) { if ((e.getStatus().equalsIgnoreCase("COMPLETE") || e.getStatus().equalsIgnoreCase("PROCESSING")) && e.getTotalProcTime() > MAX_MINUTES_ELEMENT) { totalElementException++; } } } progress = new AtomicInteger(0); total = new AtomicInteger(totalElementException); mf.setJprogressValues(total, progress); //Write exception sheet for (Nodes node : nodesHolder) { for (Elements e : node) { if ((e.getStatus().equalsIgnoreCase("COMPLETE") || e.getStatus().equalsIgnoreCase("PROCESSING")) && e.getTotalProcTime() > MAX_MINUTES_ELEMENT) { elementSheetRow = elementSheet.createRow(elementSheetRowCount++); writeElementSheet(e, elementSheetRow); mf.elementLoader(); } } } XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook) workbook); try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { workbook.write(outputStream);//Write the Excel File outputStream.close(); } workbook.close(); fis.close(); mf.generateMessage(); } catch (Exception ex) { Logger.getLogger(ExtractAndPrint.class.getName()).log(Level.SEVERE, null, ex); } } private void writeProjectDetails(Sheet mainSheet) { Row mainSheetRow; mainSheetRow = mainSheet.getRow(1); Cell cell = mainSheetRow.createCell(2); cell.setCellValue(cluster); DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm"); Date date = new Date(); mainSheetRow = mainSheet.getRow(2); cell = mainSheetRow.createCell(2); cell.setCellValue(dateFormat.format(date)); } private void writeDataSheetHeaders(Row dataSheetRow) { Cell cell = dataSheetRow.createCell(0); cell.setCellValue("Node ID"); cell = dataSheetRow.createCell(1); cell.setCellValue("Cluster"); cell = dataSheetRow.createCell(2); cell.setCellValue("Earliest Start Waiting Time"); cell = dataSheetRow.createCell(3); cell.setCellValue("Earliest Start Processing Time"); cell = dataSheetRow.createCell(4); cell.setCellValue("Latest End Processing Time"); cell = dataSheetRow.createCell(5); cell.setCellValue("Average Waiting Duration (Minutes)"); cell = dataSheetRow.createCell(6); cell.setCellValue("Average Processing Duration (Minutes)"); cell = dataSheetRow.createCell(7); cell.setCellValue("Total Processing Duration (Minutes)"); cell = dataSheetRow.createCell(8); cell.setCellValue("No of Elements Inserted"); cell = dataSheetRow.createCell(9); cell.setCellValue("No of Elements in Waiting"); cell = dataSheetRow.createCell(10); cell.setCellValue("No of Elements Processed"); cell = dataSheetRow.createCell(11); cell.setCellValue("No of Elements Processed/Hour"); cell = dataSheetRow.createCell(12); cell.setCellValue("Speed per Node (Minutes/Elements)"); cell = dataSheetRow.createCell(13); cell.setCellValue("Elements with Error"); cell = dataSheetRow.createCell(14); cell.setCellValue("Error percentage"); cell = dataSheetRow.createCell(15); cell.setCellValue("Total Images"); } private void writeDataSheet(Nodes node, Row dataSheetRow) { Cell cell = dataSheetRow.createCell(0); cell.setCellValue(node.getNodeId()); cell = dataSheetRow.createCell(1); cell.setCellValue(node.getCluster()); cell = dataSheetRow.createCell(2); cell.setCellValue(node.getEarliestSWT()); cell = dataSheetRow.createCell(3); cell.setCellValue(node.getEarliestSPT()); cell = dataSheetRow.createCell(4); cell.setCellValue(node.getLatestEPT()); cell = dataSheetRow.createCell(5); cell.setCellValue(node.getAveWaitingDuration()); cell = dataSheetRow.createCell(6); cell.setCellValue(node.getAveProcessDuration()); cell = dataSheetRow.createCell(7); cell.setCellValue(node.getTotalProcessDuration()); cell = dataSheetRow.createCell(8); cell.setCellValue(node.getNoOfElements()); cell = dataSheetRow.createCell(9); cell.setCellValue(node.getNoOfWaitingElements()); cell = dataSheetRow.createCell(10); cell.setCellValue(node.getNoOfElementsProcessed()); cell = dataSheetRow.createCell(11); cell.setCellValue(node.getAveNumberOfElementsHour()); cell = dataSheetRow.createCell(12); cell.setCellValue(node.getSpeedPerNode()); cell = dataSheetRow.createCell(13); cell.setCellValue(node.getElementWithError()); cell = dataSheetRow.createCell(14); cell.setCellValue(node.getErrorPercentage()); cell = dataSheetRow.createCell(15); cell.setCellValue(node.getExternalOutputCount()); } private void writeElementSheetHeaders(Row elementSheetRow) { Cell cell = elementSheetRow.createCell(0); cell.setCellValue("Node ID"); cell = elementSheetRow.createCell(1); cell.setCellValue("Element ID"); cell = elementSheetRow.createCell(2); cell.setCellValue("Start Processing Time"); cell = elementSheetRow.createCell(3); cell.setCellValue("End Processing Time"); cell = elementSheetRow.createCell(4); cell.setCellValue("Total Processing Time (Minutes)"); cell = elementSheetRow.createCell(5); cell.setCellValue("Status"); } private void writeElementSheet(Elements e, Row elementSheetRow) { Cell cell = elementSheetRow.createCell(0); cell.setCellValue(e.getNodeId()); cell = elementSheetRow.createCell(1); cell.setCellValue(e.getElementId()); cell = elementSheetRow.createCell(2); cell.setCellValue(e.getConvertedStartProcTime()); cell = elementSheetRow.createCell(3); cell.setCellValue(e.getConvertedEndProcTime()); cell = elementSheetRow.createCell(4); cell.setCellValue(e.getTotalProcTime()); cell = elementSheetRow.createCell(5); cell.setCellValue(e.getStatus()); } @Override public void run() { for (String s : fileHolder) { retrieveData(s); } writeLogFile(setElementsToNodes()); } }