Not yet successful attempt to move biblio.record.copy_count out of main search.
authorkenstir <kenstir@gmail.com>
Sat, 5 Dec 2015 02:44:47 +0000 (21:44 -0500)
committerkenstir <kenstir@gmail.com>
Sat, 5 Dec 2015 02:44:47 +0000 (21:44 -0500)
That call is taking 30% of the total search time, and it is not even necessary until you look at the item details.  Made sure that the orgID is properly passed from SearchCatalog through to BasicDetailsFragment.  Not working yet because my first attempt was to call getCopyCount on the main thread.  End goal: volley-based OSRF response handler.

Open-ILS/src/Android/core/src/org/evergreen_ils/accountAccess/bookbags/BookBagDetails.java
Open-ILS/src/Android/core/src/org/evergreen_ils/searchCatalog/CopyInformation.java
Open-ILS/src/Android/core/src/org/evergreen_ils/searchCatalog/RecordInfo.java
Open-ILS/src/Android/core/src/org/evergreen_ils/searchCatalog/SampleUnderlinesNoFade.java
Open-ILS/src/Android/core/src/org/evergreen_ils/searchCatalog/SearchCatalog.java
Open-ILS/src/Android/core/src/org/evergreen_ils/utils/ui/BasicDetailsFragment.java
Open-ILS/src/Android/kenstir_parse_logcat.pl
Open-ILS/src/Android/opensrf/src/org/opensrf/net/http/GatewayRequest.java

