Ejemplo n.º 1
0
def contact(request):
    title = 'Contact'
    form = contactForm(request.POST or None)
    confirm_message = None
    e = Emitter("192.168.2.133:8090/contact")
    tracker = Tracker(e, namespace="sdp", app_id="sdp-11", encode_base64=False)

    if form.is_valid():
        name = form.cleaned_data['name']
        comment = form.cleaned_data['comment']
        subject = 'Message from Mysite.com'
        message = '%s %s' % (comment, name)
        emailFrom = form.cleaned_data['email']
        # emailTo = [settings.EMAIL_HOST_USER]
        emailTo = ['*****@*****.**']
        send_mail(
            subject,
            message,
            emailFrom,
            emailTo,
            fail_silently=True,
        )
        title = 'Thanks!'
        confirm_message = 'Thanks for the message, we will get right back to you.'
        form = None
        tracker.track_page_view("192.168.2.133:8090/contact")

    context = {
        'title': title,
        'form': form,
        'confirm_message': confirm_message,
    }
    template = 'contact.html'
    return render(request, template, context)
Ejemplo n.º 2
0
def listen():
  print "attach"

  # Kafka
  consumer = KafkaConsumer(bootstrap_servers=os.environ["KAFKA_BOOTSTRAP_SRVS"], group_id=os.environ["KAFKA_GROUP_ID"])
  consumer.subscribe([os.environ["KAFKA_SOURCE_TOPIC"]])

  # Snowplow
  e = Emitter(os.environ["SP_COLLECTOR_URI"],protocol=os.environ["SP_COLLECTOR_PROTOCOL"],port=int(os.environ["SP_COLLECTOR_PORT"]),method=os.environ["SP_COLLECTOR_METHOD"])
  t = Tracker(emitters=e,namespace="cf",app_id=str(os.environ["APP_ID"]),encode_base64=True)

  for msg in consumer:
    #
    try:
      indata = json.loads(msg.value)
      
      s1 = Subject()
      s1.set_platform("app")
      s1.set_user_id("??")
      s1.set_lang("??")
      s1.set_ip_address("0.0.0.0")
      s1.set_useragent("??")
      
      t.set_subject(s1)

      t.track_self_describing_event(SelfDescribingJson("iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",{
        "data":{
          "data": indata
        },
        "schema": "iglu:"+os.environ["OPERATOR_ID"]+"/"+os.environ["APP_ID"]+"/jsonschema/1-0-0"
      }))

      t.flush()
    except Exception,Argument:
      print "Error:",str(Argument)
Ejemplo n.º 3
0
def track(type, data):
    """
    Called for the tracking of events
    input: type of event to track, data to pass
    output: No return
    """
    emitter = Emitter(SNOWPLOW_MICRO_URI,
                      buffer_size=1,
                      on_success=success,
                      on_failure=failure)
    tracker = Tracker(emitter)
    # Dictionary to contain all events we want to track and their corresponding methods.
    # So we can have a more generic way to track
    dict = {
        PAGE_VIEW: tracker.track_page_view,
        FORM_SUBMIT: tracker.track_form_submit,
        STRUCT: tracker.track_struct_event
    }
    subject = Subject()
    subject.set_platform("pc")
    tracker.set_subject(subject)
    if isinstance(data, list):
        dict[type](*data)
    else:
        dict[type](data)
    logger.setLevel(10)
Ejemplo n.º 4
0
    def setup_tracker(self):
        """Setup an instance of a tracker"""
        self.companyConfig = self.setup_config(self.companyConfig)
        self.emitter = Emitter(self.companyConfig["COLLECTOR_HOST"],
                               protocol=self.companyConfig["PROTOCOL"],
                               port=self.companyConfig["PORT"],
                               method=self.companyConfig["EMIT_METHOD"],
                               buffer_size=self.companyConfig["BUFFER_SIZE"])
        self.subject = Subject()
        self.tracker = Tracker(emitters=self.emitter,
                               subject=self.subject,
                               namespace=self.companyConfig["TRACKER_NAME"],
                               app_id=self.companyConfig["APP_ID"],
                               encode_base64=self.companyConfig["ENCODE64"])

        return self.tracker
