Esempio n. 1
0
    def get_problem_list(self, course_id, grader_id):
        """
        Get the list of problems for a given course.

        Args:
            course_id: course id that we want the problems of
            grader_id: who is grading this?  The anonymous user_id of the grader.

        Returns:
            dict with the response from the service.  (Deliberately not
            writing out the fields here--see the docs on the staff_grading view
            in the grading_controller repo)

        Raises:
            GradingServiceError: something went wrong with the connection.
        """
        params = {
            'course_id': course_id.to_deprecated_string(),
            'grader_id': grader_id
        }
        result = self.get(self.get_problem_list_url, params)
        tags = [u'course_id:{}'.format(course_id)]
        self._record_result('get_problem_list', result, tags)
        dog_stats_api.histogram(
            self._metric_name('get_problem_list.result.length'),
            len(result.get('problem_list', [])))
        return result
    def get_problem_list(self, course_id, grader_id):
        """
        Get the list of problems for a given course.

        Args:
            course_id: course id that we want the problems of
            grader_id: who is grading this?  The anonymous user_id of the grader.

        Returns:
            dict with the response from the service.  (Deliberately not
            writing out the fields here--see the docs on the staff_grading view
            in the grading_controller repo)

        Raises:
            GradingServiceError: something went wrong with the connection.
        """
        params = {'course_id': course_id.to_deprecated_string(), 'grader_id': grader_id}
        result = self.get(self.get_problem_list_url, params)
        tags = [u'course_id:{}'.format(course_id)]
        self._record_result('get_problem_list', result, tags)
        dog_stats_api.histogram(
            self._metric_name('get_problem_list.result.length'),
            len(result.get('problem_list', []))
        )
        return result
    def timer(self, metric_name, course_context):
        """
        Contextmanager which acts as a timer for the metric ``metric_name``,
        but which also yields a :class:`Tagger` object that allows the timed block
        of code to add tags and quantity measurements. Tags are added verbatim to the
        timer output. Measurements are recorded as histogram measurements in their own,
        and also as bucketed tags on the timer measurement.

        Arguments:
            metric_name: The name used to aggregate all of these metrics.
            course_context: The course which the query is being made for.
        """
        tagger = Tagger(self._sample_rate)
        metric_name = "{}.{}".format(self._metric_base, metric_name)

        start = time()
        try:
            yield tagger
        finally:
            end = time()
            tags = tagger.tags
            tags.append("course:{}".format(course_context))
            for name, size in tagger.measures:
                dog_stats_api.histogram(
                    "{}.{}".format(metric_name, name),
                    size,
                    timestamp=end,
                    tags=[tag for tag in tags if not tag.startswith("{}:".format(metric_name))],
                    sample_rate=tagger.sample_rate,
                )
            dog_stats_api.histogram(
                "{}.duration".format(metric_name), end - start, timestamp=end, tags=tags, sample_rate=tagger.sample_rate
            )
            dog_stats_api.increment(metric_name, timestamp=end, tags=tags, sample_rate=tagger.sample_rate)
    def check_for_eta(self, location):
        params = {
            'location': location,
        }
        data = self.get(self.check_eta_url, params)
        self._record_result('check_for_eta', data)
        dog_stats_api.histogram(self._metric_name('check_for_eta.eta'), data.get('eta', 0))

        return data
 def _ddog_histogram(self, evt_time, evt_name, value):
     """
     DataDog histogram method.
     """
     dog_stats_api.histogram(
         'DjangoXBlockUserStateClient.{}'.format(evt_name),
         value,
         timestamp=evt_time,
         sample_rate=self.API_DATADOG_SAMPLE_RATE,
     )