index 2642509..1c8c22f 100644 (file)
@@ -178,14 +178,12 @@ public class BookBagDetails extends ActionBarActivity {
 
             @Override
             public void run() {
-
-                ArrayList<RecordInfo> records = new ArrayList<RecordInfo>();
                 ArrayList<Integer> ids = new ArrayList<Integer>();
 
                 for (int i = 0; i < bookBag.items.size(); i++) {
                     ids.add(bookBag.items.get(i).target_copy);
                 }
-                records = search.getRecordsInfo(ids);
+                ArrayList<RecordInfo> records = search.getRecordsInfo(ids);
 
                 for (int i = 0; i < bookBag.items.size(); i++) {
                     bookBag.items.get(i).recordInfo = records.get(i);
index 9b7ad3a..6561f18 100644 (file)
@@ -36,11 +36,8 @@ public class CopyInformation implements Serializable {
     private final String TAG = CopyInformation.class.getName();
 
     public Integer org_id = -1;
-
     public String call_number_sufix;
-
     public String copy_location;
-
     public HashMap<String, String> statuses;
 
     // the hash from the request method
@@ -63,20 +60,19 @@ public class CopyInformation implements Serializable {
         statusInformation = new LinkedHashMap<String, String>();
 
         Set<Entry<String, String>> set = availableOrgStatuses.entrySet();
-
         Iterator<Entry<String, String>> it = set.iterator();
-
         while (it.hasNext()) {
             Entry<String, String> entry = it.next();
 
             if (hashValCopy.containsKey(entry.getKey())) {
                 statusInformation.put(entry.getValue(),
                         hashValCopy.get(entry.getKey()) + "");
-//                Log.d(TAG, "Added " + entry.getKey() + " "
-//                        + entry.getValue() + " "
-//                        + hashValCopy.get(entry.getKey()));
+//                Log.d(TAG, "Added " + entry.getKey() + " -> "
+//                        + entry.getValue() + " ("
+//                        + hashValCopy.get(entry.getKey()) + ")");
             }
         }
+        Log.d(TAG, "done");
     }
 
 }
index f3e07ed..47a0c12 100644 (file)
@@ -39,7 +39,7 @@ public class RecordInfo implements Serializable {
     public String author = null;
     public String pubdate = null;
     public String isbn = null;
-    public Integer doc_id = null;
+    public Integer doc_id = -1;
     public String publisher = null;
     public String subject = "";
     public String doc_type = null;
@@ -50,15 +50,13 @@ public class RecordInfo implements Serializable {
     public boolean dummy = false;
 
     public ArrayList<CopyCountInformation> copyCountListInfo = null;
-
-    public List<CopyInformation> copyInformationList = null;
+    public List<CopyInformation> copyInformationList = new ArrayList<CopyInformation>();
     public String search_format = null;
 
     public RecordInfo() {
         this.title = "Test title";
         this.author = "Test author";
         this.pubdate = "Publication date";
-        copyInformationList = new ArrayList<CopyInformation>();
 
         // marks the fact that this is a record made from no info
         this.dummy = true;
@@ -78,7 +76,6 @@ public class RecordInfo implements Serializable {
     }
 
     public RecordInfo(OSRFObject info) {
-        copyInformationList = new ArrayList<CopyInformation>();
         try {
             this.title = safeString(info.getString("title"));
             this.author = safeString(info.getString("author"));
index 3a01916..e3b7154 100644 (file)
@@ -21,47 +21,32 @@ package org.evergreen_ils.searchCatalog;
 
 import java.util.ArrayList;
 
-import android.support.v7.app.ActionBar;
 import android.view.MenuItem;
 import org.evergreen_ils.R;
-import org.evergreen_ils.accountAccess.AccountAccess;
 import org.evergreen_ils.utils.ui.*;
 import org.evergreen_ils.views.splashscreen.SplashActivity;
 
 import android.app.ProgressDialog;
 import android.content.Context;
 import android.content.Intent;
-import android.net.ConnectivityManager;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.view.ViewPager;
 import android.view.KeyEvent;
-import android.widget.Button;
-import android.widget.TextView;
 
 public class SampleUnderlinesNoFade extends BaseSampleActivity {
 
     private ArrayList<RecordInfo> records;
-
-    private Button myAccountButton;
-
-    private Button homeButton;
-
-    private TextView headerTitle;
-    
     private SearchCatalog search;
-    
     private ArrayList<RecordInfo> searchRecords;
-    
     private Context context;
-
     private ProgressDialog progressDialog;
-    
     private Runnable searchRunnableWithOffset;
 
     public static final int RETURN_DATA = 5;
-    
+    private Integer orgID = -1;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -75,6 +60,7 @@ public class SampleUnderlinesNoFade extends BaseSampleActivity {
 
         search = SearchCatalog.getInstance();
 
+        orgID = getIntent().getIntExtra("orgID", -1);
         records = (ArrayList<RecordInfo>) getIntent().getSerializableExtra("recordList");
 
         if (records.get(records.size() - 1).dummy == true)
@@ -84,10 +70,8 @@ public class SampleUnderlinesNoFade extends BaseSampleActivity {
         
         int record_position = getIntent().getIntExtra("recordPosition", 0);
         mAdapter = new SearchFragmentAdapter(getSupportFragmentManager());
-
         mPager = (ViewPager) findViewById(R.id.pager);
         mPager.setAdapter(mAdapter);
-
         mPager.setCurrentItem(record_position);
 
         UnderlinePageIndicator indicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
@@ -113,22 +97,14 @@ public class SampleUnderlinesNoFade extends BaseSampleActivity {
 
                     @Override
                     public void run() {
-                        // don't clear record list
-                        // recordList.clear();
                         if (searchRecords.size() > 0) {
-
                             for (int j = 0; j < searchRecords.size(); j++)
                                 records.add(searchRecords.get(j));
-
-                            // add extra record to display more
-                            // option button
-
                         }
                         mAdapter.notifyDataSetChanged();
                         progressDialog.dismiss();
                     }
                 });
-
             }
         };
 
@@ -171,15 +147,12 @@ public class SampleUnderlinesNoFade extends BaseSampleActivity {
                     getSearchResults.start();
             }
             return BasicDetailsFragment.newInstance(records.get(position),
-                    position + 1, search.visible);
-            
+                    position + 1, search.visible, orgID);
         }
 
         @Override
         public int getCount() {
             return records.size();
-            // return TabsView.CONTENT.length;
         }
-
     }
 }
\ No newline at end of file
index 49f8975..9455457 100644 (file)
@@ -84,7 +84,7 @@ public class SearchCatalog {
 
     public static SearchCatalog searchCatalogSingleton = null;
 
-    public String TAG = SearchCatalog.class.getSimpleName();
+    public String TAG = SearchCatalog.class.getName();
 
     // the org on which the searches will be made
     public Organisation selectedOrganization = null;
@@ -112,7 +112,7 @@ public class SearchCatalog {
     private SearchCatalog() {
     }
 
-    private HttpConnection conn() {
+    private static HttpConnection conn() {
         return GlobalConfigs.gatewayConnection();
     }
 
@@ -130,43 +130,36 @@ public class SearchCatalog {
         this.searchFormat = searchFormat;
         
         ArrayList<RecordInfo> resultsRecordInfo = new ArrayList<RecordInfo>();
-        HashMap complexParm = new HashMap<String, Integer>();
 
-        try {
-            //KCXXX I'm not too sure about this depth option
-            if (this.selectedOrganization != null) {
-                if (this.selectedOrganization.id != null)
-                    complexParm.put("org_unit", this.selectedOrganization.id);
-                if (this.selectedOrganization.level != null)
-                    complexParm.put("depth", this.selectedOrganization.level);
-            }
-            complexParm.put("limit", searchLimit);
-            complexParm.put("offset", offset);
-            if (searchClass != null) complexParm.put("default_class", searchClass);
-//            complexParm.put("offset",0);
-//            complexParm.put("visibility_limit", 3000);
-        } catch (Exception e) {
-            Log.d(TAG, "Exception in JSON " + e.getMessage());
+        HashMap complexParm = new HashMap<String, Integer>();
+        if (this.selectedOrganization != null) {
+            if (this.selectedOrganization.id != null)
+                complexParm.put("org_unit", this.selectedOrganization.id);
+            // I'm not too sure about this depth option
+            if (this.selectedOrganization.level != null)
+                complexParm.put("depth", this.selectedOrganization.level);
         }
+        complexParm.put("limit", searchLimit);
+        complexParm.put("offset", offset);
+        if (searchClass != null) complexParm.put("default_class", searchClass);
 
         String queryString = searchText;
         if (!searchFormat.isEmpty())
             queryString += " search_format(" + searchFormat + ")";
 
-        // do request and check for connectivity
+        long start_ms = System.currentTimeMillis();
+        long now_ms = start_ms;
+
+        // do request
         Object resp = Utils.doRequest(conn(), SERVICE, METHOD_MULTICLASS_QUERY,
                 new Object[] { complexParm, queryString, 1 });
-
-        ArrayList<String> ids = new ArrayList<String>();
-
         Log.d(TAG, "Sync Response: " + resp);
+        now_ms = logElapsedTime(TAG, now_ms, "search");
         if (resp == null)
             return resultsRecordInfo; // search failed or server crashed
 
         Map<String, ?> response = (Map<String, ?>) resp;
 
-        Log.d(TAG, " ids : " + response.get("ids") + " ");
-
         List<List<String>> result_ids;
         result_ids = (List<List<String>>) response.get("ids");
         Log.d(TAG, "length:"+result_ids.size());
@@ -174,6 +167,7 @@ public class SearchCatalog {
         // sometimes count is an int ("count":0) and sometimes string ("count":"1103")
         visible = Integer.parseInt(response.get("count").toString());
 
+        ArrayList<String> ids = new ArrayList<String>();
         for (int i = 0; i < result_ids.size(); i++) {
             ids.add(result_ids.get(i).get(0));
         }
@@ -184,15 +178,21 @@ public class SearchCatalog {
             Integer record_id = Integer.parseInt(ids.get(i));
 
             RecordInfo record = new RecordInfo(getItemShortInfo(record_id));
+            now_ms = logElapsedTime(TAG, now_ms, "search.getItemShortInfo");
             resultsRecordInfo.add(record);
 
             AccountAccess ac = AccountAccess.getAccountAccess();
             record.search_format = ac.fetchFormat(record_id.toString());
+            now_ms = logElapsedTime(TAG, now_ms, "search.fetchFormat");
 
+            // todo This takes 30% of the total search time but is not required for SearchCatalogListView
+            // it is needed only for BasicDetailsFragment, but that would cause it to be run on the main thread
+            // and that fails, so continue to do it here for now
             record.copyCountListInfo = getCopyCount(record_id, this.selectedOrganization.id);
             List<List<Object>> list = (List<List<Object>>) getLocationCount(
                     record_id, this.selectedOrganization.id,
                     this.selectedOrganization.level);
+            now_ms = logElapsedTime(TAG, now_ms, "search.getLocationCount");
             if (list != null)
                 for (int j = 0; j < list.size(); j++) {
                     CopyInformation copyInfo = new CopyInformation(list.get(j));
@@ -204,10 +204,17 @@ public class SearchCatalog {
                     + " Pubdate:" + record.pubdate
                     + " Publisher:" + record.publisher);
         }
+        logElapsedTime(TAG, start_ms, "search.total");
 
         return resultsRecordInfo;
     }
 
+    public long logElapsedTime(String tag, long start_ms, String s) {
+        long now_ms = System.currentTimeMillis();
+        Log.d(tag, s + ": " + (now_ms - start_ms) + "ms");
+        return now_ms;
+    }
+
     /**
      * Gets the item short info.
      * 
@@ -256,7 +263,6 @@ public class SearchCatalog {
                 METHOD_COPY_LOCATION_COUNTS, new Object[] {
                         recordID, orgID, orgDepth });
         return list;
-
     }
 
     public ArrayList<RecordInfo> getRecordsInfo(ArrayList<Integer> ids) {
@@ -283,7 +289,7 @@ public class SearchCatalog {
         this.selectedOrganization = org;
     }
 
-    public ArrayList<CopyCountInformation> getCopyCount(Integer recordID,
+    public static ArrayList<CopyCountInformation> getCopyCount(Integer recordID,
             Integer orgID) {
 
         List<?> list = (List<?>) Utils.doRequestSimple(conn(), SERVICE,
@@ -301,4 +307,11 @@ public class SearchCatalog {
 
         return copyInfoList;
     }
+
+    // should be punted to volley
+    public static void ensureCopyCount(RecordInfo record, Integer org_id) {
+        if (record.copyCountListInfo == null) {
+            record.copyCountListInfo = getCopyCount(record.doc_id, org_id);
+        }
+    }
 }
index d4b7121..c97d331 100644 (file)
@@ -61,6 +61,7 @@ public class BasicDetailsFragment extends Fragment {
     private final String TAG = BasicDetailsFragment.class.getName();
 
     private RecordInfo record;
+    private Integer orgId;
     private Integer position;
     private Integer total;
 
@@ -79,13 +80,10 @@ public class BasicDetailsFragment extends Fragment {
     private TextView copyCountTextView;
 
     private Button placeHoldButton;
-
     private Button addToBookbagButton;
 
     private LinearLayout showMore;
 
-    private SearchCatalog search = null;
-
     private GlobalConfigs globalConfigs;
 
     private ProgressDialog progressDialog;
@@ -103,32 +101,25 @@ public class BasicDetailsFragment extends Fragment {
     private int list_size = 3;
 
     public static BasicDetailsFragment newInstance(RecordInfo record,
-            Integer position, Integer total) {
+            Integer position, Integer total, Integer orgID) {
         BasicDetailsFragment fragment = new BasicDetailsFragment();
-        fragment.setDetails(record, position, total);
+        fragment.orgId = orgID;
+        fragment.record = record;
+        fragment.position = position;
+        fragment.total = total;
 
         return fragment;
     }
 
-    private void setDetails(RecordInfo record, Integer position,
-            Integer total) {
-
-        this.record = record;
-        this.position = position;
-        this.total = total;
-    }
-
     public BasicDetailsFragment() {
-
-        search = SearchCatalog.getInstance();
     }
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (savedInstanceState != null) {
-            record = (RecordInfo) savedInstanceState
-                    .getSerializable("recordInfo");
+            record = (RecordInfo) savedInstanceState.getSerializable("recordInfo");
+            orgId = savedInstanceState.getInt("orgId");
             this.position = savedInstanceState.getInt("position");
             this.total = savedInstanceState.getInt("total");
         }
@@ -165,8 +156,7 @@ public class BasicDetailsFragment extends Fragment {
 
             @Override
             public void onClick(View v) {
-                Intent intent = new Intent(getActivity()
-                        .getApplicationContext(), PlaceHold.class);
+                Intent intent = new Intent(getActivity().getApplicationContext(), PlaceHold.class);
                 intent.putExtra("recordInfo", record);
                 startActivity(intent);
             }
@@ -275,17 +265,15 @@ public class BasicDetailsFragment extends Fragment {
 
         isbnTextView.setText(record.isbn);
 
-        int current_org = 0;
-        if (search != null)
-            current_org = search.selectedOrganization.id;
+        // todo this is not working because we are on the main thread
+        //SearchCatalog.ensureCopyCount(record, orgId);
 
         Log.d(TAG, "Size " + record.copyCountListInfo.size());
-
         for (int i = 0; i < record.copyCountListInfo.size(); i++) {
-            Log.d(TAG, current_org + " "
-                    + record.copyCountListInfo.get(i).org_id + " "
-                    + record.copyCountListInfo.get(i).count);
-            if (record.copyCountListInfo.get(i).org_id == current_org) {
+//            Log.d(TAG, orgId + " "
+//                    + record.copyCountListInfo.get(i).org_id + " "
+//                    + record.copyCountListInfo.get(i).count);
+            if (record.copyCountListInfo.get(i).org_id == orgId) {
                 int total = record.copyCountListInfo.get(i).count;
                 int available = record.copyCountListInfo.get(i).available;
                 String totalCopies = getResources().getQuantityString(R.plurals.number_of_copies, total, total);
@@ -303,9 +291,9 @@ public class BasicDetailsFragment extends Fragment {
             @Override
             public void onClick(View v) {
                 // show more details
-                Intent intent = new Intent(getActivity()
-                        .getApplicationContext(), MoreCopyInformation.class);
+                Intent intent = new Intent(getActivity().getApplicationContext(), MoreCopyInformation.class);
                 intent.putExtra("recordInfo", record);
+                intent.putExtra("orgId", orgId);
                 startActivity(intent);
             }
         });
@@ -324,6 +312,7 @@ public class BasicDetailsFragment extends Fragment {
     @Override
     public void onSaveInstanceState(Bundle outState) {
         outState.putSerializable("recordInfo", record);
+        outState.putInt("orgId", this.orgId);
         outState.putInt("position", this.position);
         outState.putInt("total", this.total);
         super.onSaveInstanceState(outState);
index ccb30ef..4add957 100644 (file)
@@ -20,7 +20,11 @@ foreach my $line (@lines) {
     if ($line =~ /GatewayRequest: ([^:]+):(.+)/) {
         my($key,$val) = ($1,$2);
         if ($key eq 'result') {
-            my $obj = decode_json($val);
+            my $obj;
+            eval '$obj = decode_json($val);';
+            if ($@) {
+                $obj = '*** '.$val;
+            }
             print "$key -> ", Dumper($obj);
         } else {
             print "${key}:${val}\n";
index 64d861f..be86432 100644 (file)
@@ -24,7 +24,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
 public class GatewayRequest extends HttpRequest {
 
     private boolean readComplete;
-    private String TAG = GatewayRequest.class.getSimpleName();
+    private String TAG = GatewayRequest.class.getName();
 
     public GatewayRequest(HttpConnection conn, String service, Method method) {
         super(conn, service, method);