<commit_before># Copyright 2015 Google Inc. All Rights Reserved.

"""Module for pushing analytics data to BigQuery."""

import datetime
import logging
import os
import sys
import time

sys.path.append(os.path.join(os.path.dirname(__file__), 'third_party'))

from analytics_enums import EventType, LogField
import apiauth
import constants

from google.appengine.api import app_identity


class Analytics(object):
  """Class used to encapsulate analytics logic. Used interally in the module.

  All data is streamed to BigQuery.

  """

  def __init__(self):
    self.bigquery_table = constants.BIGQUERY_TABLE

    if constants.IS_DEV_SERVER:
      self.bigquery_dataset = constants.BIGQUERY_DATASET_LOCAL
    else:
      self.bigquery_dataset = constants.BIGQUERY_DATASET_PROD

    # Attempt to initialize a connection to BigQuery.
    self.bigquery = self._build_bigquery_object()
    if self.bigquery is None:
      logging.warning('Unable to build BigQuery API object. Logging disabled.')

  def _build_bigquery_object(self):
    return apiauth.build(scope=constants.BIGQUERY_URL,
                         service_name='bigquery',
                         version='v2')

  def _timestamp_from_millis(self, time_ms):
    """Convert back to seconds as float and then to ISO format."""
    return datetime.datetime.fromtimestamp(float(time_ms)/1000.).isoformat()

  def report_event(self, event_type, room_id=None, time_ms=None,
                   client_time_ms=None, host=None, flow_id=None):
    """Report an event to BigQuery.

    Args:
      event_type: One of analytics.EventType.
      room_id: Room ID related to the given event type.
      time_ms: Time that the event occurred on the server. Will be automatically
               populated if not given explicitly.
      client_time_ms: Time that an event occurred on the client, if the event
                      originated on the client.
      host: Hostname this is being logged on.
    """
    # Be forgiving. If an event is a string or is an unknown number we
    # still log it but log it as the string value.
    event_type_name = EventType.Name.get(event_type, str(event_type))
    event = {LogField.EVENT_TYPE: event_type_name}

    if room_id is not None:
      event[LogField.ROOM_ID] = room_id

    if flow_id is not None:
      event[LogField.FLOW_ID] = flow_id

    if client_time_ms is not None:
      event[LogField.CLIENT_TIMESTAMP] = self._timestamp_from_millis(
          client_time_ms)<commit_msg>update Args on report_event function<commit_after># Copyright 2015 Google Inc. All Rights Reserved.

"""Module for pushing analytics data to BigQuery."""

import datetime
import logging
import os
import sys
import time

sys.path.append(os.path.join(os.path.dirname(__file__), 'third_party'))

from analytics_enums import EventType, LogField
import apiauth
import constants

from google.appengine.api import app_identity


class Analytics(object):
  """Class used to encapsulate analytics logic. Used interally in the module.

  All data is streamed to BigQuery.

  """

  def __init__(self):
    self.bigquery_table = constants.BIGQUERY_TABLE

    if constants.IS_DEV_SERVER:
      self.bigquery_dataset = constants.BIGQUERY_DATASET_LOCAL
    else:
      self.bigquery_dataset = constants.BIGQUERY_DATASET_PROD

    # Attempt to initialize a connection to BigQuery.
    self.bigquery = self._build_bigquery_object()
    if self.bigquery is None:
      logging.warning('Unable to build BigQuery API object. Logging disabled.')

  def _build_bigquery_object(self):
    return apiauth.build(scope=constants.BIGQUERY_URL,
                         service_name='bigquery',
                         version='v2')

  def _timestamp_from_millis(self, time_ms):
    """Convert back to seconds as float and then to ISO format."""
    return datetime.datetime.fromtimestamp(float(time_ms)/1000.).isoformat()

  def report_event(self, event_type, room_id=None, time_ms=None,
                   client_time_ms=None, host=None, flow_id=None):
    """Report an event to BigQuery.

    Args:
      event_type: One of analytics.EventType.
      room_id: Room ID related to the given event type.
      time_ms: Time that the event occurred on the server. Will be automatically
               populated if not given explicitly.
      client_time_ms: Time that an event occurred on the client, if the event
                      originated on the client.
      host: Hostname this is being logged on.
      flow_id: ID to group a set of events together.
    """
    # Be forgiving. If an event is a string or is an unknown number we
    # still log it but log it as the string value.
    event_type_name = EventType.Name.get(event_type, str(event_type))
    event = {LogField.EVENT_TYPE: event_type_name}

    if room_id is not None:
      event[LogField.ROOM_ID] = room_id

    if flow_id is not None:
      event[LogField.FLOW_ID] = flow_id

    if client_time_ms is not None:
      event[LogField.CLIENT_TIMESTAMP] = self._timestamp_from_millis(
          client_time_ms)
{'fraction_non_alphanumeric': 0.07273449920508744, 'fraction_numerical': 0.003577106518282989, 'mean_word_length': 3.1976647206005, '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': '5973483', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1459, 'n_words': 476}