| 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
  | 
        
        
           | 3 | 
           rodolico | 
           49 | 
                           );
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
           sub readDB {
  | 
        
        
            | 
            | 
           54 | 
              my ($filename) = @_;
  | 
        
        
            | 
            | 
           55 | 
              my $yaml = YAML::Tiny->new( {} );
  | 
        
        
            | 
            | 
           56 | 
              if ( -f $filename ) {
  | 
        
        
            | 
            | 
           57 | 
                 $yaml = YAML::Tiny->read( $filename );
  | 
        
        
            | 
            | 
           58 | 
              }
  | 
        
        
            | 
            | 
           59 | 
              return $yaml->[0];
  | 
        
        
            | 
            | 
           60 | 
           }
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
           sub writeDB {
  | 
        
        
            | 
            | 
           63 | 
              my ($filename,$data) = @_;
  | 
        
        
            | 
            | 
           64 | 
              my $yaml = YAML::Tiny->new( $data );
  | 
        
        
            | 
            | 
           65 | 
              $yaml->write( $filename );
  | 
        
        
            | 
            | 
           66 | 
           }
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
           | 4 | 
           rodolico | 
           68 | 
           sub loadNodePopulations {
  | 
        
        
            | 
            | 
           69 | 
              return if $main::nodePopulations;
  | 
        
        
            | 
            | 
           70 | 
              $main::nodePopulations = &readDB( $main::nodePopulationDBName );
  | 
        
        
            | 
            | 
           71 | 
           }
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
           sub report {
  | 
        
        
            | 
            | 
           74 | 
              if ( $main::reportFormat eq 'tsv' ) {
  | 
        
        
            | 
            | 
           75 | 
                 return &report_tsv( @_ );
  | 
        
        
            | 
            | 
           76 | 
              } else {
  | 
        
        
            | 
            | 
           77 | 
                 return &report_screen( @_ );
  | 
        
        
            | 
            | 
           78 | 
              }
  | 
        
        
            | 
            | 
           79 | 
           }
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
           | 3 | 
           rodolico | 
           81 | 
           sub report_tsv {
  | 
        
        
            | 
            | 
           82 | 
              my ( $header, $data ) = @_;
  | 
        
        
            | 
            | 
           83 | 
              my @output;
  | 
        
        
            | 
            | 
           84 | 
              push @output, join( "\t", @$header );
  | 
        
        
            | 
            | 
           85 | 
              for( my $line = 0; $line < @$data; $line++ ) {
  | 
        
        
            | 
            | 
           86 | 
                 push @output, join( "\t", @{$data->[$line]} );
  | 
        
        
            | 
            | 
           87 | 
              } # for
  | 
        
        
            | 
            | 
           88 | 
              return join( "\n", @output ) . "\n";
  | 
        
        
            | 
            | 
           89 | 
           }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
           sub report_screen {
  | 
        
        
            | 
            | 
           92 | 
              my ( $header, $data ) = @_;
  | 
        
        
            | 
            | 
           93 | 
              my @output;
  | 
        
        
            | 
            | 
           94 | 
              my @widths;
  | 
        
        
            | 
            | 
           95 | 
              my $column;
  | 
        
        
            | 
            | 
           96 | 
              my $row;
  | 
        
        
            | 
            | 
           97 | 
              # First, initialize by using the length of the headers
  | 
        
        
            | 
            | 
           98 | 
              for ( $column = 0; $column < @$header; $column++ ) {
  | 
        
        
            | 
            | 
           99 | 
                 @widths[$column] = length( $header->[$column] );
  | 
        
        
            | 
            | 
           100 | 
              }
  | 
        
        
            | 
            | 
           101 | 
              # now, go through all data in each row, for each column, and increment the width if it is larger
  | 
        
        
            | 
            | 
           102 | 
              for ( $row = 0; $row < @$data; $row++ ) {
  | 
        
        
            | 
            | 
           103 | 
                 for ( $column = 0; $column < @$header; $column++ ) {
  | 
        
        
            | 
            | 
           104 | 
                    $widths[$column] = length( $data->[$row][$column] ) 
  | 
        
        
            | 
            | 
           105 | 
                       if length( $data->[$row][$column] ) > $widths[$column];
  | 
        
        
            | 
            | 
           106 | 
                 } # for column
  | 
        
        
            | 
            | 
           107 | 
              } # for row
  | 
        
        
            | 
            | 
           108 | 
              # actually do the print now
  | 
        
        
            | 
            | 
           109 | 
              my @format;
  | 
        
        
            | 
            | 
           110 | 
              for ( $column = 0; $column < @widths; $column++ ) {
  | 
        
        
            | 
            | 
           111 | 
                 push ( @format, '%' . $widths[$column] . 's' );
  | 
        
        
            | 
            | 
           112 | 
              }
  | 
        
        
            | 
            | 
           113 | 
              my $format = join( ' ', @format ) . "\n";
  | 
        
        
            | 
            | 
           114 | 
              my $output = sprintf( $format, @$header );
  | 
        
        
            | 
            | 
           115 | 
              for ( $row = 0; $row < @$data; $row++ ) {
  | 
        
        
            | 
            | 
           116 | 
                 $output .= sprintf( $format, @{$data->[$row]} );
  | 
        
        
            | 
            | 
           117 | 
              } # for row
  | 
        
        
            | 
            | 
           118 | 
              return $output;
  | 
        
        
            | 
            | 
           119 | 
           }
  |