Esempio n. 6
0
    def check_for_eta(self, location):
        params = {
            'location': location,
        }
        data = self.get(self.check_eta_url, params)
        self._record_result('check_for_eta', data)
        dog_stats_api.histogram(self._metric_name('check_for_eta.eta'),
                                data.get('eta', 0))

        return data
 def _ddog_histogram(self, evt_time, evt_name, value):
     """
     DataDog histogram method.
     """
     dog_stats_api.histogram(
         'DjangoXBlockUserStateClient.{}'.format(evt_name),
         value,
         timestamp=evt_time,
         sample_rate=self.API_DATADOG_SAMPLE_RATE,
     )
Esempio n. 8
0
    def get_data_for_location(self, problem_location, student_id):
        if isinstance(problem_location, UsageKey):
            problem_location = problem_location.to_deprecated_string()
        params = {"location": problem_location, "student_id": student_id}
        result = self.get(self.get_data_for_location_url, params)
        self._record_result("get_data_for_location", result)
        for key in result.keys():
            if key in ("success", "error", "version"):
                continue

            dog_stats_api.histogram(self._metric_name("get_data_for_location.{}".format(key)), result[key])
        return result
Esempio n. 9
0
    def get_problem_list(self, course_id, grader_id):
        params = {"course_id": course_id.to_deprecated_string(), "student_id": grader_id}
        result = self.get(self.get_problem_list_url, params)

        if "problem_list" in result:
            for problem in result["problem_list"]:
                problem["location"] = course_id.make_usage_key_from_deprecated_string(problem["location"])

        self._record_result("get_problem_list", result)
        dog_stats_api.histogram(
            self._metric_name("get_problem_list.result.length"), len(result.get("problem_list", []))
        )
        return result
Esempio n. 10
0
    def get_flagged_problem_list(self, course_id):
        params = {
            'course_id': course_id.to_deprecated_string(),
        }

        data = self.get(self.flagged_problem_list_url, params)

        tags = [u'course_id:{}'.format(course_id.to_deprecated_string())]
        self._record_result('get_flagged_problem_list', data, tags)
        dog_stats_api.histogram(
            self._metric_name('get_flagged_problem_list.length'),
            len(data.get('flagged_submissions', [])))
        return data
    def get_problem_list(self, course_id, grader_id):
        params = {'course_id': course_id.to_deprecated_string(), 'student_id': grader_id}
        result = self.get(self.get_problem_list_url, params)

        if 'problem_list' in result:
            for problem in result['problem_list']:
                problem['location'] = course_id.make_usage_key_from_deprecated_string(problem['location'])

        self._record_result('get_problem_list', result)
        dog_stats_api.histogram(
            self._metric_name('get_problem_list.result.length'),
            len(result.get('problem_list',[]))
        )
        return result
    def get_flagged_problem_list(self, course_id):
        params = {
            'course_id': course_id.to_deprecated_string(),
        }

        data = self.get(self.flagged_problem_list_url, params)

        tags = [u'course_id:{}'.format(course_id.to_deprecated_string())]
        self._record_result('get_flagged_problem_list', data, tags)
        dog_stats_api.histogram(
            self._metric_name('get_flagged_problem_list.length'),
            len(data.get('flagged_submissions', []))
        )
        return data
    def get_data_for_location(self, problem_location, student_id):
        if isinstance(problem_location, UsageKey):
            problem_location = problem_location.to_deprecated_string()
        params = {'location': problem_location, 'student_id': student_id}
        result = self.get(self.get_data_for_location_url, params)
        self._record_result('get_data_for_location', result)
        for key in result.keys():
            if key in ('success', 'error', 'version'):
                continue

            dog_stats_api.histogram(
                self._metric_name('get_data_for_location.{}'.format(key)),
                result[key],
            )
        return result
Esempio n. 14
0
    def get_grading_status_list(self, course_id, student_id):
        params = {
            'student_id': student_id,
            'course_id': course_id.to_deprecated_string(),
        }

        data = self.get(self.grading_status_list_url, params)

        tags = [u'course_id:{}'.format(course_id.to_deprecated_string())]
        self._record_result('get_grading_status_list', data, tags)
        dog_stats_api.histogram(
            self._metric_name('get_grading_status_list.length'),
            len(data.get('problem_list', [])),
            tags=tags)
        return data
