473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

need help adding to a dynamic counter

112 100+
I am reading in a file to see what delays I am getting on what IP address:

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. QuoteBlockTiming exceeded 1000 ms: 1104 --- Fri Nov 06 08:30:22 CST 2009 170.137.94.11 Class key = 245
  10. QuoteBlockTiming exceeded 1000 ms: 1274 --- Fri Nov 06 08:30:22 CST 2009 170.137.94.4 Class key = 2452
  11. QuoteBlockTiming exceeded 1000 ms: 1207 --- Fri Nov 06 08:30:25 CST 2009 170.137.94.95 Class key = 692
  12. QuoteBlockTiming exceeded 1000 ms: 2919 --- Fri Nov 06 08:30:26 CST 2009 170.137.94.1 Class key = 1070
  13. QuoteBlockTiming exceeded 1000 ms: 1153 --- Fri Nov 06 08:30:31 CST 2009 170.137.94.4 Class key = 6921
  14. QuoteBlockTiming exceeded 1000 ms: 2622 --- Fri Nov 06 08:30:32 CST 2009 170.137.94.3 Class key = 4430
  15. QuoteBlockTiming exceeded 1000 ms: 2249 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
  16. QuoteBlockTiming exceeded 1000 ms: 2250 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
  17. QuoteBlockTiming exceeded 1000 ms: 1940 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
  18. QuoteBlockTiming exceeded 1000 ms: 1938 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
  19. QuoteBlockTiming exceeded 1000 ms: 1889 --- Fri Nov 06 08:30:33 CST 2009 170.137.94.1 Class key = 4430
  20. QuoteBlockTiming exceeded 1000 ms: 1153 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.3 Class key = 4430
  21. QuoteBlockTiming exceeded 1000 ms: 1156 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
  22. QuoteBlockTiming exceeded 1000 ms: 1155 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
  23. QuoteBlockTiming exceeded 1000 ms: 1151 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
  24. QuoteBlockTiming exceeded 1000 ms: 1152 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
  25. QuoteBlockTiming exceeded 1000 ms: 1148 --- Fri Nov 06 08:30:35 CST 2009 170.137.94.1 Class key = 4430
  26. QuoteBlockTiming exceeded 1000 ms: 1291 --- Fri Nov 06 08:30:38 CST 2009 170.137.94.95 Class key = 123
  27. QuoteBlockTiming exceeded 1000 ms: 1412 --- Fri Nov 06 08:31:13 CST 2009 170.137.94.11 Class key = 692
  28. QuoteBlockTiming exceeded 1000 ms: 1354 --- Fri Nov 06 08:32:11 CST 2009 170.137.94.4 Class key = 6921
  29. QuoteBlockTiming exceeded 1000 ms: 1041 --- Fri Nov 06 08:41:52 CST 2009 170.137.94.1 Class key = 6921
  30. QuoteBlockTiming exceeded 1000 ms: 1011 --- Fri Nov 06 08:56:09 CST 2009 170.137.94.1 Class key = 6921
  31.  
  32.  
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.

Expand|Select|Wrap|Line Numbers
  1. Outlier Hosts    1+ sec    2+ sec    3+ sec    5+ sec    30+ sec
  2. ( 170.137.94.11 )                    
  3. ( 170.137.94.3 )                    
  4. ( 170.137.94.4 )                    
  5. ( 170.137.94.95 )                    
  6. ( 170.137.94.96 )                    
  7. ( 170.137.94.1 )                    
  8. ( 170.137.94.2 )                    
  9. ( 170.137.88.2 )                    
  10. TOTAL                    
  11.  
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.

Expand|Select|Wrap|Line Numbers
  1.             BufferedReader cas_fixReader;
  2.             int duration = 0;
  3.             int sheet6Row = 1;
  4.             int arrayCount = 0;
  5.             int durationCount = 0;
  6.  
  7.             String [] casArray = new String[200];
  8.             outlierMaker cas_fixMaker = new outlierMaker();
  9.             outlierX cas_fix = new outlierX();
  10.             outlierMonitor=new File(outlierLocation);
  11.             cas_fixReader = new BufferedReader(new FileReader(outlierMonitor));
  12.             String cas_fixLine;
  13. //This is checking to see that the line is not null
  14.          while ((cas_fixLine = cas_fixReader.readLine()) != null) {
  15.                 if (cas_fixLine.startsWith("QuoteBlockTiming")) {
  16. //the make method is in another class that stores all the info like IP address, //time, duration, etc.  
  17.                         cas_fix = cas_fixMaker.make(cas_fixLine);
  18. //the getFE() method is in a different class and this is where I store the IP //address in the one dimension array
  19.                         casArray[arrayCount] = cas_fix.getFE();
  20.  
  21. //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.
  22.                         duration = cas_fix.getM_duration();
  23.  
  24.                         arrayCount++;
  25.                         }
  26.                 }
  27.             for( ;arrayCount < 200; arrayCount++)
  28.                 casArray[arrayCount] = "TOTAL";
  29.  
  30. //Sorting the array and getting rid of duplicates
  31. //I want the IP address displayed only once in the excel file but            //counting the number different duration rangers occur
  32.             Set set = new HashSet(Arrays.asList(casArray));
  33.             String [] array2 = (String[])(set.toArray(new String[set.size()]));
  34.             Arrays.sort(array2, String.CASE_INSENSITIVE_ORDER);
  35.  
  36.             for(String IPCasArray : array2){
  37.                 sheet6.addCell(new Label(0, sheet6Row, IPCasArray));  
  38. }
  39.  
