Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Epoch based parser

by spikeinc (Acolyte)
on Aug 11, 2013 at 10:22 UTC ( #1048996=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Epoch based parser
in thread Epoch based parser

Hi Ken,

After reading through and doing some homework, I added on to your code to do pretty much what I needed and it works :) . I have finetuned a few things based on my requirements. Here is my code. I might have used a longer way to do things and maybe there are shortcuts to do it. But I would like you to check my code and let me know if what I have done is ok or any enhancements/ improvements can be done.

Even if I have done mistakes ,I am quite happy I learnt quite a lot

#!/usr/bin/env perl -l use strict; use warnings; use Time::Local; use JSON; use Time::Piece; my $poll_hour = 1; my $poll_minute = 20; my $data = decode_json join '' => map { chomp; $_ } (<DATA>); use Text::Table; my $tb = Text::Table->new( "Date", "", "Start Time", "End Time", "Time Taken" , "Egress:I +ngress" , "AADATA" ); for (@{$data->{aaData}}) { my $difftime=(@$_[1] - @$_[0]); if (is_poll_time(@$_[0,1])) { # print "@$_"; @$_[0] = scalar(localtime(@$_[0])); @$_[1] = scalar(localtime(@$_[1])); my $day = substr(@$_[0],0,10); my $stime = substr(@$_[0],11,8); my $year = substr(@$_[0],20,4); my $etime = substr(@$_[1],11,8); #my @dateend = split(' ',@$_[1])); $tb->load( [ $day, $year, $stime , $etime ,convert_time($difftime) , @$_[ +8] , @$_[9] ], ); } } print $tb; sub is_poll_time { for ($poll_hour=1; $poll_hour<=24; $poll_hour++){ for (@_) { my $t = gmtime $_; return 1 if $t->hour == $poll_hour && $t->min == $poll_minute; } } return 0; } sub convert_time { my $time = shift; my $days = int($time / 86400); $time -= ($days * 86400); my $hours = int($time / 3600); $time -= ($hours * 3600); my $minutes = int($time / 60); my $seconds = $time % 60; $days = $days < 1 ? '' : $days .'d '; $hours = $hours < 1 ? '' : $hours .'h '; $minutes = $minutes < 1 ? '' : $minutes . ' min '; $time = $days . $hours . $minutes . $seconds . ' sec'; return $time; } __DATA__ {"iTotalDisplayRecords":"90","iTotalRecords":"90","sColumns":"startTim +e,endTime,remoteNode,srcIP,srcPort,destIP,destPort,egress,ingress","a +aData":[["1375856507","137 5858307","WAN","FF:80:D1","192.168.167.37",3649,"10.219.72.3",80,"0,20 +","149,186"],["1375856506","1375858307","WAN","FF:C3:77","192.168.167 +.28",3910,"10.219.72.3",80 ,"65536,65641","149,188"],["1375856505","1375858305","WAN","FF:C9:7D", +"192.168.167.9",1944,"10.219.72.3",80,"1935584,273470","150,264"],["1 +375856501","1375858301","W AN","FF:C2:DB","192.168.167.30",4606,"10.219.72.3",80,"1608068,1118911 +","132,189"],["1375856498","1375858298","WAN","FF:C5:4E","192.168.167 +.38",2611,"10.219.72.3",80 ,"920588,455833","150,193"],["1375856495","1375858295","WAN","FF:C7:90 +","192.168.167.16",2036,"10.219.72.3",80,"1755036,470171","147,185"], +["1375856494","1375858294" ,"WAN","FF:C1:FD","192.168.167.34",2118,"10.219.72.3",80,"633624,58520 +7","177,224"],["1375856491","1375858291","WAN","FF:C4:EF","192.168.16 +7.23",3875,"10.219.72.3",8 0,"2316316,1906209","132,191"],["1375856471","1375858271","WAN","FF:C6 +:67","192.168.167.5",4794,"10.219.72.3",80,"9765028,4016161","147,103 +4"],["1375856470","1375858 271","WAN","FF:CD:09","192.168.167.11",1707,"10.219.72.3",80,"2860552, +533884","168,199"],["1375856465","1375858265","WAN","FF:C7:E1","192.1 +68.167.15",2142,"10.219.72 .3",80,"6639576,1934965","147,185"],["1375856459","1375858260","WAN"," +FF:C2:99","192.168.167.31",4478,"10.219.72.3",80,"16729692,3831326"," +146,260"],["1375856445","1 375858246","WAN","FF:C6:CC","192.168.167.22",4674,"10.219.72.3",80,"42 +30804,3165895","147,691"],["1375856437","1375858237","WAN","FF:C9:F9" +,"192.168.167.4",4956,"10. 219.72.3",80,"9396960,1990474","147,185"],["1375856433","1375858234"," +WAN","FF:C9:A7","192.168.167.17",3899,"10.219.72.3",80,"9708088,78299 +78","147,9798"],["13758564 32","1375858232","WAN","FF:C8:23","192.168.167.2",3204,"10.219.72.3",8 +0,"12722744,8198182","147,308"],["1375856432","1375858232","WAN","FF: +C3:EA","192.168.167.27",44 49,"10.219.72.3",80,"8621880,8641627","146,240"],["1375856412","137585 +8213","WAN","FF:C7:E8","192.168.167.13",1319,"10.219.72.3",80,"119101 +96,11969965","147,1460"],[ "1375856370","1375858170","WAN","FF:CA:4E","192.168.167.14",3123,"10.2 +19.72.3",80,"12963152,5276668","147,665"],["1375856504","1375857704", +"WAN","FF:F8:7C","192.168. 167.57",65011,"10.219.72.3",53517,"1642500,308139","0,16"],["137585649 +5","1375857695","WAN","FF:F8:0D","192.168.167.56",63669,"10.219.72.3" +,80,"4042956,1150040","177 ,207"],["1375856485","1375857685","WAN","FF:F8:7C","192.168.167.57",65 +005,"10.219.72.3",80,"1546748,821480","177,208"],["1375856495","13758 +57510","WAN","FF:C8:A2","1 92.168.167.19",2598,"10.219.72.3",80,"4138988,1346531","168,319"],["13 +75856488","1375857499","WAN","FF:C9:25","192.168.167.10",1159,"10.219 +.72.3",80,"4595712,3295110 ","147,730"],["1375856497","1375857481","WAN","FF:C7:65","192.168.167. +20",4464,"10.219.72.3",80,"4903764,1402164","168,267"],["1375856497", +"1375857476","WAN","FF:C5: B6","192.168.167.21",1802,"10.219.72.3",80,"4182311,1138853","150,741" +],["1375856502","1375857475","WAN","FF:F8:48","192.168.167.59",51847, +"10.219.72.3",53511,"26126 00,394743","0,16"],["1375856497","1375857474","WAN","FF:83:4D","192.16 +8.167.26",4174,"10.219.72.3",80,"2752512,763324","167,215"],["1375856 +502","1375857474","WAN","F F:F8:48","192.168.167.59",51844,"10.219.72.3",80,"566384,201703","146, +185"],["1375856474","1375857472","WAN","FF:C8:81","192.168.167.18",10 +78,"10.219.72.3",80,"33669 12,696540","168,294"],["1375856504","1375857453","WAN","FF:C3:54","192 +.168.167.29",1846,"10.219.72.3",80,"1241616,696810","177,209"],["1375 +856503","1375856508","WAN" ,"FF:CD:4A","192.168.167.7",1400,"10.219.72.3",80,"2389792,167571","13 +6,203"],["1375856490","1375856507","WAN","FF:CE:56","192.168.167.3",2 +011,"10.219.72.3",80,"4182 311,850324","150,183"],["1375856498","1375856506","WAN","FF:CA:6B","19 +2.168.167.12",1981,"10.219.72.3",80,"2699046,1124066","133,756"],["13 +75856501","1375856506","WA N","FF:C6:E3","192.168.167.6",3140,"10.219.72.3",80,"2257178,494035"," +133,174"],["1375856470","1375856505","WAN","FF:C3:77","192.168.167.28 +",3879,"10.219.72.3",80,"4 161362,4123838","177,242"],["1375856503","1375856505","WAN","FF:F8:0D" +,"192.168.167.56",63674,"10.219.72.3",53514,"4563776,798503","0,20"], +["1375856491","1375856503" ,"WAN","FF:F8:7C","192.168.167.57",65008,"10.219.72.3",53475,"4563776, +642254","0,20"],["1375856502","1375856502","WAN","FF:F8:48","192.168. +167.59",51842,"10.219.72.3 ",80,"1429,824","132,175"],["1375856497","1375856502","WAN","FF:F8:48" +,"192.168.167.59",51840,"10.219.72.3",53496,"5338624,714602","0,20"], +["1375856480","1375856501" ,"WAN","FF:C3:54","192.168.167.29",1828,"10.219.72.3",80,"6128421,3193 +443","167,198"],["1375856490","1375856501","WAN","FF:F8:48","192.168. +167.59",51831,"10.219.72.3 ",80,"2389792,212440","135,175"],["1375856488","1375856501","WAN","FF: +C9:7D","192.168.167.9",1941,"10.219.72.3",80,"4161362,674756","178,24 +0"],["1375856477","1375856 500","WAN","FF:CD:4A","192.168.167.7",1390,"10.219.72.3",80,"2699046,4 +45565","133,204"],["1375856481","1375856500","WAN","FF:C6:E3","192.16 +8.167.6",3136,"10.219.72.3 ",80,"4182311,1278644","150,185"],["1375856487","1375856499","WAN","FF +:C2:DB","192.168.167.30",4604,"10.219.72.3",80,"2834057,1749320","132 +,197"],["1375856466","1375 856498","WAN","FF:C5:4E","192.168.167.38",2577,"10.219.72.3",80,"41613 +62,1507534","178,232"],["1375856485","1375856497","WAN","FF:CA:6B","1 +92.168.167.12",1977,"10.21 9.72.3",80,"2834057,1171220","133,304"],["1375856495","1375856496","WA +N","FF:F8:48","192.168.167.59",51837,"10.219.72.3",53490,"2384896,222 +035","0,20"],["1375856408" ,"1375856496","WAN","FF:83:4D","192.168.167.26",4108,"10.219.72.3",80, +"17796913,7366167","146,331"],["1375856494","1375856496","WAN","FF:F8 +:0D","192.168.167.56",6366 7,"10.219.72.3",53486,"5338624,1082762","0,20"],["1375856484","1375856 +495","WAN","FF:C5:B6","192.168.167.21",1799,"10.219.72.3",80,"4161362 +,1316715","178,554"],["137 5856483","1375856494","WAN","FF:F8:0D","192.168.167.56",63656,"10.219. +72.3",80,"6128421,2735602","167,196"],["1375856493","1375856494","WAN +","FF:C7:90","192.168.167. 16",2034,"10.219.72.3",80,"1429,824","133,176"],["1375856434","1375856 +494","WAN","FF:C8:A2","192.168.167.19",2593,"10.219.72.3",80,"1779691 +3,5349326","147,912"],["13 75856440","1375856494","WAN","FF:C7:65","192.168.167.20",4461,"10.219. +72.3",80,"17796913,4219966","147,993"],["1375856489","1375856493","WA +N","FF:F8:0D","192.168.167 .56",63664,"10.219.72.3",53464,"2384896,226987","0,20"],["1375856472", +"1375856492","WAN","FF:C7:90","192.168.167.16",2031,"10.219.72.3",80, +"2389792,204182","136,176" ],["1375856379","1375856492","WAN","FF:C1:FD","192.168.167.34",2096,"1 +0.219.72.3",80,"6128421,4529607","167,197"],["1375856491","1375856491 +","WAN","FF:F8:48","192.16 8.167.59",51834,"10.219.72.3",53476,"0,4","0,4"],["1375856482","137585 +6491","WAN","FF:F8:48","192.168.167.59",51829,"10.219.72.3",53437,"22 +56896,427276","0,20"],["13 75856483","1375856491","WAN","FF:F8:7C","192.168.167.57",65003,"10.219 +.72.3",53441,"5338624,792304","0,20"],["1375856481","1375856490","WAN +","FF:F8:48","192.168.167. 59",51823,"10.219.72.3",80,"2699046,1067833","132,174"],["1375856484", +"1375856490","WAN","FF:C4:EF","192.168.167.23",3869,"10.219.72.3",80, +"2257178,1508892","132,198 "],["1375856489","1375856489","WAN","FF:F8:0D","192.168.167.56",63661, +"10.219.72.3",53462,"0,4","0,4"],["1375856487","1375856487","WAN","FF +:C9:25","192.168.167.10",1 157,"10.219.72.3",80,"1429,822","133,173"],["1375856467","1375856487", +"WAN","FF:CE:56","192.168.167.3",2006,"10.219.72.3",80,"4161362,96468 +8","178,209"],["1375856444 ","1375856486","WAN","FF:C9:7D","192.168.167.9",1938,"10.219.72.3",80, +"6128421,864904","168,340"],["1375856484","1375856485","WAN","FF:F8:0 +D","192.168.167.56",63658, "10.219.72.3",53443,"2256896,1039336","0,20"],["1375856468","137585648 +5","WAN","FF:C2:DB","192.168.167.30",4574,"10.219.72.3",80,"2257178,3 +18147","132,173"],["137585 6464","1375856484","WAN","FF:F8:7C","192.168.167.57",64968,"10.219.72. +3",80,"6128421,2522269","167,197"],["1375856463","1375856484","WAN"," +FF:CA:6B","192.168.167.12" ,1973,"10.219.72.3",80,"2257178,578070","133,1147"],["1375856471","137 +5856483","WAN","FF:C9:25","192.168.167.10",1154,"10.219.72.3",80,"238 +9792,287545","136,214"],[" 1375856364","1375856483","WAN","FF:F8:0D","192.168.167.56",63540,"10.2 +19.72.3",80,"17796913,10834116","146,183"],["1375856482","1375856483" +,"WAN","FF:F8:0D","192.168 .167.56",63653,"10.219.72.3",53436,"1032192,202992","0,20"],["13758564 +50","1375856483","WAN","FF:C4:EF","192.168.167.23",3859,"10.219.72.3" +,80,"4182311,2673002","149 ,249"],["1375856453","1375856483","WAN","FF:C5:B6","192.168.167.21",17 +96,"10.219.72.3",80,"6128421,1720678","168,701"],["1375856475","13758 +56482","WAN","FF:F8:7C","1 92.168.167.57",65000,"10.219.72.3",53398,"2384896,225894","0,20"],["13 +75856450","1375856482","WAN","FF:80:D1","192.168.167.37",3643,"10.219 +.72.3",80,"4161362,871817" ,"177,261"],["1375856481","1375856481","WAN","FF:F8:48","192.168.167.5 +9",51826,"10.219.72.3",53431,"1032192,166348","0,20"],["1375856480"," +1375856481","WAN","FF:F8:0 D","192.168.167.56",63650,"10.219.72.3",53430,"1628016,598969","0,20"] +,["1375856450","1375856481","WAN","FF:C6:E3","192.168.167.6",3132,"10 +.219.72.3",80,"4161362,102 6987","178,209"],["1375856480","1375856481","WAN","FF:F8:48","192.168. +167.59",51821,"10.219.72.3",53428,"1628016,300089","0,20"],["13758564 +51","1375856480","WAN","FF :F8:48","192.168.167.59",51785,"10.219.72.3",80,"2834057,889744","132, +175"],["1375856479","1375856480","WAN","FF:F8:0D","192.168.167.56",63 +647,"10.219.72.3",53427,"1 162752,96819","0,20"],["1375856479","1375856479","WAN","FF:F8:48","192 +.168.167.59",51818,"10.219.72.3",53426,"1162752,98143","0,20"],["1375 +856475","1375856479","WAN" ,"FF:F8:0D","192.168.167.56",63644,"10.219.72.3",53393,"4252672,107755 +7","0,20"],["1375856477","1375856479","WAN","FF:F8:48","192.168.167.5 +9",51815,"10.219.72.3",534 20,"4252672,618283","0,20"],["1375856401","1375856477","WAN","FF:C3:54 +","192.168.167.29",1786,"10.219.72.3",80,"17796913,3514777","146,185" +],["1375856475","137585647 7","WAN","FF:F8:48","192.168.167.59",51812,"10.219.72.3",53401,"456377 +6,834693","0,20"]]}