def save_tweet(data):
    #print "save_tweet"
    #print data

    indata = data

    e = Emitter(args.sp_collector_uri,
                protocol=args.sp_collector_protocol,
                port=int(args.sp_collector_port),
                method=args.sp_collector_method)
    t = Tracker(emitters=e,
                namespace="cf",
                app_id=args.sp_app_id,
                encode_base64=True)

    s1 = Subject()
    s1.set_platform("web")
    s1.set_user_id(str(indata.get("user_id")))
    s1.set_lang(str(indata.get("lang")))
    #s1.set_ip_address(str(indata.get("i_ip")))
    s1.set_useragent(str(indata.get("source")))

    t.set_subject(s1)

    t.track_self_describing_event(
        SelfDescribingJson(
            "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",
            {
                "data": {
                    "data": indata
                },
                "schema":
                "iglu:com.rbox24/" + args.sp_app_id + "/jsonschema/1-0-0"
            }))

    t.flush()
    print "Tweet sent to collector, time:", time.time()
Ejemplo n.º 6
0
                "channel": snowplow_channel,
                "program_id": svc_code,
                "parent_id": pgm_code,
                "program_name": pgm_name,
                "transaction_name": svc_name
            })

        return chooseservice

    @staticmethod
    def get_finish(svc_quantity, accurate_time):
        inaccurate_flag = accurate_time != 1
        finishservice = SelfDescribingJson(
            'iglu:ca.bc.gov.cfmspoc/finish/jsonschema/1-0-0', {
                "inaccurate_time": inaccurate_flag,
                "count": svc_quantity
            })
        return finishservice


# Set up core Snowplow environment
if SnowPlow.call_snowplow_flag:
    s = Subject()  # .set_platform("app")
    e = AsyncEmitter(SnowPlow.sp_endpoint,
                     on_failure=SnowPlow.failure,
                     protocol="https")
    t = Tracker(e,
                encode_base64=False,
                app_id=SnowPlow.sp_appid,
                namespace=SnowPlow.sp_namespace)
Ejemplo n.º 7
0
unsent_events = []


def g(x, y):
    print(str(x) + " events sent successfully!")
    print("Events not sent. They have been stored in unsent_events:")

    for event_dict in y:
        print(event_dict)
        unsent_events.append(event_dict)


e = Emitter(URL, buffer_size=3, on_success=f, on_failure=g)

t = Tracker(e)

# This doesn't cause the emitter to send a request because the buffer_size was
# set to 3, not 1
t.track_page_view("http://www.myowndomainhere.dev")

# This does cause the emitter to try to send all 6 events
t.track_page_view("http://www.myowndomainhere.dev/signin")
t.track_page_view("http://www.myowndomainhere.dev/home")
t.track_page_view("http://www.myowndomainhere.dev/somepage")
t.track_page_view("http://www.myowndomainhere.dev/anotherpage")
t.track_page_view("http://www.myowndomainhere.dev/logout")

# Since the method is GET by default, 6 separate requests are sent
# If any of them are unsuccessful, they will be stored in the unsent_events
# variable
Ejemplo n.º 8
0
    print "sp_collector_protocol:", args.sp_collector_protocol
    print "sp_collector_port:", args.sp_collector_port
    print "sp_collector_method:", args.sp_collector_method
    print "sp_app_id:", args.sp_app_id
    print "--"

    print "Tracker setup..."
    #global emiter
    #global tracker
    emiter = Emitter(buffer_size=50,
                     endpoint=args.sp_collector_uri,
                     protocol=args.sp_collector_protocol,
                     port=int(args.sp_collector_port),
                     method=args.sp_collector_method)
    tracker = Tracker(emitters=emiter,
                      namespace="cf",
                      app_id=args.sp_app_id,
                      encode_base64=True)

    print "Starting listening to Twitter..."

    #global period_counter
    aggregates = {}
    period_counter = int(time.time())

    l = StdOutListener()
    auth = OAuthHandler(args.consumer_key, args.consumer_secret)
    auth.set_access_token(args.access_token, args.access_token_secret)
    stream = Stream(auth, l)

    filter_arr = args.filter.split(",")
    print "filter_arr:", filter_arr
