From 77dc7c8f4f7aa897aca5ecf6fffb93ee398cf08b Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 22:20:56 +0000 Subject: [PATCH 01/12] Forward postgres from vagrant to localhost port to save running it on your dev machine locally --- Vagrantfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vagrantfile b/Vagrantfile index 203385a..3b7acc6 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -5,7 +5,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "ubuntu/trusty64" - config.vm.network "forwarded_port", guest: 5432, host: 15432 + config.vm.network "forwarded_port", guest: 5432, host: 5432 config.vm.network "forwarded_port", guest: 5000, host: 15000 config.vm.network "forwarded_port", guest: 80, host:10080 config.vm.network "forwarded_port", guest: 15672, host: 15673 From 7144ac04ad912d354059f9e5b901bfe186963841 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 22:21:11 +0000 Subject: [PATCH 02/12] Make sure app runs in debug mode when run from run.py --- run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.py b/run.py index ca668fd..6d93de1 100644 --- a/run.py +++ b/run.py @@ -24,4 +24,4 @@ # This section only applies when you are running run.py directly if __name__ == '__main__': blockbuster.bb_logging.logger.info("Running http on port 5000") - blockbuster.app.run(host='0.0.0.0') \ No newline at end of file + blockbuster.app.run(host='0.0.0.0', debug=True) \ No newline at end of file From c47cf5ea0d55bed89a12b63840c56a8afcb32076 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 22:21:34 +0000 Subject: [PATCH 03/12] Move stats endpoint to API Routes section and secure it with authentication --- blockbuster/bb_routes.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/blockbuster/bb_routes.py b/blockbuster/bb_routes.py index b60bce6..602e5bf 100644 --- a/blockbuster/bb_routes.py +++ b/blockbuster/bb_routes.py @@ -48,6 +48,7 @@ def decorated(*args, **kwargs): return f(*args, **kwargs) return decorated + # Core App Routes @app.route("/InboundSMS/", methods=['POST']) def post_inboundsms(): @@ -55,20 +56,22 @@ def post_inboundsms(): return bb_request_processor.process_twilio_request(request) -@app.route("/stats/", methods=['GET']) -def get_stats(): - stats = bb_api_request_processor.APIRequestProcessor().service_stats_get() - bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'GET_STATS', stats) - return stats - - @app.route("/status/", methods=['GET']) def get_status(): status = bb_api_request_processor.APIRequestProcessor().service_status_get() bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'GET_STATUS', status) return status + # API Routes +@app.route("/api/v1.0/stats/", methods=['GET']) +@requires_auth +def get_stats(): + result = bb_api_request_processor.APIRequestProcessor().service_stats_get() + bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'GET_STATS', result) + return jsonify(stats=result) + + @app.route("/api/v1.0/cars/", methods=['GET']) @requires_auth def uri_get_cars(): From 1413ae22368b3477840059bd3091c0a56a69357c Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 22:22:05 +0000 Subject: [PATCH 04/12] Update database connector so that it returns a proper stats son object rather than HTML --- blockbuster/bb_dbconnector_pg.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/blockbuster/bb_dbconnector_pg.py b/blockbuster/bb_dbconnector_pg.py index b8fe88d..7f0ee09 100644 --- a/blockbuster/bb_dbconnector_pg.py +++ b/blockbuster/bb_dbconnector_pg.py @@ -491,32 +491,20 @@ def db_stats_check(self): datetime.date.today().day) try: - sql = "SELECT * from stats_usage " \ + sql = "SELECT date::text, instance_name, name, count from stats_usage " \ "where date = %s;" data = (today,) - self.cursor.execute(sql, data) - rows = self.cursor.fetchall() - if len(rows) < 1: - return "No User Activity Today" - - stats = "" - if len(rows) > 1: - i = 0 - stats = "" - for row in rows: - statrow = str(row[0]) + ", " + str(row[1]) + ", " + str(row[2] + ", " + str(row[3])) - stats = stats + statrow + "
" - i += 1 - stats = stats + "" - - return stats, 200 + self.dict_cursor.execute(sql, data) + rows = self.dict_cursor.fetchall() + return rows except psycopg2.DatabaseError, e: self.log_exception(e) log.error("Error getting stats from database.\n" + str(e)) - return "500: Error retrieving stats from database", 500 + empty = {} + return {} # Take a mobile number, return a list of active blocks from that number def get_list_of_blocks_for_blocker(self, blocker_mobile): From e4df2b0b782a4083b8f8cb8213d41081d98f3c9d Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 22:39:42 +0000 Subject: [PATCH 05/12] Formatting --- blockbuster/bb_routes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blockbuster/bb_routes.py b/blockbuster/bb_routes.py index 602e5bf..0601ef4 100644 --- a/blockbuster/bb_routes.py +++ b/blockbuster/bb_routes.py @@ -67,7 +67,8 @@ def get_status(): @app.route("/api/v1.0/stats/", methods=['GET']) @requires_auth def get_stats(): - result = bb_api_request_processor.APIRequestProcessor().service_stats_get() + result = bb_api_request_processor.APIRequestProcessor()\ + .service_stats_get() bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'GET_STATS', result) return jsonify(stats=result) From 76659513a102e69d6eeea8c3d7a15aa677c1bf78 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:13:57 +0000 Subject: [PATCH 06/12] Add new DB upgrade script --- sql/008_1.23.00-1.24.00.sql | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sql/008_1.23.00-1.24.00.sql diff --git a/sql/008_1.23.00-1.24.00.sql b/sql/008_1.23.00-1.24.00.sql new file mode 100644 index 0000000..d0acbb6 --- /dev/null +++ b/sql/008_1.23.00-1.24.00.sql @@ -0,0 +1,50 @@ +BEGIN; + +-- Rename the status_usage view to use v_XXXXXX naming convention +ALTER VIEW IF EXISTS stats_usage RENAME TO v_stats_usage; +ALTER VIEW IF EXISTS active_move_requests RENAME TO v_active_move_requests; +ALTER VIEW IF EXISTS active_blocks RENAME TO v_active_blocks; + + +-- Create Table: tbl_users_api +CREATE TABLE tbl_users_api +( + api_user_ref uuid NOT NULL DEFAULT uuid_generate_v4(), + username text NOT NULL, + password text NOT NULL DEFAULT 0, + enabled boolean NOT NULL DEFAULT true, + CONSTRAINT tbl_users_api_pkey PRIMARY KEY (api_user_ref), + CONSTRAINT tbl_users_api_username_key UNIQUE (username) +) +WITH ( + OIDS=FALSE +); +ALTER TABLE tbl_users_api + OWNER TO blockbuster; + + +-- Create Index: username +CREATE INDEX username + ON tbl_users_api + USING btree + (username COLLATE pg_catalog."default"); + +-- Index: mobile +CREATE INDEX mobile + ON users + USING btree + (mobile COLLATE pg_catalog."default"); + +-- Index: registration +CREATE INDEX registration + ON registrations + USING btree + (registration COLLATE pg_catalog."default"); + + +-- Update schema version to 1.24.00 +UPDATE general +SET value = '1.24.00' +WHERE key = 'version'; + +COMMIT; From be73506aac8e0dfe8cb873889940bb1b5583e590 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:14:07 +0000 Subject: [PATCH 07/12] Add upgrade script to master creation script --- sql/UpgradeDB.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql/UpgradeDB.sql b/sql/UpgradeDB.sql index 1b2908e..3e5cd0a 100644 --- a/sql/UpgradeDB.sql +++ b/sql/UpgradeDB.sql @@ -4,4 +4,5 @@ \i ./004_1.18.00-1.19.00.sql \i ./005_1.19.00-1.20.00.sql \i ./006_1.20.00-1.20.01.sql -\i ./007_1.20.01-1.23.00.sql \ No newline at end of file +\i ./007_1.20.01-1.23.00.sql +\i ./008_1.23.00-1.24.00.sql \ No newline at end of file From 839dc131921fd3eb8c3016ff08ffa0f1b03f1063 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:14:38 +0000 Subject: [PATCH 08/12] Update stats query so that it returns the date field as a string --- blockbuster/bb_dbconnector_pg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockbuster/bb_dbconnector_pg.py b/blockbuster/bb_dbconnector_pg.py index b8fe88d..646ed6f 100644 --- a/blockbuster/bb_dbconnector_pg.py +++ b/blockbuster/bb_dbconnector_pg.py @@ -491,7 +491,7 @@ def db_stats_check(self): datetime.date.today().day) try: - sql = "SELECT * from stats_usage " \ + sql = "SELECT date::text, instance_name, name, count from v_stats_usage " \ "where date = %s;" data = (today,) From c941830fd55853b10ca177763642eb391de692cc Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:14:57 +0000 Subject: [PATCH 09/12] Update query to follow new view naming convention (v_xxxxx) --- blockbuster/bb_dbconnector_pg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockbuster/bb_dbconnector_pg.py b/blockbuster/bb_dbconnector_pg.py index 646ed6f..46aeee7 100644 --- a/blockbuster/bb_dbconnector_pg.py +++ b/blockbuster/bb_dbconnector_pg.py @@ -942,7 +942,7 @@ def api_blocks_getall(self): log.debug("Getting all blocks") try: - sql = "SELECT * from active_blocks;" + sql = "SELECT * from v_active_blocks;" self.dict_cursor.execute(sql,) rows = self.dict_cursor.fetchall() return rows From 6f2ae1ea283cc597c328b16560c22d27e846c1fe Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:17:43 +0000 Subject: [PATCH 10/12] Resolve conflicts --- blockbuster/bb_dbconnector_pg.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/blockbuster/bb_dbconnector_pg.py b/blockbuster/bb_dbconnector_pg.py index 46aeee7..c349177 100644 --- a/blockbuster/bb_dbconnector_pg.py +++ b/blockbuster/bb_dbconnector_pg.py @@ -496,27 +496,15 @@ def db_stats_check(self): data = (today,) - self.cursor.execute(sql, data) - rows = self.cursor.fetchall() - if len(rows) < 1: - return "No User Activity Today" - - stats = "" - if len(rows) > 1: - i = 0 - stats = "" - for row in rows: - statrow = str(row[0]) + ", " + str(row[1]) + ", " + str(row[2] + ", " + str(row[3])) - stats = stats + statrow + "
" - i += 1 - stats = stats + "" - - return stats, 200 + self.dict_cursor.execute(sql, data) + rows = self.dict_cursor.fetchall() + return rows except psycopg2.DatabaseError, e: self.log_exception(e) log.error("Error getting stats from database.\n" + str(e)) - return "500: Error retrieving stats from database", 500 + empty = {} + return {} # Take a mobile number, return a list of active blocks from that number def get_list_of_blocks_for_blocker(self, blocker_mobile): From 43b41391ece673a847781b19885541ae7c9d22a2 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:24:27 +0000 Subject: [PATCH 11/12] Make sure result can be written to log --- blockbuster/bb_routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockbuster/bb_routes.py b/blockbuster/bb_routes.py index 0601ef4..7261229 100644 --- a/blockbuster/bb_routes.py +++ b/blockbuster/bb_routes.py @@ -69,7 +69,7 @@ def get_status(): def get_stats(): result = bb_api_request_processor.APIRequestProcessor()\ .service_stats_get() - bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'GET_STATS', result) + bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'GET_STATS', str(result)) return jsonify(stats=result) From 101e57572ddabff81f08e7b0513021b5b04fcc70 Mon Sep 17 00:00:00 2001 From: Matt Stibbs Date: Thu, 8 Jan 2015 23:26:12 +0000 Subject: [PATCH 12/12] Uplift version number --- blockbuster/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockbuster/__init__.py b/blockbuster/__init__.py index 5a3baad..1f71476 100644 --- a/blockbuster/__init__.py +++ b/blockbuster/__init__.py @@ -1,6 +1,6 @@ __author__ = 'matt' -__version__ = '1.23.05' -__db_schema_version__ = '1.23.00' +__version__ = '1.24.00' +__db_schema_version__ = '1.24.00' from flask import Flask app = Flask(__name__)