Michael Shipper > Data-Range-Compare-1.030 > Data::Range::Compare::Cookbook::Recipe_DateTime

Download:
Data-Range-Compare-1.030.tar.gz

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Source  

NAME ^

Data::Range::Compare::Cookbook::Recipe_DateTime - DateTime Range HOWTO

SYNOPSIS ^

  use DateTime;
  use Data::Range::Compare;

 my @vpn_a=
 (
   # start
   DateTime->new(qw(year 2010 month 01 day 02 hour 10 minute 01 second 59))
   ,
   # end
   DateTime->new(qw(year 2010 month 01 day 02 hour 10 minute 05 second 47))
   ,

   # start
   DateTime->new(qw(year 2010 month 05 day 02 hour 07 minute 41 second 32))
   ,
   # end
   DateTime->new(qw(year 2010 month 05 day 02 hour 08 minute 00 second 16))
  );

  my @vpn_b= ( 
   # start
   DateTime->new(qw(year 2010 month 05 day 02 hour 07 minute 41 second 32))
   ,
   # end
   DateTime->new(qw(year 2010 month 05 day 02 hour 07 minute 58 second 13))
   ,

   # start
   DateTime->new(qw(year 2010 month 01 day 02 hour 10 minute 03 second 59))
   ,
   # end
   DateTime->new(qw(year 2010 month 01 day 02 hour 10 minute 04 second 37))
  );

  my @vpn_c=
  (
    DateTime->new(qw(year 2010 month 01 day 02 hour 10 minute 03 second 59))
    ,
    DateTime->new(qw(year 2010 month 01 day 02 hour 10 minute  04 second 37))

    ,
    DateTime->new(qw(year 2010 month 05 day 02 hour 07 minute 41 second 32))
    ,
    DateTime->new(qw(year 2010 month 05 day 02 hour 07 minute 58 second 13))

    ,
    DateTime->new(qw(year 2010 month 05 day 02 hour 07 minute 59 second 07))
    ,
    DateTime->new(qw(year 2010 month 05 day 02 hour 08 minute 00 second 16))

    ,
    DateTime->new(qw(year 2010 month 06 day 18 hour 10 minute 58 second 21))
    ,
    DateTime->new(qw(year 2010 month 06 day 18 hour 22 minute 06 second 55))
  );

  my %helper;

  # create a simple function to handle comparing dates
  sub cmp_values { DateTime->compare( $_[0],$_[1] ) }

  # Now set cmp_values in %helper
  $helper{cmp_values}=\&cmp_values;

  # create a simple function to calculate the next second
  sub add_one { $_[0]->clone->add(seconds=>1) }

  # Now set add_one in %helper
  $helper{add_one}=\&add_one;

  # create a simple function to calculate the previous second
  sub sub_one { $_[0]->clone->subtract(seconds=>1) }

  # quick and dirty formatting tool
  sub format_range ($) {
    my $s=$_[0];
    join ' - '
      ,$s->range_start->strftime('%F %T')
      ,$s->range_end->strftime('%F %T')
  }

  # load the outage timestamps and output a little info
  my @parsed;
  my @vpn_name=qw(vpn_a vpn_b vpn_c);
  foreach my $outages (\@vpn_a,\@vpn_b,\@vpn_c) {
    my $id=shift @vpn_name;
    my $row=[];
    push @parsed,$row;
    print "\nVPN_ID $id\n";
    while(my ($dt_start,$dt_end)=splice(@$outages,0,2)) {
      my $range=Data::Range::Compare->new(\%helper,$dt_start,$dt_end);
      print format_range($range),"\n";
      push @$row,$range;
    }
  }

  # now compare our outages
  my $sub=Data::Range::Compare->range_compare(\%helper,\@parsed);

  while(my ($vpn_a,$vpn_b,$vpn_c)=$sub->()) {
    next unless
      !$vpn_a->missing
       &&
      !$vpn_b->missing
       &&
      !$vpn_c->missing;
    my $common=Data::Range::Compare->get_common_range(
      \%helper
      ,[$vpn_a,$vpn_c,$vpn_b]
    );

    my $common=Data::Range::Compare->get_common_range(
      \%helper
      ,[$vpn_a,$vpn_c,$vpn_b]
    );

    print "\nCommon outage range: "
      ,format_range($common)
      ,"\n"
      ,"Total Downtime: Months: $outage->{months}"
      ," Days: $outage->{days} Minutes: $outage->{minutes}"
      ," Seconds: $outage->{seconds}\n"
      ,'vpn_a '
      ,format_range($vpn_a)
      ,' '
      ,($vpn_a->missing ? 'up' : 'down')
      ,"\n"
      ,'vpn_b '
      ,format_range($vpn_b)
      ,' '
      ,($vpn_b->missing ? 'up' : 'down')
      ,"\n"

      ,'vpn_c '
      ,format_range($vpn_c)
      ,' '
      ,($vpn_c->missing ? 'up' : 'down')
      ,"\n";
  }

