3

I have a Python-script gathering some metrics and saving them to RethinkDB. I have also written a small Flask-application to display the data on a dashboard.

Now I need to run a query to find all rows in a table newer than 1 hour. This is what I got so far:

tzinfo = pytz.timezone('Europe/Oslo')
start_time = tzinfo.localize(datetime.now() - timedelta(hours=1))
r.table('metrics').filter( lambda m:
    m.during(start_time, r.now())
    ).run(connection)

When I try to visit the page I get this error message:

ReqlRuntimeError: Not a TIME pseudotype: `{
"listeners":    "6469",
"time": {
    "$reql_type$":  "TIME",
    "epoch_time":   1447581600,
    "timezone": "+01:00"
    }
}` in:
    r.table('metrics').filter(lambda var_1:
        var_1.during(r.iso8601('2015-11-18T12:06:20.252415+01:00'), r.now()))

I googled a bit and found this thread which seems to be a similar problem: https://github.com/rethinkdb/rethinkdb/issues/4827, so I revisited how I add new rows to the database as well to see if that was the issue:

def _fix_tz(timestamp):
    tzinfo = pytz.timezone('Europe/Oslo')        
    dt = datetime.strptime(timestamp[:-10], '%Y-%m-%dT%H:%M:%S')                 
    return tzinfo.localize(dt) 
...
for row in res:                                                              
    ... remove some data, manipulate some other data ...                                                       
    r.db('metrics',                           
         {'time': _fix_tz(row['_time']),
          ...                              
          ).run(connection)

'_time' retrieved by my data collection-script contains some garbage I remove, and then create a datetime-object. As far as I can understand from the RethinkDB documentation I should be able to insert these directly, and if I use "data explorer" in RethinkDB's Admin Panel my rows look like this:

{
    ...
    "time": Sun Oct 25 2015 00:00:00 GMT+02:00
}

Update: I did another test and created a small script to insert data and then retrieve it

import rethinkdb as r

conn = r.connect(host='localhost', port=28015, db='test')

r.table('timetests').insert({
    'time': r.now(),
    'message': 'foo!'
    }).run(conn)

r.table('timetests').insert({
    'time': r.now(),
    'message': 'bar!'
    }).run(conn)

cursor = r.table('timetests').filter(
    lambda t: t.during(r.now() - 3600, r.now())
    ).run(conn)

I still get the same error message:

$ python timestamps.py 
Traceback (most recent call last):
  File "timestamps.py", line 21, in <module>
    ).run(conn)
  File "/Users/tsg/.virtualenv/p4-datacollector/lib/python2.7/site-packages/rethinkdb/ast.py", line 118, in run
    return c._start(self, **global_optargs)
  File "/Users/tsg/.virtualenv/p4-datacollector/lib/python2.7/site-packages/rethinkdb/net.py", line 595, in _start
    return self._instance.run_query(q, global_optargs.get('noreply', False))
  File "/Users/tsg/.virtualenv/p4-datacollector/lib/python2.7/site-packages/rethinkdb/net.py", line 457, in run_query
    raise res.make_error(query)
rethinkdb.errors.ReqlQueryLogicError: Not a TIME pseudotype: `{
    "id":   "5440a912-c80a-42dd-9d27-7ecd6f7187ad",
    "message":  "bar!",
    "time": {
        "$reql_type$":  "TIME",
        "epoch_time":   1447929586.899,
        "timezone": "+00:00"
    }
}` in:
r.table('timetests').filter(lambda var_1: var_1.during((r.now() - r.expr(3600)), r.now()))
liasis
  • 96
  • 8

1 Answers1

4

I finally figured it out. The error is in the lambda-expression. You need to use .during() on a specific field. If not the query will try to wrestle the whole row/document into a timestamp

This code works:

cursor = r.table('timetests').filter(
    lambda t: t['time'].during(r.now() - 3600, r.now())
    ).run(conn)
liasis
  • 96
  • 8