Пример #1
0
def record(request):
    """ Respond to the "/api/latest/monitoring/record" URL.

        We record a new event based on the supplied parameters.
    """
    if request.method != "POST":
        return HttpResponseNotAllowed(["POST"])

    params = request.POST

    # Check the caller's authentication.

    # ...eventually.

    # Get our event parameters.

    if "source" in params:
        source = params['source']
    else:
        return HttpResponseBadRequest("Missing required 'source' parameter")

    if "type" in params:
        type = params['type']
    else:
        return HttpResponseBadRequest("Missing required 'type' parameter")

    if "primary_value" in params:
        primary_value = params['primary_value']
    else:
        primary_value = None

    if "secondary_value" in params:
        secondary_value = params['secondary_value']
    else:
        secondary_value = None

    if "text" in params:
        text = params['text']
    else:
        text = None

    # Add the event to the system.

    err_msg = event_recorder.record(source, type,
                                    primary_value, secondary_value, text)

    if err_msg == None:
        return HttpResponse("OK")
    else:
        return HttpResponse(err_msg)
Пример #2
0
def record(source, type, primary_value=None, secondary_value=None, text=None):
    """ Record the occurrence of an event.

        The parameters are as follows:

            'source'

                A string indicating the source of this event.

            'type'

                A string indicating the type of event.

            'primary_value'

                An integer giving the primary value for this event, if any.

            'secondary_value'

                An integer giving the secondary value for this event, if any.

            'text'

                Some text to associate with this event, if any.

        If the Monitoring API is installed on this computer, we call the
        monitoring API directly to do the work.  Otherwise, we send off an HTTP
        request to the monitoring API machine to record the event remotely.

        If an error occurred, we raise a suitable RuntimeError so that the
        caller will know that something went wrong.
    """
    if "dataCommons.monitoringAPI" in settings.INSTALLED_APPS:
        # The monitoring API is directly available -> call it.
        from dataCommons.monitoringAPI import event_recorder
        err_msg = event_recorder.record(source, type,
                                        primary_value, secondary_value, text)
        if err_msg != None:
            raise RuntimeError(err_msg)
    else:
        # The monitoring API is on another computer -> send off an HTTP
        # request.
        raise RuntimeError("Remote event reporting is not implemented yet")