def detect_change(data):
    mydb = connect_to_db()
    mycursor = mydb.cursor(prepared=True)
    for i in range(len(data)):
        item = data[i]
        print(item.condition_text)
        print("last_value: {}".format(item.last_value))
        condition = Condition(item.condition_text)
        query_string = 'http://{}/marketdata/{}'.format(marketdata_host, item.symbol)
        dat = requests.get(url = query_string).json()
        is_condition_true = condition.evaluate(dat)
        last_value = item.last_value
        item.last_value = is_condition_true
        data[i] = item
        print(dat)
        print(condition.evaluate(dat))

        # Only notify if true and it previously was not true, to avoid constant notifications
        if condition.evaluate(dat) and last_value != is_condition_true:
            user = fetch_user(item.user_id, mycursor)
            print('Notifying user: {}'.format(user.username))
            text = "{}%20your%20notification%20for%20{}:%20{}".format(user.username, item.symbol, item.condition_text)
            if item.notification_method == NOTIFICATION_METHOD.SMS:
                query_string = 'http://{}:{}/notify/text?message={}&number=test_num'.format(notification_service_host, notification_service_port, text)
                requests.post(url = query_string)
            elif item.notification_method == NOTIFICATION_METHOD.EMAIL:
                query_string = 'http://{}:{}/notify/email?message={}&email=test_address'.format(notification_service_host, notification_service_port, text)
                requests.post(url = query_string)
            elif item.notification_method == NOTIFICATION_METHOD.BOTH:
                query_string = 'http://{}:{}/notify/text?message={}&number=test_num'.format(notification_service_host, notification_service_port, text)
                requests.post(url = query_string)
                query_string = 'http://{}:{}/notify/email?message={}&email=test_address'.format(notification_service_host, notification_service_port, text)
                requests.post(url = query_string)
    mydb.disconnect()
    return data
Beispiel #2
0
 def test_equality_false(self):
     cond = Condition('x == y')
     self.assertEqual(False, cond.evaluate(self.data), 'Should be equal')
Beispiel #3
0
 def test_multiple_conditions_beginning_and_end_brackets(self):
     cond = Condition('( x == y or z < 1 ) and ( x == y and z == x)')
     # Will evaluate to ( False or True ) and ( False and True ) 
     self.assertEqual(False, cond.evaluate(self.data), 'Should be equal')
Beispiel #4
0
 def test_multiple_conditions_end_brackets(self):
     cond = Condition('x == y or ( x == z and y > z )')
     # Will evaluate to False or ( True and True ) 
     self.assertEqual(True, cond.evaluate(self.data), 'Should be equal')
Beispiel #5
0
 def test_multiple_conditions_beginning_brackets_2(self):
     # Will evaluate to ( False and True) and True
     cond = Condition('( x == y and x >= 1 ) and z == 1')
     self.assertEqual(False, cond.evaluate(self.data), 'Should be equal')
Beispiel #6
0
 def test_multiple_conditions_with_and(self):
     cond = Condition('x == y and x >= 1')
     self.assertEqual(False, cond.evaluate(self.data), 'Should be equal')
Beispiel #7
0
 def test_multiple_conditions_with_or(self):
     cond = Condition('x == y or x >= 1')
     self.assertEqual(True, cond.evaluate(self.data), 'Should be equal')