vicinity calc uses geo module for recalculations. bulk calculation breaks org units...
authorLlewellyn Marshall <llewellyn.marshall@ncdcr.gov>
Thu, 11 Aug 2022 15:03:32 +0000 (11:03 -0400)
committerLlewellyn Marshall <llewellyn.marshall@ncdcr.gov>
Thu, 11 Aug 2022 15:03:32 +0000 (11:03 -0400)
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/eg2/src/app/staff/admin/server/org-unit-shipping-hub-distance.component.ts
Open-ILS/src/eg2/src/app/staff/share/admin-page/admin-page.component.ts
Open-ILS/src/perlmods/lib/OpenILS/Application/Geo.pm
Open-ILS/src/perlmods/lib/OpenILS/Utils/VicinityCalculator.pm

index c4f5202..ed7318a 100644 (file)
@@ -4134,7 +4134,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
                        <link field="name" reltype="has_a" key="name" map="" class="coust"/>
                </links>
        </class>
-    <class id="aoushd" controller="open-ils.cstore" oils_obj:fieldmapper="actor::org_unit_shipping_hub_distance" oils_persist:tablename="actor.org_unit_shipping_hub_distance" reporter:label="Organizational Unit Shipping Hub Distance">
+    <class id="aoushd" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="actor::org_unit_shipping_hub_distance" oils_persist:tablename="actor.org_unit_shipping_hub_distance" reporter:label="Organizational Unit Shipping Hub Distance">
                <fields oils_persist:primary="id" oils_persist:sequence="actor.org_unit_shipping_hub_distance_id_seq">
                        <field name="id" />
                        <field name="orig_hub" reporter:datatype="org_unit"/>
@@ -4142,9 +4142,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
                        <field name="distance"  reporter:datatype="int"/>
                </fields>
                <links>
-                       <link field="org_unit" reltype="has_a" key="id" map="" class="aou"/>
-                       <link field="hub" reltype="has_a" key="id" map="" class="aou"/>
+                       <link field="orig_hub" reltype="has_a" key="id" map="" class="aou"/>
+                       <link field="dest_hub" reltype="has_a" key="id" map="" class="aou"/>
                </links>
+               <permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
+                       <actions>
+                               <create permission="UPDATE_ORG_UNIT" context_field="orig_hub"/>
+                               <retrieve/>
+                               <update permission="UPDATE_ORG_UNIT" context_field="orig_hub"/>
+                               <delete permission="UPDATE_ORG_UNIT" context_field="orig_hub"/>
+                       </actions>
+               </permacrud>
        </class>    
        <class id="acpn" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="asset::copy_note" oils_persist:tablename="asset.copy_note" reporter:label="Copy Note">
                <fields oils_persist:primary="id" oils_persist:sequence="asset.copy_note_id_seq">
index 8e7b731..535a0a5 100644 (file)
@@ -15,9 +15,9 @@ import {AuthService} from '@eg/core/auth.service';
       <eg-staff-banner bannerText="{{classLabel}} Configuration" i18n-bannerText>
       </eg-staff-banner>
       <div class="alert alert-info" i18n>
-        Entries in this table contain the distance in miles between shipping locations. Shipping hubs are configured from the Orginizational Units page. These numbers are used for sorting hold targets during inter-library lending. Entries can be created manually or calculated using the free Bing Maps API if a key has been set up in the opensrf core config file. <b>Running the API will remove any existing data from this table</b>.    
+        Entries in this table contain the distance in kilometers between shipping hub locations. If configured for your service, driving distance will be used instead of as-the-crow-flies. Shipping hubs are configured from the Orginizational Units page. These numbers are used for sorting hold targets during inter-library lending. Entries can be created manually or calculated using your geolocation service. <b>Recalculating the distances will clear out any existing data.</b>.    
       </div>
-      <button [disabled]="calculating" class="btn btn-outline-dark" (click)="calculateDistances()">Calculate with API</button>
+      <button [disabled]="calculating" class="btn btn-outline-dark" (click)="calculateDistances()">Calculate with Geolocation Service</button>
       <eg-progress-inline *ngIf="calculating" ></eg-progress-inline>
       <br>
       <br>
@@ -44,7 +44,7 @@ export class OrgUnitShippingHubDistanceComponent implements OnInit {
     constructor(
         private route: ActivatedRoute,
         private idl: IdlService,
-        private net: NetService
+        private net: NetService,
         private auth: AuthService
     ) {
     }
index d9242bd..1cbaf3f 100644 (file)
@@ -237,9 +237,10 @@ export class AdminPageComponent implements OnInit {
         this.canCreate = false;
         const pc = this.idlClassDef.permacrud || {};
         const perms = pc.create ? pc.create.perms : [];
-        if (perms.length === 0) { return; }
-
+        console.log(perms);
+        if (perms.length === 0) { return; }   
         this.perm.hasWorkPermAt(perms, true).then(permMap => {
+            console.log(permMap);
             Object.keys(permMap).forEach(key => {
                 if (permMap[key].length > 0) {
                     this.canCreate = true;
index ddf2a16..3e8ade3 100644 (file)
@@ -131,19 +131,17 @@ sub _post_request {
     my ($bing, $uri, $form, $json_coder) = @_;
     my $json = $json_coder->encode($form); 
     return unless $uri;
-    $logger->info($uri);
-    $logger->info($form);
+    #$logger->info($uri);
+    #$logger->info($form);
     my $res = $bing->{response} = $bing->ua->post($uri,'Content-Length' => 3500,'Content-Type' => 'application/json',Content => $json);
     unless($res->is_success){
         $logger->error("API ERROR\n");
+        my @error = split /\n/, $res->decoded_content;
+        foreach(@error){
+            $logger->error($_);
+        }
         return;
-    }
-
-    my @error = split /\n/, $res->decoded_content;
-    foreach(@error){
-        $logger->error($_);
-    }
+    } 
     # Change the content type of the response from 'application/json' so
     # HTTP::Message will decode the character encoding.
     $res->content_type('text/plain');
@@ -204,7 +202,7 @@ sub calculate_bulk_driving_distance {
             $dcoord{'longitude'} = $_->[1];
             push(@destinations, \%dcoord);
         }
-        $logger->info(Dumper(\@destinations));
+        #$logger->info(Dumper(\@destinations));
         
         # find out how many coords we can process per request.
         my $budget = int(2500 / scalar(@origins));
@@ -268,7 +266,7 @@ sub calculate_bulk_driving_distance {
 
         return \@results;
     }
-    return [];
+    return \@results;
 }
 
 __PACKAGE__->register_method(
index d9ee04e..11ad652 100644 (file)
@@ -49,21 +49,25 @@ sub calculate_distance_matrix {
     my $geo = OpenSRF::AppSession->create('open-ils.geo');
     my $geo_request = $geo->request('open-ils.geo.calculate_bulk_driving_distance',
             $self->{auth}, \@origins, \@destinations);
-    my @distance_matrix = @{$geo_request};
+    my $result = $geo_request->recv();
+    my $content = $result->content();            
+    $logger->info("content from API: ".Dumper($content));
+    my @distance_matrix = @{$content};
     if(@distance_matrix){
         $self->{editor}->xact_begin;
         # clear out existing matrix
+        $logger->info("Old distance matrix is being cleared out.");
         $self->clear_hub_distances();
-        for my $ref (@distance_matrix) {
-            for (@$ref){
+        foreach(@distance_matrix) {
+                print(Dumper $_);
                 # create our AOUSHD objects for the data returned
                 my $dist = Fieldmapper::actor::org_unit_shipping_hub_distance->new;
-                $dist->orig_hub($hub_ids[$_->{originIndex}]);
-                $dist->dest_hub($hub_ids[$_->{destinationIndex}]);
-                $dist->distance($_->{travelDistance});
+                $dist->orig_hub($hub_ids[$_->{origin}]);
+                $dist->dest_hub($hub_ids[$_->{destination}]);
+                $dist->distance($_->{distance});
                 # place AOUSHD into the DB
                 $self->{editor}->runmethod('create', 'actor.org_unit_shipping_hub_distance', 'aoushd', $dist);
-            }
+            
         }
         # commit to DB 
         $self->{editor}->xact_commit;
@@ -73,6 +77,36 @@ sub calculate_distance_matrix {
     }
 }
 
+sub get_coord_from_ou {
+my($self,@org_ids) = @_;
+    my @ma = $self->{editor}->json_query({
+        select => {
+            aoa => [
+                {
+                    column => 'org_unit',
+                },
+                {
+                    column => 'latitude',
+                },{
+                    column => 'longitude',
+                },{
+                    column => 'address_type',
+                }             
+            ]
+        },
+        from => 'aoa',
+        where => {org_unit=>[@org_ids], address_type=>['MAILING']}
+    });
+    my %coords;
+   
+    for my $ref (@ma) {
+        for (@$ref){
+            $coords{$_->{org_unit}} = [$_->{latitude}, $_->{longitude}];
+        }
+    }
+    return %coords;
+}
+
 # remove all existing distance calculations.
 # TODO make this all happen in one query
 # what could the analog to DELETE FROM TABLE be?