Пример #1
0
  def post(self):
    user = self.get_user()
    if not user:
      return

    query_id = self.request.get('query_id')
    #user_email = self.request.get('user')
    data = self.request.get('data')
    time = self.request.get('time') #self.request.get('time', '')

    # get the question from the DB
    # get the user from the DB
    # add a Response object to the DB
    
    #user = User.get_by_email(user_email)
    query = db.get(query_id) #hmmm

    dp = DataPoint(
      text = data,
      query = query,
      timestamp = datetime.now()
    )

    dp.lt_put()
    ActionLog.log('NewDatapoint', query.user, query.name)

    self.response.out.write('Got it!')
Пример #2
0
def send_query(query):
    if query.user.query_medium == "sms":
        send_query_by_sms(query)
    else:
        send_by_email(query)

    logging.info("Sent Query: " + query.name + " for user " + query.user.email)
    query.lastSentAt = datetime.now()  # refresh the query
    query.put()  # commit it

    ActionLog.log("SentQuery")
Пример #3
0
  def post(self):
    ActionLog.log('ReceivedSMS')

    sender_phone = self.request.get('From')
    body = self.request.get('Body')

    # normalize the phone number to 10 digits
    # '+12268681112' => '2268681112'
    sender_phone = sender_phone[len(sender_phone) - 10:]
    logging.info('Got text from ' + sender_phone)

    # get the user
    user = User.get_by_phone(sender_phone)
    if not user:
      logging.error("Couldn't get the user for phone: " + sender_phone)
      return

    # parse the response
    query_value_pairs = self.parse_body(body)

    for query_name in query_value_pairs:
      value = query_value_pairs[query_name]

      if query_name == '' or value == '':
        logging.error('Got a bad response');
        return

      query = Query.get_by_user_and_name(user, query_name)

      if not query:
        logging.error("Couldn't get query for user " + user.email + ' and query ' + query.name)
        continue

      timestamp = datetime.now()
      
      dp = DataPoint(
        user = user,
        query = query,
        text = value,
        timestamp = timestamp,
      )

      dp.lt_put()

      ActionLog.log('NewDatapoint', user, query.name)

      query.refresh()

      logging.info('Received datapoint ' + query_name + ': ' + value + '\n')

    self.response.out.write('<Response><Sms>Got it!</Sms></Response>')
Пример #4
0
    def track(self):
        start = time()

        response = handler(self)

        end = time()

        walltime = int((end - start) * 1000)

        data = {"walltime": walltime}

        ActionLog.log(action="Walltime", data=json.dumps(data), page=self.request.path)

        return response
Пример #5
0
  def receive(self, mail_message):
    # we need to make a new data point

    # get the user from the sender field
    user_email = mail_message.sender[ mail_message.sender.find('<') + 1 : mail_message.sender.rfind('>') ]
    user = User.get_by_email(user_email)

    if is_admin(user) and mail_message.to.find('users@') != -1:
      forward_to_users(mail_message)
      return

    if mail_message.to.find('feedback@') != -1:
      forward_to_admins(mail_message)
      return

    # get the datapoint from the body
    data = ''
    query_name = mail_message.subject[ mail_message.subject.rfind("Re:") + 4: ]
  
    query = Query.get_by_user_and_name(user, query_name)

    for content_type, body in mail_message.bodies('text/html'):
      # basically deprecated, since we're having users submit forms
      # straight from the emails now.
      logging.info('Message Body: ' + body.decode())
      data = body.decode()[ : body.decode().find('<')]

    if data == None:
      data = ''

    # get the time from now()
    timestamp = datetime.now()

    log_str = "Want to create a new datapoint for user %s and with value %s and query name %s and query %s and timestamp %s" % (user_email, data, query_name, query.key(), timestamp)

    logging.info("Received a message from " + mail_message.sender)
    logging.info(log_str)

    # create and put the datapoint
    # dp = DataPoint(...)
    dp = DataPoint( 
      text = data,
      query = query,
      timestamp = timestamp)
      
    dp.lt_put()
    ActionLog.log('ReceivedEmail')
    ActionLog.log('NewDatapoint', data=query.name, user=user)

    query.refresh()
Пример #6
0
def send_by_email(query):
    # get the user
    user = query.user
    subject = query.name
    to = user.email
    sender = "Lifetracker <*****@*****.**>"
    # construct the message
    body = query.text

    params = {"query_id": str(query.key()), "query_text": query.text}

    body = open("ui/html/email_form.html").read() % params

    # send the message
    message = mail.EmailMessage(sender=sender, subject=subject, to=to, html=body)

    message.send()

    ActionLog.log("SentEmail")
