Skip to content

Instantly share code, notes, and snippets.

@jdiverp
Last active July 18, 2024 16:47
Show Gist options
  • Select an option

  • Save jdiverp/b658d13d489ee1e23e0800d08cbea1e4 to your computer and use it in GitHub Desktop.

Select an option

Save jdiverp/b658d13d489ee1e23e0800d08cbea1e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# would prefer to use IRWS instead of TSC stuff for the report
my $mango = '';
$mango= 'mango-eval.u.washington.edu';
$mango= 'mango.u.washington.edu';
my $MU = "/usr/local/bin/mu -c ~pass/.cconfig -h $mango -a 4";
#create a select for records updated before today's import
my $date= `date +%Y-%m-%d`;
chomp ($date);
my $allSpokane_select = "adhoc com=(SELECT v.validid from validation v join regid r on r.validation_id = v.validation_id and r.status_code=50 and r.test = 0 and v.source_code=20)";
my $allSpokane_output = "spokane.$date.all.out";
my $expiration_select = "adhoc com=(SELECT v.validid from validation v join regid r on r.validation_id = v.validation_id and r.status_code=50 and r.test = 0 and v.source_code=20 and v.status_code=1 where v.updated < \"$date\")";
my $expiration_output = "spokane.$date.expire.out";
my $expiration_file = "spokane.$date.expire.mango";
my $updates_file = "spokane.$date.updates.mango";
my $report_file = "spokane.$date.uwnetid_report.txt";
# email address validation. Taken from https://perlmaven.com/email-validation-using-regular-expression-in-perl
my $REXusername = qr/[a-z0-9_+]([a-z0-9_+'.\-]*[a-z0-9_+'\-])?/;
my $REXdomain = qr/[a-z0-9.-]+/;
print "This tool allows you to generate a set of mango directives to load source 20 for the next year.\n\n";
print "All residual mango directive files will be named for the current year [$date]:\n";
print "File must be tab delimited and have 6 fields, no more:\n";
print "\nEnter the file to parse/readin: ";
chomp ( $file = <stdin>);
if(! -f $file ) {
print "File does not exist as a file! Quitting:\n";
exit;
}
open(FILE, $file);
my @medres = <FILE>;
# If we use a unix formated file the below is not necessary.
#my @medres = split(/\r/, $medres[0]);
close FILE;
# declare arrays that well use for output generation
my @mango_updates = ();
my @mango_expires = ();
my @user_report = ();
# validate each EID appears only once, and don't expire people who weren't in the feed but were not updated today
my %in_feed;
#generate list of all existing records so we can determine if a insert or an update is required
my %existing_records;
system("echo '$allSpokane_select' | $MU -q -y -r $allSpokane_output -d 1");
open(FILE, $allSpokane_output);
my @all = <FILE>;
foreach my $line (@all ) {
$i++;
chomp($line);
my ($val) = split (/\|/, $line);
# skip expirting anyone that was in the feed, important
$existing_records{$val} = 1;
}
# import loop
my $i=0;
foreach my $line (@medres) {
$i++;
chomp($line);
#$line =~ s/\r//g;
my @elements = split(/\t/, $line);
my $lastname = $elements[0];
my $firstname = $elements[1];
my $middlename = $elements[2];
my $npi_number = $elements[3];
my $email_addr = lc($elements[4]);
$email_addr =~ s/ //g;
my $prov_id = 'P'.$elements[5];
my $command = 'updval';
$command = 'insval' if ! defined $existing_records{$prov_id};
#updval lname=(Adkins) fname=(Jake Marshall) npi_number=(1750743951) contact_email=(jacob.adkins@providence.org) validid=(P457289) source_code=20 status_code=1 category_code=145 in_feed=1 -canonical
die ("ERROR: Row $i has an invalid name\n") if( $lastname eq '' or $firstname eq '' );
die ("ERROR: Row $i has an invalid npi number ($npi_number)\n") if( $npi_number !~ /^\d{10}$/ );
die ("ERROR: Row $i has no contact email ($email_addr)\n") if( $email_addr !~ /^$REXusername\@$REXdomain$/);
die ("ERROR: Row $i has an invalid providence ID ($prov_id)\n") if( $prov_id !~ /^P\d{5,7}$/ );
die ("ERROR: Row $i id number appears more than once ($prov_id)\n") if( defined $in_feed{$prov_id} );
die ("ERROR: Row $i more than 6 fields\n") if( defined $elements[6] );
my $fname_mango = "$firstname $middlename";
$fname_mango =~ s/\s+$//;
push (@mango_updates, "$command lname=($lastname) fname=($fname_mango) contact_email=[($email_addr)] npi_number=($npi_number) validid=($prov_id) source_code=20 status_code=1 categories=[{category_code=145}] in_feed=1 -canonical");
$in_feed{$prov_id} = 1;
my $uwnetid = get_uwnetid($prov_id);
push (@user_report, (join("\t", @elements). "\t$uwnetid"));
}
open (REPORT, ">$report_file");
print REPORT join ("\n", @user_report);
close REPORT;
# Process updat directives
open (UPDATE, ">$updates_file");
print UPDATE join ("\n", @mango_updates). "\n";
close UPDATE;
print join ("\n", @mango_updates). "\n";
print "\nStep 1: Send to $mango? ";
$answer = '';
chomp ( $answer = <stdin>);
while ($answer ne 'yes' and $answer ne 'no') {
#print "you answered \"$answer\"\n";
print "Please answer yes or no: ";
chomp ($answer = <stdin>);
}
exit if ($answer eq 'no');
system("$MU -f $updates_file");
#generate expiration list
system("echo '$expiration_select' | $MU -q -y -r $expiration_output -d 1");
open(FILE, $expiration_output);
my @expire= <FILE>;
foreach my $line (@expire) {
chomp($line);
next if $line eq '0|record matched|';
my ($val) = split (/\|/, $line);
# skip expirting anyone that was in the feed, important
next if defined ($in_feed{$val});
push (@mango_expires, "updval validid=$val source_code=20 status_code=3");
}
#process expiration directives
open (EXPIRE, ">$expiration_file");
print EXPIRE join ("\n", @mango_expires). "\n";
close EXPIRE;
print join ("\n", @mango_expires). "\n";
print "\nStep 2: Send expiration directives to $mango? ";
$answer = '';
chomp ( $answer = <stdin>);
while ($answer ne 'yes' and $answer ne 'no') {
#print "you answered \"$answer\"\n";
print "Please answer yes or no: ";
chomp ($answer = <stdin>);
}
exit if ($answer eq 'no');
system("$MU -f $expiration_file");
#thats it
print "DONE!\n";
exit;
sub get_uwnetid {
my $id = shift;
my $p = `echo "seluwn val=20=$id status_code=30" | $MU -q |grep -o -E 'uwnetid=[^)]+' |sed 's/uwnetid=(//'|head -1`;
chomp $p;
#print "get_uwnetid says \"$p\"\n";
return '' if (!defined $p or $p eq '');
return $p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment