Subversion Repositories havirt

Rev

Rev 4 | Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 rodolico 1
#!/usr/bin/env perl
2
 
3
# Common library for havirt. Basically, just a place to put things which may be used by any
4 rodolico 4
# part of havirt. More for organizations purposes.
3 rodolico 5
 
4 rodolico 6
# Copyright 2024 Daily Data, Inc.
7
# 
8
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
9
# conditions are met:
10
#
11
#   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12
#   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
13
#   in the documentation and/or other materials provided with the distribution.
14
#   Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
15
#   from this software without specific prior written permission.
16
# 
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
18
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 
24
 
3 rodolico 25
# v0.0.1 20240602 RWR
26
# Initial setup
27
 
28
package havirt;
29
 
30
 
31
use warnings;
32
use strict;  
33
 
34
use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
35
 
4 rodolico 36
# define the version number
37
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
38
use version;
39
our $VERSION = version->declare("0.0.1");
40
 
41
 
3 rodolico 42
use Exporter;
43
 
44
our @ISA = qw( Exporter );
45
our @EXPORT = qw( 
46
                  &readDB &writeDB
4 rodolico 47
                  &report
48
                  &loadNodePopulations
10 rodolico 49
                  &loadNodeDB
50
                  &loadVirtDB
3 rodolico 51
                );
52
 
53
 
54
 
55
sub readDB {
56
   my ($filename) = @_;
57
   my $yaml = YAML::Tiny->new( {} );
58
   if ( -f $filename ) {
59
      $yaml = YAML::Tiny->read( $filename );
60
   }
61
   return $yaml->[0];
62
}
63
 
64
sub writeDB {
65
   my ($filename,$data) = @_;
66
   my $yaml = YAML::Tiny->new( $data );
67
   $yaml->write( $filename );
68
}
69
 
4 rodolico 70
sub loadNodePopulations {
71
   return if $main::nodePopulations;
72
   $main::nodePopulations = &readDB( $main::nodePopulationDBName );
73
}
74
 
75
sub report {
76
   if ( $main::reportFormat eq 'tsv' ) {
77
      return &report_tsv( @_ );
78
   } else {
79
      return &report_screen( @_ );
80
   }
81
}
82
 
3 rodolico 83
sub report_tsv {
84
   my ( $header, $data ) = @_;
85
   my @output;
86
   push @output, join( "\t", @$header );
87
   for( my $line = 0; $line < @$data; $line++ ) {
88
      push @output, join( "\t", @{$data->[$line]} );
89
   } # for
90
   return join( "\n", @output ) . "\n";
91
}
92
 
93
sub report_screen {
94
   my ( $header, $data ) = @_;
95
   my @output;
96
   my @widths;
97
   my $column;
98
   my $row;
99
   # First, initialize by using the length of the headers
100
   for ( $column = 0; $column < @$header; $column++ ) {
101
      @widths[$column] = length( $header->[$column] );
102
   }
103
   # now, go through all data in each row, for each column, and increment the width if it is larger
104
   for ( $row = 0; $row < @$data; $row++ ) {
105
      for ( $column = 0; $column < @$header; $column++ ) {
106
         $widths[$column] = length( $data->[$row][$column] ) 
107
            if length( $data->[$row][$column] ) > $widths[$column];
108
      } # for column
109
   } # for row
110
   # actually do the print now
111
   my @format;
112
   for ( $column = 0; $column < @widths; $column++ ) {
113
      push ( @format, '%' . $widths[$column] . 's' );
114
   }
115
   my $format = join( ' ', @format ) . "\n";
116
   my $output = sprintf( $format, @$header );
117
   for ( $row = 0; $row < @$data; $row++ ) {
118
      $output .= sprintf( $format, @{$data->[$row]} );
119
   } # for row
120
   return $output;
121
}
10 rodolico 122
 
123
# read the node database into memory, if it was not already loaded
124
sub loadNodeDB {
125
   print "In loadNodeDB, reading $main::nodeDBName\n" if $main::DEBUG > 1;
126
   return if $main::nodeDB;
127
   $main::nodeDB = &main::readDB( $main::nodeDBName );
128
   print "Success\n" if $main::DEBUG > 1;
129
}
130
 
131
sub loadVirtDB {
132
   return if $main::virtDB;
133
   $main::virtDB = &main::readDB( $main::domainDBName );
134
}
135