@app.route('/user/<username>/countries/')
@app.route('/countries/')
@auth.login_required
def countries(username=None):
  user_db = auth.current_user_db()
  if username and user_db.username != username:
    if not user_db.admin:
      return flask.abort(404)
    user_db = model.User.get_by('username', username)
    if not user_db:
      return flask.abort(404)

  event_dbs, next_cursor = user_db.get_event_dbs(
      order='timestamp,accuracy,created', limit=-1
    )

  country_dbs_ = {}
  country = {
    'country_code': '',
    'seconds': 0,
    'current': False,
  }
  last_event_db = None

  for event_db in event_dbs:
    if event_db.layover:
      continue
    if last_event_db:
      if last_event_db.country_code not in country_dbs_:
        country_dbs_[last_event_db.country_code] = copy.copy(country)
        country_dbs_[last_event_db.country_code]['country_code'] = last_event_db.country_code
      country_dbs_[last_event_db.country_code]['seconds'] += (event_db.timestamp - last_event_db.timestamp).total_seconds()
    last_event_db = event_db

  # current country
  if last_event_db:
    if last_event_db.country_code not in country_dbs_:
      country_dbs_[last_event_db.country_code] = copy.copy(country)
      country_dbs_[last_event_db.country_code]['country_code'] = last_event_db.country_code
    country_dbs_[last_event_db.country_code]['seconds'] += (datetime.utcnow() - last_event_db.timestamp).total_seconds()
    country_dbs_[last_event_db.country_code]['current'] = True


  country_dbs = []
  total = 0

  for country_code in country_dbs_:
    country = country_dbs_[country_code]
    country['seconds'] = int(country['seconds'])
    country['hours'] = int(country['seconds'] / 60 / 60)
    country['days'] = int(round(country['seconds'] / 60 / 60 / 24))
    country['months'] = country['days'] / 30.0
    country['years'] = country['days'] / 365.0
    country['country'] = iso.ISO_3166[country['country_code']]
    country_dbs.append(country)
    total += country['seconds']

  country_dbs = sorted(country_dbs, key=lambda c: c['seconds'], reverse=True)
  return flask.render_template(
      'event/stats.html',
      html_class='stats countries',
      title=_('My Stats'),
      event_dbs=event_dbs,
      country_dbs=country_dbs,
      total=total,
      next_url=util.generate_next_url(next_cursor),
      user_db=user_db,
    )
dataset/ETHPy150Open lipis/life-line/main/control/event.py/countries/original