<commit_before>bongo       = require 'bongo'
KodingError = require '../error'

{ secure, signature } = bongo

{ argv } = require 'optimist'

_ = require 'lodash'

KONFIG        = require('koding-config-manager').load("main.#{argv.c}")
{ socialapi } = KONFIG
exchangeName  = "#{socialapi.eventExchangeName}:0"
exchangeOpts  = { autoDelete: no, durable:yes, type :'fanout', confirm: true }

Analytics = require('analytics-node')
analytics = new Analytics(KONFIG.segment)

mqClient = null

module.exports = class Tracker extends bongo.Base

  @share()

  @set
    sharedMethods:
      static:
        track: (signature String, Object, Function)

  { forcedRecipientEmail, forcedRecipientUsername, defaultFromMail } = KONFIG.email

  EVENT_TYPE = 'api.mail_send'

  @types =
    START_REGISTER            : 'started to register'
    FINISH_REGISTER           : 'finished register'
    LOGGED_IN                 : 'logged in'
    CONFIRM_USING_TOKEN       : 'confirmed & logged in using token'
    REQUEST_NEW_PASSWORD      : '<PASSWORD>'
    CHANGED_PASSWORD          : '<PASSWORD>'
    REQUEST_EMAIL_CHANGE      : 'requested pin to change email'
    CHANGED_EMAIL             : 'changed their email'
    INVITED_GROUP             : 'was invited to a group'
    INVITED_CREATE_TEAM       : 'was invited to create a team'
    SENT_FEEDBACK             : 'sent feedback'
    TEAMS_ACCEPTED_INVITATION : 'accepted invitation'

  @properties = {}

  @properties[@types.FINISH_REGISTER] = {
    category: 'NewAccount', label: 'VerifyAccount'
  }

  @identifyAndTrack = (username, event, eventProperties = {}, callback = -> ) ->
    @identify username, {}, (err) =>
      return callback err  if err

      @track username, event, eventProperties, callback


  @identify = (username, traits = {}, callback = -> ) ->

    return  unless KONFIG.sendEventsToSegment

    # use `forcedRecipientEmail` for both username and email
    if forcedRecipientEmail
      username     = forcedRecipientUsername
      traits.email = forcedRecipientEmail

    traits = @addDefaults traits
    analytics.identify { userId: username, traits }

    # force flush so identify call doesn't sit in queue, while events
    # from Go/other systems are being sent
    analytics.flush (err, batch) ->
      console.error "flushing identify failed: #{err} @sent-hil"  if err
      callback err


  @track$ = secure (client, subject, options = {}, callback) ->

    unless account = client?.connection?.delegate
      err = '[Tracker.track$] Account is not set!'
      console.error err
      return callback new KodingError err

    { profile: { nickname } } = account

    event = { subject }

    @track nickname, event, options

    callback()

  errMQClientNotSet = new Error 'Tracker: RabbitMQ client not set'

  @track = (username, event, options = {}, callback = -> ) ->

    return callback null  unless KONFIG.sendEventsToSegment

    _.extend options, @properties[event.subject]

    # use `forcedRecipientEmail` for both username and email
    if forcedRecipientEmail
      username = forcedRecipientUsername
      event.to  = forcedRecipientEmail

    event.from       or= defaultFromMail
    event.properties   = @addDefaults { options, username }

    unless mqClient
      console.error errMQClientNotSet
      return callback errMQClientNotSet

    sendMessage = ->
      mqClient.exchange "#{exchangeName}", exchangeOpts, (exchange) ->
        unless exchange
          err = new Error "Tracker: Exchange not found to queue: #{exchangeName}"
          console.error err
          return callback err

        exchange.publish '', event, { type: EVENT_TYPE }, (errored, err) ->
          return callback err

    if mqClient.readyEmitted then sendMessage()
    else mqClient.on 'ready', -> sendMessage()


  @page = (userId, name, category, properties) ->

    return  unless KONFIG.sendEventsToSegment

    userId = KONFIG.forcedRecipientEmail or userId

    options = { userId, name, category, properties }
    @addDefaults options
    analytics.page options


  @alias = (previousId, userId) ->

    return  unless KONFIG.sendEventsToSegment

    userId = KONFIG.forcedRecipientEmail or userId

    options = { previousId, userId }
    @addDefaults options
    analytics.alias options


  @group = (groupId, userId) ->

    return  unless KONFIG.sendEventsToSegment

    userId = KONFIG.forcedRecipientEmail or userId

    options = { groupId, userId }
    @addDefaults options
    analytics.group options


  @addDefaults = (opts) ->
    opts['env']      = KONFIG.environment
    opts['hostname'] = KONFIG.hostname
    opts


  @setMqClient = (m) -> mqClient = m<commit_msg>Social: remove dependency of RMQ from Tracker<commit_after>bongo       = require 'bongo'