Thanks for the help!!
Nov 6 '09 #1

✓ answered 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:

Expand|Select|Wrap|Line Numbers
  1. public class LatencyCounter {
  2.  
  3.     private static final int[] BIN_VALUES = {1000, 2000, 3000, 5000, 30000};
  4.  
  5.         // the counts are the number of times values were
  6.         // added that exceeded the bin value and were less
  7.         // than or equal to the next
  8.     private int[] counts;
  9.  
  10.     public LatencyCounter() {
  11.         counts = new int[BIN_VALUES.length];
  12.     }
  13.  
  14.         /** Records the fact that a given value was observed. */
  15.     public void add(int value) {
  16.         for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
  17.             if(value > BIN_VALUES[ndx]) {
  18.                 counts[ndx]++;
  19.                 return;
  20.             }
  21.         }
  22.     }
  23.  
  24.         /**
  25.          * Returns the number of times that observed values exceeded a given value.
  26.          * TODO this could return the number of observations recorded in the
  27.          * bin containing value.
  28.          */
  29.     public int getExceedCount(int value) {
  30.         int ret = 0;
  31.         for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
  32.             if(BIN_VALUES[ndx] >= value) {
  33.                 ret += counts[ndx];
  34.             } else {
  35.                 break;
  36.             }
  37.         }
  38.         return ret;
  39.     }
  40. }
  41.  
  42. import java.util.HashMap;
  43. import java.util.Map;
  44. import java.util.Set;
  45.  
  46. public class LatencyCounterMap {
  47.  
  48.     private Map<String,LatencyCounter> map = new HashMap<String,LatencyCounter>();
  49.  
  50.         /**
  51.          * Records the fact that a given ip experienced a latency of a given value.
  52.          */
  53.     public void add(String ip, int value) {
  54.         LatencyCounter counter = map.get(ip);
  55.         if(counter == null) {
  56.             counter = new LatencyCounter();
  57.             map.put(ip, counter);
  58.         }
  59.         counter.add(value);
  60.     }
  61.  
  62.         /**
  63.          * Returns the number of times that observed values exceeded a given value
  64.          * for a given ip address.
  65.          */
  66.     public int getExceedCount(String ip, int value) {
  67.         LatencyCounter counter = map.get(ip);
  68.         if(counter == null) {
  69.             return 0;
  70.         }
  71.         return counter.getExceedCount(value);
  72.     }
  73.  
  74.     public Set<String> getAddressSet() {
  75.         return map.keySet();
  76.     }
  77.  
  78. }
  79.  
  80.  
  81. public class LatencyTest {
  82.     public static void main(String[] args) {
  83.             // a data structure that remembers values from
  84.             // lines in the data file
  85.         LatencyCounterMap map = new LatencyCounterMap();
  86.  
  87.             // simulate reading the file
  88.         map.add("170.137.94.95", 1684);
  89.         map.add("170.137.94.2", 1640);
  90.         map.add("170.137.94.2", 1146);
  91.         map.add("170.137.94.96", 1528);
  92.         map.add("170.137.94.1", 1660);
  93.         map.add("170.137.94.95", 3275);
  94.         map.add("170.137.94.11", 2769);
  95.         map.add("170.137.94.11", 1104);
  96.  
  97.             // produce some output
  98.         for(String ip :map.getAddressSet()) {
  99.             System.out.printf(
  100.                     "%-17s%4d%4d%4d%4d%4d%n",
  101.                     ip, map.getExceedCount(ip, 1000), map.getExceedCount(ip, 2000),
  102.                     map.getExceedCount(ip, 3000), map.getExceedCount(ip, 5000),
  103.                     map.getExceedCount(ip, 30000));
  104.         }
  105.     }
  106. }
  107.  
Which gives the following output:

Expand|Select|Wrap|Line Numbers
  1. 170.137.94.2        2   0   0   0   0
  2. 170.137.94.1        1   0   0   0   0
  3. 170.137.94.11       2   1   0   0   0
  4. 170.137.94.96       1   0   0   0   0
  5. 170.137.94.95       2   1   1   0   0
  6.  

