def test_get_cursor(self, mc):
     conn = MySQLConnector()
     conn.connection.is_connected = Mock(return_value=True)
     conn.connection.cursor.return_value = 42
     self.assertEqual(conn.get_cursor(), 42)
     conn.connection.is_connected.return_value = False
     self.assertIsNone(conn.get_cursor())
 def test_close(self, mc):
     conn = MySQLConnector()
     conn.connection.is_connected = Mock(return_value=False)
     conn.close()
     self.assertFalse(conn.connection.close.mock_calls)
     conn.connection.is_connected.return_value = True
     conn.close()
     conn.connection.close.assert_any_call()
Exemple #3
0
def get_mysql_connector(mysql_config):
    logging.info('Attempt to connect to mysql')
    if '' in mysql_config.values():
        logging.info('MySQL parameters are not specified')
        exit()
    logging.info('Now connecting to MySQL')
    mysql_connector = MySQLConnector()
    mysql_connector.create_connection(mysql_config)
    logging.info('Connected to MySQL database')
    return mysql_connector
Exemple #4
0
    def get(self):
        user_cookie = self.get_argument('user_cookie', '')
        if not user_cookie:
            self.set_status(400, reason='incorrect data')
            print 'INF: incorrect request data'
            return
        user_cn = MySQLConnector()
        role_id = user_cn.role_by_cookie(user_cookie)

        self.write(json.dumps({'id': role_id}))
	def get(self):
		user_cookie = self.get_argument('user_cookie', '')
		if not user_cookie:
			self.set_status(400, reason='incorrect data')
			print 'INF: incorrect request data'
			return
		user_cn = MySQLConnector()
		role_id = user_cn.role_by_cookie(user_cookie)

		self.write(json.dumps({'id': role_id}))
Exemple #6
0
    def delete(self):
        self.set_status(200)
        user_cookie = self.get_argument('user_cookie', '')
        if not user_cookie:
            self.set_status(400, reason='incorrect data')
            print 'INF: incorrect request data'
            return

        user_cn = MySQLConnector()
        user_cn.del_cookie(user_cookie)
        print 'INF: cookie {} was deleted'.format(user_cookie)
Exemple #7
0
    def put(self):
        user_data = json.loads(self.request.body)

        print "DEB: add user {}".format(user_data['username'])
        user_cn = MySQLConnector()
        user_id = user_cn.insert_user(user_data)

        if user_id != None:
            self.set_status(201)
        else:
            self.set_status(409, reason='unable to insert user')
	def delete(self):
		self.set_status(200)
		user_cookie = self.get_argument('user_cookie', '')
		if not user_cookie:
			self.set_status(400, reason='incorrect data')
			print 'INF: incorrect request data'
			return

		user_cn = MySQLConnector()
		user_cn.del_cookie(user_cookie)
		print 'INF: cookie {} was deleted'.format(user_cookie)
	def put(self):
		user_data = json.loads(self.request.body)

		print "DEB: add user {}".format(user_data['username'])
		user_cn = MySQLConnector()
		user_id = user_cn.insert_user(user_data)

		if user_id  != None:
			self.set_status(201)
		else:
			self.set_status(409, reason='unable to insert user')
Exemple #10
0
 def get(self):
     self.set_status(200)
     user_cookie = self.get_argument('user_cookie', '')
     if not user_cookie:
         self.set_status(400, reason='incorrect data')
         print 'INF: incorrect request data'
         return
     print 'INF: check cookie {} in db'.format(user_cookie)
     user_cn = MySQLConnector()
     user_id = user_cn.get_user_by_cookie(user_cookie)
     if user_id is None:
         self.set_status(401, reason='no user for cookie')
         return
Exemple #11
0
	def get(self):
		self.set_status(200)
		user_cookie = self.get_argument('user_cookie', '')
		if not user_cookie:
			self.set_status(400, reason='incorrect data')
			print 'INF: incorrect request data'
			return
		print 'INF: check cookie {} in db'.format(user_cookie)
		user_cn = MySQLConnector()
		user_id = user_cn.get_user_by_cookie(user_cookie)
		if user_id is None:
			self.set_status(401, reason='no user for cookie')
			return
Exemple #12
0
	def post(self):
		self.set_status(202)
		user_data = {'username': '', 'password': ''}
		files = {}
		headers = {}
		util.parse_body_arguments(self.request.headers['Content-Type'], self.request.body, files, headers)

		for k in user_data.keys():
			if k not in files.keys():
				self.set_status(400, reason='incorrect data')
				print 'INF: incorrect request data'
				return
			user_data[k] = files[k][0]

		print "DEB: login username " + user_data['username']
		user_cn = MySQLConnector()
		user_cookie = user_cn.create_cookie(user_data)
		if user_cookie is None:
			self.set_status(401, reason='no such user-password pair') # unauthorized
			return

		self.write(json.dumps({'user_cookie': user_cookie}))
Exemple #13
0
    def post(self):
        self.set_status(202)
        user_data = {'username': '', 'password': ''}
        files = {}
        headers = {}
        util.parse_body_arguments(self.request.headers['Content-Type'],
                                  self.request.body, files, headers)

        for k in user_data.keys():
            if k not in files.keys():
                self.set_status(400, reason='incorrect data')
                print 'INF: incorrect request data'
                return
            user_data[k] = files[k][0]

        print "DEB: login username " + user_data['username']
        user_cn = MySQLConnector()
        user_cookie = user_cn.create_cookie(user_data)
        if user_cookie is None:
            self.set_status(
                401, reason='no such user-password pair')  # unauthorized
            return

        self.write(json.dumps({'user_cookie': user_cookie}))
Exemple #14
0
def fill_submits(sqlite_cursor, base_dir, origin, mysql_config):
    if '' in mysql_config.values():
        logging.error('MySQL parameters are not specified')
        exit()
    logging.info('Now connecting to MySQL')
    from mysql_connector import MySQLConnector
    mysql_connector = MySQLConnector()
    mysql_connector.create_connection(mysql_config)
    logging.info('Connected to MySQL database')
    ej_cursor = mysql_connector.get_cursor()
    logging.info("Filling database from XML's and MySQL database")
    fill_from_xml(sqlite_cursor, ej_cursor, base_dir, origin)
    mysql_connector.close()
 def test_create_connection(self, mc):
     conn = MySQLConnector()
     conn.close = Mock()
     conn.create_connection({'a': 1, 'b': 2})
     conn.close.assert_any_call()
     mc.connect.assert_called_with(a=1, b=2)
    def fill_recommendations_table(self, limit=False):
        processing = 0
        log = 0
        if limit:
            self.our_db_cursor.execute('SELECT id FROM users WHERE id > ? AND id < ?', (limit[0], limit[1]))
        else:
            self.our_db_cursor.execute('SELECT id FROM users')
        logging.info('Loading users ids..')
        database_users_ids = self.our_db_cursor.fetchall()
        users_num = len(database_users_ids)
        logging.info('Loaded {} users ids'.format(users_num))
        # the number of such sequences is the problem_ref-problem_ref
        number_of_sequences = dict()
        logging.info('Users pocessing..')
        for user_id_row in database_users_ids:
            processing += 1
            if processing >= 100:
                log += 1
                processing = 0
                logging.info('Processed {} from {}'.format(log * 100, users_num))
            self.our_db_cursor.execute('SELECT problem_ref '
                                       'FROM submits '
                                       'WHERE user_ref=? AND outcome=? '
                                       'ORDER BY timestamp', (user_id_row['id'], 'OK'))

            sorted_problems_refs = self.our_db_cursor.fetchall()

            for list_id, problem_ref_row in enumerate(sorted_problems_refs[:-1]):
                problem_ref = problem_ref_row['problem_ref']
                next_ref = sorted_problems_refs[list_id + 1]['problem_ref']

                start_problem_id = self._get_problem_id_by_problem_ref(problem_ref)
                next_problem_id = self._get_problem_id_by_problem_ref(next_ref)

                if start_problem_id in number_of_sequences:
                    if next_problem_id not in number_of_sequences[start_problem_id]:
                        number_of_sequences[start_problem_id][next_problem_id] = 0
                    number_of_sequences[start_problem_id][next_problem_id] += 1
                else:
                    number_of_sequences[start_problem_id] = {next_problem_id: 1}

        logging.info('Processed {} from {}'.format(log * 100 + processing, users_num))

        result = dict()
        logging.info('Data processing')
        for problem_id_start in number_of_sequences:
            for problem_id_next in number_of_sequences[problem_id_start]:
                node = (number_of_sequences[problem_id_start][problem_id_next], problem_id_next)

                if problem_id_start in result:
                    result[problem_id_start].append(node)
                else:
                    result[problem_id_start] = [node]

        logging.info('Output database connection to recording')
        mysql_connector = MySQLConnector()
        mysql_connector.create_connection(self.output_db_config)
        logging.info('Connected to MySQL database')

        self.stats_db_cursor = mysql_connector.get_cursor()

        logging.info('Writing in database')
        for key in result:
            result[key].sort()
            for some in result[key][:min(len(result[key]), NUMBER_OF_RECOMMENDATIONS_TO_THE_PROBLEM)]:
                self._write_to_db(key, some[1])

        logging.info('Committing changes')
        mysql_connector.connection.commit()
        mysql_connector.close()
        logging.info('Output mysql database closed')