Output

  VPN_ID vpn_a
  2010-01-02 10:01:59 - 2010-01-02 10:05:47
  2010-05-02 07:41:32 - 2010-05-02 08:00:16

  VPN_ID vpn_b
  2010-05-02 07:41:32 - 2010-05-02 07:58:13
  2010-01-02 10:03:59 - 2010-01-02 10:04:37

  VPN_ID vpn_c
  2010-01-02 10:03:59 - 2010-01-02 10:04:37
  2010-05-02 07:41:32 - 2010-05-02 07:58:13
  2010-05-02 07:59:07 - 2010-05-02 08:00:16
  2010-06-18 10:58:21 - 2010-06-18 22:06:55

 Common outage range: 2010-01-02 10:03:59 - 2010-01-02 10:04:37
 Total Downtime: Months: 0 Days: 0 Minutes: 0 Seconds: 38
 vpn_a 2010-01-02 10:01:59 - 2010-01-02 10:05:47 down
 vpn_b 2010-01-02 10:03:59 - 2010-01-02 10:04:37 down
 vpn_c 2010-01-02 10:03:59 - 2010-01-02 10:04:37 down

 Common outage range: 2010-05-02 07:41:32 - 2010-05-02 07:58:13
 Total Downtime: Months: 0 Days: 0 Minutes: 16 Seconds: 41
 vpn_a 2010-05-02 07:41:32 - 2010-05-02 08:00:16 down
 vpn_b 2010-05-02 07:41:32 - 2010-05-02 07:58:13 down
 vpn_c 2010-05-02 07:41:32 - 2010-05-02 07:58:13 down

DESCRIPTION ^

This Recipe provides an example based on DateTime object manipulation.

Lets say we have a bank with 3 vpns to its data center. When all of the vpns are off line the bank itself is unable to process customer transactions. This example shows how to calculate the exact time the bank is completely disconnected from the data center and when it comes back online.

Ingredients ^

This example relies on DateTime.

Creating %helper

Data::Range::Compare relies on %helper to do its internal work. This section explains how to populate %helper to work with DateTime.

Create a hash %helper.

Example

  my %helper

Creating our Objects to Compare

The constructor call to "new Data::Range::Compare(\%helper,$start,$end)" represents the creation of a new instance. Although this may be an over generalized concept, what $start and $end can contain is not.

In this example we are creating our Date::Range::Compare Objects with a $start and $end value made from 2 DateTime objects. Prior examples used letters of the alphabet, and the default values are integers, but in this case we are doing something special; we are creating an object that represents a range based on other objects: DateTime in particular.

Lets examine the loop that pulls the vpn arrays apart.

Calculating our outages

Now we really get down to the details of the outage. So lets examine this code a little further.

AUTHOR ^

Michael Shipper

COPYRIGHT ^

Copyright 2010 Michael Shipper. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO ^

Data::Range::Compare::Cookbook

syntax highlighting: