001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.commons.lang3.StringUtils; 027import org.junit.Test; 028import org.kuali.common.util.pojo.Instance; 029 030public class EC2Instances { 031 032 // 15 * large = $2628.00 033 // 4 * medium = $350.40 034 // 1 * c1.medium = $105.5 035 036 // Hours per day = 24 037 // Hours per year = 24 * 365 = 8760 038 // Hours per month = 8760 / 12 = 730 039 040 // Small = $43.80 041 // Medium = $87.60 042 // Large = $175.20 043 // X-Large = $350.40 044 // c1.medium = $105.85 045 private static final Map<String, Double> COSTS = getMonthlyServerCosts(); 046 047 protected static Map<String, Double> getMonthlyServerCosts() { 048 Map<String, Double> map = new HashMap<String, Double>(); 049 map.put("m1.small", 43.8); 050 map.put("m1.medium", 87.6); 051 map.put("m1.large", 175.2); 052 map.put("m1.xlarge", 350.4); 053 map.put("c1.medium", 105.85); 054 return map; 055 } 056 057 @Test 058 public void parseTextFile() { 059 try { 060 String filename = "/tmp/ks-instances.txt"; 061 List<String> lines = LocationUtils.readLines(filename); 062 List<Instance> instances = new ArrayList<Instance>(); 063 for (int i = 0; i < lines.size(); i++) { 064 String line = lines.get(i); 065 if (line.startsWith("RESERVATION")) { 066 Instance instance = getInstance(lines, i); 067 instances.add(instance); 068 } 069 } 070 for (Instance i : instances) { 071 Map<String, String> tags = i.getTags(); 072 String name = tags.get("Name"); 073 if (name != null) { 074 i.setName(name); 075 } 076 } 077 Iterator<Instance> itr = instances.iterator(); 078 while (itr.hasNext()) { 079 Instance i = itr.next(); 080 if (i.getName().startsWith("env")) { 081 itr.remove(); 082 continue; 083 } 084 if (i.getName().equals("temp-server-for-jc")) { 085 itr.remove(); 086 continue; 087 } 088 if (i.getState().equals("stopped")) { 089 itr.remove(); 090 continue; 091 } 092 } 093 Collections.sort(instances); 094 List<Object[]> rows = new ArrayList<Object[]>(); 095 int sequence = 1; 096 double savings = 0; 097 for (Instance i : instances) { 098 savings += i.getMonthlyCost(); 099 String cost = FormatUtils.getCurrency(i.getMonthlyCost()); 100 if (i.getState().equals("stopped")) { 101 cost = "-"; 102 } 103 Object[] row = { sequence++, i.getName(), i.getId(), i.getSize(), i.getState(), cost }; 104 rows.add(row); 105 } 106 List<String> columns = Arrays.asList("#", "name", "id", "size", "state", "monthly cost"); 107 org.kuali.common.util.log.LoggerUtils.logTable("KS Instances", columns, rows); 108 System.out.println("Total: " + FormatUtils.getCurrency(savings)); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 } 113 114 protected static Instance getInstance(List<String> lines, int index) { 115 int endIndex = getEndIndex(lines, index); 116 String res = lines.get(index); 117 String ins = lines.get(index + 1); 118 Assert.isTrue(res.startsWith("RESERVATION")); 119 Assert.isTrue(ins.startsWith("INSTANCE")); 120 Map<String, String> tags = getTags(lines, index, endIndex); 121 String[] tokens = StringUtils.splitPreserveAllTokens(ins, "\t"); 122 String id = tokens[1]; 123 String state = tokens[5]; 124 String size = tokens[9]; 125 double monthlyCost = COSTS.get(size); 126 Instance instance = new Instance(); 127 instance.setId(id); 128 instance.setSize(size); 129 instance.setState(state); 130 instance.setStartIndex(index); 131 instance.setTags(tags); 132 instance.setMonthlyCost(monthlyCost); 133 return instance; 134 } 135 136 protected static Map<String, String> getTags(List<String> lines, int start, int end) { 137 Map<String, String> tags = new HashMap<String, String>(); 138 for (int i = start; i <= end; i++) { 139 String line = lines.get(i); 140 if (line.startsWith("TAG")) { 141 String[] tokens = StringUtils.splitPreserveAllTokens(line, "\t"); 142 String key = tokens[3]; 143 String val = tokens[4]; 144 tags.put(key, val); 145 } 146 } 147 return tags; 148 } 149 150 protected static int getEndIndex(List<String> lines, int index) { 151 for (int i = index + 1; i < lines.size(); i++) { 152 String line = lines.get(i); 153 if (line.startsWith("RESERVATION")) { 154 return i; 155 } 156 if (i == lines.size() - 1) { 157 return i; 158 } 159 } 160 throw new IllegalStateException("Unable to determine end index"); 161 } 162 163}