And the output:

Date Start Time End Time Time Taken Egress:Ingress AAD +ATA Tue Aug 6 2013 23:20:59 23:51:00 30 min 1 sec 16729692,3831326 146 +,260 Tue Aug 6 2013 23:20:45 23:50:46 30 min 1 sec 4230804,3165895 147 +,691 Tue Aug 6 2013 23:20:37 23:50:37 30 min 0 sec 9396960,1990474 147 +,185 Tue Aug 6 2013 23:20:33 23:50:34 30 min 1 sec 9708088,7829978 147 +,9798 Tue Aug 6 2013 23:20:32 23:50:32 30 min 0 sec 12722744,8198182 147 +,308 Tue Aug 6 2013 23:20:32 23:50:32 30 min 0 sec 8621880,8641627 146 +,240 Tue Aug 6 2013 23:20:12 23:50:13 30 min 1 sec 11910196,11969965 147 +,1460 Tue Aug 6 2013 23:20:08 23:21:36 1 min 28 sec 17796913,7366167 146 +,331 Tue Aug 6 2013 23:20:34 23:21:34 1 min 0 sec 17796913,5349326 147 +,912 Tue Aug 6 2013 23:20:40 23:21:34 54 sec 17796913,4219966 147 +,993 Tue Aug 6 2013 23:20:44 23:21:26 42 sec 6128421,864904 168 +,340 Tue Aug 6 2013 23:20:50 23:21:23 33 sec 4182311,2673002 149 +,249 Tue Aug 6 2013 23:20:53 23:21:23 30 sec 6128421,1720678 168 +,701 Tue Aug 6 2013 23:20:50 23:21:22 32 sec 4161362,871817 177 +,261 Tue Aug 6 2013 23:20:50 23:21:21 31 sec 4161362,1026987 178 +,209 Tue Aug 6 2013 23:20:51 23:21:20 29 sec 2834057,889744 132 +,175 Tue Aug 6 2013 23:20:01 23:21:17 1 min 16 sec 17796913,3514777 146 +,185