Esempio n. 15
0
    def get_data_for_location(self, problem_location, student_id):
        if isinstance(problem_location, UsageKey):
            problem_location = problem_location.to_deprecated_string()
        params = {'location': problem_location, 'student_id': student_id}
        result = self.get(self.get_data_for_location_url, params)
        self._record_result('get_data_for_location', result)
        for key in result.keys():
            if key in ('success', 'error', 'version'):
                continue

            dog_stats_api.histogram(
                self._metric_name('get_data_for_location.{}'.format(key)),
                result[key],
            )
        return result
    def get_grading_status_list(self, course_id, student_id):
        params = {
            'student_id': student_id,
            'course_id': course_id.to_deprecated_string(),
        }

        data = self.get(self.grading_status_list_url, params)

        tags = [u'course_id:{}'.format(course_id.to_deprecated_string())]
        self._record_result('get_grading_status_list', data, tags)
        dog_stats_api.histogram(
            self._metric_name('get_grading_status_list.length'),
            len(data.get('problem_list', [])),
            tags=tags
        )
        return data
Esempio n. 17
0
    def timer(self, metric_name, course_context):
        """
        Contextmanager which acts as a timer for the metric ``metric_name``,
        but which also yields a :class:`Tagger` object that allows the timed block
        of code to add tags and quantity measurements. Tags are added verbatim to the
        timer output. Measurements are recorded as histogram measurements in their own,
        and also as bucketed tags on the timer measurement.

        Arguments:
            metric_name: The name used to aggregate all of these metrics.
            course_context: The course which the query is being made for.
        """
        tagger = Tagger(self._sample_rate)
        metric_name = "{}.{}".format(self._metric_base, metric_name)

        start = time()
        try:
            yield tagger
        finally:
            end = time()
            tags = tagger.tags
            tags.append('course:{}'.format(course_context))
            for name, size in tagger.measures:
                dog_stats_api.histogram(
                    '{}.{}'.format(metric_name, name),
                    size,
                    timestamp=end,
                    tags=[
                        tag for tag in tags
                        if not tag.startswith('{}:'.format(metric_name))
                    ],
                    sample_rate=tagger.sample_rate,
                )
            dog_stats_api.histogram(
                '{}.duration'.format(metric_name),
                end - start,
                timestamp=end,
                tags=tags,
                sample_rate=tagger.sample_rate,
            )
            dog_stats_api.increment(
                metric_name,
                timestamp=end,
                tags=tags,
                sample_rate=tagger.sample_rate,
            )
    def get_problem_list(self, course_id, grader_id):
        params = {
            'course_id': course_id.to_deprecated_string(),
            'student_id': grader_id
        }
        result = self.get(self.get_problem_list_url, params)

        if 'problem_list' in result:
            for problem in result['problem_list']:
                problem[
                    'location'] = course_id.make_usage_key_from_deprecated_string(
                        problem['location'])

        self._record_result('get_problem_list', result)
        dog_stats_api.histogram(
            self._metric_name('get_problem_list.result.length'),
            len(result.get('problem_list', [])))
        return result
