Repository: internetarchive/openlibrary

Issue:
/admin/imports fails to load locally
### Description
Follow up on conversation between myself, @cdrini and @hornc 
https://internetarchive.slack.com/archives/C0ETZV72L/p1565797192464100

When visiting http://localhost:8080/admin/imports I am unable to render the page.

debug=true shows the following stack trace:
```
Python | /usr/local/lib/python2.7/dist-packages/DBUtils/SteadyDB.py in tough_method, line 604
-- | --
GET http://localhost:8080/admin/imports
````

Questions:
1) Does https://openlibrary.org/admin/imports work?
2) Is the page still relevant
3) How can we get the page to render locally?

### Evidence/Screenshot
| On Local | On Prod |
| ---- | ---- |
| ![image](https://user-images.githubusercontent.com/6251786/63545689-036d3b80-c4f6-11e9-8a8f-b0205bda2b55.png) | ![image](https://user-images.githubusercontent.com/905545/63562591-50164c80-c5b2-11e9-9fff-e61b5b21e2c4.png) |

### Relevant url
http://localhost:8080/admin/imports ; https://openlibrary.org/admin/imports

### Stakeholders
@cdrini @hornc 


Discussion:
Is it possible we just need to create a database table import_items ?

@hornc could you add a picture of what this page looks like on prod?
@cdrini 
![image](https://user-images.githubusercontent.com/905545/63562591-50164c80-c5b2-11e9-9fff-e61b5b21e2c4.png)

Thanks! Added to the desc.
What else needs to be done considering #2326 is merged?
I still can not visit this page locally seeing:
```
/openlibrary/openlibrary/templates/admin/imports.html: error in processing template: ProgrammingError: relation "import_item" does not exist LINE 1: SELECT count(*) as count FROM import_item WHERE status='pend... ^ (falling back to default template)
```
I am not the right assignee for this bug...
@cdrini I can replicate on Python 3.  This looks like the database SELECT method is incorrect.
https://github.com/internetarchive/openlibrary/blob/master/openlibrary/core/imports.py#L96-L105

Resolving patch:
diff --git a/openlibrary/core/imports.py b/openlibrary/core/imports.py
--- a/openlibrary/core/imports.py
+++ b/openlibrary/core/imports.py
@@ -6,6 +6,8 @@
 import time
 import web
 
+from psycopg2.errors import UndefinedTable
+
 from . import db
 
 logger = logging.getLogger("openlibrary.imports")
@@ -88,20 +90,25 @@ class Stats:
     def get_imports_per_hour(self):
         """Returns the number imports happened in past one hour duration.
         """
-        result = db.query(
-            "SELECT count(*) as count FROM import_item" +
-            " WHERE import_time > CURRENT_TIMESTAMP - interval '1' hour")
+        try:
+            result = db.query(
+                "SELECT count(*) as count FROM import_item" +
+                " WHERE import_time > CURRENT_TIMESTAMP - interval '1' hour")
+        except UndefinedTable:
+            logger.exception("Database table import_item may not exist on localhost")
+            return 0
         return result[0].count
 
     def get_count(self, status=None):
-        if status:
-            where = "status=$status"
-        else:
-            where = "1=1"
-        rows = db.select("import_item",
-            what="count(*) as count",
-            where=where,
-            vars=locals())
+        where = "status=$status" if status else "1=1"
+        try:
+            rows = db.select("import_item",
+                what="count(*) as count",
+                where=where,
+                vars=locals())
+        except UndefinedTable:
+            logger.exception("Database table import_item may not exist on localhost")
+            return 0
         return rows[0].count
 
     def get_count_by_status(self, date=None):
@@ -109,25 +116,33 @@ def get_count_by_status(self, date=None):
         return dict([(row.status, row.count) for row in rows])
 
     def get_count_by_date_status(self, ndays=10):
-        result = db.query(
-            "SELECT added_time::date as date, status, count(*)" +
-            " FROM import_item " +
-            " WHERE added_time > current_date - interval '$ndays' day"
-            " GROUP BY 1, 2" +
-            " ORDER BY 1 desc",
-            vars=locals())
+        try: 
+            result = db.query(
+                "SELECT added_time::date as date, status, count(*)" +
+                " FROM import_item " +
+                " WHERE added_time > current_date - interval '$ndays' day"
+                " GROUP BY 1, 2" +
+                " ORDER BY 1 desc",
+                vars=locals())
+        except UndefinedTable:
+            logger.exception("Database table import_item may not exist on localhost")
+            return []
         d = defaultdict(dict)
         for row in result:
             d[row.date][row.status] = row.count
         return sorted(d.items(), reverse=True)
 
     def get_books_imported_per_day(self):
-        rows = db.query(
-            "SELECT import_time::date as date, count(*) as count"
-            " FROM import_item" +
-            " WHERE status='created'"
-            " GROUP BY 1" +
-            " ORDER BY 1")
+        try:
+            rows = db.query(
+                "SELECT import_time::date as date, count(*) as count"
+                " FROM import_item" +
+                " WHERE status='created'"
+                " GROUP BY 1" +
+                " ORDER BY 1")
+        except UndefinedTable:
+            logger.exception("Database table import_item may not exist on localhost")
+            return []
         return [[self.date2millis(row.date), row.count] for row in rows]
 
     def date2millis(self, date):
@@ -136,15 +151,16 @@ def date2millis(self, date):
     def get_items(self, date=None, order=None, limit=None):
         """Returns all rows with given added date.
         """
-        if date:
-            where = "added_time::date = $date"
-        else:
-            where = "1 = 1"
-        return db.select("import_item",
-            where=where,
-            order=order,
-            limit=limit,
-            vars=locals())
+        where = "added_time::date = $date" if date else "1 = 1"
+        try:
+            return db.select("import_item",
+                where=where,
+                order=order,
+                limit=limit,
+                vars=locals())
+        except UndefinedTable:
+            logger.exception("Database table import_item may not exist on localhost")
+            return []
 
     def get_items_summary(self, date):
         """Returns all rows with given added date.


Test patch: