import json
import itertools
import logging

__author__ = 'patrickwalsh'

from flask import Flask
from redis import Redis

app = Flask(__name__)
logger = logging.getLogger(__name__)

redis = Redis()


IID_INDEX = 'index'

@app.route('/intersections')
def get_all_intersections():
    try:
        nodes = redis.smembers(IID_INDEX)

        feed = itertools.imap(redis.hgetall, nodes)
        dehydrated = itertools.imap(dehydrate, feed)
        return json.dumps(dict(
            objects=list(dehydrated)
        ))
    except Exception as e:
        logger.exception(e)


def dehydrate(node):
    keys = ['sid', 'id', 'updated_at', 'lat', 'lon', 'osm_id', 'min_cong', 'max_cong', 'avg_cong']
    data = {key: node.get(key, None) for key in keys}

    return data


if __name__ == '__main__':
    app.run()
import json
import itertools
import logging

__author__ = 'patrickwalsh'

from flask import Flask
from redis import Redis

app = Flask(__name__)
logger = logging.getLogger(__name__)

redis = Redis()


IID_INDEX = 'index'

@app.route('/intersections')
def get_all_intersections():
    try:
        # nodes = redis.smembers(IID_INDEX)
        # all nodes are namespaced with iid
        nodes = redis.keys('iid:*')
        feed = itertools.imap(redis.hgetall, nodes)
        dehydrated = itertools.imap(dehydrate, feed)
        return json.dumps(dict(
            objects=list(dehydrated)
        ))
    except Exception as e:
        logger.exception(e)


def dehydrate(node):
    keys = ['sid', 'id', 'updated_at', 'lat', 'lon', 'osm_id', 'min_cong', 'max_cong', 'avg_cong']
    data = {key: node.get(key, None) for key in keys}

    return data


if __name__ == '__main__':
    app.run()
Update fetching nodes to manually get all keys with iid prefix instead of using an index
Update fetching nodes to manually get all keys with iid prefix instead of using an index

  import json
  import itertools
  import logging
  
  __author__ = 'patrickwalsh'
  
  from flask import Flask
  from redis import Redis
  
  app = Flask(__name__)
  logger = logging.getLogger(__name__)
  
  redis = Redis()
  
  
  IID_INDEX = 'index'
  
  @app.route('/intersections')
  def get_all_intersections():
      try:
-         nodes = redis.smembers(IID_INDEX)
+         # nodes = redis.smembers(IID_INDEX)
- 
+         # all nodes are namespaced with iid
+         nodes = redis.keys('iid:*')
          feed = itertools.imap(redis.hgetall, nodes)
          dehydrated = itertools.imap(dehydrate, feed)
          return json.dumps(dict(
              objects=list(dehydrated)
          ))
      except Exception as e:
          logger.exception(e)
  
  
  def dehydrate(node):
      keys = ['sid', 'id', 'updated_at', 'lat', 'lon', 'osm_id', 'min_cong', 'max_cong', 'avg_cong']
      data = {key: node.get(key, None) for key in keys}
  
      return data
  
  
  if __name__ == '__main__':
      app.run()
Update fetching nodes to manually get all keys with iid prefix instead of using an index
## Code Before:
import json
import itertools
import logging

__author__ = 'patrickwalsh'

from flask import Flask
from redis import Redis

app = Flask(__name__)
logger = logging.getLogger(__name__)

redis = Redis()


IID_INDEX = 'index'

@app.route('/intersections')
def get_all_intersections():
    try:
        nodes = redis.smembers(IID_INDEX)

        feed = itertools.imap(redis.hgetall, nodes)
        dehydrated = itertools.imap(dehydrate, feed)
        return json.dumps(dict(
            objects=list(dehydrated)
        ))
    except Exception as e:
        logger.exception(e)


def dehydrate(node):
    keys = ['sid', 'id', 'updated_at', 'lat', 'lon', 'osm_id', 'min_cong', 'max_cong', 'avg_cong']
    data = {key: node.get(key, None) for key in keys}

    return data


if __name__ == '__main__':
    app.run()
## Instruction:
Update fetching nodes to manually get all keys with iid prefix instead of using an index
## Code After:
import json
import itertools
import logging

__author__ = 'patrickwalsh'

from flask import Flask
from redis import Redis

app = Flask(__name__)
logger = logging.getLogger(__name__)

redis = Redis()


IID_INDEX = 'index'

@app.route('/intersections')
def get_all_intersections():
    try:
        # nodes = redis.smembers(IID_INDEX)
        # all nodes are namespaced with iid
        nodes = redis.keys('iid:*')
        feed = itertools.imap(redis.hgetall, nodes)
        dehydrated = itertools.imap(dehydrate, feed)
        return json.dumps(dict(
            objects=list(dehydrated)
        ))
    except Exception as e:
        logger.exception(e)


def dehydrate(node):
    keys = ['sid', 'id', 'updated_at', 'lat', 'lon', 'osm_id', 'min_cong', 'max_cong', 'avg_cong']
    data = {key: node.get(key, None) for key in keys}

    return data


if __name__ == '__main__':
    app.run()