Monthly Archives: February 2017

Dev. Defense

Customers, internal or external for that matter, regularly ask for one thing , imagine another and actually need a third. How can a developer defend oneself before such customers ? Use this; really.

  1. Please tell me what is expected of this product. Better yet draft it on a napkin.
  2. I have limited telepathy which decreases exponentially by distance. Please communicate succinctly via other means.
  3. I did not think of the other 400 possibilities you could have used  because my creativity is limited by my imagination. I never imagined this, or that for that matter.

Artifactory Stats via PERL

#!/usr/bin/perl
#
# Glean data from artifactory
#

use JSON;
use Data::Dumper;

# using api key for akarageo, fix as necessary
$CREDS=”user:apikey”;
$API=’https://artifactory.mydomain.com/artifactory/api’;

%MAG = (bytes => 1 ,
KB => 1024,
MB => 1024*1024,
GB => 1024*1024*1024,
TB => 1024*1024*1024*1024);

# if you want to sort by percentage
sub byperc {
$a->{percentage} <=> $b->{percentage} || $a->{filesCount} <=> $b->{filesCount}
}
# if you want to sort by storage size

sub bysize {
# convert to numbers and then compare
my ($bytes,$mag)=split(‘ ‘,$a->{usedSpace});
$a->{realSpace}=$bytes * $MAG{$mag};

($bytes,$mag)=split(‘ ‘,$b->{usedSpace});
$b->{realSpace}=$bytes * $MAG{$mag};

$b->{realSpace} <=> $a->{realSpace} || $a->{filesCount} <=> $b->{filesCount} ||   $a->{itemsCount} <=> $b->{itemsCount}
}

# the most interestinf part
sub printrepositoriesSummaryList {
my ($perl_scalar)=@_;
# data size page
print “<h2>Repositories Summary List</h2>\n”;
print “<table><tr>\n<th>Repository Name</th><th>Percentage</th><th>Used   Space</th><th>Folders</th><th>Items</th><th>Files</th></tr>\n”;
foreach $arr ($perl_scalar->{repositoriesSummaryList}) {
my @sarr = sort bysize @$arr;
foreach $hashref (@sarr) {
print “<tr>”;
print “<td>”.$hashref->{‘repoKey’}.”</td>”;
print “<td>”.$hashref->{‘percentage’}.”<ctd>”;
print “<td>”.$hashref->{‘usedSpace’}.”</td>”;
print “<td>”.$hashref->{‘foldersCount’}.”</td>”;
print “<td>”.$hashref->{‘itemsCount’}.”</td>”;
print “<td>”.$hashref->{‘filesCount’}.”</td>”;
print “</tr>\n”;
}
}
print “</table>”;
}

# global stats
sub printfileStoreSummary{
my ($perl_scalar)=@_;
print “<h3>File Store Summary</h2>”;
print “<table><tr><th>Directory</th><th>Total Space</th><th>Used Spacee</th>  <th>Free Space</th></tr><tr>”;
print “<td>” .$perl_scalar->{fileStoreSummary}{storageDirectory} .”</td>\n”;
print “<td>” .$perl_scalar->{fileStoreSummary}{totalSpace} .”</td>\n”;
print “<td>” .$perl_scalar->{fileStoreSummary}{usedSpace} .”</td>\n”;
print “<td>” .$perl_scalar->{fileStoreSummary}{freeSpace} .”</td>\n”;
print “</tr></table>\n”;
}
# may be interesting some time
sub printbinariesSummary{
my ($perl_scalar)=@_;
print “<h2>Binaries Summary</h2>\n<table>\n”;
foreach $key (keys($perl_scalar->{binariesSummary})) {
print “<tr><td>$key</td><td>” .$perl_scalar->{binariesSummary}{$key} .”</td></tr>\n”;
}
print “</table>\n”;
}
#main Code
$content=`/usr/bin/curl -s -u ${CREDS} ${API}/storageinfo`;
$json = JSON->new->allow_nonref;
$perl_scalar = $json->decode($content);

#$pretty_printed = $json->pretty->encode( $perl_scalar ); # pretty-printing
#print $pretty_printed ;

print “<h2> Artifactory Statistics </h2>”;

printfileStoreSummary($perl_scalar);
#printbinariesSummary($perl_scalar);
printrepositoriesSummaryList($perl_scalar);