KodingError = require '../error'

{ secure, signature } = bongo

{ argv } = require 'optimist'

_ = require 'lodash'

KONFIG        = require('koding-config-manager').load("main.#{argv.c}")
{ socialapi } = KONFIG
exchangeName  = "#{socialapi.eventExchangeName}:0"
exchangeOpts  = { autoDelete: no, durable:yes, type :'fanout', confirm: true }

Analytics = require('analytics-node')
analytics = new Analytics(KONFIG.segment)

module.exports = class Tracker extends bongo.Base

  @share()

  @set
    sharedMethods:
      static:
        track: (signature String, Object, Function)

  { forcedRecipientEmail, forcedRecipientUsername, defaultFromMail } = KONFIG.email

  EVENT_TYPE = 'api.mail_send'

  @types =
    START_REGISTER            : 'started to register'
    FINISH_REGISTER           : 'finished register'
    LOGGED_IN                 : 'logged in'
    CONFIRM_USING_TOKEN       : 'confirmed & logged in using token'
    REQUEST_NEW_PASSWORD      : '<PASSWORD>'
    CHANGED_PASSWORD          : '<PASSWORD>'
    REQUEST_EMAIL_CHANGE      : 'requested pin to change email'
    CHANGED_EMAIL             : 'changed their email'
    INVITED_GROUP             : 'was invited to a group'
    INVITED_CREATE_TEAM       : 'was invited to create a team'
    SENT_FEEDBACK             : 'sent feedback'
    TEAMS_ACCEPTED_INVITATION : 'accepted invitation'

  @properties = {}

  @properties[@types.FINISH_REGISTER] = {
    category: 'NewAccount', label: 'VerifyAccount'
  }

  @identifyAndTrack = (username, event, eventProperties = {}, callback = -> ) ->
    @identify username, {}, (err) =>
      return callback err  if err

      @track username, event, eventProperties, callback


  @identify = (username, traits = {}, callback = -> ) ->

    return  unless KONFIG.sendEventsToSegment

    # use `forcedRecipientEmail` for both username and email
    if forcedRecipientEmail
      username     = forcedRecipientUsername
      traits.email = forcedRecipientEmail

    traits = @addDefaults traits
    analytics.identify { userId: username, traits }

    # force flush so identify call doesn't sit in queue, while events
    # from Go/other systems are being sent
    analytics.flush (err, batch) ->
      console.error "flushing identify failed: #{err} @sent-hil"  if err
      callback err


  @track$ = secure (client, subject, options = {}, callback) ->

    unless account = client?.connection?.delegate
      err = '[Tracker.track$] Account is not set!'
      console.error err
      return callback new KodingError err

    { profile: { nickname } } = account

    event = { subject }

    @track nickname, event, options

    callback()

  @track = (username, event, options = {}, callback = -> ) ->

    return callback null  unless KONFIG.sendEventsToSegment

    _.extend options, @properties[event.subject]

    # use `forcedRecipientEmail` for both username and email
    if forcedRecipientEmail
      username = forcedRecipientUsername
      event.to  = forcedRecipientEmail

    event.from       or= defaultFromMail
    event.properties   = @addDefaults { options, username }

    require("./socialapi/requests").publishMailEvent event, (err) =>
      callback err # do not cause trailing parameters


  @page = (userId, name, category, properties) ->

    return  unless KONFIG.sendEventsToSegment

    userId = KONFIG.forcedRecipientEmail or userId

    options = { userId, name, category, properties }
    @addDefaults options
    analytics.page options


  @alias = (previousId, userId) ->

    return  unless KONFIG.sendEventsToSegment

    userId = KONFIG.forcedRecipientEmail or userId

    options = { previousId, userId }
    @addDefaults options
    analytics.alias options


  @group = (groupId, userId) ->

    return  unless KONFIG.sendEventsToSegment

    userId = KONFIG.forcedRecipientEmail or userId

    options = { groupId, userId }
    @addDefaults options
    analytics.group options


  @addDefaults = (opts) ->
    opts['env']      = KONFIG.environment
    opts['hostname'] = KONFIG.hostname
    opts
{'fraction_non_alphanumeric': 0.09591534873136875, 'fraction_numerical': 0.00022755717373990216, 'mean_word_length': 2.9648173207036534, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '4222188', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2488, 'n_words': 697}