• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to nest array loops

 
Anthony Stump
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a logic question.

I had developed a "Alarm Tool" in PHP that pulls SNMP traps from MySQL. I have one table for the Alarms, (net_snmp.AlarmTable) - and a few reference tables - AlarmHost and AlarmDefinitions. AlarmHost stores all my LAN IP addresses and has metadata on those devices. AlarmDefinitions stores a "pattern match" column, and a short alarm description, severity, and action to take.

PHP was easy. Yes, I know.... all I had to do was 3 SQL queries, and then in my main while loop iterating through the Alarms, I did preg matches on the "pattern" to the "alarm text", and matched Alarm hosts to the alarm based on Source IP address.

Java is - a bit more complex.

I am following the standard model (or so as the book I learned it from has.)

I have the Alarm, AlarmHost, and AlarmDefintions models stored in WEB-INF/classes/models/SysMon/
I have the GetAlarmsAction, GetAlarmHostsAction, GetAlarmDefinitionsAction stored in WEB-INF/classes/action/SysMon/
I have the SysMonDAO stored in WEB-INF/dao/
I have the ControllerServlet stored in WEB-INF/servlet/
I have the Alarm.jsp page stored in Tomcat/

I initially tried to use JSTL tags to do the nested c:while loops inside the main c:while loop that iterates the main Alarm Table, and then use the JSTL functions to do preg matching - but I'm finding that the JSTL loop is not functioning properly. My assumption is the loop returning matches to non matches and instead just putting the same value in every other loop ran. (Possibly because there is no break option out of the JSTL while loop?) My philosophy is touch the database the least, let Java do the iterations. Either that or I would have X * X * X queries being ran - performance wise I would logically think that preg matching on already ran queries in memory would be much faster. (Correct me if I'm wrong)

First question - probably the best way to do this is in the servlet. What servlet should I do this in though? The DAO, the ControllerServlet, or create a new Servlet that does the nested loops.
Or is using JSTL the best option?

Here is the JSTL iteration that is failing me. Yes, I know this is probably not the ideal way as I mentioned above, I just don't know what servlet to put it in....



Here is the DAO where the "magic" happens:



(I use 's for HTML and "s for Java-side JSP. I do this in PHP too. This allows me not to have to escape "s in HTML if the HTML is generated with 's instead.)
 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are those timestamps really Strings in the database?

Could you construct a model that allowed you to build a single List<Alarm> where Alarm contains all the data you need to display, and get all that data in a single query?
It would save all but the outer for-each loop in the JSP.
When I see that much data crunching on the page I do ponder ways to reduce it.
 
Anthony Stump
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Don't know why I didn't think of that :)

 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!