<commit_before>"""Handle user related queries."""
import logging

from google.appengine.api import namespace_manager
from google.appengine.api import users
from google.appengine.ext import ndb

import flask
import pusher

from common import creds, public_creds, utils


# pylint: disable=invalid-name
blueprint = flask.Blueprint('user', __name__)


class Person(ndb.Model):
  # key_name is userid
  email = ndb.StringProperty(required=False)


def get_user_from_namespace():
  namespace = namespace_manager.get_namespace()
  assert namespace != ''
  return namespace


def get_user():
  """Return the user_id for the current logged in user."""
  user = users.get_current_user()
  assert user is not None
  assert user.email() is not None

  person = Person.get_or_insert(
      user.user_id(), email=user.email())
  return person.key.id()


@blueprint.route('/', methods=['GET'])
def get_user_request():
  """Return the user's json."""
  user_id = get_user()
  person = Person.get_by_id(user_id)
  values = person.to_dict()
  values['id'] = user_id
  return flask.jsonify(**values)


def send_event(**kwargs):
  """Post events back to the pi."""
  batch = flask.g.get('user_events', None)
  if batch is None:
    batch = []
    setattr(flask.g, 'user_events', batch)
  batch.append(kwargs)


def push_events():
  """Push all the events that have been caused by this request."""
  events = flask.g.get('user_events', None)
  setattr(flask.g, 'user_events', None)
  if events is None:
    return

  logging.info('Sending %d events to user', len(events))

  # Now figure out what channel to post these to.
  # Can't use user.get_user as we might not be in
  # a user's request (might be a device update
  # from the proxy).  So we use the namespace
  # instead.  Horrid.
  user_id = get_user_from_namespace()
  channel_id = 'private-%s' % user_id
  logging.info('Sending to channel %s', channel_id)

  # Push them to pusher, as we've migrated
  # the UI to that.
  pusher_client = pusher.Pusher(
      app_id=creds.pusher_app_id,
      key=public_creds.pusher_key,
      secret=creds.pusher_secret,
      encoder=flask.json.JSONEncoder)

  for encoded_events in utils.limit_json_batch(events):
    pusher_client[channel_id].trigger('events', encoded_events)


# UI calls /api/user/channel_auth with its
# (id & secret) auth and channel name == private-user_id.
# pusher client library makes a callback
# to this end point to check the client
# can use said channel.
@blueprint.route('/channel_auth', methods=['POST'])
def pusher_client_auth_callback():<commit_msg>Make batches smaller for pusher<commit_after>"""Handle user related queries."""
import logging

from google.appengine.api import namespace_manager
from google.appengine.api import users
from google.appengine.ext import ndb

import flask
import pusher

from common import creds, public_creds, utils


# pylint: disable=invalid-name
blueprint = flask.Blueprint('user', __name__)


class Person(ndb.Model):
  # key_name is userid
  email = ndb.StringProperty(required=False)


def get_user_from_namespace():
  namespace = namespace_manager.get_namespace()
  assert namespace != ''
  return namespace


def get_user():
  """Return the user_id for the current logged in user."""
  user = users.get_current_user()
  assert user is not None
  assert user.email() is not None

  person = Person.get_or_insert(
      user.user_id(), email=user.email())
  return person.key.id()


@blueprint.route('/', methods=['GET'])
def get_user_request():
  """Return the user's json."""
  user_id = get_user()
  person = Person.get_by_id(user_id)
  values = person.to_dict()
  values['id'] = user_id
  return flask.jsonify(**values)


def send_event(**kwargs):
  """Post events back to the pi."""
  batch = flask.g.get('user_events', None)
  if batch is None:
    batch = []
    setattr(flask.g, 'user_events', batch)
  batch.append(kwargs)


def push_events():
  """Push all the events that have been caused by this request."""
  events = flask.g.get('user_events', None)
  setattr(flask.g, 'user_events', None)
  if events is None:
    return

  logging.info('Sending %d events to user', len(events))

  # Now figure out what channel to post these to.
  # Can't use user.get_user as we might not be in
  # a user's request (might be a device update
  # from the proxy).  So we use the namespace
  # instead.  Horrid.
  user_id = get_user_from_namespace()
  channel_id = 'private-%s' % user_id
  logging.info('Sending to channel %s', channel_id)

  # Push them to pusher, as we've migrated
  # the UI to that.
  pusher_client = pusher.Pusher(
      app_id=creds.pusher_app_id,
      key=public_creds.pusher_key,
      secret=creds.pusher_secret,
      encoder=flask.json.JSONEncoder)

  for encoded_events in utils.limit_json_batch(events, max_size=10000):
    pusher_client[channel_id].trigger('events', encoded_events)


# UI calls /api/user/channel_auth with its
# (id & secret) auth and channel name == private-user_id.
# pusher client library makes a callback
# to this end point to check the client
# can use said channel.
@blueprint.route('/channel_auth', methods=['POST'])
def pusher_client_auth_callback():
{'fraction_non_alphanumeric': 0.11365399534522885, 'fraction_numerical': 0.000969743987587277, 'mean_word_length': 4.230223123732252, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1511246', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1653, 'n_words': 500}