Esempio n. 19
0
def perform_request(method,
                    url,
                    data_or_params=None,
                    raw=False,
                    metric_action=None,
                    metric_tags=None,
                    paged_results=False):

    if metric_tags is None:
        metric_tags = []

    metric_tags.append(u'method:{}'.format(method))
    if metric_action:
        metric_tags.append(u'action:{}'.format(metric_action))

    if data_or_params is None:
        data_or_params = {}
    headers = {
        'X-Edx-Api-Key': getattr(settings, "COMMENTS_SERVICE_KEY", None),
        'Accept-Language': get_language(),
    }
    request_id = uuid4()
    request_id_dict = {'request_id': request_id}

    if method in ['post', 'put', 'patch']:
        data = data_or_params
        params = request_id_dict
    else:
        data = None
        params = merge_dict(data_or_params, request_id_dict)
    with request_timer(request_id, method, url, metric_tags):
        response = requests.request(method,
                                    url,
                                    data=data,
                                    params=params,
                                    headers=headers,
                                    timeout=5)

    metric_tags.append(u'status_code:{}'.format(response.status_code))
    if response.status_code > 200:
        metric_tags.append(u'result:failure')
    else:
        metric_tags.append(u'result:success')

    dog_stats_api.increment('comment_client.request.count', tags=metric_tags)

    if 200 < response.status_code < 500:
        raise CommentClientRequestError(response.text, response.status_code)
    # Heroku returns a 503 when an application is in maintenance mode
    elif response.status_code == 503:
        raise CommentClientMaintenanceError(response.text)
    elif response.status_code == 500:
        raise CommentClient500Error(response.text)
    else:
        if raw:
            return response.text
        else:
            try:
                data = response.json()
            except ValueError:
                raise CommentClientError(
                    u"Comments service returned invalid JSON for request {request_id}; first 100 characters: '{content}'"
                    .format(request_id=request_id,
                            content=response.text[:100]))
            if paged_results:
                dog_stats_api.histogram(
                    'comment_client.request.paged.result_count',
                    value=len(data.get('collection', [])),
                    tags=metric_tags)
                dog_stats_api.histogram('comment_client.request.paged.page',
                                        value=data.get('page', 1),
                                        tags=metric_tags)
                dog_stats_api.histogram(
                    'comment_client.request.paged.num_pages',
                    value=data.get('num_pages', 1),
                    tags=metric_tags)
            return data
Esempio n. 20
0
def perform_request(method, url, data_or_params=None, raw=False,
                    metric_action=None, metric_tags=None, paged_results=False):

    if metric_tags is None:
        metric_tags = []

    metric_tags.append(u'method:{}'.format(method))
    if metric_action:
        metric_tags.append(u'action:{}'.format(metric_action))

    if data_or_params is None:
        data_or_params = {}
    headers = {
        'X-Edx-Api-Key': getattr(settings, "COMMENTS_SERVICE_KEY", None),
        'Accept-Language': get_language(),
    }
    request_id = uuid4()
    request_id_dict = {'request_id': request_id}

    if method in ['post', 'put', 'patch']:
        data = data_or_params
        params = request_id_dict
    else:
        data = None
        params = merge_dict(data_or_params, request_id_dict)
    with request_timer(request_id, method, url, metric_tags):
        response = requests.request(
            method,
            url,
            data=data,
            params=params,
            headers=headers,
            timeout=20 # to solve issue https://app.asana.com/0/15875049854186/166307883409960
        )

    metric_tags.append(u'status_code:{}'.format(response.status_code))
    if response.status_code > 200:
        metric_tags.append(u'result:failure')
    else:
        metric_tags.append(u'result:success')

    dog_stats_api.increment('comment_client.request.count', tags=metric_tags)

    if 200 < response.status_code < 500:
        raise CommentClientRequestError(response.text, response.status_code)
    # Heroku returns a 503 when an application is in maintenance mode
    elif response.status_code == 503:
        raise CommentClientMaintenanceError(response.text)
    elif response.status_code == 500:
        raise CommentClient500Error(response.text)
    else:
        if raw:
            return response.text
        else:
            try:
                data = response.json()
            except ValueError:
                raise CommentClientError(
                    u"Comments service returned invalid JSON for request {request_id}; first 100 characters: '{content}'".format(
                        request_id=request_id,
                        content=response.text[:100]
                    )
                )
            if paged_results:
                dog_stats_api.histogram(
                    'comment_client.request.paged.result_count',
                    value=len(data.get('collection', [])),
                    tags=metric_tags
                )
                dog_stats_api.histogram(
                    'comment_client.request.paged.page',
                    value=data.get('page', 1),
                    tags=metric_tags
                )
                dog_stats_api.histogram(
                    'comment_client.request.paged.num_pages',
                    value=data.get('num_pages', 1),
                    tags=metric_tags
                )
            return data
Esempio n. 21
0
def perform_request(method,
                    url,
                    data_or_params=None,
                    raw=False,
                    metric_action=None,
                    metric_tags=None,
                    paged_results=False):
    # To avoid dependency conflict
    from django_comment_common.models import ForumsConfig
    config = ForumsConfig.current()

    if not config.enabled:
        raise CommentClientMaintenanceError('service disabled')

    if metric_tags is None:
        metric_tags = []

    metric_tags.append(u'method:{}'.format(method))
    if metric_action:
        metric_tags.append(u'action:{}'.format(metric_action))

    if data_or_params is None:
        data_or_params = {}
    headers = {
        'X-Edx-Api-Key': config.api_key,
        'Accept-Language': get_language(),
    }
    request_id = uuid4()
    request_id_dict = {'request_id': request_id}

    if method in ['post', 'put', 'patch']:
        data = data_or_params
        params = request_id_dict
    else:
        data = None
        params = merge_dict(data_or_params, request_id_dict)
    with request_timer(request_id, method, url, metric_tags):
        response = requests.request(method,
                                    url,
                                    data=data,
                                    params=params,
                                    headers=headers,
                                    timeout=config.connection_timeout)

    metric_tags.append(u'status_code:{}'.format(response.status_code))
    if response.status_code > 200:
        metric_tags.append(u'result:failure')
    else:
        metric_tags.append(u'result:success')

    dog_stats_api.increment('comment_client.request.count', tags=metric_tags)

    if 200 < response.status_code < 500:
        raise CommentClientRequestError(response.text, response.status_code)
    # Heroku returns a 503 when an application is in maintenance mode
    elif response.status_code == 503:
        raise CommentClientMaintenanceError(response.text)
    elif response.status_code == 500:
        raise CommentClient500Error(response.text)
    else:
        if raw:
            return response.text
        else:
            try:
                data = response.json()
            except ValueError:
                message = (u"Comments service returned invalid JSON "
                           "for request {request_id} at url {url} "
                           "with HTTP status code {status_code}; "
                           "first 100 characters: '{content}'")
                raise CommentClientError(
                    message.format(
                        request_id=request_id,
                        url=response.url,
                        status_code=response.status_code,
                        content=response.text[:100],
                    ))
            if paged_results:
                dog_stats_api.histogram(
                    'comment_client.request.paged.result_count',
                    value=len(data.get('collection', [])),
                    tags=metric_tags)
                dog_stats_api.histogram('comment_client.request.paged.page',
                                        value=data.get('page', 1),
                                        tags=metric_tags)
                dog_stats_api.histogram(
                    'comment_client.request.paged.num_pages',
                    value=data.get('num_pages', 1),
                    tags=metric_tags)
            return data
