Database Reference
In-Depth Information
raise ValueError('id entry missing from
argument')
device = models.Device.get_by_device_id(device_id)
if not device:
raise KeyError('id %s not valid' % device_id)
# Extract the UTC day from the timestamp in the
record.
ts = int(arg.get('ts', 0.0))
day = datetime.utcfromtimestamp(ts)
# Save the record using the streaming API.
result = bigquery.tabledata().insertAll(
projectId=PROJECT_ID,
datasetId='logs',
tableId='device_' + day.strftime("%Y%m%d"),
body=dict(rows=[
# Generate a suitable insert id.
{'insertId': ('%s:%d' % (device_id, ts)),
'json': arg}])).execute()
if 'error' in result or result.get('insertErrors'):
logging.error('Insert failed: ' +
unicode(result))
return {}
app = webapp2.WSGIApplication([
# ELIDED
webapp2.Route(r'/command/register',
handler=RegisterHandler, name='register'),
webapp2.Route(r'/command/record',
handler=RecordHandler, name='record'),
], debug=True)
The registration handler looks up the supplied registration ID in Datastore,
and if it finds it, the handler adds a new device record with the data supplied
by the client. Chapter 11 looks at this device registration information in
more detail and explains how to pull it into BigQuery for analytics. Here you
focus on the handler, which handles the logs sent by the clients, particularly
because this method directly integrates with BigQuery via the streaming
insert API.
Search WWH ::




Custom Search