4 2233
ndedhia1
112 100+
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
Nov 6 '09 #2
pbrockway2
151 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. public class LatencyCounter {
  2.  
  3.     private static final int[] BIN_VALUES = {1000, 2000, 3000, 5000, 30000};
  4.  
  5.         // the counts are the number of times values were
  6.         // added that exceeded the bin value and were less
  7.         // than or equal to the next
  8.     private int[] counts;
  9.  
  10.     public LatencyCounter() {
  11.         counts = new int[BIN_VALUES.length];
  12.     }
  13.  
  14.         /** Records the fact that a given value was observed. */
  15.     public void add(int value) {
  16.         for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
  17.             if(value > BIN_VALUES[ndx]) {
  18.                 counts[ndx]++;
  19.                 return;
  20.             }
  21.         }
  22.     }
  23.  
  24.         /**
  25.          * Returns the number of times that observed values exceeded a given value.
  26.          * TODO this could return the number of observations recorded in the
  27.          * bin containing value.
  28.          */
  29.     public int getExceedCount(int value) {
  30.         int ret = 0;
  31.         for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
  32.             if(BIN_VALUES[ndx] >= value) {
  33.                 ret += counts[ndx];
  34.             } else {
  35.                 break;
  36.             }
  37.         }
  38.         return ret;
  39.     }
  40. }
  41.  
  42. import java.util.HashMap;
  43. import java.util.Map;
  44. import java.util.Set;
  45.  
  46. public class LatencyCounterMap {
  47.  
  48.     private Map<String,LatencyCounter> map = new HashMap<String,LatencyCounter>();
  49.  
  50.         /**
  51.          * Records the fact that a given ip experienced a latency of a given value.
  52.          */
  53.     public void add(String ip, int value) {
  54.         LatencyCounter counter = map.get(ip);
  55.         if(counter == null) {
  56.             counter = new LatencyCounter();
  57.             map.put(ip, counter);
  58.         }
  59.         counter.add(value);
  60.     }
  61.  
  62.         /**
  63.          * Returns the number of times that observed values exceeded a given value
  64.          * for a given ip address.
  65.          */
  66.     public int getExceedCount(String ip, int value) {
  67.         LatencyCounter counter = map.get(ip);
  68.         if(counter == null) {
  69.             return 0;
  70.         }
  71.         return counter.getExceedCount(value);
  72.     }
  73.  
  74.     public Set<String> getAddressSet() {
  75.         return map.keySet();
  76.     }
  77.  
  78. }
  79.  
  80.  
  81. public class LatencyTest {
  82.     public static void main(String[] args) {
  83.             // a data structure that remembers values from
  84.             // lines in the data file
  85.         LatencyCounterMap map = new LatencyCounterMap();
  86.  
  87.             // simulate reading the file
  88.         map.add("170.137.94.95", 1684);
  89.         map.add("170.137.94.2", 1640);
  90.         map.add("170.137.94.2", 1146);
  91.         map.add("170.137.94.96", 1528);
  92.         map.add("170.137.94.1", 1660);
  93.         map.add("170.137.94.95", 3275);
  94.         map.add("170.137.94.11", 2769);
  95.         map.add("170.137.94.11", 1104);
  96.  
  97.             // produce some output
  98.         for(String ip :map.getAddressSet()) {
  99.             System.out.printf(
  100.                     "%-17s%4d%4d%4d%4d%4d%n",
  101.                     ip, map.getExceedCount(ip, 1000), map.getExceedCount(ip, 2000),
  102.                     map.getExceedCount(ip, 3000), map.getExceedCount(ip, 5000),
  103.                     map.getExceedCount(ip, 30000));
  104.         }
  105.     }
  106. }
  107.  
Which gives the following output:

Expand|Select|Wrap|Line Numbers
  1. 170.137.94.2        2   0   0   0   0
  2. 170.137.94.1        1   0   0   0   0
  3. 170.137.94.11       2   1   0   0   0
  4. 170.137.94.96       1   0   0   0   0
  5. 170.137.94.95       2   1   1   0   0
  6.  
Nov 7 '09 #3
ndedhia1
112 100+
THANKS FOR THE HELP!!!
i really appreciate it..
Nov 9 '09 #4
pbrockway2
151 Expert 100+
You're welcome.

(The message you have entered is too short. Please lengthen your message to at least 20 characters.)
Nov 10 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Daniel Keller | last post by:
Hello! I'm trying to set up a page system using "dynamic" SSI. That means that I normally use the following on my website: <!--#include virtual="file.inc" --> Now I want to make this...
2
by: the friendly display name | last post by:
Hello newsgroup.. Following problem: I have two buttons on a page, If I click on button1, an additional textbox should be dynamical created and added to a panel. If I click on button2, one...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
6
by: | last post by:
Why is my progress bar displayed only after it's reached 100%? <html> <style type="text/css"> #bar{ width: 10px; height: 14px; color: white; font-size: 12px; overflow: hidden;
12
by: googlinggoogler | last post by:
Hi, Im new to C++ and trying to self teach myself whilst I sit at my unentertaining day job (thought i'd put that across before im accused of cheating on my homework, im 45...) Anyway I'm...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
3
blackstormdragon
by: blackstormdragon | last post by:
It seems pointers and dynamic arrays are giving me a hard time. Heres part of the assignment. We have to create a class named Student that has three member variables. One of the variables is called...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.