Ejemplo n.º 1
0
    def _fetch_remote_stats_async(self, node_ip, newer_than, include_lists):
        # Security header
        headers = {SECRET_HEADER: options.secret}
        # Build query arguments
        arguments = {}
        if include_lists is not None:
            arguments['include_lists'] = include_lists.asdict()
        if newer_than:
            arguments['newer_than'] = newer_than

        url = "http://{ip}:{port}/{path}".format(ip=node_ip,
                                                 port=constants.HERMES_PORT,
                                                 path=self.method_path)
        request = httpclient.HTTPRequest(url=url,
                                         method='GET',
                                         body=json.dumps(arguments),
                                         headers=headers,
                                         request_timeout=REQUEST_TIMEOUT,
                                         allow_nonstandard_methods=True)
        async_client = httpclient.AsyncHTTPClient()

        try:
            # Send Future object to coroutine and suspend till result is ready
            response = yield async_client.fetch(request)
        except HTTPError as e:
            logging.error("Failed to get stats from {} ({})".format(url, e))
            raise gen.Return(None)

        try:
            snapshot = json.loads(response.body)
            raise gen.Return(
                converter.stats_from_dict(self.stats_model, snapshot))
        except TypeError as err:
            msg = u"Can't parse stats snapshot ({})".format(err)
            raise BadStatsListFormat(msg), None, sys.exc_info()[2]
Ejemplo n.º 2
0
def get_stats_from_file(json_file_name, stats_class):
  with open(os.path.join(TEST_DATA_DIR, json_file_name)) as json_file:
    raw_dict = json.load(json_file)
    stats_dict = {
      ip: converter.stats_from_dict(stats_class, snapshot)
      for ip, snapshot in raw_dict.iteritems()
    }
    return raw_dict, stats_dict
Ejemplo n.º 3
0
def get_stats_from_file(json_file_name, stats_class):
    with open(os.path.join(TEST_DATA_DIR, json_file_name)) as json_file:
        raw_dict = json.load(json_file)
        stats_dict = {
            ip: converter.stats_from_dict(stats_class, snapshot)
            for ip, snapshot in raw_dict.iteritems()
        }
        return raw_dict, stats_dict
Ejemplo n.º 4
0
    def _fetch_remote_stats_async(self, node_ip, max_age, include_lists):
        # Security header
        headers = {SECRET_HEADER: options.secret}
        # Build query arguments
        arguments = {}
        if include_lists is not None:
            arguments['include_lists'] = include_lists.asdict()
        if max_age is not None:
            arguments['max_age'] = max_age

        url = "http://{ip}:{port}/{path}".format(ip=node_ip,
                                                 port=constants.HERMES_PORT,
                                                 path=self.method_path)
        request = httpclient.HTTPRequest(url=url,
                                         method='GET',
                                         body=json.dumps(arguments),
                                         headers=headers,
                                         request_timeout=STATS_REQUEST_TIMEOUT,
                                         allow_nonstandard_methods=True)
        async_client = httpclient.AsyncHTTPClient()

        # Send Future object to coroutine and suspend till result is ready
        response = yield async_client.fetch(request, raise_error=False)
        if response.code >= 400:
            if response.body:
                logging.error(
                    u"Failed to get stats from {url} ({code} {reason}, BODY: {body})"
                    .format(url=url,
                            code=response.code,
                            reason=response.reason,
                            body=response.body))
            else:
                logging.error(
                    u"Failed to get stats from {url} ({code} {reason})".format(
                        url=url, code=response.code, reason=response.reason))
            raise gen.Return(u"{} {}".format(response.code, response.reason))

        try:
            snapshot = json.loads(response.body)
            raise gen.Return(
                converter.stats_from_dict(self.stats_model, snapshot))
        except TypeError as err:
            msg = u"Can't parse stats snapshot ({})".format(err)
            raise BadStatsListFormat(msg), None, sys.exc_info()[2]
Ejemplo n.º 5
0
  def _fetch_remote_stats_async(self, node_ip, max_age, include_lists):
    # Security header
    headers = {SECRET_HEADER: options.secret}
    # Build query arguments
    arguments = {}
    if include_lists is not None:
      arguments['include_lists'] = include_lists.asdict()
    if max_age is not None:
      arguments['max_age'] = max_age

    url = "http://{ip}:{port}/{path}".format(
      ip=node_ip, port=constants.HERMES_PORT, path=self.method_path)
    request = httpclient.HTTPRequest(
      url=url, method='GET', body=json.dumps(arguments), headers=headers,
      request_timeout=STATS_REQUEST_TIMEOUT, allow_nonstandard_methods=True
    )
    async_client = httpclient.AsyncHTTPClient()

    # Send Future object to coroutine and suspend till result is ready
    response = yield async_client.fetch(request, raise_error=False)
    if response.code >= 400:
      if response.body:
        logging.error(
          u"Failed to get stats from {url} ({code} {reason}, BODY: {body})"
          .format(url=url, code=response.code, reason=response.reason,
                  body=response.body)
        )
      else:
        logging.error(
          u"Failed to get stats from {url} ({code} {reason})"
          .format(url=url, code=response.code, reason=response.reason)
        )
      raise gen.Return(u"{} {}".format(response.code, response.reason))

    try:
      snapshot = json.loads(response.body)
      raise gen.Return(converter.stats_from_dict(self.stats_model, snapshot))
    except TypeError as err:
      msg = u"Can't parse stats snapshot ({})".format(err)
      raise BadStatsListFormat(msg), None, sys.exc_info()[2]