def call_snowplow(request_id,json_object):
    
    # Use the global emitter and tracker dicts
    global e
    global t

    # callbacks are documented in
    # - https://github.com/snowplow/snowplow/wiki/Python-Tracker#emitters

    # callback for passed calls
    def on_success(successfully_sent_count):
        log("INFO","Emitter call PASSED on request_id: {}.".format(request_id))
        backoff_outside_the_scope = 1 # reset the backoff if this is successful
        # get previous try number, choose larger of 0 or query result and add 1
        try_number = max(i for i in [0,db_query("SELECT MAX(try_number) FROM caps.snowplow_calls WHERE request_id = %s ;", (request_id, ))[0]] if i is not None) + 1
        log("DEBUG","Try number: {}".format(try_number))
        snowplow_tuple = (
            str(request_id),
            str(200),
            str(try_number),
            json_object['env'],
            json_object['namespace'],
            json_object['app_id'],
            json_object['dvce_created_tstamp'],
            json.dumps(json_object['event_data_json'])
        )
        snowplow_id = db_query(snowplow_calls_sql, snowplow_tuple)[0]
        log("INFO","snowplow call table insertion PASSED on request_id: {} and snowplow_id: {}.".format(request_id, snowplow_id))
    
    # callback for failed calls
    failed_try = 0
    def on_failure(successfully_sent_count, failed_events):
        # increment the failed try
        nonlocal failed_try
        failed_try += 1

        # sleep according to the number indexed by failed_try in the fibonacci sequence
        sleep_time = binets_formula(failed_try)
        #log("INFO","Emitter call FAILED on request_id {} on try {}. Seconds until re-attempt: {}.".format(request_id,failed_try,sleep_time))
        log("INFO","Emitter call FAILED on request_id {} on try {}. No re-attempt will be made.".format(request_id,failed_try))
        
        # Leaving this sleep delay until inputting after a failed event is ready
        #sleep(sleep_time)

        # failed_events should always contain only one event, because ASyncEmitter has a buffer size of 1
        for event in failed_events:
            # get previous try number, choose larger of 0 or query result and add 1
            # try_number = max(i for i in [0,db_query("SELECT MAX(try_number) FROM caps.snowplow_calls WHERE request_id = %s ;", (request_id, ))[0]] if i is not None) + 1
            # log("DEBUG","Try number: {}".format(try_number))
            snowplow_tuple = (
                str(request_id),
                str(400),
                str(failed_try),
                json_object['env'],
                json_object['namespace'],
                json_object['app_id'],
                json_object['dvce_created_tstamp'],
                json.dumps(json_object['event_data_json'])
            )
            snowplow_id = db_query(snowplow_calls_sql, snowplow_tuple)[0]
            log("INFO","snowplow call table insertion PASSED on request_id: {} and snowplow_id: {}.".format(request_id, snowplow_id))
            # Re-attempt the event call by inputting it back to the emitter
            #e[tracker_identifier].input(event)
    
    tracker_identifier = json_object['env'] + "-" + json_object['namespace'] + "-" + json_object['app_id']
    log("DEBUG","New request with tracker_identifier {}".format(tracker_identifier))

    # logic to switch between SPM and Production Snowplow.
    # TODO: Fix SSL problem so to omit the anonymization proxy, since we connect from a Gov IP, not a personal machine
    sp_endpoint = os.getenv("SP_ENDPOINT_{}".format(json_object['env'].upper()))
    log("DEBUG","Using Snowplow Endpoint {}".format(sp_endpoint))

    # Set up the emitter and tracker. If there is already one for this combination of env, namespace, and app-id, reuse it
    # TODO: add error checking
    if tracker_identifier not in e:
        # defaults to a GET method, defaults to a buffer size of 1; buffer is flushed once full.
        e[tracker_identifier] = AsyncEmitter(sp_endpoint, protocol="https", on_success=on_success, on_failure=on_failure)
    if tracker_identifier not in t:
        t[tracker_identifier] = Tracker(e[tracker_identifier], encode_base64=False, app_id=json_object['app_id'], namespace=json_object['namespace'])

    # Build event JSON
    # TODO: add error checking
    event = SelfDescribingJson(json_object['event_data_json']['schema'], json_object['event_data_json']['data'])
    # Build contexts
    # TODO: add error checking
    contexts = [] 
    for context in json_object['event_data_json']['contexts']:
        contexts.append(SelfDescribingJson(context['schema'], context['data']))

    # Send call to Snowplow
    # TODO: add error checking
    t[tracker_identifier].track_self_describing_event(event, contexts, tstamp=json_object['dvce_created_tstamp'])
Ejemplo n.º 10
0
def tracker(request):
    tracker = Tracker(Emitter("payments.savantdigital.net"),
                      namespace="sdp",
                      app_id="sdp-11",
                      encode_base64=False)
    return render(request, response, context)
Ejemplo n.º 11
0
def create_tracker():
    e = Emitter(SNOWPLOW_MICRO_URL)
    return Tracker(e)
 def load(self, options: PluginLoadOptions) -> None:
     self._logger = options.logger
     emitter = AsyncEmitter(**self._options._asdict(), )
     self._tracker = Tracker(emitter)
Ejemplo n.º 13
0
#See https://github.com/snowplow/snowplow/wiki/Python-Tracker
#   and https://github.com/snowplow-proservices/ca.bc.gov-schema-registry
from snowplow_tracker import Subject, Tracker, AsyncEmitter
from snowplow_tracker import SelfDescribingJson
import time
import random

# Set up core Snowplow environment
s = Subject()  #.set_platform("app")
e = AsyncEmitter("spm.gov.bc.ca", protocol="https")
t = Tracker(e, encode_base64=False, app_id='demo')

# get time stamp to create new "citizen" (be sure to convert to a string)
client_id = int(time.time())

# Set some sample values for example events
citizen = SelfDescribingJson('iglu:ca.bc.gov.cfmspoc/citizen/jsonschema/3-0-0',
                             {
                                 "client_id": client_id,
                                 "service_count": 1,
                                 "quick_txn": False
                             })

office = SelfDescribingJson('iglu:ca.bc.gov.cfmspoc/office/jsonschema/1-0-0', {
    "office_id": 8,
    "office_type": "non-reception"
})

agent = SelfDescribingJson('iglu:ca.bc.gov.cfmspoc/agent/jsonschema/2-0-0', {
    "agent_id": 42,
    "role": "CSR",
Ejemplo n.º 14
0
    ],
}

CRED_TYPE_SYNONYMS = {
    "registration": "registration.registries.ca",
    "relationship": "relationship.registries.ca",
    "business_number": "relationship.registries.ca",
}

# Set up core Snowplow environment for api tracking
SP_APP_ID = os.getenv("SP_TRACKING_APP_ID", "orgbook_api_local_dev")
SP_EMITTER = AsyncEmitter(os.getenv("SP_TRACKING_EMITTER",
                                    "spm.apps.gov.bc.ca"),
                          protocol=os.getenv("SP_TRACKING_EMITTER_PROTOCOL",
                                             "https"))
SP_TRACKER = Tracker(SP_EMITTER, encode_base64=False, app_id=SP_APP_ID)

LOGIN_URL = "rest_framework:login"
LOGOUT_URL = "rest_framework:logout"

# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True
def call_snowplow(request_id, json_object):
    '''Callback executed when an emitter is flushed successfully'''
    # Debugging request_id to see if it's being evaluated by the callbacks
    logger.info("Request ID on call_snowplow function: %s", request_id)

    # Use the global emitter and tracker dicts
    global e
    global t

    def callback_log_inscope():
        logger.info("callback_log_inscope has Request ID: %s", request_id)

    # callbacks are documented in
    # - https://github.com/snowplow/snowplow/wiki/Python-Tracker#emitters

    # callback for passed calls
    def on_success(successfully_sent_count):
        logger.info('\'on_success\' callback with %s successful events',
                    successfully_sent_count)
        callback_log_inscope()
        logger.info("Emitter call PASSED on request_id: %s.", request_id)
        # get previous try number, choose larger of 0 or query result and add 1
        max_try_number_query = ("SELECT MAX(try_number) "
                                "FROM caps.snowplow_calls "
                                "WHERE request_id = %s ;")
        try_number = max(i for i in [
            0,
            single_response_query(max_try_number_query, (request_id, ))[0]
        ] if i is not None) + 1
        logger.debug("Try number: %s", try_number)
        snowplow_tuple = (str(request_id), str(200), str(try_number),
                          json_object['env'], json_object['namespace'],
                          json_object['app_id'],
                          json_object['dvce_created_tstamp'],
                          json.dumps(json_object['event_data_json']))
        snowplow_id = single_response_query(snowplow_calls_sql,
                                            snowplow_tuple)[0]
        logger.info(
            "snowplow call table insertion PASSED on "
            "request_id: %s and snowplow_id: %s.", request_id, snowplow_id)

    # callback for failed calls
    failed_try = 0

    def on_failure(successfully_sent_count, failed_events):
        '''Callback executed when an emitter flush results in any failures'''
        # increment the failed try
        logger.warning(
            '\'on_failure\' callback: %s events successfully '
            'emitted, %s events returned by emitter with an error '
            'response', successfully_sent_count, len(failed_events))
        nonlocal failed_try
        failed_try += 1

        logger.info(
            'Emitter call FAILED on request_id %s on try %s. '
            'No re-attempt will be made.', request_id, failed_try)

        # failed_events should always contain only one event,
        # because ASyncEmitter has a buffer size of 1
        for event in failed_events:
            logger.warning('event failure: %s', event)
            snowplow_tuple = (str(request_id), str(400), str(failed_try),
                              json_object['env'], json_object['namespace'],
                              json_object['app_id'],
                              json_object['dvce_created_tstamp'],
                              json.dumps(json_object['event_data_json']))
            snowplow_id = single_response_query(snowplow_calls_sql,
                                                snowplow_tuple)[0]
            logger.info(
                "snowplow call table insertion PASSED on request_id: "
                "%s and snowplow_id: %s.", request_id, snowplow_id)
            # Re-attempt the event call by inputting it back to the emitter

    tracker_identifier = "{}-{}-{}".format(json_object['env'],
                                           json_object['namespace'],
                                           json_object['app_id'])
    logger.debug("New request with tracker_identifier %s", tracker_identifier)

    # logic to switch between SPM and Production Snowplow.
    sp_route = os.getenv("SP_ENDPOINT_{}".format(json_object['env'].upper()))
    logger.debug("Using Snowplow Endpoint %s", sp_route)

    # Set up the emitter and tracker. If there is already one for this
    # combination of env, namespace, and app-id, reuse it
    # TODO: add error checking
    # TEMP COMMENTED OUT TO AVOID USING THE GLOBAL DICT OF EMITTERS/TRACKERS
    # if tracker_identifier not in e:
    #     e[tracker_identifier] = AsyncEmitter(
    #         sp_route,
    #         protocol="https",
    #         on_success=on_success,
    #         on_failure=on_failure)
    #
    # if tracker_identifier not in t:
    #     t[tracker_identifier] = Tracker(
    #         e[tracker_identifier],
    #         encode_base64=False,
    #         app_id=json_object['app_id'],
    #         namespace=json_object['namespace'])

    this_ASyncEmitter = AsyncEmitter(sp_route,
                                     protocol="https",
                                     on_success=on_success,
                                     on_failure=on_failure)
    this_Tracker = Tracker(this_ASyncEmitter,
                           encode_base64=False,
                           app_id=json_object['app_id'],
                           namespace=json_object['namespace'])

    # Build event JSON
    # TODO: add error checking
    event = SelfDescribingJson(json_object['event_data_json']['schema'],
                               json_object['event_data_json']['data'])
    # Build contexts
    # TODO: add error checking
    contexts = []
    for context in json_object['event_data_json']['contexts']:
        contexts.append(SelfDescribingJson(context['schema'], context['data']))

    # Send call to Snowplow
    # TODO: add error checking
    # TEMP COMMENTED OUT TO AVOID USING THE GLOBAL DICT OF EMITTERS/TRACKERS
    # t[tracker_identifier].track_self_describing_event(
    #     event, contexts, tstamp=json_object['dvce_created_tstamp'])

    this_Tracker.track_self_describing_event(
        event, contexts, tstamp=json_object['dvce_created_tstamp'])
Ejemplo n.º 16
0
    def http_get(self, payload):
        sp_logger.info("Sending GET request to {}...".format(self.endpoint))
        sp_logger.debug("Payload: {}".format(payload))
        r = requests.get(self.endpoint, params=payload, timeout=5.0)

        msg = "GET request finished with status code: " + str(r.status_code)
        if self.is_good_status_code(r.status_code):
            sp_logger.info(msg)
        else:
            sp_logger.warning(msg)
        return r


emitter = TimeoutEmitter()
tracker = Tracker(emitter, namespace="cf", app_id="dbt")

active_user = None


class User:

    def __init__(self, cookie_dir):
        self.do_not_track = True
        self.cookie_dir = cookie_dir

        self.id = None
        self.invocation_id = str(uuid.uuid4())
        self.run_started_at = datetime.now(tz=pytz.utc)

    def state(self):
Ejemplo n.º 17
0
from flask import Flask, render_template, request, redirect
from snowplow_tracker import Emitter, Tracker
from snowplow_tracker import SelfDescribingJson

app = Flask(__name__)

email_addresses = []
e = Emitter("localhost:8080")
t = Tracker(e, namespace="python", app_id="hello_bears")


@app.route('/emails', methods=['GET'])
def emails():
    t.track_self_describing_event(
        SelfDescribingJson(
            "iglu:com.hellobears/email_addresses_viewed/jsonschema/1-0-0",
            {"test": "stewart"}))
    return render_template('emails.html', email_addresses=email_addresses)


@app.route('/signup', methods=['POST'])
def signup():
    email = request.form['email']
    email_addresses.append(email)

    t.track_self_describing_event(
        SelfDescribingJson(
            "iglu:com.hellobears/email_address_submitted/jsonschema/1-0-0",
            {"email_address": email}))
    return redirect('/')