IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 15, 2009, 11:34:36 AM (17 years ago)
Author:
bills
Message:

calculate appropriate widths for the columns

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DataStoreServer/scripts/dsprodindex

    r17381 r23876  
    2727}
    2828
    29 # build the header line
    30 my $header = "# filesetID|time registered     |type     |";
     29# first get the column names
    3130
    32 # now add the product specific columns that are defined
    33 # not particularly elegant. I guess I should have fetched the row as an array
    34 # note: we don't do any formatting for these columns. The width of the string
    35 # in the database should be padded if a nice width is desired
     31# we have at least 3 columns
     32my @header_col = ("# filesetID", "time registered", "type");
     33my $numCols = 3;
     34
     35# now add any product specific columns that are defined. Note if there is no header value
     36# then any values in the rows will be ignored
    3637my $last_prod_col = -1;
    37 if (defined $prod->{prod_col_0}) {
    38     $last_prod_col = 0;
    39     $header .= "$prod->{prod_col_0}|";
    40     if (defined $prod->{prod_col_1}) {
    41         $last_prod_col = 1;
    42         $header .= "$prod->{prod_col_1}|";
    43         if (defined $prod->{prod_col_2}) {
    44             $last_prod_col = 2;
    45             $header .= "$prod->{prod_col_2}|";
    46             if (defined $prod->{prod_col_3}) {
    47                 $last_prod_col = 3;
    48                 $header .= "$prod->{prod_col_3}|";
    49                 if (defined $prod->{prod_col_4}) {
    50                     $last_prod_col = 4;
    51                     $header .= "$prod->{prod_col_4}|";
    52                     if (defined $prod->{prod_col_5}) {
    53                         $last_prod_col = 5;
    54                         $header .= "$prod->{prod_col_5}|";
    55                         if (defined $prod->{prod_col_6}) {
    56                             $last_prod_col = 6;
    57                             $header .= "$prod->{prod_col_6}|";
    58                             if (defined $prod->{prod_col_7}) {
    59                                 $last_prod_col = 7;
    60                                 $header .= "$prod->{prod_col_7}|";
    61                             }
    62                         }
    63                     }
    64                 }
    65             }
    66         }
    67     }
     38for (my $i = 0; $i < 8; $i++) {
     39    my $col_key = "prod_col_$i";
     40    my $col_name = $prod->{$col_key};
     41
     42    last if ! defined $col_name;
     43
     44    $header_col[$numCols++] = $col_name;
     45    $last_prod_col = $i;
     46}
     47
     48# now set up arrays for the values for each column.
     49my @column;
     50for (my $i = 0; $i < $numCols; $i++) {
     51
     52    my @col = ($header_col[$i]);
     53
     54    $column[$i] = \@col;
    6855}
    6956   
     
    8976        # XXX: the spec doesn't say what should happen in this case.
    9077        # Here we follow the conductor implementation and return the whole list.
    91         # This may not be the right thing to do.
     78        # This may not be the right thing to do. It might be better to throw an error
    9279    }
    9380}
     
    9683$stmt->execute();
    9784
    98 # XXX: dsproductls expects a header even if there are no matching filesets
    99 print "$header\n";
    100 
     85# we at least have the header row to output
     86my $numRows = 1;
    10187while( my $row = $stmt->fetchrow_hashref()) {
    10288
     89    $numRows++;
    10390    my $daytime = $row->{reg_time};
    10491    (my $date, my $time) = split " ", $daytime;
     
    10895    my $line = sprintf "%-11s|%-20s|%-9s|", $row->{fileset_name}, $reg_time, $row->{type};
    10996
    110     # now add the product specific columns that are defined
    111     # again using an array would have made this look nicer
    112     if ($last_prod_col >= 0) {
    113         $line .= "$row->{prod_col_0}|";
    114         if ($last_prod_col >= 1) {
    115             $line .= "$row->{prod_col_1}|";
    116             if ($last_prod_col >= 2) {
    117                 $line .= "$row->{prod_col_2}|";
    118                 if ($last_prod_col >= 3) {
    119                     $line .= "$row->{prod_col_3}|";
    120                     if ($last_prod_col >= 4) {
    121                         $line .= "$row->{prod_col_4}|";
    122                         if ($last_prod_col >= 5) {
    123                             $line .= "$row->{prod_col_5}|";
    124                             if ($last_prod_col >= 6) {
    125                                 $line .= "$row->{prod_col_6}|";
    126                                 if ($last_prod_col >= 7) {
    127                                     $line .= "$row->{prod_col_7}|";
    128                                 }
    129                             }
    130                         }
    131                     }
    132                 }
    133             }
     97    # this probably would have been simpler had I used fetchrow array
     98    push @{$column[0]}, $row->{fileset_name};
     99    push @{$column[1]}, $reg_time;
     100    push @{$column[2]}, $row->{type};
     101   
     102    for (my $i = 0; $i <= $last_prod_col; $i++) {
     103        my $col_key = "prod_col_$i";
     104        my $col_val = $row->{$col_key};
     105
     106        if (!defined($col_val)) {
     107            $col_val = "null";
     108        }
     109        push @{$column[$i+3]}, $col_val;
     110    }
     111}
     112
     113# find the largest value width in each column
     114my @col_widths;
     115for (my $c = 0; $c < $numCols; $c++) {
     116    $col_widths[$c] = 0;
     117    my $col = $column[$c];
     118    for (my $i = 0; $i < $numRows; $i++) {
     119        my $val = $col->[$i];
     120        if (!$val) {
     121            die "value for column $c in row $i is null";
     122            next;
     123        }
     124        my $len = length($val);
     125        if ($len > $col_widths[$c]) {
     126            $col_widths[$c] = $len;
    134127        }
    135128    }
     129}
    136130
    137     print "$line\n";
     131# print out the results
     132for (my $r = 0; $r < $numRows; $r++) {
     133    for (my $c = 0; $c < $numCols; $c++) {
     134        my $val = $column[$c]->[$r];
     135        my $width = $col_widths[$c];
     136        if ($val) {
     137            printf "%-*s|", $width, $val;
     138        } else {
     139            die "value for column $c in row $r is null";
     140        }
     141    }
     142    print "\n";
    138143}
     144   
Note: See TracChangeset for help on using the changeset viewer.