Commit a7430d9d authored by Mike Hibler's avatar Mike Hibler

Add -G option to assign lat/lng info to users.

This is a hack. The info is extracted from the intermediate files we
generate when creating the "usermap" info.
parent a4573f71
......@@ -33,7 +33,7 @@
# Each node record look like:
# (node_ix,name,IP,MAC)
# Each experiment record looks like:
# (expt_ix,name,proj_ix)
# (expt_ix,name,proj_ix,start,end)
# Each user record looks like:
# (user_ix,name,affil,city,state,country,zip)
# Each project record looks like:
......@@ -55,11 +55,12 @@ sub usage()
print STDERR " -a Get info for all nodes\n";
print STDERR " -n node Get info for a specific node\n";
print STDERR " -A Anonymize info\n";
print STDERR " -G Attempt to assign Lat/Lon info to users\n";
print STDERR " -N Print in normalized CSV format\n";
print STDERR " -S Print in summary form\n";
}
my $optlist = "adhANSs:e:n:";
my $optlist = "adhAGNSs:e:n:";
my $debug = 0;
my $anonymize = 0;
my $normalize = 0;
......@@ -67,6 +68,7 @@ my $summary = 0;
my $starttime;
my $endtime;
my $allnodes = 0;
my $dogeo = 0;
my $node;
sub nodehistory($$;$);
......@@ -120,6 +122,9 @@ if (defined($options{a})) {
if (defined($options{A})) {
$anonymize++;
}
if (defined($options{G})) {
$dogeo++;
}
if (defined($options{N})) {
$normalize = 1;
$summary = 0;
......@@ -287,19 +292,38 @@ foreach my $ue (@$users) {
my $state = $ue->{'state'};
my $country = $ue->{'country'};
my $zip = $ue->{'zip'};
my $lat = $ue->{'latitude'};
my $lng = $ue->{'longitude'};
if ($summary) {
$affil =~ s/^\s+//; $affil =~ s/'//g;
$city =~ s/^\s+//; $city =~ s/'//g;
$state =~ s/^\s+//; $state =~ s/'//g;
$country =~ s/^\s+//; $country =~ s/'//g;
$zip =~ s/^\s+//; $zip =~ s/'//g;
print "index='$uix', name='$name', affil='$affil', city='$city', state='$state', country='$country', zip='$zip'\n";
print "index='$uix', name='$name', affil='$affil', city='$city', state='$state', country='$country', zip='$zip'";
if ($dogeo) {
print ", latitude='$lat', longitude='$lng'";
}
print "\n";
} elsif ($normalize) {
print "user_index,name,affil,city,state,country,zip\n"
if ($uix == 1);
print "$uix,$name,$affil,$city,$state,$country,$zip\n";
if ($uix == 1) {
print "user_index,name,affil,city,state,country,zip";
if ($dogeo) {
print ",latitude,longitude";
}
print "\n";
}
print "$uix,$name,$affil,$city,$state,$country,$zip";
if ($dogeo) {
print ",$lat,$lng";
}
print "\n";
} else {
print "$uix: name='$name', affil='$affil', city='$city', state='$state', country='$country', zip='$zip'\n";
print "$uix: name='$name', affil='$affil', city='$city', state='$state', country='$country', zip='$zip'";
if ($dogeo) {
print ", latitude='$lat', longitude='$lng'";
}
print "\n";
}
}
......@@ -626,5 +650,114 @@ sub users($)
$pe->{'users'} = \@pusers;
}
if ($dogeo) {
geoize(\@users);
}
return \@users;
}
#
# For geo info we use the intermediate files generated by the process of
# updating our usermap. Part of that process is to generate a filename
# based on the name the user gave and filling that file with the output
# of a Google aps API query (see the emulab-utah repo, gmaps subdir).
# We combine the city/state/country info the same way we did to create
# name of the json intermediate file.
#
sub geoize($)
{
my ($uref) = @_;
my $infodir = "/users/mike/emulab-utah/gmaps/json";
if (!-d $infodir) {
return;
}
my @files = `/bin/ls -1 $infodir`;
if (@files == 0) {
return;
}
require JSON;
#
# Make a hash indexed by the user provided name and providing
# a canonical name, latitude, and longitude.
#
my %cities = ();
foreach my $file (@files) {
next
if ($file !~ /^(.*)\.json$/);
my $ename = $1;
next
if (!open(FD, "<$infodir/$file"));
my $json;
{
local $/;
$json = <FD>;
}
close(FD);
my $doc = JSON::decode_json($json);
next
if (!$doc || $doc->{'status'} ne "OK");
my $res = $doc->{'results'}[0];
$cities{$ename}{'fname'} = $res->{'formatted_address'};
$cities{$ename}{'lat'} = $res->{'geometry'}->{'location'}->{'lat'};
$cities{$ename}{'lng'} = $res->{'geometry'}->{'location'}->{'lng'};
}
#require Data::Dumper;
#print "Info:\n";
#print Data::Dumper::Dumper(\%cities);
#
# Take our user entries, convert the city/state/country into the
# format used by our usermap files.
#
foreach $user (@$uref) {
my $city = $user->{'city'};
my $state = $user->{'state'};
my $country = $user->{'country'};
my $key;
$city =~ s/^\s+//;
$city =~ s/\s+$//;
$state =~ s/^\s+//;
$state =~ s/\s+$//;
$country =~ s/^\s+//;
$country =~ s/\s+$//;
$city = lc($city);
$state = lc($state);
$country = lc($country);
if ($country ne "usa" && $country ne "us" &&
$country ne "" && $country ne "NULL") {
$key = "$city-$country";
} else {
$key = "$city-$state-$country";
}
$key =~ s/\s+/-/g;
$key =~ s/[^\w-]//g;
again:
if (exists($cities{$key})) {
$user->{'latitude'} = $cities{$key}{'lat'};
$user->{'longitude'} = $cities{$key}{'lng'};
$user->{'location'} = $cities{$key}{'fname'};
} elsif ($key =~ /(.*)-us$/) {
$key = "$1-usa";
goto again;
} else {
print STDERR "*** user '" . $user->{'name'} .
"': could not find geo info for '$key' (city='".
$user->{'city'} . "', state='" .
$user->{'state'} . "', country='" .
$user->{'country'} . "')\n";
$user->{'latitude'} = "";
$user->{'longitude'} = "";
$user->{'location'} = "";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment