📄 availcalculations.java
字号:
* Castors sections * @param label * Section name in the xml * @param descr * Section descr. */ private void lastNMonthsAvailability(int nMonths, long endTime, CatSections catSections, String label, String descr) { ThreadCategory.setPrefix(LOG4J_CATEGORY); org.apache.log4j.Category log = ThreadCategory.getInstance(this.getClass()); log.debug("Inside lastNMonthsAvailability"); Rows rows = new Rows(); int numMonths = 0; int numDays = getDaysForMonth(endTime); Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date(endTime)); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(year, month, numDays, 23, 59, 59); endTime = calendar.getTime().getTime(); SimpleDateFormat fmt = new SimpleDateFormat("MMM, yyyy"); String periodEnd = fmt.format(new java.util.Date(endTime)); TreeMap treeMap = new TreeMap(); // Holds all the month/percent // values to be displayed in order // on pdf. String periodFrom = null; while (numMonths++ < nMonths) { int serviceCount = 0; long outage = 0; log.debug("Number of days " + numDays + " in month of " + new Date(endTime)); long rollingWindow = numDays * ROLLING_WINDOW * 1l; // // get the outage and service count. // ListIterator listIter = m_nodes.listIterator(); while (listIter.hasNext()) { Node node = (Node) listIter.next(); serviceCount += node.getServiceCount(); outage += node.getOutage(endTime, rollingWindow); } double percentAvail; if (serviceCount > 0) percentAvail = 100.0 * (1 - (outage * 1.0) / (1.0 * serviceCount * rollingWindow)); else percentAvail = 100.0; treeMap.put(new java.util.Date(endTime), formatNumber(percentAvail + "")); periodFrom = fmt.format(new java.util.Date(endTime)); calendar = new GregorianCalendar(); calendar.setTime(new Date(endTime)); month = calendar.get(Calendar.MONTH); year = calendar.get(Calendar.YEAR); day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(year, month - 1, 1, 0, 0, 0); endTime = calendar.getTime().getTime(); month = calendar.get(Calendar.MONTH); year = calendar.get(Calendar.YEAR); day = calendar.get(Calendar.DAY_OF_MONTH); numDays = getDaysForMonth(endTime); calendar.set(year, month, numDays, 23, 59, 59); endTime = calendar.getTime().getTime(); } Set keyDates = treeMap.keySet(); Iterator iter = keyDates.iterator(); while (iter.hasNext()) { Date key = (Date) iter.next(); Value dateValue = new Value(); SimpleDateFormat fmtmp = new SimpleDateFormat("MMM"); dateValue.setContent(fmtmp.format(key) + ""); dateValue.setType("title"); Value value = new Value(); String percent = (String) treeMap.get(key); value.setContent(percent); value.setType("data"); Row row = new Row(); row.addValue(dateValue); row.addValue(value); rows.addRow(row); } Col col = new Col(); col.addColTitle(0, "Date"); col.addColTitle(1, "Percentage Availability"); ClassicTable table = new ClassicTable(); table.setCol(col); table.setRows(rows); Section section = new Section(); section.setClassicTable(table); section.setSectionName("last12MoAvail"); section.setSectionTitle(label); section.setSectionDescr(descr); section.setPeriod(periodFrom + " to " + periodEnd); section.setSectionIndex(m_sectionIndex); m_sectionIndex++; catSections.addSection(section); log.debug("Leaving lastNMonthsAvailability"); } /** * Returns the number of days in the month, also considers checks for leap * year. * * @param isLeap * the leap year flag. * @param month * The month whose days count is reqd */ private static synchronized int getDays(boolean isLeap, int month) { switch (month) { case 0: case 2: case 4: case 6: case 7: case 9: case 11: return 31; case 3: case 5: case 8: case 10: return 30; case 1: if (isLeap) return 29; else return 28; } return -1; } /** * Returns the number of Days in the month * * @param endTime * The end of the month (time in milliseconds) */ private int getDaysForMonth(long endTime) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(new java.util.Date(endTime)); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); int days = getDays(calendar.isLeapYear(year), month); return (getDays(calendar.isLeapYear(year), month)); } /** * Compute N days daily service availability. * * @param endTime * End time * @param catSections * Castors sections * @param label * Section name in the xml * @param descr * Section descr. */ private void lastNDaysDailyServiceAvailability(int days, long endTime, CatSections catSections, String label, String descr) { ThreadCategory.setPrefix(LOG4J_CATEGORY); org.apache.log4j.Category log = ThreadCategory.getInstance(this.getClass()); log.debug("Inside lastNDaysDailyServiceAvailability " + days); String serviceName; Iterator iterator; long outage; String periodFrom = ""; SimpleDateFormat fmtmp = new SimpleDateFormat("dd MMM, yyyy"); String periodTo = ""; periodTo = fmtmp.format(new java.util.Date(endTime)); Iterator monitoredIter = m_monitoredServices.iterator(); while (monitoredIter.hasNext()) { TreeMap treeMap = new TreeMap(); Rows rows = new Rows(); String service = (String) monitoredIter.next(); log.debug("SERvice " + service); TreeMap m_lastNOfftreeMap = new TreeMap(); long curTime = endTime; Map svcOutages = null; if (m_services != null) svcOutages = (Map) m_services.get(service); if (svcOutages == null || svcOutages.size() <= 0) { int daysCnt = 0; while (daysCnt++ < days) { log.debug("DAy 100 % : " + daysCnt); periodFrom = fmtmp.format(new java.util.Date(curTime)); treeMap.put(new java.util.Date(curTime), new Double(100.0)); curTime -= ROLLING_WINDOW; } Set keys = treeMap.keySet(); Iterator iter = keys.iterator(); while (iter.hasNext()) { Date tmp = (Date) iter.next(); Value dateValue = new Value(); SimpleDateFormat fmt = new SimpleDateFormat("dd"); dateValue.setContent(fmt.format(tmp) + ""); dateValue.setType("title"); Value value = new Value(); value.setContent("100.0"); value.setType("data"); Row row = new Row(); row.addValue(dateValue); row.addValue(value); rows.addRow(row); } Col col = new Col(); col.addColTitle(0, "Date"); col.addColTitle(1, "Percentage Availability"); ClassicTable table = new ClassicTable(); table.setCol(col); table.setRows(rows); Section section = new Section(); section.setClassicTable(table); section.setPeriod(periodFrom + " to " + periodTo); section.setSectionName(label + service); section.setSectionTitle(label + service); section.setSectionDescr(descr + service); section.setSectionIndex(m_sectionIndex); m_sectionIndex++; catSections.addSection(section); } else { int daysCnt = 0; while (daysCnt++ < days) { log.debug("DAy : " + daysCnt + " end time " + new Date(curTime) + " " + " ROLLING_WINDOW " + ROLLING_WINDOW); int serviceCnt = 0; long outageTime = 0; // For each node in the service table. // // Iterate each svc node for getting the ifservice Set keys = svcOutages.keySet(); Iterator iter = keys.iterator(); while (iter.hasNext()) { IfService ifservice = (IfService) iter.next(); log.debug(ifservice); OutageSvcTimesList outageList = (OutageSvcTimesList) svcOutages.get(ifservice); if (outageList != null) { outage = outageList.getDownTime(curTime, ROLLING_WINDOW); // Keep track of the number of services being // monitored. // outageTime += outage; } serviceCnt++; } log.debug("Outage Time " + outageTime); long den = (ROLLING_WINDOW * serviceCnt); double outag = 1.0 * outageTime; double denom = 1.0 * den; double cal = 0; if (den > 0) cal = 100.0 * (1 - (outag / denom)); treeMap.put(new java.util.Date(curTime), new Double(cal)); periodFrom = fmtmp.format(new java.util.Date(curTime)); log.debug("Added to svc list " + new java.util.Date(curTime)); curTime -= ROLLING_WINDOW; } Set keys = treeMap.keySet(); Iterator iter = keys.iterator(); while (iter.hasNext()) { Date tmp = (Date) iter.next(); Value dateValue = new Value(); SimpleDateFormat fmt = new SimpleDateFormat("dd"); dateValue.setContent(fmt.format(tmp) + ""); dateValue.setType("title"); Double val = (Double) treeMap.get(tmp); Value value = new Value(); value.setContent("" + val); value.setType("data"); Row row = new Row(); row.addValue(dateValue); row.addValue(value); rows.addRow(row); } Col col = new Col(); col.addColTitle(0, "Date"); col.addColTitle(1, "Percentage Availability"); ClassicTable table = new ClassicTable(); table.setCol(col); table.setRows(rows); Section section = new Section(); section.setClassicTable(table); section.setPeriod(periodFrom + " to " + periodTo); section.setSectionName(label + service); section.setSectionTitle(label + service); section.setSectionDescr(descr + service); section.setSectionIndex(m_sectionIndex); m_sectionIndex++; catSections.addSection(section); } } log.debug("Leaving lastNDaysDailyServiceAvailability"); } /** * Format the number (String) and return 6 digits of the number */ private String formatNumber(String num) { if (num.indexOf(".") == 0) { num = "0" + num; } if (num.indexOf(".") == -1) { num = num + ".0"; } num = num + "000000"; return (num.substring(0, num.indexOf(".") + 6)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -