# -*- coding: utf-8 -*-
'''
Data class for accessing data for API calls.
'''
from mlab_api.data.table_config import get_table_config
from mlab_api.constants import TABLE_KEYS
from mlab_api.data.base_data import Data
from mlab_api.decorators import add_ids, add_id
import mlab_api.data.bigtable_utils as bt
import mlab_api.data.data_utils as du
from mlab_api.data.asn_data_util import get_bt_results

class ServerAsnData(Data):
    '''
    Connect to BigTable and pull down data.
    '''

    def get_server_info(self, server_id):
        '''
        Get info for a client

        server_id = id of server.
        '''

        # we are using a hack from list tables
        # so grab the first match from a list table faceted by server ids'
        table_name = du.list_table("clients", "servers")

        table_config = get_table_config(self.table_configs, None, table_name)

        key_fields = du.get_key_fields([server_id], table_config)
        return get_bt_results(key_fields, table_config, self.get_pool())

    @add_ids('client_asn_number')
    def get_server_clients(self, server_id, include_data):
        '''
        Get list and info of client isps for a server

        server_id = id of server.
        include_data = boolean indicating whether to include data attributes
            in results or not.
        '''
        return self.get_list_data(server_id, 'servers', 'clients', include_data)


    @add_ids('location_key')
    def get_server_locations(self, server_id, include_data):
        '''
        Get list and info of locations for a server

        server_id = id of server.
        include_data = boolean indicating whether to include data attributes
            in results or not.
        '''
        return self.get_list_data(server_id, 'servers', 'locations',
                                  include_data)


    @add_id('server_asn_number')
    def get_server_metrics(self, server_id, timebin, starttime, endtime):
        '''
        Get data for specific location at a specific
        frequency between start and stop times.

        server_id = id of server.
        timebin = time aggregation key.
        starttime = start time for metric query.
        endtime = end time for metric query.
        '''

        table_config = get_table_config(self.table_configs, timebin,
                                        TABLE_KEYS["servers"])

        location_key_fields = du.get_key_fields([server_id], table_config)
        formatted = bt.get_time_metric_results(
            location_key_fields, self.get_pool(), timebin, starttime, endtime,
            table_config, "servers")

        return formatted
