def create_index(cls, data, author, timestamp): """Create a indexing job.""" conn = connect_DB() status = "Done" result = cls.register_index(conn, status, data, author, timestamp) app_logger.info('Construct indexing job based on Index Object.') return result
def retrieve_indexID(cls, indexID): """Retrieve indexing job with ID.""" conn = connect_DB() result = cls.check_index_status(conn, indexID) app_logger.info( 'Retrieve indexing job with the ID: {0}'.format(indexID)) return result
def check_index_status(conn, indexID): """Check the indexing job status.""" db_cursor = conn.cursor() # Insert a row of data for row in db_cursor.execute( 'SELECT rowid, indexstatus, data, author, timestamp FROM indexes WHERE rowid=?', (indexID, )): result = { 'id': row[0], 'status': row[1], 'author': row[3], 'data': row[2], 'timestamp': row[4] } app_logger.info( 'Check status in the database for indexing job ID: {0}'.format( indexID)) break else: result = None app_logger.warning( 'Check status: There is no record for indexing job ID: {0}'. format(indexID)) db_cursor.close() return result
def on_delete(self, req, resp, indexID): """Respond on GET request to index endpoint.""" data = IndexingData() data.delete_indexID(indexID) resp.data = json.dumps({"deletedID": indexID}, indent=1, sort_keys=True) resp.content_type = 'application/json' app_logger.info( 'Deleted/DELETE /index data with ID: {0}.'.format(indexID)) resp.status = falcon.HTTP_200
def delete_index(conn, indexID): """Detele the indexing job data.""" db_cursor = conn.cursor() # Delete a row of data db_cursor.execute('DELETE FROM indexes WHERE rowid=?', (indexID, )) # Save (commit) the changes conn.commit() app_logger.info( 'Delete index in the database with indexing job ID: {0}'.format( indexID)) db_cursor.close() return
def on_post(self, req, resp, parsed): """Respond on GET request to index endpoint.""" data = IndexingData() timestamp = datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') index_args = [parsed.get('data'), parsed.get('author'), timestamp] response = data.create_index(*index_args) resp.data = json.dumps(response, indent=1, sort_keys=True) resp.content_type = 'application/json' resp.status = falcon.HTTP_202 app_logger.info('Creating/POST new data with ID: {0}.'.format( response['id']))
def register_index(conn, status, data, author, timestamp): """Create the indexing job.""" db_cursor = conn.cursor() # Insert a row of data db_cursor.execute("INSERT INTO indexes VALUES (?, ?, ?, ?)", (status, data, author, timestamp)) # Save (commit) the changes conn.commit() result = {'id': db_cursor.lastrowid, 'status': status} app_logger.info( 'Create row in the database with indexing job ID: {0}'.format( db_cursor.lastrowid)) db_cursor.close() return result
def on_get(self, req, resp, indexID): """Respond on GET request to index endpoint.""" data = IndexingData() response = data.retrieve_indexID(indexID) if response is not None: resp.data = json.dumps(response, indent=1, sort_keys=True) resp.content_type = 'application/json' resp.status = falcon.HTTP_200 app_logger.info( 'GET /index the data with ID: {0}.'.format(indexID)) else: raise falcon.HTTPGone() app_logger.warning( 'Indexing job with ID: {0} is gone/deleted.'.format(indexID))
def create_table(conn, create_table_sql): """Create a table from the create_table_sql statement. :param conn: Connection object :param create_table_sql: a CREATE TABLE statement :return: """ try: db_cursor = conn.cursor() db_cursor.execute(create_table_sql) app_logger.info( 'Creating tables in the database if they do not exist.') except Exception as error: app_logger.error('Error {0}'.format(error)) return error
def update_index_status(conn, indexID, status): """Update the indexing job status information.""" db_cursor = conn.cursor() db_cursor.execute("SELECT rowid FROM indexes WHERE rowid = ?", (indexID, )) data = db_cursor.fetchone() if data is None: app_logger.info( 'There is no record for indexing job ID:'.format(indexID)) pass else: db_cursor.execute('UPDATE indexes SET indexstatus=? WHERE rowid=?', (status, indexID)) # Save (commit) the changes conn.commit() app_logger.info( 'Update status in the database for indexing job ID: {0}'. format(indexID)) db_cursor.close() return
def connect_DB(db_file=None): """Connect to DB by parsing configuration.""" db_filename = '' if db_file is None: db_filename = 'data.db' else: db_filename = db_file try: conn = sqlite3.connect(db_filename) app_logger.info('Connecting to database.') tb_indexes_exists = "SELECT name FROM sqlite_master WHERE type='table' AND name='indexes'" if not conn.execute(tb_indexes_exists).fetchone(): create_table( conn, """create table if not exists indexes (indexstatus text, data text, author text, timestamp datetime)""" ) return conn except Exception as error: app_logger.error('Connection Failed!\ \nError Code is {0};\ \nError Content is {1};'.format(error.args[0], error.args[1])) return error
def delete_indexID(cls, indexID): """Delete indexing job with ID.""" conn = connect_DB() cls.delete_index(conn, indexID) app_logger.info('Delete indexing job with the ID: {0}'.format(indexID)) return
def on_get(self, req, resp): """Respond on GET request to map endpoint.""" resp.status = falcon.HTTP_200 app_logger.info('Finished operations on /health GET Request.')
resp.content_type = 'application/json' resp.status = falcon.HTTP_200 app_logger.info( 'GET /index the data with ID: {0}.'.format(indexID)) else: raise falcon.HTTPGone() app_logger.warning( 'Indexing job with ID: {0} is gone/deleted.'.format(indexID)) def on_delete(self, req, resp, indexID): """Respond on GET request to index endpoint.""" data = IndexingData() data.delete_indexID(indexID) resp.data = json.dumps({"deletedID": indexID}, indent=1, sort_keys=True) resp.content_type = 'application/json' app_logger.info( 'Deleted/DELETE /index data with ID: {0}.'.format(indexID)) resp.status = falcon.HTTP_200 do_index = IndexClass() get_index = IndexingResource() webapp = falcon.API() webapp.add_route('/health', HealthCheck()) webapp.add_route('/%s/index' % (api_version), do_index) webapp.add_route('/%s/index/{indexID}' % (api_version), get_index) app_logger.info('App is running.')