need help adding to a dynamic counter | Member | | Join Date: Jan 2009
Posts: 64
| |
I am reading in a file to see what delays I am getting on what IP address: -
-
QuoteBlockTiming exceeded 1000 ms: 1684 --- Fri Nov 06 06:09:10 CST 2009 170.137.94.95 Class key = 649126730 block size = 1 session = W_MAIN
-
QuoteBlockTiming exceeded 1000 ms: 1640 --- Fri Nov 06 06:09:18 CST 2009 170.137.94.2 Class key = 649126749 block size = 1 session = W_MAIN
-
QuoteBlockTiming exceeded 1000 ms: 1146 --- Fri Nov 06 06:28:25 CST 2009 170.137.88.2 Class key = 649643807 block size = 1 session = Unknown Session
-
QuoteBlockTiming exceeded 1000 ms: 1528 --- Fri Nov 06 06:31:10 CST 2009 170.137.94.96 Class key = 713208957 block size = 1 session = W_MAIN
-
QuoteBlockTiming exceeded 1000 ms: 1660 --- Fri Nov 06 06:31:21 CST 2009 170.137.94.1 Class key = 713207756 block size = 1 session = W_MAIN
-
QuoteBlockTiming exceeded 1000 ms: 3275 --- Fri Nov 06 08:30:15 CST 2009 170.137.94.95 Class key = 469764136 block size = 1 session = W_MAIN
-
QuoteBlockTiming exceeded 1000 ms: 2769 --- Fri Nov 06 08:30:15 CST 2009 170.137.94.11 Class key = 469764136 block size = 31 session = W_MAIN
-
QuoteBlockTiming exceeded 1000 ms: 1104 --- Fri Nov 06 08:30:22 CST 2009 170.137.94.11 Class key = 245
-
QuoteBlockTiming exceeded 1000 ms: 1274 --- Fri Nov 06 08:30:22 CST 2009 170.137.94.4 Class key = 2452
-
QuoteBlockTiming exceeded 1000 ms: 1207 --- Fri Nov 06 08:30:25 CST 2009 170.137.94.95 Class key = 692
-
QuoteBlockTiming exceeded 1000 ms: 2919 --- Fri Nov 06 08:30:26 CST 2009 170.137.94.1 Class key = 1070
-
QuoteBlockTiming exceeded 1000 ms: 1153 --- Fri Nov 06 08:30:31 CST 2009 170.137.94.4 Class key = 6921
-
QuoteBlockTiming exceeded 1000 ms: 2622 --- Fri Nov 06 08:30:32 CST 2009 170.137.94.3 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 2249 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 2250 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1940 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1938 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1889 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1153 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.3 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1156 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1155 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1151 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1152 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1148 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
-
QuoteBlockTiming exceeded 1000 ms: 1291 --- Fri Nov 06 08:30:38 CST 2009 170.137.94.95 Class key = 123
-
QuoteBlockTiming exceeded 1000 ms: 1412 --- Fri Nov 06 08:31:13 CST 2009 170.137.94.11 Class key = 692
-
QuoteBlockTiming exceeded 1000 ms: 1354 --- Fri Nov 06 08:32:11 CST 2009 170.137.94.4 Class key = 6921
-
QuoteBlockTiming exceeded 1000 ms: 1041 --- Fri Nov 06 08:41:52 CST 2009 170.137.94.1 Class key = 6921
-
QuoteBlockTiming exceeded 1000 ms: 1011 --- Fri Nov 06 08:56:09 CST 2009 170.137.94.1 Class key = 6921
-
-
I am putting the IP address, the latency duration (the number after ms:), the date, etc into separate variables every time i read in a line. I then store the IP address in a one dimensional array and then after it reads in the next line, those variables are over written with the next duration, IP address, date, etc, but again, the IP address is put into the array into the next index. The same IP address, as shown in the above tag can come up few times with duration latencies.
The problem that I am having is that I would like to create an excel file, which I have, that in the first column, shows the IP address, and then in the next few columns, shows how many times this IP address had a latency of over 1 second, 2 seconds, 3 seconds, 5 seconds and 30 seconds. - Outlier Hosts 1+ sec 2+ sec 3+ sec 5+ sec 30+ sec
-
( 170.137.94.11 )
-
( 170.137.94.3 )
-
( 170.137.94.4 )
-
( 170.137.94.95 )
-
( 170.137.94.96 )
-
( 170.137.94.1 )
-
( 170.137.94.2 )
-
( 170.137.88.2 )
-
TOTAL
-
I am having trouble in capturing the total number for each individual IP adress. I can set a counter every time I read a line, but that variable has to be reset to 0 because the next line may be a different IP address and I do not want to reflect one IP addresses latency on a row with another IP address.
Should I use a two dimensional array and store the IP address and duration each time I read in a line and than how do I do the check to see if the IP address is the same, then I have to check the duration and increment the variable whether its greater then 1 second, greater then 2 seconds, 3, seconds, 5 seconds, 30 seconds.
here is some of the code I have now reading in the file, putting the IP address in an array and displaying the excel file. -
BufferedReader cas_fixReader;
-
int duration = 0;
-
int sheet6Row = 1;
-
int arrayCount = 0;
-
int durationCount = 0;
-
-
String [] casArray = new String[200];
-
outlierMaker cas_fixMaker = new outlierMaker();
-
outlierX cas_fix = new outlierX();
-
outlierMonitor=new File(outlierLocation);
-
cas_fixReader = new BufferedReader(new FileReader(outlierMonitor));
-
String cas_fixLine;
- //This is checking to see that the line is not null
-
while ((cas_fixLine = cas_fixReader.readLine()) != null) {
-
if (cas_fixLine.startsWith("QuoteBlockTiming")) {
- //the make method is in another class that stores all the info like IP address, //time, duration, etc.
-
cas_fix = cas_fixMaker.make(cas_fixLine);
- //the getFE() method is in a different class and this is where I store the IP //address in the one dimension array
-
casArray[arrayCount] = cas_fix.getFE();
-
- //here is where I get the duration so that I can do some checkign to see if it is //greater then 1 second only or 2 seconds only or 3 seconds only or 5 seconds //only or thirty seconds but I do not know how and where to increment the //counter for this and add that to my excel file. the getIM_duration method is in //another class.
-
duration = cas_fix.getM_duration();
-
-
arrayCount++;
-
}
-
}
-
for( ;arrayCount < 200; arrayCount++)
-
casArray[arrayCount] = "TOTAL";
-
- //Sorting the array and getting rid of duplicates
-
//I want the IP address displayed only once in the excel file but //counting the number different duration rangers occur
-
Set set = new HashSet(Arrays.asList(casArray));
-
String [] array2 = (String[])(set.toArray(new String[set.size()]));
-
Arrays.sort(array2, String.CASE_INSENSITIVE_ORDER);
-
-
for(String IPCasArray : array2){
-
sheet6.addCell(new Label(0, sheet6Row, IPCasArray));
-
}
-
Thanks for the help!!
| |
best answer - posted by pbrockway2 |
I'm not sure String[200][2] is such a good idea. Even if you decide to treat the ip address as a string, the latency is an integral value and should be modelled with an int.
Create an Entry class that includes the information you wish to extract from a line of the file. (like ip address and latency). Then as you read the file, build up a list (eg ArrayList<Entry> etc) of these entries.
Creating the output will be a matter of going through the list and counting the number of times a given latency was exceeded for a given address.
-----
Another approach would be to build up the latency frequencies as you read the file.
The basic data structure here would be a map whose key is the ip address and whose value is a counter which remembers the number of times the latency exceeded various values. Creating the output is easier in this case since all the data will be stored in the map's entry set.
Something like the following might be suggestive: -
public class LatencyCounter {
-
-
private static final int[] BIN_VALUES = {1000, 2000, 3000, 5000, 30000};
-
-
// the counts are the number of times values were
-
// added that exceeded the bin value and were less
-
// than or equal to the next
-
private int[] counts;
-
-
public LatencyCounter() {
-
counts = new int[BIN_VALUES.length];
-
}
-
-
/** Records the fact that a given value was observed. */
-
public void add(int value) {
-
for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
-
if(value > BIN_VALUES[ndx]) {
-
counts[ndx]++;
-
return;
-
}
-
}
-
}
-
-
/**
-
* Returns the number of times that observed values exceeded a given value.
-
* TODO this could return the number of observations recorded in the
-
* bin containing value.
-
*/
-
public int getExceedCount(int value) {
-
int ret = 0;
-
for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
-
if(BIN_VALUES[ndx] >= value) {
-
ret += counts[ndx];
-
} else {
-
break;
-
}
-
}
-
return ret;
-
}
-
}
-
-
import java.util.HashMap;
-
import java.util.Map;
-
import java.util.Set;
-
-
public class LatencyCounterMap {
-
-
private Map<String,LatencyCounter> map = new HashMap<String,LatencyCounter>();
-
-
/**
-
* Records the fact that a given ip experienced a latency of a given value.
-
*/
-
public void add(String ip, int value) {
-
LatencyCounter counter = map.get(ip);
-
if(counter == null) {
-
counter = new LatencyCounter();
-
map.put(ip, counter);
-
}
-
counter.add(value);
-
}
-
-
/**
-
* Returns the number of times that observed values exceeded a given value
-
* for a given ip address.
-
*/
-
public int getExceedCount(String ip, int value) {
-
LatencyCounter counter = map.get(ip);
-
if(counter == null) {
-
return 0;
-
}
-
return counter.getExceedCount(value);
-
}
-
-
public Set<String> getAddressSet() {
-
return map.keySet();
-
}
-
-
}
-
-
-
public class LatencyTest {
-
public static void main(String[] args) {
-
// a data structure that remembers values from
-
// lines in the data file
-
LatencyCounterMap map = new LatencyCounterMap();
-
-
// simulate reading the file
-
map.add("170.137.94.95", 1684);
-
map.add("170.137.94.2", 1640);
-
map.add("170.137.94.2", 1146);
-
map.add("170.137.94.96", 1528);
-
map.add("170.137.94.1", 1660);
-
map.add("170.137.94.95", 3275);
-
map.add("170.137.94.11", 2769);
-
map.add("170.137.94.11", 1104);
-
-
// produce some output
-
for(String ip :map.getAddressSet()) {
-
System.out.printf(
-
"%-17s%4d%4d%4d%4d%4d%n",
-
ip, map.getExceedCount(ip, 1000), map.getExceedCount(ip, 2000),
-
map.getExceedCount(ip, 3000), map.getExceedCount(ip, 5000),
-
map.getExceedCount(ip, 30000));
-
}
-
}
-
}
-
Which gives the following output: -
170.137.94.2 2 0 0 0 0
-
170.137.94.1 1 0 0 0 0
-
170.137.94.11 2 1 0 0 0
-
170.137.94.96 1 0 0 0 0
-
170.137.94.95 2 1 1 0 0
-
| | Member | | Join Date: Jan 2009
Posts: 64
| | | re: need help adding to a dynamic counter
Also, would this maybe help.
I create a two dimensional array. String [][] casArray = new String[200][2];
I make the first one 200 because I dont know how larger the file will get and how many IP addresses I get, and I just fill the ends with a random letter that isnt used. Storage space is not a problem.
The first Array holds the IPaddresses and the second array holds the duration.
Since its in a file its saved as a string initially so I think this works.
Then I sort the array. I then check to see if the current index for the IPaddress array equals the next index's IPaddress, and if it does, I check the next one and so on. When I find one that doesnt match, I go back to the previous ones to get the duration, check to see what ranges they fall in and increment the counter. I record this and set the counters back to 0 and then go on from where I left and do this over again till I hit the end of the array.
I dont know how and if this works and if it even sounds good. I am just stuck and trying to figure this out. If this does work, can I get an example of this codewise because I am pretty lost when it comes to 2 dimensional arrays. I am new to java so that is why i am using a list. I can use hash tables, maps anything.
Please help.
thanks
| | Newbie | | Join Date: Nov 2007
Posts: 27
| | | re: need help adding to a dynamic counter
I'm not sure String[200][2] is such a good idea. Even if you decide to treat the ip address as a string, the latency is an integral value and should be modelled with an int.
Create an Entry class that includes the information you wish to extract from a line of the file. (like ip address and latency). Then as you read the file, build up a list (eg ArrayList<Entry> etc) of these entries.
Creating the output will be a matter of going through the list and counting the number of times a given latency was exceeded for a given address.
-----
Another approach would be to build up the latency frequencies as you read the file.
The basic data structure here would be a map whose key is the ip address and whose value is a counter which remembers the number of times the latency exceeded various values. Creating the output is easier in this case since all the data will be stored in the map's entry set.
Something like the following might be suggestive: -
public class LatencyCounter {
-
-
private static final int[] BIN_VALUES = {1000, 2000, 3000, 5000, 30000};
-
-
// the counts are the number of times values were
-
// added that exceeded the bin value and were less
-
// than or equal to the next
-
private int[] counts;
-
-
public LatencyCounter() {
-
counts = new int[BIN_VALUES.length];
-
}
-
-
/** Records the fact that a given value was observed. */
-
public void add(int value) {
-
for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
-
if(value > BIN_VALUES[ndx]) {
-
counts[ndx]++;
-
return;
-
}
-
}
-
}
-
-
/**
-
* Returns the number of times that observed values exceeded a given value.
-
* TODO this could return the number of observations recorded in the
-
* bin containing value.
-
*/
-
public int getExceedCount(int value) {
-
int ret = 0;
-
for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
-
if(BIN_VALUES[ndx] >= value) {
-
ret += counts[ndx];
-
} else {
-
break;
-
}
-
}
-
return ret;
-
}
-
}
-
-
import java.util.HashMap;
-
import java.util.Map;
-
import java.util.Set;
-
-
public class LatencyCounterMap {
-
-
private Map<String,LatencyCounter> map = new HashMap<String,LatencyCounter>();
-
-
/**
-
* Records the fact that a given ip experienced a latency of a given value.
-
*/
-
public void add(String ip, int value) {
-
LatencyCounter counter = map.get(ip);
-
if(counter == null) {
-
counter = new LatencyCounter();
-
map.put(ip, counter);
-
}
-
counter.add(value);
-
}
-
-
/**
-
* Returns the number of times that observed values exceeded a given value
-
* for a given ip address.
-
*/
-
public int getExceedCount(String ip, int value) {
-
LatencyCounter counter = map.get(ip);
-
if(counter == null) {
-
return 0;
-
}
-
return counter.getExceedCount(value);
-
}
-
-
public Set<String> getAddressSet() {
-
return map.keySet();
-
}
-
-
}
-
-
-
public class LatencyTest {
-
public static void main(String[] args) {
-
// a data structure that remembers values from
-
// lines in the data file
-
LatencyCounterMap map = new LatencyCounterMap();
-
-
// simulate reading the file
-
map.add("170.137.94.95", 1684);
-
map.add("170.137.94.2", 1640);
-
map.add("170.137.94.2", 1146);
-
map.add("170.137.94.96", 1528);
-
map.add("170.137.94.1", 1660);
-
map.add("170.137.94.95", 3275);
-
map.add("170.137.94.11", 2769);
-
map.add("170.137.94.11", 1104);
-
-
// produce some output
-
for(String ip :map.getAddressSet()) {
-
System.out.printf(
-
"%-17s%4d%4d%4d%4d%4d%n",
-
ip, map.getExceedCount(ip, 1000), map.getExceedCount(ip, 2000),
-
map.getExceedCount(ip, 3000), map.getExceedCount(ip, 5000),
-
map.getExceedCount(ip, 30000));
-
}
-
}
-
}
-
Which gives the following output: -
170.137.94.2 2 0 0 0 0
-
170.137.94.1 1 0 0 0 0
-
170.137.94.11 2 1 0 0 0
-
170.137.94.96 1 0 0 0 0
-
170.137.94.95 2 1 1 0 0
-
| | Member | | Join Date: Jan 2009
Posts: 64
| | | re: need help adding to a dynamic counter
THANKS FOR THE HELP!!!
i really appreciate it..
| | Newbie | | Join Date: Nov 2007
Posts: 27
| | | re: need help adding to a dynamic counter
You're welcome.
(The message you have entered is too short. Please lengthen your message to at least 20 characters.)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|