<commit_before>import requests
import dateutil.parser
import re
import numpy as np
import json

SERVICE_ENDPOINT = 'https://api.applicationinsights.io'
QUERY_ENDPOINT_PATH_TEMPLATE = 'beta/apps/{0}/query'

# Regex for TimeSpan
TIMESPAN_PATTERN = re.compile(r'((?P<d>[0-9]*).)?(?P<h>[0-9]{2}):(?P<m>[0-9]{2}):(?P<s>[0-9]{2})(.(?P<ms>[0-9]*))?')

###################
## Analytics Client
###################
class AnalyticsClient(object):
    """description of class"""

    def __init__(self, app_id = None, app_key = None):
        if not app_id:
            self._get_app_config_from_file()
        else:
            self._app_id = app_id
            self._app_key = app_key

    def query(self, query):
        data = { 'query' :  query }
        headers = { 
            'x-api-key': self._app_key,
            'Content-Type': 'application/json',
            'Accept-Encoding': 'gzip,deflate'
            }
        full_url = '{0}/{1}'.format(SERVICE_ENDPOINT, QUERY_ENDPOINT_PATH_TEMPLATE.format(self._app_id))
        response = requests.post(full_url, headers=headers, json=data)
        
        # validate response was good
        response.raise_for_status()

        # get the data table
        data = response.json()
        tables = data["Tables"]
        data_table = tables[0]

        result_parser = AnalyticsResultParser(data_table)
        return result_parser.to_result_dictionary()

    def _get_app_config_from_file(self):
        import os.path
        local_config_file = '.analytics_client_config'

        file_path = os.path.join('.', local_config_file)
        if not os.path.isfile(file_path):
              file_path = os.path.join(os.path.expanduser('~'), local_config_file)
              if not os.path.isfile(file_path):
                  raise Exception('App id and key must be provided')<commit_msg>support timespan argument for query<commit_after>import requests
import dateutil.parser
import re
import numpy as np
import json

SERVICE_ENDPOINT = 'https://api.applicationinsights.io'
QUERY_ENDPOINT_PATH_TEMPLATE = 'beta/apps/{0}/query'

# Regex for TimeSpan
TIMESPAN_PATTERN = re.compile(r'((?P<d>[0-9]*).)?(?P<h>[0-9]{2}):(?P<m>[0-9]{2}):(?P<s>[0-9]{2})(.(?P<ms>[0-9]*))?')

###################
## Analytics Client
###################
class AnalyticsClient(object):
    """description of class"""

    def __init__(self, app_id = None, app_key = None):
        if not app_id:
            self._get_app_config_from_file()
        else:
            self._app_id = app_id
            self._app_key = app_key

    def query(self, query, timespan=None):
        data = { 'query' :  query }
        if timespan:
            data['timespan'] = timespan
        headers = { 
            'x-api-key': self._app_key,
            'Content-Type': 'application/json',
            'Accept-Encoding': 'gzip,deflate'
            }
        full_url = '{0}/{1}'.format(SERVICE_ENDPOINT, QUERY_ENDPOINT_PATH_TEMPLATE.format(self._app_id))
        response = requests.post(full_url, headers=headers, json=data)
        
        # validate response was good
        response.raise_for_status()

        # get the data table
        data = response.json()
        tables = data["Tables"]
        data_table = tables[0]

        result_parser = AnalyticsResultParser(data_table)
        return result_parser.to_result_dictionary()

    def _get_app_config_from_file(self):
        import os.path
        local_config_file = '.analytics_client_config'

        file_path = os.path.join('.', local_config_file)
        if not os.path.isfile(file_path):
              file_path = os.path.join(os.path.expanduser('~'), local_config_file)
              if not os.path.isfile(file_path):
                  raise Exception('App id and key must be provided')
{'fraction_non_alphanumeric': 0.1487778958554729, 'fraction_numerical': 0.009032943676939426, 'mean_word_length': 2.70935960591133, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6771394', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1186, 'n_words': 224}