Use DictCursor for stat query
This commit is contained in:
parent
5458bedd09
commit
a9f1220bca
1 changed files with 3 additions and 7 deletions
10
backend.py
10
backend.py
|
@ -4,6 +4,7 @@
|
|||
import os
|
||||
import falcon
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
import json
|
||||
|
||||
|
||||
|
@ -29,19 +30,14 @@ def standard_headers(resp):
|
|||
class StatsResource(object):
|
||||
def on_get(self, req, resp):
|
||||
db = db_connect()
|
||||
cur = db.cursor()
|
||||
cur = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
||||
cur.execute("SELECT count(*) as events_count, max(createdate) as last_created, max(lastupdate) as last_updated from events;")
|
||||
stat = cur.fetchone()
|
||||
cur.close()
|
||||
db.close()
|
||||
|
||||
standard_headers(resp)
|
||||
body = {
|
||||
"events_count": stat[0],
|
||||
"last_created": stat[1],
|
||||
"last_updated": stat[2]
|
||||
}
|
||||
resp.body = json.dumps(body)
|
||||
resp.body = json.dumps(dict(stat))
|
||||
resp.status = falcon.HTTP_200
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue