def create_app(self): ''' Create base test app ''' if not TestApp.init: API.add_namespace(LOCATIONS_NS) API.add_namespace(SERVER_ASN_NS) API.add_namespace(CLIENT_ASN_NS) API.add_namespace(DEBUG_NS) API.init_app(app) TestApp.init = True return app
from flask_restplus import fields from mlab_api.rest_api import API from mlab_api.format_utils import meta_results_to_csv, meta_data_to_csv, \ meta_data_in_row_to_csv, meta_in_row_to_csv from mlab_api.models.base_models import LOCATION_META_FIELDS, \ METRIC_META_FIELDS, LOCATION_CLIENT_META_FIELDS, \ LOCATION_CLIENT_SERVER_META_FIELDS, LOCATION_SEARCH_META_FIELDS, \ SEARCH_DATA_FIELDS, LOCATION_INFO_DATA_FIELDS, LOCATION_SERVER_META_FIELDS # ------------------------------------------- # Locations: search # ------------------------------------------- LOCATION_SEARCH_RESULT_FIELDS = API.model('Search Result', { 'meta': fields.Nested(LOCATION_SEARCH_META_FIELDS, required=True), 'data': fields.Nested(SEARCH_DATA_FIELDS, required=False) }) LOCATION_SEARCH_MODEL = API.model('Location Search Model', { 'results': fields.List(fields.Nested(LOCATION_SEARCH_RESULT_FIELDS), required=True) }) def location_search_to_csv(data): ''' Convert location search results to CSV ''' return meta_data_in_row_to_csv(data, LOCATION_SEARCH_META_FIELDS, SEARCH_DATA_FIELDS)
''' from flask_restplus import fields from mlab_api.rest_api import API from mlab_api.format_utils import meta_results_to_csv, \ meta_data_in_row_to_csv, meta_data_to_csv from mlab_api.models.base_models import SEARCH_DATA_FIELDS, \ METRIC_META_FIELDS, SERVER_SEARCH_META_FIELDS, SERVER_META_FIELDS # ------------------------------------------- # Servers: search # ------------------------------------------- SERVER_SEARCH_RESULTS_FIELD = API.model('Server ASN Search Result', { 'meta': fields.Nested(SERVER_SEARCH_META_FIELDS, required=True), 'data': fields.Nested(SEARCH_DATA_FIELDS, required=True) }) SERVER_SEARCH_MODEL = API.model('Server ASN Search Results', { 'results': fields.List(fields.Nested(SERVER_SEARCH_RESULTS_FIELD), required=True) }) def server_search_to_csv(data): ''' Converts server search results into a CSV ''' return meta_data_in_row_to_csv(data, SERVER_SEARCH_META_FIELDS, SEARCH_DATA_FIELDS)
from flask_restplus import fields from mlab_api.rest_api import API from mlab_api.id_utils import location_id, location_client_id, \ location_server_id, location_client_server_id, client_id, server_id, \ client_server_id # ---------------------------------------------------- # Generic Data Fields # ---------------------------------------------------- SEARCH_DATA_FIELDS = API.model( 'Search Data', { 'last_three_months_test_count': fields.Integer(description="Test counts over last 3 months."), 'last_six_months_test_count': fields.Integer(description="Test counts in last six months"), 'last_year_test_count': fields.Integer(description="Test counts in last year"), 'test_count': fields.Integer(description="Test counts over entire MLab dataset"), }) # Unfortunately, currently some models include test count in meta and some do # it in data (e.g., see location info vs location+client info). # Ideally this gets fixed at some point. SEARCH_META_FIELDS = API.model( 'Search Meta', { 'last_three_months_test_count': fields.Integer(description="Test counts over last 3 months."), 'last_six_months_test_count': fields.Integer(description="Test counts in last six months"),
from mlab_api.url_utils import get_time_window, get_filter, normalize_key from mlab_api.models.location_models import LOCATION_SERVER_LIST_MODEL, \ location_server_list_to_csv from mlab_api.models.client_models import CLIENT_SERVER_LIST_MODEL, \ client_server_list_to_csv from mlab_api.models.server_models import SERVER_SEARCH_MODEL, \ server_search_to_csv, SERVER_INFO_MODEL, server_info_to_csv, \ SERVER_METRIC_MODEL, server_metric_to_csv from mlab_api.decorators import format_response from mlab_api.stats import ANALYTICS SERVER_ASN_NS = API.namespace('servers', description='Server ASN specific API') @SERVER_ASN_NS.route('/search') class ServerSearch(Resource): ''' Server Search ''' @API.expect(SEARCH_ARGUMENTS) @format_response(server_search_to_csv) @API.marshal_with(SERVER_SEARCH_MODEL) def get(self): """ Search for Servers matching a query. """
from flask_restplus import fields from mlab_api.rest_api import API from mlab_api.format_utils import meta_results_to_csv, meta_data_to_csv, \ meta_data_in_row_to_csv, meta_in_row_to_csv from mlab_api.models.base_models import SEARCH_DATA_FIELDS, \ METRIC_META_FIELDS, CLIENT_SEARCH_META_FIELDS, CLIENT_META_FIELDS, \ CLIENT_SERVER_META_FIELDS # ------------------------------------------- # Clients: search # ------------------------------------------- CLIENT_SEARCH_RESULT_FIELDS = API.model('Client ASN Search Result', { 'meta': fields.Nested(CLIENT_SEARCH_META_FIELDS, required=True), 'data': fields.Nested(SEARCH_DATA_FIELDS, required=True) }) CLIENT_SEARCH_MODEL = API.model('Client ASN Search Results', { 'results': fields.List(fields.Nested(CLIENT_SEARCH_RESULT_FIELDS), required=True) }) def client_search_to_csv(data): ''' Convert client search results to CSV ''' return meta_data_in_row_to_csv(data, CLIENT_SEARCH_META_FIELDS, SEARCH_DATA_FIELDS)
location_metric_to_csv, LOCATION_CLIENT_METRIC_MODEL, \ location_client_metric_to_csv, LOCATION_SERVER_METRIC_MODEL, \ location_server_metric_to_csv, LOCATION_CLIENT_SERVER_METRIC_MODEL, \ location_client_server_metric_to_csv, LOCATION_INFO_MODEL, \ location_info_to_csv, LOCATION_CHILDREN_MODEL, location_children_to_csv, \ LOCATION_CLIENT_ISP_INFO_MODEL, location_client_isp_info_to_csv from mlab_api.url_utils import get_time_window, normalize_key, get_filter from mlab_api.decorators import format_response from mlab_api.rest_api import API from mlab_api.stats import ANALYTICS # this is the namespace that gets included elsewhere. LOCATIONS_NS = API.namespace('locations', description='Location specific API') @LOCATIONS_NS.route('/search') class LocationSearch(Resource): ''' Location Search Resource ''' @API.expect(SEARCH_ARGUMENTS) @format_response(location_search_to_csv) @API.marshal_with(LOCATION_SEARCH_MODEL) def get(self): """ Get all Locations matching a query """ args = SEARCH_ARGUMENTS.parse_args(request)
# -*- coding: utf-8 -*- # pylint: disable=no-self-use ''' Sample Raw Data ''' from mlab_api.rest_api import API from mlab_api.data.data import RAW_DATA as DATA from flask_restplus import Resource RAW_NS = API.namespace('raw', description='') @RAW_NS.route('/tests') class RawTests(Resource): ''' Raw data tests ''' def get(self): """ Returns a sample of raw upload/download data including lat/lon positions. """ results = DATA.get_raw_test_results() return results
location_server_list_to_csv, LOCATION_METRIC_MODEL, \ location_metric_to_csv, LOCATION_CLIENT_METRIC_MODEL, \ location_client_metric_to_csv, LOCATION_SERVER_METRIC_MODEL, \ location_server_metric_to_csv, LOCATION_CLIENT_SERVER_METRIC_MODEL, \ location_client_server_metric_to_csv, LOCATION_INFO_MODEL, \ location_info_to_csv, LOCATION_CHILDREN_MODEL, location_children_to_csv, \ LOCATION_CLIENT_ISP_INFO_MODEL, location_client_isp_info_to_csv from mlab_api.url_utils import get_time_window, normalize_key, get_filter from mlab_api.decorators import format_response from mlab_api.rest_api import API from mlab_api.stats import ANALYTICS # this is the namespace that gets included elsewhere. LOCATIONS_NS = API.namespace('locations', description='Location specific API') @LOCATIONS_NS.route('/search') class LocationSearch(Resource): ''' Location Search Resource ''' @API.expect(SEARCH_ARGUMENTS) @format_response(location_search_to_csv) @API.marshal_with(LOCATION_SEARCH_MODEL) def get(self): """ Get all Locations matching a query """
from flask_restplus import fields from mlab_api.rest_api import API from mlab_api.id_utils import location_id, location_client_id, \ location_server_id, location_client_server_id, client_id, server_id, \ client_server_id # ---------------------------------------------------- # Generic Data Fields # ---------------------------------------------------- SEARCH_DATA_FIELDS = API.model('Search Data', { 'last_three_months_test_count': fields.Integer( description="Test counts over last 3 months."), 'last_six_months_test_count': fields.Integer( description="Test counts in last six months"), 'last_year_test_count': fields.Integer( description="Test counts in last year"), 'test_count': fields.Integer( description="Test counts over entire MLab dataset"), }) # Unfortunately, currently some models include test count in meta and some do # it in data (e.g., see location info vs location+client info). # Ideally this gets fixed at some point. SEARCH_META_FIELDS = API.model('Search Meta', { 'last_three_months_test_count': fields.Integer( description="Test counts over last 3 months."), 'last_six_months_test_count': fields.Integer( description="Test counts in last six months"), 'last_year_test_count': fields.Integer( description="Test counts in last year"),
from flask_restplus import fields from mlab_api.rest_api import API from mlab_api.format_utils import meta_results_to_csv, meta_data_to_csv, \ meta_data_in_row_to_csv, meta_in_row_to_csv from mlab_api.models.base_models import LOCATION_META_FIELDS, \ METRIC_META_FIELDS, LOCATION_CLIENT_META_FIELDS, \ LOCATION_CLIENT_SERVER_META_FIELDS, LOCATION_SEARCH_META_FIELDS, \ SEARCH_DATA_FIELDS, LOCATION_INFO_DATA_FIELDS, LOCATION_SERVER_META_FIELDS # ------------------------------------------- # Locations: search # ------------------------------------------- LOCATION_SEARCH_RESULT_FIELDS = API.model( 'Search Result', { 'meta': fields.Nested(LOCATION_SEARCH_META_FIELDS, required=True), 'data': fields.Nested(SEARCH_DATA_FIELDS, required=False) }) LOCATION_SEARCH_MODEL = API.model( 'Location Search Model', { 'results': fields.List(fields.Nested(LOCATION_SEARCH_RESULT_FIELDS), required=True) }) def location_search_to_csv(data): ''' Convert location search results to CSV '''
from mlab_api.endpoints.raw import RAW_NS from mlab_api.decorators import format_from_url_decorator, download_decorator # API is defined here from mlab_api.rest_api import API ROOT = logging.getLogger() ROOT.setLevel(logging.DEBUG) # This provides CORS for all API Requests and adds in our media type coercing # based on `format` API.decorators = [cors.crossdomain(origin='*'), format_from_url_decorator, download_decorator] # Add namespaces defined in endpoints module API.add_namespace(LOCATIONS_NS) API.add_namespace(CLIENT_ASN_NS) API.add_namespace(SERVER_ASN_NS) API.add_namespace(RAW_NS) # init API with Flask App API.init_app(app) DEBUG_FLAG = False API_MODE = os.environ.get("API_MODE") print(API_MODE) if API_MODE == 'staging' or API_MODE == 'sandbox': DEBUG_FLAG = True API.add_namespace(DEBUG_NS) else:
# -*- coding: utf-8 -*- # pylint: disable=no-self-use ''' Potentially helpful debugging routes. ''' import os from mlab_api.rest_api import API from mlab_api.data.data import LOCATION_DATA as DATA from flask_restplus import Resource DEBUG_NS = API.namespace('debug', description='Debug Help') @DEBUG_NS.route('/connection') class Connection(Resource): ''' Debug Connection Resource ''' def get(self): """ Lists BigTable Connection Details Indicate if a BigTable connection has been made \ and if so, what tables are accessible to the API. """ pool = DATA.get_pool() if pool: with pool.connection() as connection: return { "message": "Connection",
# -*- coding: utf-8 -*- # pylint: disable=no-self-use ''' Potentially helpful debugging routes. ''' from mlab_api.rest_api import API from mlab_api.data.data import LOCATION_DATA as DATA from flask_restplus import Resource DEBUG_NS = API.namespace('debug', description='Debug Help') @DEBUG_NS.route('/connection') class Connection(Resource): ''' Debug Connection Resource ''' def get(self): """ Lists BigTable Connection Details Indicate if a BigTable connection has been made \ and if so, what tables are accessible to the API. """ pool = DATA.get_pool() if pool: with pool.connection() as connection: return {"message": "Connection", "tables": connection.tables()} else: return {"error": 'No Connection', "tables": []}
from mlab_api.parsers import DATE_ARGUMENTS, SEARCH_ARGUMENTS, \ INCLUDE_DATA_ARGUMENTS, TOP_ARGUMENTS from mlab_api.url_utils import get_time_window, get_filter, normalize_key from mlab_api.models.location_models import LOCATION_CLIENT_LIST_MODEL, \ location_client_list_to_csv from mlab_api.models.client_models import CLIENT_SEARCH_MODEL, \ client_search_to_csv, CLIENT_INFO_MODEL, client_info_to_csv, \ CLIENT_METRIC_MODEL, client_metric_to_csv, \ CLIENT_SERVER_METRIC_MODEL, client_server_metric_to_csv, \ CLIENT_SERVER_LIST_MODEL, client_server_list_to_csv from mlab_api.decorators import format_response CLIENT_ASN_NS = API.namespace('clients', description='Client ASN specific API') @CLIENT_ASN_NS.route('/search') class ClientAsnSearch(Resource): ''' Client Search ''' @API.expect(SEARCH_ARGUMENTS) @format_response(client_search_to_csv) @API.marshal_with(CLIENT_SEARCH_MODEL) def get(self): """ Search clients for a given query """