Esempio n. 22
0
def perform_request(
    method, url, data_or_params=None, raw=False, metric_action=None, metric_tags=None, paged_results=False
):

    if metric_tags is None:
        metric_tags = []

    metric_tags.append(u"method:{}".format(method))
    if metric_action:
        metric_tags.append(u"action:{}".format(metric_action))

    if data_or_params is None:
        data_or_params = {}
    headers = {"X-Edx-Api-Key": getattr(settings, "COMMENTS_SERVICE_KEY", None), "Accept-Language": get_language()}
    request_id = uuid4()
    request_id_dict = {"request_id": request_id}

    if method in ["post", "put", "patch"]:
        data = data_or_params
        params = request_id_dict
    else:
        data = None
        params = merge_dict(data_or_params, request_id_dict)
    with request_timer(request_id, method, url, metric_tags):
        response = requests.request(method, url, data=data, params=params, headers=headers, timeout=30)

    metric_tags.append(u"status_code:{}".format(response.status_code))
    if response.status_code > 200:
        metric_tags.append(u"result:failure")
    else:
        metric_tags.append(u"result:success")

    dog_stats_api.increment("comment_client.request.count", tags=metric_tags)

    if 200 < response.status_code < 500:
        raise CommentClientRequestError(response.text, response.status_code)
    # Heroku returns a 503 when an application is in maintenance mode
    elif response.status_code == 503:
        raise CommentClientMaintenanceError(response.text)
    elif response.status_code == 500:
        raise CommentClient500Error(response.text)
    else:
        if raw:
            return response.text
        else:
            try:
                data = response.json()
            except ValueError:
                raise CommentClientError(
                    u"Comments service returned invalid JSON for request {request_id}; first 100 characters: '{content}'".format(
                        request_id=request_id, content=response.text[:100]
                    )
                )
            if paged_results:
                dog_stats_api.histogram(
                    "comment_client.request.paged.result_count", value=len(data.get("collection", [])), tags=metric_tags
                )
                dog_stats_api.histogram(
                    "comment_client.request.paged.page", value=data.get("page", 1), tags=metric_tags
                )
                dog_stats_api.histogram(
                    "comment_client.request.paged.num_pages", value=data.get("num_pages", 1), tags=metric_tags
                )
            return data
Esempio n. 23
0
def perform_request(method, url, data_or_params=None, raw=False,
                    metric_action=None, metric_tags=None, paged_results=False):
    # To avoid dependency conflict
    from django_comment_common.models import ForumsConfig
    config = ForumsConfig.current()

    if not config.enabled:
        raise CommentClientMaintenanceError('service disabled')

    if metric_tags is None:
        metric_tags = []

    metric_tags.append(u'method:{}'.format(method))
    if metric_action:
        metric_tags.append(u'action:{}'.format(metric_action))

    if data_or_params is None:
        data_or_params = {}
    headers = {
        'X-Edx-Api-Key': config.api_key,
        'Accept-Language': get_language(),
    }
    request_id = uuid4()
    request_id_dict = {'request_id': request_id}

    if method in ['post', 'put', 'patch']:
        data = data_or_params
        params = request_id_dict
    else:
        data = None
        params = merge_dict(data_or_params, request_id_dict)
    with request_timer(request_id, method, url, metric_tags):
        response = requests.request(
            method,
            url,
            data=data,
            params=params,
            headers=headers,
            timeout=config.connection_timeout
        )

    metric_tags.append(u'status_code:{}'.format(response.status_code))
    if response.status_code > 200:
        metric_tags.append(u'result:failure')
    else:
        metric_tags.append(u'result:success')

    dog_stats_api.increment('comment_client.request.count', tags=metric_tags)

    if 200 < response.status_code < 500:
        raise CommentClientRequestError(response.text, response.status_code)
    # Heroku returns a 503 when an application is in maintenance mode
    elif response.status_code == 503:
        raise CommentClientMaintenanceError(response.text)
    elif response.status_code == 500:
        raise CommentClient500Error(response.text)
    else:
        if raw:
            return response.text
        else:
            try:
                data = response.json()
            except ValueError:
                raise CommentClientError(
                    u"Invalid JSON response for request {request_id}; first 100 characters: '{content}'".format(
                        request_id=request_id,
                        content=response.text[:100]
                    )
                )
            if paged_results:
                dog_stats_api.histogram(
                    'comment_client.request.paged.result_count',
                    value=len(data.get('collection', [])),
                    tags=metric_tags
                )
                dog_stats_api.histogram(
                    'comment_client.request.paged.page',
                    value=data.get('page', 1),
                    tags=metric_tags
                )
                dog_stats_api.histogram(
                    'comment_client.request.paged.num_pages',
                    value=data.get('num_pages', 1),
                    tags=metric_tags
                )
            return data