Comment on Re^4: Epoch based parser
Select or Download Code
Re^5: Epoch based parser
by kcott (Abbot) on Aug 12, 2013 at 06:35 UTC

    There's some issues with is_poll_time(). You're looping through all hours except one: 1 to 24 won't pick up 00:20. As it looks like you want all hours, that loop is entirely redundant; all you need is:

    sub is_poll_time { for (@_) { my $t = gmtime $_; return 1 if $t->min == $poll_minute; } return 0; }

    If you had needed that loop, it would have better written more like Perl and less like C. Consider whether you think this is clearer:

    for my $poll_hour (0 .. 24) { # Process $poll_hour here }

    Assigning to @$_[0] and @$_[1] is a poor choice: you're altering the original data. I question whether doing that is necessary here; however, as a general rule of thumb, assign to a new variable rather than altering the original data.

    Pulling out parts of date/time strings with substr and doing various arithmetic calculations to generate seconds, minutes, etc. is prone to errors and, as far as I can see, extra work you don't need to do. I repeat what I said in my initial reply:

    "Check the Time::Piece documentation to see how I've used it and for ways to format the output ..."

    Use indentation to show the logical structure of your code. This makes it easier for everyone (including yourself) to read and follow. It also makes it less prone to errors and, when errors do exist, easier to find them. You only have a small script so uneven and inconsistent identation was not a huge impediment; when you start writing more complex code it will make a big difference. See "perlstyle - Perl style guide" for hints and tips in this area.

    Please do not post vast swathes of data such as you've done here. Only post enough to illustrate whatever point you're making. If you do consider it necessary to post pages of data, use <readmore> of <spoiler> tags which are explained here: "Writeup Formatting Tips".

    -- Ken

      Thanks for the review Ken! I ve now become more perl-aware than yesday :)

      Thanks for helping me undestand. I used the Time::Piece to get the year/date/format that I wanted and now the code looks pretty neat.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048996]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-07-22 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (116 votes), past polls