Пример #7
0
  def post(self):
    user = self.get_user()
    if not user:
      return

    name = self.request.get("name")
    frequency = int(self.request.get("frequency"))
    text = self.request.get('text')
    user_email = self.request.get('user_email') 
    format = self.request.get('format').lower()
    template_id = self.request.get('template_id')
    # for now, default to always 
    ask_when = ['morning', 'afternoon', 'evening']

    user = User.get_by_email(user_email)

    query = Query(
      name = name,
      # we should really do normalization inside the Query constructor,
      # but it will take some time to figure out how, lacking a nearby
      # python badass
      normalized_name = Query.normalize_name(name), 
      text = text,
      frequency = frequency,
      user = user,
      format = format,
      ask_when = ask_when,
    )
  
    if str(template_id) != "0":
      query.template = db.get(template_id)

    query.put()

    ActionLog.log('NewMetric', user)

    self.response.out.write(query.key());
Пример #8
0
def worst_walltime(page):
    yesterday = datetime.now() - timedelta(hours=24)

    request_times = ActionLog.get(action="Walltime", timewindow=yesterday, page=page).fetch(1000)

    if len(request_times) < 1:
        return None

    max_rt = 0

    for rt in request_times:
        try:
            data = json.loads(rt.data)
        except TypeError:
            continue
        except ValueError:
            continue

        if int(data["walltime"]) > max_rt:
            max_rt = int(data["walltime"])

    return max_rt
Пример #9
0
def average_walltime(page):
    yesterday = datetime.now() - timedelta(hours=24)

    request_times = ActionLog.get(action="Walltime", timewindow=yesterday, page=page).fetch(1000)

    if len(request_times) < 1:
        return None

    total_walltime = 0
    for rt in request_times:
        try:
            data = json.loads(rt.data)
        except TypeError:
            continue
        except ValueError:
            continue

        wt = int(data["walltime"])
        total_walltime += wt

    avg_walltime = total_walltime / len(request_times)
    return avg_walltime
Пример #10
0
  def get(self):
    user = self.get_user()
    if not user:
      return

    logout_url = users.create_logout_url(self.request.uri)
    
    yesterday = datetime.now() - timedelta(hours=24)

    new_datapoints = ActionLog.get(
      action = 'NewDatapoint',
      timewindow = yesterday,
    ).count()

    new_metrics = ActionLog.get(
      action = 'NewMetric', 
      timewindow = yesterday,
    ).count()

    queries_sent = ActionLog.get(
      action = 'SentQuery',
      timewindow = yesterday,
    ).count()

    sms_sent = ActionLog.get(
      action = 'SentSMS',
      timewindow = yesterday,
    ).count()

    emails_sent = ActionLog.get(
      action = 'SentEmail',
      timewindow = yesterday,
    ).count()

    emails_received = ActionLog.get(
      action = 'ReceivedEmail',
      timewindow = yesterday,
    ).count()

    sms_received = ActionLog.get(
      action = 'ReceivedSMS',
      timewindow = yesterday,
    ).count()

    new_logins = ActionLog.get(
      action = 'FirstTimeLogin',
      timewindow = yesterday,
    ).count()

    dashboard_avg_walltime = average_walltime('/dashboard')
    data_avg_walltime = average_walltime('/data')
    home_avg_walltime = average_walltime('/')
    analyze_avg_walltime = average_walltime('/analyze')
    analyze_json_avg_walltime = average_walltime('/analyzeJSON')

    dashboard_worst_walltime = worst_walltime('/dashboard')
    data_worst_walltime = worst_walltime('/data')
    home_worst_walltime = worst_walltime('/')
    analyze_worst_walltime = worst_walltime('/analyze')
    analyze_json_worst_walltime = worst_walltime('/analyzeJSON')

    # hackey high-number for now.
    total_metrics = Query.all().count(100000)
    total_datapoints = DataPoint.all().count(100000)

    f = open('intern/html/engagement.html')
    html = f.read()

    params = {
      'new_datapoints': new_datapoints,
      'new_metrics': new_metrics,
      'total_metrics': total_metrics,
      'total_datapoints': total_datapoints,
      'queries_sent': queries_sent,
      'sms_sent': sms_sent,
      'emails_sent': emails_sent,
      'sms_received': sms_received,
      'emails_received': emails_received,
      'new_logins': new_logins,
      'dashboard_walltime': dashboard_avg_walltime,
      'data_walltime': data_avg_walltime,
      'home_walltime': home_avg_walltime,
      'analyze_json_walltime': analyze_json_avg_walltime,
      'analyze_walltime': analyze_avg_walltime,
      'dashboard_worst_walltime': dashboard_worst_walltime,
      'data_worst_walltime': data_worst_walltime,
      'home_worst_walltime': home_worst_walltime,
      'analyze_json_worst_walltime': analyze_json_worst_walltime,
      'analyze_worst_walltime': analyze_worst_walltime,
    }

    self.response.out.write(html % params)
Пример #11
0
def send_query_by_sms(query):
    text = query.text + ' Please reply "' + query.name + ': value"'
    send_sms(query.user.phone, text)

    ActionLog.log("SentSMS")