text:
"""Tests for apache_beam.runners.worker.sdk_worker."""

# pytype: skip-file

import contextlib
import logging
import unittest
from collections import namedtuple

import grpc
import hamcrest as hc
import mock

from apache_beam.coders import VarIntCoder
from apache_beam.internal.metrics.metric import Metrics as InternalMetrics
from apache_beam.metrics import monitoring_infos
from apache_beam.metrics.execution import MetricsEnvironment
from apache_beam.portability.api import beam_fn_api_pb2
from apache_beam.portability.api import beam_fn_api_pb2_grpc
from apache_beam.portability.api import beam_runner_api_pb2
from apache_beam.portability.api import metrics_pb2
from apache_beam.runners.worker import sdk_worker
from apache_beam.runners.worker import statecache
from apache_beam.runners.worker.sdk_worker import BundleProcessorCache
from apache_beam.runners.worker.sdk_worker import GlobalCachingStateHandler
from apache_beam.runners.worker.sdk_worker import SdkWorker
from apache_beam.utils import thread_pool_executor

_LOGGER = logging.getLogger(__name__)

class BeamFnControlServicer(beam_fn_api_pb2_grpc.BeamFnControlServicer):
  def __init__(self, requests, raise_errors=True):
    self.requests = requests
    self.instruction_ids = set(r.instruction_id for r in requests)
    self.responses = {}
    self.raise_errors = raise_errors

  def Control(self, response_iterator, context):
    for request in self.requests:
      _LOGGER.info("Sending request %s", request)
      yield request
    for response in response_iterator:
      _LOGGER.info("Got response %s", response)
      if response.instruction_id != -1:
        assert response.instruction_id in self.instruction_ids
        assert response.instruction_id not in self.responses
        self.responses[response.instruction_id] = response
        if self.raise_errors and response.error:
          raise RuntimeError(response.error)
        elif len(self.responses) == len(self.requests):
          _LOGGER.info("All %s instructions finished.", len(self.requests))
          return
    raise RuntimeError(
        "Missing responses: %s" %
        (self.instruction_ids - set(self.responses.keys())))

class SdkWorkerTest(unittest.TestCase):
  def _get_process_bundles(self, prefix, size):
    return [
        beam_fn_api_pb2.ProcessBundleDescriptor(
            id=str(str(prefix) + "-" + str(ix)),
            transforms={
                str(ix): beam_runner_api_pb2.PTransform(unique_name=str(ix))
            }) for ix in range(size)
    ]

  def _check_fn_registration_multi_request(self, *args):
    """Check the function registration calls to the sdk_harness.

    Args:
     tuple of request_count, number of process_bundles per request and workers
     counts to process the request.
    """
    for (request_count, process_bundles_per_request) in args:
      requests = []
      process_bundle_descriptors = []

      for i in range(request_count):
        pbd = self._get_process_bundles(i, process_bundles_per_request)
        process_bundle_descriptors.extend(pbd)
        requests.append(
            beam_fn_api_pb2.InstructionRequest(
                instruction_id=str(i),
                register=beam_fn_api_pb2.RegisterRequest(
                    process_bundle_descriptor=process_bundle_descriptors)))

      test_controller = BeamFnControlServicer(requests)

      server = grpc.server(thread_pool_executor.shared_unbounded_instance())
      beam_fn_api_pb2_grpc.add_BeamFnControlServicer_to_server(
          test_controller, server)
      test_port = server.add_insecure_port("[::]:0")
      server.start()

      harness = sdk_worker.SdkHarness(
          "localhost:%s" % test_port, state_cache_size=100)
      harness.run()

      self.assertEqual(
          harness._bundle_processor_cache.fns,
          {item.id: item
           for item in process_bundle_descriptors})

  def test_fn_registration(self):
    self._check_fn_registration_multi_request((1, 4), (4, 4))

  def test_inactive_bundle_processor_returns_empty_progress_response(self):
    bundle_processor = mock.MagicMock()
    bundle_processor_cache = BundleProcessorCache(None, None, {})
    bundle_processor_cache.activate('instruction_id')
    worker = SdkWorker(bundle_processor_cache)
    split_request = beam_fn_api_pb2.InstructionRequest(
        instruction_id='progress_instruction_id',
        process_bundle_progress=beam_fn_api_pb2.ProcessBundleProgressRequest(
            instruction_id='instruction_id'))
    self.assertEqual(
        worker.do_instruction(split_request),
        beam_fn_api_pb2.InstructionResponse(
            instruction_id='progress_instruction_id',
            process_bundle_progress=beam_fn_api_pb2.
            ProcessBundleProgressResponse()))

    # Add a mock bundle processor as if it was running before it's released
    bundle_processor_cache.active_bundle_processors['instruction_id'] = (
        'descriptor_id', bundle_processor)
    bundle_processor_cache.release('instruction_id')
    self.assertEqual(
        worker.do_instruction(split_request),
        beam_fn_api_pb2.InstructionResponse(
            instruction_id='progress_instruction_id',
            process_bundle_progress=beam_fn_api_pb2.
            ProcessBundleProgressResponse()))

  def test_failed_bundle_processor_returns_failed_progress_response(self):
    bundle_processor = mock.MagicMock()
    bundle_processor_cache = BundleProcessorCache(None, None, {})
    bundle_processor_cache.activate('instruction_id')
    worker = SdkWorker(bundle_processor_cache)

    # Add a mock bundle processor as if it was running before it's discarded
    bundle_processor_cache.active_bundle_processors['instruction_id'] = (
        'descriptor_id', bundle_processor)
    bundle_processor_cache.discard('instruction_id')
    split_request = beam_fn_api_pb2.InstructionRequest(
        instruction_id='progress_instruction_id',
        process_bundle_progress=beam_fn_api_pb2.ProcessBundleProgressRequest(
            instruction_id='instruction_id'))
    hc.assert_that(
        worker.do_instruction(split_request).error,
        hc.contains_string(
            'Bundle processing associated with instruction_id has failed'))

  def test_inactive_bundle_processor_returns_empty_split_response(self):
    bundle_processor = mock.MagicMock()
    bundle_processor_cache = BundleProcessorCache(None, None, {})
    bundle_processor_cache.activate('instruction_id')
    worker = SdkWorker(bundle_processor_cache)
    split_request = beam_fn_api_pb2.InstructionRequest(
        instruction_id='split_instruction_id',
        process_bundle_split=beam_fn_api_pb2.ProcessBundleSplitRequest(
            instruction_id='instruction_id'))
    self.assertEqual(
        worker.do_instruction(split_request),
        beam_fn_api_pb2.InstructionResponse(
            instruction_id='split_instruction_id',
            process_bundle_split=beam_fn_api_pb2.ProcessBundleSplitResponse()))

    # Add a mock bundle processor as if it was running before it's released
    bundle_processor_cache.active_bundle_processors['instruction_id'] = (
        'descriptor_id', bundle_processor)
    bundle_processor_cache.release('instruction_id')
    self.assertEqual(
        worker.do_instruction(split_request),
        beam_fn_api_pb2.InstructionResponse(
            instruction_id='split_instruction_id',
            process_bundle_split=beam_fn_api_pb2.ProcessBundleSplitResponse()))

  def get_responses(self, instruction_requests):
    """Evaluates and returns {id: InstructionResponse} for the requests."""
    test_controller = BeamFnControlServicer(instruction_requests)

    server = grpc.server(thread_pool_executor.shared_unbounded_instance())
    beam_fn_api_pb2_grpc.add_BeamFnControlServicer_to_server(
        test_controller, server)
    test_port = server.add_insecure_port("[::]:0")
    server.start()

    harness = sdk_worker.SdkHarness(
        "localhost:%s" % test_port, state_cache_size=100)
    harness.run()
    return test_controller.responses

  def test_harness_monitoring_infos_and_metadata(self):
    # Clear the process wide metric container.
    MetricsEnvironment.process_wide_container().reset()
    # Create a process_wide metric.
    urn = 'my.custom.urn'
    labels = {'key': 'value'}
    InternalMetrics.counter(urn=urn, labels=labels, process_wide=True).inc(10)

    harness_monitoring_infos_request = beam_fn_api_pb2.InstructionRequest(
        instruction_id="monitoring_infos",
        harness_monitoring_infos=beam_fn_api_pb2.HarnessMonitoringInfosRequest(
        ))

    responses = self.get_responses([harness_monitoring_infos_request])

    expected_monitoring_info = monitoring_infos.int64_counter(
        urn, 10, labels=labels)
    monitoring_data = (
        responses['monitoring_infos'].harness_monitoring_infos.monitoring_data)

    # Request the full MonitoringInfo metadata for the returned short_ids.
    short_ids = list(monitoring_data.keys())
    monitoring_infos_metadata_request = beam_fn_api_pb2.InstructionRequest(
        instruction_id="monitoring_infos_metadata",
        monitoring_infos=beam_fn_api_pb2.MonitoringInfosMetadataRequest(
            monitoring_info_id=short_ids))

    responses = self.get_responses([monitoring_infos_metadata_request])

    # Request the full MonitoringInfo metadata to be returned now.
    expected_monitoring_info.ClearField("payload")

    # Verify that one of the returned monitoring infos is our expected
    # monitoring info.
    short_id_to_mi = (
        responses['monitoring_infos_metadata'].monitoring_infos.monitoring_info)
    found = False
    for mi in short_id_to_mi.values():
      # Clear the timestamp before comparing
      mi.ClearField("start_time")
      if mi == expected_monitoring_info:
        found = True
    self.assertTrue(found, str(responses['monitoring_infos_metadata']))

  def test_failed_bundle_processor_returns_failed_split_response(self):
    bundle_processor = mock.MagicMock()
    bundle_processor_cache = BundleProcessorCache(None, None, {})
    bundle_processor_cache.activate('instruction_id')
    worker = SdkWorker(bundle_processor_cache)

    # Add a mock bundle processor as if it was running before it's discarded
    bundle_processor_cache.active_bundle_processors['instruction_id'] = (
        'descriptor_id', bundle_processor)
    bundle_processor_cache.discard('instruction_id')
    split_request = beam_fn_api_pb2.InstructionRequest(
        instruction_id='split_instruction_id',
        process_bundle_split=beam_fn_api_pb2.ProcessBundleSplitRequest(
            instruction_id='instruction_id'))
    hc.assert_that(
        worker.do_instruction(split_request).error,
        hc.contains_string(
            'Bundle processing associated with instruction_id has failed'))

class CachingStateHandlerTest(unittest.TestCase):
  def test_caching(self):

    coder = VarIntCoder()
    coder_impl = coder.get_impl()

    class FakeUnderlyingState(object):
      """Simply returns an incremented counter as the state "value."
      """
      def set_counter(self, n):
        self._counter = n

      def get_raw(self, *args):
        self._counter += 1
        return coder.encode(self._counter), None

      @contextlib.contextmanager
      def process_instruction_id(self, bundle_id):
        yield

    underlying_state = FakeUnderlyingState()
    state_cache = statecache.StateCache(100)
    caching_state_hander = GlobalCachingStateHandler(
        state_cache, underlying_state)

    state1 = beam_fn_api_pb2.StateKey(
        bag_user_state=beam_fn_api_pb2.StateKey.BagUserState(
            user_state_id='state1'))
    state2 = beam_fn_api_pb2.StateKey(
        bag_user_state=beam_fn_api_pb2.StateKey.BagUserState(
            user_state_id='state2'))
    side1 = beam_fn_api_pb2.StateKey(
        multimap_side_input=beam_fn_api_pb2.StateKey.MultimapSideInput(
            transform_id='transform', side_input_id='side1'))
    side2 = beam_fn_api_pb2.StateKey(
        iterable_side_input=beam_fn_api_pb2.StateKey.IterableSideInput(
            transform_id='transform', side_input_id='side2'))

    state_token1 = beam_fn_api_pb2.ProcessBundleRequest.CacheToken(
        token=b'state_token1',
        user_state=beam_fn_api_pb2.ProcessBundleRequest.CacheToken.UserState())
    state_token2 = beam_fn_api_pb2.ProcessBundleRequest.CacheToken(
        token=b'state_token2',
        user_state=beam_fn_api_pb2.ProcessBundleRequest.CacheToken.UserState())
    side1_token1 = beam_fn_api_pb2.ProcessBundleRequest.CacheToken(
        token=b'side1_token1',
        side_input=beam_fn_api_pb2.ProcessBundleRequest.CacheToken.SideInput(
            transform_id='transform', side_input_id='side1'))
    side1_token2 = beam_fn_api_pb2.ProcessBundleRequest.CacheToken(
        token=b'side1_token2',
        side_input=beam_fn_api_pb2.ProcessBundleRequest.CacheToken.SideInput(
            transform_id='transform', side_input_id='side1'))

    def get_as_list(key):
      return list(caching_state_hander.blocking_get(key, coder_impl))

    underlying_state.set_counter(100)
    with caching_state_hander.process_instruction_id('bundle1', []):
      self.assertEqual(get_as_list(state1), [101])  # uncached
      self.assertEqual(get_as_list(state2), [102])  # uncached
      self.assertEqual(get_as_list(state1), [101])  # cached on bundle
      self.assertEqual(get_as_list(side1), [103])  # uncached
      self.assertEqual(get_as_list(side2), [104])  # uncached

    underlying_state.set_counter(200)
    with caching_state_hander.process_instruction_id(
        'bundle2', [state_token1, side1_token1]):
      self.assertEqual(get_as_list(state1), [201])  # uncached
      self.assertEqual(get_as_list(state2), [202])  # uncached
      self.assertEqual(get_as_list(state1), [201])  # cached on state token1
      self.assertEqual(get_as_list(side1), [203])  # uncached
      self.assertEqual(get_as_list(side1), [203])  # cached on side1_token1
      self.assertEqual(get_as_list(side2), [204])  # uncached
      self.assertEqual(get_as_list(side2), [204])  # cached on bundle

    underlying_state.set_counter(300)
    with caching_state_hander.process_instruction_id(
        'bundle3', [state_token1, side1_token1]):
      self.assertEqual(get_as_list(state1), [201])  # cached on state token1
      self.assertEqual(get_as_list(state2), [202])  # cached on state token1
      self.assertEqual(get_as_list(state1), [201])  # cached on state token1
      self.assertEqual(get_as_list(side1), [203])  # cached on side1_token1
      self.assertEqual(get_as_list(side1), [203])  # cached on side1_token1
      self.assertEqual(get_as_list(side2), [301])  # uncached
      self.assertEqual(get_as_list(side2), [301])  # cached on bundle

    underlying_state.set_counter(400)
    with caching_state_hander.process_instruction_id(
        'bundle4', [state_token2, side1_token1]):
      self.assertEqual(get_as_list(state1), [401])  # uncached
      self.assertEqual(get_as_list(state2), [402])  # uncached
      self.assertEqual(get_as_list(state1), [401])  # cached on state token2
      self.assertEqual(get_as_list(side1), [203])  # cached on side1_token1
      self.assertEqual(get_as_list(side1), [203])  # cached on side1_token1
      self.assertEqual(get_as_list(side2), [403])  # uncached
      self.assertEqual(get_as_list(side2), [403])  # cached on bundle

    underlying_state.set_counter(500)
    with caching_state_hander.process_instruction_id(
        'bundle5', [state_token2, side1_token2]):
      self.assertEqual(get_as_list(state1), [401])  # cached on state token2
      self.assertEqual(get_as_list(state2), [402])  # cached on state token2
      self.assertEqual(get_as_list(state1), [401])  # cached on state token2
      self.assertEqual(get_as_list(side1), [501])  # uncached
      self.assertEqual(get_as_list(side1), [501])  # cached on side1_token2
      self.assertEqual(get_as_list(side2), [502])  # uncached
      self.assertEqual(get_as_list(side2), [502])  # cached on bundle

  class UnderlyingStateHandler(object):
    """Simply returns an incremented counter as the state "value."
    """
    def __init__(self):
      self._encoded_values = []
      self._continuations = False

    def set_value(self, value, coder):
      self._encoded_values = [coder.encode(value)]

    def set_values(self, values, coder):
      self._encoded_values = [coder.encode(value) for value in values]

    def set_continuations(self, continuations):
      self._continuations = continuations

    def get_raw(self, _state_key, continuation_token=None):
      if self._continuations and len(self._encoded_values) > 0:
        if not continuation_token:
          continuation_token = '0'
        idx = int(continuation_token)
        next_token = str(idx +
                         1) if idx + 1 < len(self._encoded_values) else None
        return self._encoded_values[idx], next_token
      else:
        return b''.join(self._encoded_values), None

    def append_raw(self, _key, bytes):
      self._encoded_values.append(bytes)

    def clear(self, *args):
      self._encoded_values = []

    @contextlib.contextmanager
    def process_instruction_id(self, bundle_id):
      yield

  def test_append_clear_with_preexisting_state(self):
    state = beam_fn_api_pb2.StateKey(
        bag_user_state=beam_fn_api_pb2.StateKey.BagUserState(
            user_state_id='state1'))

    cache_token = beam_fn_api_pb2.ProcessBundleRequest.CacheToken(
        token=b'state_token1',
        user_state=beam_fn_api_pb2.ProcessBundleRequest.CacheToken.UserState())

    coder = VarIntCoder()

    underlying_state_handler = self.UnderlyingStateHandler()
    state_cache = statecache.StateCache(100)
    handler = GlobalCachingStateHandler(state_cache, underlying_state_handler)

    def get():
      return handler.blocking_get(state, coder.get_impl())

    def append(iterable):
      handler.extend(state, coder.get_impl(), iterable)

    def clear():
      handler.clear(state)

    # Initialize state
    underlying_state_handler.set_value(42, coder)
    with handler.process_instruction_id('bundle', [cache_token]):
      # Append without reading beforehand
      append([43])
      self.assertEqual(get(), [42, 43])
      clear()
      self.assertEqual(get(), [])
      append([44, 45])
      self.assertEqual(get(), [44, 45])
      append((46, 47))
      self.assertEqual(get(), [44, 45, 46, 47])
      clear()
      append(range(1000))
      self.assertEqual(get(), list(range(1000)))

  def test_continuation_token(self):
    underlying_state_handler = self.UnderlyingStateHandler()
    state_cache = statecache.StateCache(100)
    handler = GlobalCachingStateHandler(state_cache, underlying_state_handler)

    coder = VarIntCoder()

    state = beam_fn_api_pb2.StateKey(
        bag_user_state=beam_fn_api_pb2.StateKey.BagUserState(
            user_state_id='state1'))

    cache_token = beam_fn_api_pb2.ProcessBundleRequest.CacheToken(
        token=b'state_token1',
        user_state=beam_fn_api_pb2.ProcessBundleRequest.CacheToken.UserState())

    def get(materialize=True):
      result = handler.blocking_get(state, coder.get_impl())
      return list(result) if materialize else result

    def get_type():
      return type(get(materialize=False))

    def append(*values):
      handler.extend(state, coder.get_impl(), values)

    def clear():
      handler.clear(state)

    underlying_state_handler.set_continuations(True)
    underlying_state_handler.set_values([45, 46, 47], coder)
    with handler.process_instruction_id('bundle', [cache_token]):
      self.assertEqual(
          get_type(), GlobalCachingStateHandler.ContinuationIterable)
      self.assertEqual(get(), [45, 46, 47])
      append(48, 49)
      self.assertEqual(
          get_type(), GlobalCachingStateHandler.ContinuationIterable)
      self.assertEqual(get(), [45, 46, 47, 48, 49])
      clear()
      self.assertEqual(get_type(), list)
      self.assertEqual(get(), [])
      append(1)
      self.assertEqual(get(), [1])
      append(2, 3)
      self.assertEqual(get(), [1, 2, 3])
      clear()
      for i in range(1000):
        append(i)
      self.assertEqual(get_type(), list)
      self.assertEqual(get(), [i for i in range(1000)])

class ShortIdCacheTest(unittest.TestCase):
  def testShortIdAssignment(self):
    TestCase = namedtuple('TestCase', ['expected_short_id', 'info'])
    test_cases = [
        TestCase(*args) for args in [
            (
                "1",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:user:distribution_int64:v1",
                    type="beam:metrics:distribution_int64:v1")),
            (
                "2",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:element_count:v1",
                    type="beam:metrics:sum_int64:v1")),
            (
                "3",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:ptransform_progress:completed:v1",
                    type="beam:metrics:progress:v1")),
            (
                "4",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:user:distribution_double:v1",
                    type="beam:metrics:distribution_double:v1")),
            (
                "5",
                metrics_pb2.MonitoringInfo(
                    urn="TestingSentinelUrn", type="TestingSentinelType")),
            (
                "6",
                metrics_pb2.MonitoringInfo(
                    urn=
                    "beam:metric:pardo_execution_time:finish_bundle_msecs:v1",
                    type="beam:metrics:sum_int64:v1")),
            # This case and the next one validates that different labels
            # with the same urn are in fact assigned different short ids.
            (
                "7",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:user:sum_int64:v1",
                    type="beam:metrics:sum_int64:v1",
                    labels={
                        "PTRANSFORM": "myT",
                        "NAMESPACE": "harness",
                        "NAME": "metricNumber7"
                    })),
            (
                "8",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:user:sum_int64:v1",
                    type="beam:metrics:sum_int64:v1",
                    labels={
                        "PTRANSFORM": "myT",
                        "NAMESPACE": "harness",
                        "NAME": "metricNumber8"
                    })),
            (
                "9",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:user:top_n_double:v1",
                    type="beam:metrics:top_n_double:v1",
                    labels={
                        "PTRANSFORM": "myT",
                        "NAMESPACE": "harness",
                        "NAME": "metricNumber7"
                    })),
            (
                "a",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:element_count:v1",
                    type="beam:metrics:sum_int64:v1",
                    labels={"PCOLLECTION": "myPCol"})),
            # validate payload is ignored for shortId assignment
            (
                "3",
                metrics_pb2.MonitoringInfo(
                    urn="beam:metric:ptransform_progress:completed:v1",
                    type="beam:metrics:progress:v1",
                    payload=b"this is ignored!"))
        ]
    ]

    cache = sdk_worker.ShortIdCache()

    for case in test_cases:
      self.assertEqual(
          case.expected_short_id,
          cache.get_short_id(case.info),
          "Got incorrect short id for monitoring info:\n%s" % case.info)

    # Retrieve all of the monitoring infos by short id, and verify that the
    # metadata (everything but the payload) matches the originals
    actual_recovered_infos = cache.get_infos(
        case.expected_short_id for case in test_cases).values()
    for recoveredInfo, case in zip(actual_recovered_infos, test_cases):
      self.assertEqual(
          monitoringInfoMetadata(case.info),
          monitoringInfoMetadata(recoveredInfo))

    # Retrieve short ids one more time in reverse
    for case in reversed(test_cases):
      self.assertEqual(
          case.expected_short_id,
          cache.get_short_id(case.info),
          "Got incorrect short id on second retrieval for monitoring info:\n%s"
          % case.info)

def monitoringInfoMetadata(info):
  return {
      descriptor.name: value
      for descriptor,
      value in info.ListFields() if not descriptor.name == "payload"
  }

if __name__ == "__main__":
  logging.getLogger().setLevel(logging.INFO)
  unittest.main()


text:
If you want to contribute to an ESLint repo, please use a GitHub pull request. This is the fastest way for us to evaluate your code and to merge it into the code base. Please don't file an issue with snippets of code. Doing so means that we need to manually merge the changes in and update any appropriate tests. That decreases the likelihood that your code is going to get included in a timely manner. Please use pull requests.

## Getting Started

If you'd like to work on a pull request and you've never submitted code before, follow these steps:

1. Sign our [Contributor License Agreement](https://cla.js.foundation/eslint/eslint).
1. Set up a [development environment](../development-environment.md).
1. If you want to implement a breaking change or a change to the core, ensure there's an issue that describes what you're doing and the issue has been accepted. You can create a new issue or just indicate you're [working on an existing issue](working-on-issues.md). Bug fixes, documentation changes, and other pull requests do not require an issue.

After that, you're ready to start working on code.

## Working with Code

The process of submitting a pull request is fairly straightforward and generally follows the same pattern each time:

1. [Create a new branch](#step1)
2. [Make your changes](#step2)
3. [Rebase onto upstream](#step3)
4. [Run the tests](#step4)
5. [Double check your submission](#step5)
6. [Push your changes](#step6)
7. [Submit the pull request](#step7)

Details about each step are found below.

### Step 1: Create a new branch

The first step to sending a pull request is to create a new branch in your ESLint fork. Give the branch a descriptive name that describes what it is you're fixing, such as:

```shell
$ git checkout -b issue1234
```

You should do all of your development for the issue in this branch.

**Note:** Do not combine fixes for multiple issues into one branch. Use a separate branch for each issue you're working on.

### Step 2: Make your changes

Make the changes to the code and tests, following the [code conventions](../code-conventions.md) as you go. Once you have finished, commit the changes to your branch:

```shell
$ git add -A
$ git commit
```

All ESLint projects follow [Conventional Commits](https://www.conventionalcommits.org/) for our commit messages. Here's an example commit message:

```pt
tag: Short description of what you did

Longer description here if necessary

Fixes #1234
```

The first line of the commit message (the summary) must have a specific format. This format is checked by our build tools.

The `tag` is one of the following:

* `fix` - for a bug fix.
* `feat` - either for a backwards-compatible enhancement or for a rule change that adds reported problems.
* `fix!` - for a backwards-incompatible bug fix.
* `feat!` - for a backwards-incompatible enhancement or feature.
* `docs` - changes to documentation only.
* `chore` - for changes that aren't user-facing.
* `build` - changes to build process only.
* `refactor` - a change that doesn't affect APIs or user experience.
* `test` - just changes to test files.
* `ci` - changes to our CI configuration files and scripts.
* `perf` - a code change that improves performance.

Use the [labels of the issue you are working on](working-on-issues.md#issue-labels) to determine the best tag.

The message summary should be a one-sentence description of the change, and it must be 72 characters in length or shorter. If the pull request addresses an issue, then the issue number should be mentioned in the body of the commit message in the format `Fixes #1234`. If the commit doesn't completely fix the issue, then use `Refs #1234` instead of `Fixes #1234`.

Here are some good commit message summary examples:

```pt
build: Update Travis to only test Node 0.10
fix: Semi rule incorrectly flagging extra semicolon
chore: Upgrade Esprima to 1.2, switch to using comment attachment
```

The commit message format is important because these messages are used to create a changelog for each release. The tag and issue number help to create more consistent and useful changelogs.

### Step 3: Rebase onto upstream

Before you send the pull request, be sure to rebase onto the upstream source. This ensures your code is running on the latest available code.

```shell
git fetch upstream
git rebase upstream/main
```

### Step 4: Run the tests

After rebasing, be sure to run all of the tests once again to make sure nothing broke:

```shell
npm test
```

If there are any failing tests, update your code until all tests pass.

### Step 5: Double check your submission

With your code ready to go, this is a good time to double-check your submission to make sure it follows our conventions. Here are the things to check:

* Make sure your commit is formatted correctly.
* The pull request must have a description. The description should explain what you did and how its effects can be seen.
* The commit message is properly formatted.
* The change introduces no functional regression. Be sure to run `npm test` to verify your changes before submitting a pull request.
* Make separate pull requests for unrelated changes. Large pull requests with multiple unrelated changes may be closed without merging.
* All changes must be accompanied by tests, even if the feature you're working on previously had no tests.
* All user-facing changes must be accompanied by appropriate documentation.
* Follow the [Code Conventions](../code-conventions.md).

### Step 6: Push your changes

Next, push your changes to your clone:

```shell
git push origin issue1234
```

If you are unable to push because some references are old, do a forced push instead:

```shell
git push -f origin issue1234
```

### Step 7: Send the pull request

Now you're ready to send the pull request. Go to your ESLint fork and then follow the [GitHub documentation](https://help.github.com/articles/creating-a-pull-request) on how to send a pull request.

## Following Up

Once your pull request is sent, it's time for the team to review it. As such, please make sure to:

1. Monitor the status of the Travis CI build for your pull request. If it fails, please investigate why. We cannot merge pull requests that fail Travis for any reason.
1. Respond to comments left on the pull request from team members. Remember, we want to help you land your code, so please be receptive to our feedback.
1. We may ask you to make changes, rebase, or squash your commits.

### Updating the Commit Message

If your commit message is in the incorrect format, you'll be asked to update it. You can do so via:

```shell
$ git commit --amend
```

This will open up your editor so you can make changes. After that, you'll need to do a forced push to your branch:

```shell
$ git push origin issue1234 -f
```

### Updating the Code

If we ask you to make code changes, there's no need to close the pull request and create a new one. Just go back to the branch on your fork and make your changes. Then, when you're ready, you can add your changes into the branch:

```shell
$ git add -A
$ git commit
$ git push origin issue1234
```

When updating the code, it's usually better to add additional commits to your branch rather than amending the original commit, because reviewers can easily tell which changes were made in response to a particular review. When we merge pull requests, we will squash all the commits from your branch into a single commit on the `main` branch.

The commit messages in subsequent commits do not need to be in any specific format because these commits do not show up in the changelog.

### Rebasing

If your code is out-of-date, we might ask you to rebase. That means we want you to apply your changes on top of the latest upstream code. Make sure you have set up a [development environment](../development-environment.md) and then you can rebase using these commands:

```shell
$ git fetch upstream
$ git rebase upstream/main
```

You might find that there are merge conflicts when you attempt to rebase. Please [resolve the conflicts](https://help.github.com/articles/resolving-merge-conflicts-after-a-git-rebase/) and then do a forced push to your branch:

```shell
$ git push origin issue1234 -f
```

redpajama_set_name: RedPajamaGithub


text:
//
//  TableViewBuJuVC.m
//  KenshinPro
//
//  Created by kenshin on 17/4/11.
//  Copyright © 2017年 Kenshin. All rights reserved.
//

#import "TableViewBuJuVC.h"
#import "VCView.h"
#import "PureTableViewVC.h"

@interface TableViewBuJuVC ()

@property (nonatomic, strong) UITableView                   *tableView;

@end

@implementation TableViewBuJuVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    /*我有这样的想法:就是一个页面不管什么内容都可以方在一个tableView里面,这样可以兼容小屏手机
      可以用分区来区分UI的类型,也可以全部放在TableView的HeaderView中
      一下是我的测试代码,可以用纯代码布局 or xib
     */
    [self initTableViewBuJuVCUI];

}

- (void)initTableViewBuJuVCUI
{
    self.navigationItem.title = @"内容全在表头";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    _tableView.backgroundColor = colorHomeBlue;

    VCView *vView = [[VCView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 1474)];//高度从xib或取的

    _tableView.tableHeaderView = vView;
    [self.view addSubview:_tableView];

    //点击一个按钮
    [vView clickButtonWithResultBlock:^(VCView *view) {
        NSLog(@"诚实的李老师");
    }];

    //点击黄色按钮
    WS(ws);
    [vView clickButtonNextPage:^(VCView *view) {
        PureTableViewVC *vc = [[PureTableViewVC alloc] init];
        [ws.navigationController pushViewController:vc animated:YES];
    }];

}

- (void)dealloc
{
    [Tools NSLogClassDestroy:self];

}
@end


text:
sitemap:
  changefreq: hourly
  priority: 0.9
---

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% for page in site.pages %}
    <url>
      <loc>https://developers.google.com{{ site.baseurl }}{{ page.url | canonical_url }}</loc>
      <lastmod>{% if page.article.updated_on %}{{ page.article.updated_on }}{% elsif page.article.written_on %}{{ page.article.written_on }}{% else %}2014-05-22{% endif %}</lastmod>
      <changefreq>{% if page.sitemap.changefreq %}{{ page.sitemap.changefreq }}{% else %}weekly{% endif %}</changefreq>
      <priority>{% if page.sitemap.priority %}{{ page.sitemap.priority }}{% else %}0.5{% endif %}</priority>
    </url>
  {% endfor %}
</urlset>


text:
//https://kbase.us/services/fba_model_services/ //production fba service not deployed

// This saves a request by service name, method, params, and promise
// Todo: Make as module
function Cache() {
    var cache = [];

    this.get = function(service, method, params) {
        for (var i in cache) {
            var obj = cache[i];
            if (service != obj['service']) continue;
            if (method != obj['method']) continue;
            if ( angular.equals(obj['params'], params) ) { return obj; }
        }
        return undefined;
    }

    this.put = function(service, method, params, prom) {
        var obj = {};
        obj['service'] = service;
        obj['method'] = method;
        obj['prom'] = prom;
        obj['params'] = params;
        cache.push(obj);
        //console.log('Cache after the last "put"', cache)
    }
}

function KBCacheClient(token) {
    var auth = {};
    auth.token = token;

    var setup = configJSON.setup;
    if (setup) {
        fba_url = configJSON[setup].fba_url;
        ws_url = configJSON[setup].workspace_url;
        ujs_url = configJSON[setup].user_job_state_url;
    } else {
        fba_url = configJSON.prod.fba_url;
        ws_url = configJSON.prod.workspace_url;
        ujs_url = configJSON.prod.user_job_state_url;
    }

    console.log('FBA URL is:', fba_url);
    console.log('Workspace URL is:', ws_url);
    console.log('User Job State URL is:', ujs_url);

    var fba = new fbaModelServices(fba_url, auth);
    var kbws = new Workspace(ws_url, auth);
    var ujs = new UserAndJobState(ujs_url, auth);

    var cache = new Cache();

    this.req = function(service, method, params) {
        if (service == 'fba') {
            // use whatever workspace server that was configured.
            // this makes it possible to use the production workspace server
            // with the fba server.   Fixme:  fix once production fba server is ready.
            params.wsurl = ws_url;
        }

        // see if api call has already been made
        var data = cache.get(service, method, params);

        // return the promise ojbect if it has
        if (data) return data.prom;

        // otherwise, make request
        var prom = undefined;
        if (service == 'fba') {
            console.log('Making request:', 'fba.'+method+'('+JSON.stringify(params)+')');
            var prom = fba[method](params);
        } else if (service == 'ws') {
            console.log('Making request:', 'kbws.'+method+'('+JSON.stringify(params)+')');
            var prom = kbws[method](params);
        }

        // save the request and it's promise objct
        cache.put(service, method, params, prom)
        return prom;
    }

    // make publically accessible methods that
    this.fba = fba;
    this.ws = kbws;
    this.ujs = ujs

    this.nar = new ProjectAPI(ws_url, token);

    this.token = token;
}

function getBio(type, loaderDiv, callback) {
    var fba = new fbaModelServices('https://kbase.us/services/fba_model_services/');
//    var kbws = new workspaceService('http://kbase.us/services/workspace_service/');
//    var kbws = new workspaceService('http://140.221.84.209:7058');

    var kbws = new Workspace('http://kbase.us/services/ws');

    // This is not cached yet; waiting to compare performanced.
    loaderDiv.append('
\

\

\

')

    var bioAJAX = fba.get_biochemistry({});

    var chunk = 250;
    k = 1;
    $.when(bioAJAX).done(function(d){
        if (type == 'cpds') {
            var objs = d.compounds;
        } else if (type == 'rxns') {
            var objs = d.reactions;
        }
        var total = objs.length;
        var iterations = parseInt(total / chunk);
        var data = [];
        for (var i=0; i<iterations; i++) {
            var cpd_subset = objs.slice( i*chunk, (i+1)*chunk -1);
            if (type == 'cpds') {
                var prom = fba.get_compounds({compounds: cpd_subset });
            } else if (type == 'rxns') {
                var prom = fba.get_reactions({reactions: cpd_subset });
            }

            $.when(prom).done(function(obj_data){
                k = k + 1;
                data = data.concat(obj_data);
                var percent = (data.length / total) * 100+'%';
                $('.progress-bar').css('width', percent);

                if (k == iterations) {
                    $('.progress').remove();
                    callback(data)
                }
            });
        }
    })
}

function ProjectAPI(ws_url, token) {
    var self = this;

    var auth = {}
    auth.token = token;
    var ws_client = new Workspace(ws_url, auth);

    // We probably don't want to do error handeling in api functions,
    // so we should deprecate these as well.
    var legit_ws_id = /^\w+$/;
    // regex for a numeric workspace id of the form "ws.####.obj.###"
    var legit_narr_ws_id = /^ws\.(\d+)\.obj\.(\d+)$/;

    // Fields in a workspace metadata object
    var ws_meta_fields = ['id','owner','moddate','objects',
              'user_permission','global_permission'];

    // function used in reduce() to map ws_meta array into a dict
    var ws_meta_dict = function (ws_meta){
        return ws_meta.reduce ( function( prev, curr, index){
                        prev[ws_meta_fields[index]] = curr;
                        return(prev);
                },{});
    };

    // Fields in an workspace object metadata object
    obj_meta_fields = ['id','type','moddate','instance','command',
               'lastmodifier','owner','workspace','ref','chsum',
               'metadata', 'objid'];

    // function to map obj_meta array into a dict
    var obj_meta_dict = function (obj_meta) {
        return obj_meta.reduce( function( prev, curr, index){
                        prev[obj_meta_fields[index]] = curr;
                        return(prev);
                    },{});
    };
    // Fields in an workspace object metadata object
    obj_meta_fields2 = ['id','type','moddate','instance','command',
               'lastmodifier','owner','workspace','ref','chsum',
               'metadata'];

    // function to map obj_meta array into a dict
    var obj_meta_dict2 = function (obj_meta) {
        return obj_meta.reduce( function( prev, curr, index){
                        prev[obj_meta_fields[index]] = curr;
                        return(prev);
                    },{});
    };

    // We are tagging workspaces with _SOMETHING objects to distinguish
    // project workspaces from other containers. Using this in case we
    // decide to include other types of workspaces
    var ws_tag = {
        project : '_project'
    };

    var ws_tag_type = 'KBaseNarrative.Metadata';
    var narrative_type = 'KBaseNarrative.Narrative';

    // Fields in an empty narrative
    var empty_narrative = {type: narrative_type,
                           data: {nbformat: 3,
                                  nbformat_minor: 0,
                                  metadata: { format: "ipynb",
                                           creator: "",
                                           ws_name: "",
                                           name: "",
                                           type: "Narrative",
                                           description: "",
                                           data_dependencies: [ ]
                                        },
                                  worksheets: [
                                               {
                                                   cells: [
                                                       {
                                                           collapsed: false,
                                                           input: "",
                                                           outputs: [ ],
                                                           language: "python",
                                                           metadata: { },
                                                           cell_type: "code"
                                                       }
                                                   ],
                                                   metadata: { }
                                               }
                                           ]
                                 },
                        }

    // Empty project tag template
    var empty_proj_tag = {
        id : ws_tag.project,
        type : ws_tag_type,
        data : { description : 'Tag! You\'re a project!' },
        workspace : undefined,
        metadata : {},
        auth : undefined
    };

    // Empty ws2 project tag template
    var empty_ws2_proj_tag = {
        name : ws_tag.project,
        type : ws_tag_type,
        data : { description : '' },
        workspace : undefined,
        meta : {},
	provenance : [],
	hidden : 1
    };

    // id of the div containing the kbase login widget to query for auth
    // info. Set this to a different value if the div has been named differently.

    // Common error handler callback
    var error_handler = function() {
        // neat trick to pickup all arguments passed in
        var results = [].slice.call(arguments);
        console.log( "Error in method call. Response was: ", results);
    };

    /*
     * This is a handler to pickup get_workspaces() results and
     * filter out anything that isn't a project workspace
     * (basically only include it if it has a _project object
     * within)
     */
    var filter_wsobj = function (p_in) {
        console.log('p_in', p_in)

        /*
        for (var i in p_in.res) {
            var ws = p_in.res[i];

            // if there is global read or any permissions
            if (ws[4] == "r" || ws[5].match(/r|w|a/)) {

            }
        }
        */

        var prom = ws_client.list_objects({type: 'KBaseNarrative.Metadata',
                                           showHidden: 1});
        return prom
    };

    /*

  var def_params = {perms : ['r', 'w', 'a'],
                          filter_tag : ws_tag.project};
        var p = $.extend( def_params, p_in);

        var ignore = /^core/;

        // ignore any workspace with the name prefix of "core"
        // and ignore workspace that doesn't match perms
        var reduce_ws_meta = function ( prev, curr) {
            if ( ignore.test(curr[0])) {
                return( prev);
            }
            if ( p.perms.indexOf(curr[4]) >= 0 ) {
                return( prev.concat([curr]));
            } else {
                return( prev);
            }
       };

        // get non-core workspaces with correct permissions
        var ws_match = p.res.reduce(reduce_ws_meta,[]);
        // extract workspace ids into a list
        var ws_ids = ws_match.map( function(v) { return v[0]});
        */

    /**
     * Ensures that a USER_ID:home workspace exists and is tagged as a project.
     * If one does not exist, it calls 'new_project' and makes one.
     */
    this.ensure_home_project = function(userId) {

        // if we don't have a userid, don't do anything.
        if (!userId)
            return;

        var projId = userId + ":home";

        var prom = ws_client.get_object({ type: ws_tag_type,
                                          workspace: projId,
                                          id: ws_tag.project });
        $.when(prom).then(
            undefined,                  // don't need to do anything if it already has one.
            $.proxy(function(error) {   // if no project USER_ID:home exists, make one
                this.new_project({
                    project_id: projId,
                    error_callback: function(error) {  // Just fails more or less silently for now
                        console.debug("Error while creating home project!");
                        console.debug(error);
                    },
                });
            }, this)
        );
    };

    // Get all the workspaces that match the values of the
    // permission array. Defaults to only workspaces that the
    // currently logged in user owns/administers
    // The callback will recieve a hash keyed on workspace
    // name, where the values are dictionaries keyed on ws_meta_fields
    // if a specific workspace name is given its metadata will be
    // returned
    this.get_projects = function( p_in ) {
        var def_params = { perms : ['a'],
                           workspace_id : undefined };
        var p = $.extend( def_params, p_in);

        console.log('calling get projects')
//        META_ws = ws_client.list_objects( {} );

        var prom = ws_client.list_objects({type: 'KBaseNarrative.Metadata',
                                           showHidden: 1});
        //var prom = $.when( META_ws).then( function(result) {
         //               return filter_wsobj( { res: result, perms: p.perms });
          //         });
        return prom

    };

    // Get the object metadata for the specified workspace id and return a
    // dictionary keyed on "objectId" where the
    // value is a dictionary keyed on obj_meta_fields
    this.get_project = function( p_in ) {
        var def_params = { callback : undefined,
                   workspace_id : undefined };

        var p = $.extend( def_params, p_in);

        var ws_meta = ws_client.list_workspace_objects( {workspace: p.workspace_id});
        $.when( ws_meta).then( function (results) {
                       var res = {};
                       $.each( results, function (index, val) {
                               var obj_meta = obj_meta_dict( val);
                               res[val[0]] = obj_meta;
                           });
                       p.callback( res);
                       });
    };

    // Get the individual workspace object named. Takes names
    // in 2 parts, in the workspace_id and object_id
    // fields, and the type in the type field. Returns the
    // object as the workspace service would return it,
    // as a dict with 'data' and 'metadata' fields
    // if the type is given, it will save one RPC call,
    // otherwise we fetch the metadata for it and then
    // grab the type
    this.get_object = function( p_in ) {
        var def_params = { callback : undefined,
                   workspace_id : undefined,
                   object_id : undefined,
                   //type : undefined,
                   error_callback: error_handler
                 };
        var p = $.extend( def_params, p_in);

        var del_fn = ws_client.get_object( { type: p.type,
                                             workspace: p.workspace,
                                             id: p.object_id });

        $.when( del_fn).then( p.callback, p.error_callback);

    };

    // Delete the object in the specified workspace/object id and return a
    // dictionary keyed on the obj_meta_fields for the result
    this.delete_object = function( p_in ) {
        console.error('project.delete_object was removed since due to redundancy . use set_workspace_permissions in workspace api');
    };

    // Create an new workspace and tag it with a project tag. The name
    // *must" conform to the future workspace rules about object naming,
    // the it can only contain [a-zA-Z0-9_]. We show the error prompt
    // if it fails to conform
    // If it is passed the id of an existing workspace, add the _project
    // tag object to it
    this.new_project = function( p_in ) {
        var def_params = { project_id : undefined,
                           def_perm : 'n',
                           description: ''};

        var p = $.extend( def_params, p_in);

        function tag_ws() {
            var proj = $.extend(true,{},empty_ws2_proj_tag);
            proj.data.description = p_in.description;

            var params = { objects : [proj] };
            params.workspace = p.project_id;
            var ws_fn2 = ws_client.save_objects( params);

            return ws_fn2;
        };

        var ws_exists = ws_client.get_workspacemeta( {workspace : p.project_id });
        var prom =  $.when(ws_exists).then(tag_ws, function() {
            var ws_fn = ws_client.create_workspace( { workspace : p.project_id,
                                                      globalread : p.def_perm })
            var prom = $.when(ws_fn).done(function() {
                return tag_ws();
            })
            return prom;
        });

        return prom;
    };

    // returns a project description (out of the project object)
    this.get_project_description = function(proj_name) {
        var prom = ws_client.get_object({ type: ws_tag_type,
                                          workspace: proj_name,
                                          id: ws_tag.project });
        var p = $.when(prom).then(function(r) {
            return r.data.description;
        })
        return p;
    }

    // returns a project description (out of the project object)
    this.set_project_description = function(proj_name, descript) {
        var proj = $.extend({}, empty_ws2_proj_tag);
        proj.data.description = descript;
        var p = ws_client.save_objects( {workspace: proj_name, objects: [proj]});

        return p;
    }

    // Delete a workspace(project)
    // will wipe out everything there, so make sure you prompt "Are you REALLY SURE?"
    // before calling this.
    // the callback gets the workspace metadata object of the deleted workspace
    this.delete_project = function( p_in ) {
        var def_params = { callback : undefined,
                           project_id : undefined,
                           error_callback: error_handler };

        var p = $.extend( def_params, p_in);

        //if ( legit_ws_id.test(p.project_id)) {
            var ws_def_fun = ws_client.delete_workspace({
                                      workspace: p.project_id});
            $.when( ws_def_fun).then( p.callback,
                          p.error_callback
                        );
        //} else {
        //    console.error( "Bad project id: ",p.project_id);
        //}
    };

    // Get the permissions for a project, returns a 2 element
    // hash identical to get_workspacepermissions
    // 'default' : perm // default global perms
    // 'user_id1' : perm1 // special permission for user_id1
    // ...
    // 'user_idN' : permN // special permission for user_idN
    this.get_project_perms = function( p_in ) {
        var def_params = { callback : undefined,
                   project_id : undefined,
                   error_callback : error_handler
                 };
        var p = $.extend( def_params, p_in);

        var perm_fn =  ws_client.get_permissions( {
                                          workspace : p.project_id });
        return $.when( perm_fn).fail(function(){p.error_callback});

    };

    // Set the permissions for a project, takes a project_id
    // and a perms hash that is the same as the return format
    // from get_project_perms
    // 'default' : perm // default global perms
    // 'user_id1' : perm1 // special permission for user_id1
    // ...
    // 'user_idN' : permN // special permission for user_idN
    // The return results seem to be broken
    this.set_project_perms = function( p_in ) {
        console.error('set_project_perms was removed since due to redundancy . use set_workspace_permissions in workspace api')

    };

    // Will search the given list of project_ids for objects of type narrative
    // if no project_ids are given, then all a call will be made to get_projects
    // first - avoid doing this if possible as it will be miserably slow.
    // an array of obj_meta dictionaries will be handed to the callback
    this.get_narratives = function(p_in) {
        var def_params = { project_ids : undefined,
                           error_callback : error_handler,
                           type: narrative_type,
                           error_callback: error_handler };

        var p = $.extend( def_params, p_in);

        if (p.project_ids) {
            return all_my_narratives( p.project_ids);
        } else {
            var proj_prom = self.get_projects().then( function(pdict) {
                var project_names = []
                for (var i=0; i <pdict.length;i++) {
                    project_names.push(pdict[i][7])
                }
                return all_my_narratives(project_names);
            });
            return proj_prom
        }

        function all_my_narratives(project_ids) {
            var prom = ws_client.list_objects({
                     workspaces: project_ids, type: p.type, showHidden: 1});
            return prom
        };

    };

    this._parse_object_id_string = function(p) {
        if (p.fq_id != undefined) {
            var re = p.fq_id.match( legit_narr_ws_id);
            if (re) {
                p.project_id = re[1];
                p.narrative_id = re[2];
            } else {
                p.error_callback("Cannot parse fq_id: " + p.fq_id);
            }
        }
    }

    //copy a narrative and dependencies to the home workspace
    //
    // The inputs are based on the numeric workspace id and numeric objid, which
    // are using the narrative URLs. Alternative you can just pass in a string
    // like "ws.637.obj.1" in the fq_id (fully qualified id) and this will parse
    // it out into the necessary components.
    // returns a dicts with the following keys:
    // fq_id - the fully qualified id the new narrative of the form "ws.####.obj.###"
    //
    // known issues: in the case of a name collision in a narrative dependency,
    // the dependency is renamed by appending a timestamp. This is not currently
    // altered in the narrative or in the narrative object metadata.
    this.copy_narrative = function (p_in) {
        //TODO all these functions need some cleanup, they're nasty
        var def_params = {
                project_id : undefined, // numeric workspace id
                narrative_id : undefined, //numeric object id
                fq_id : undefined, // string of the form "ws.{numeric ws id}.obj.{numeric obj id}"
                callback: undefined,
                error_callback: error_handler
                };
        var p = $.extend( def_params, p_in);
        this._parse_object_id_string(p);
        var self = this;
        var metadata_fn = ws_client.get_object_info([{wsid: p.project_id, objid : p.narrative_id}], 1);
        $.when(metadata_fn).then(function(list_obj_info) {
            if (list_obj_info.length != 1) {
                p.error_callback( "Error: narrative ws." + p.project_id +
                        ".obj." + p.narrative_id + " not found");
            } else {
                var obj_info = list_obj_info[0];
                var narname = obj_info[1];
                var home = USER_ID + ":home";
                self._get_narrative_deps_from_obj_info({
                    obj_info: obj_info,
                    callback: function(deps) {
                        var copynar = function(newname) {
                            self._copy_one_object(obj_info[6], obj_info[1],
                                    obj_info[4], home, newname,
                                    function(res) {
                                        var fq = "ws." + res[6] + ".obj." + res[0];
                                        p.callback({fq_id: fq})
                                    }, p.error_callback
                                );
                        };
                        var ts = self._get_time_stamp();
                        $.when(ws_client.get_object_info(
                            [{workspace: home, name: narname}], 0,
                            function(result) { //success, the object exists
                                copynar(narname + "-" + ts);
                            },
                            function(result) { //fail
                                if (!/^No object with .+ exists in workspace [:\w]+$/
                                        .test(result.error.message)) {
                                    p.error_callback(result.error.message);
                                }
                                //everything's cool, the object doesn't exist in home
                                copynar(narname)
                            })
                        );

                        $.each(deps.deps, function(key, val) {
                            var newdep = deps.deps[key].overwrite ? deps.deps[key].name
                                    + "-" + ts : deps.deps[key].name;
                            self._copy_one_object(obj_info[6], key, null,
                                    home, newdep, function() {}, p.error_callback);
                        });
                    }
                });
            }
        });
    };

    this._copy_one_object = function(ws, obj, ver, wstar, objtar, callback,
            error_callback) {
        if (ver == null) {
            var prom = ws_client.get_object_info([{ref: ws + "/" + obj}], 0);
            $.when(prom).done(
                function(list_obj_info) {
                    if (list_obj_info.length != 1) {
                        error_callback( "Error: narrative ws." + p.project_id +
                                ".obj." + p.narrative_id + " not found");
                    } else {
                        ver = list_obj_info[0][4];
                        ws = list_obj_info[0][6];
                        obj = list_obj_info[0][0];
                        ws_client.copy_object({from: {ref: ws + "/" + obj + "/" + ver},
                            to: {ref: wstar + "/" + objtar}},
                            callback,
                            function(result) {//failed
//                                console.log("error copying object");
                                error_callback(result.error.message);
                            }
                       );
                    }
                });
            $.when(prom).fail(
                function(res) {
//                    console.log("error copying object: " + res.error.message);
                    error_callback(res.error.message);
                }
            );
        } else {
            ws_client.copy_object(
                {from: {ref: ws + "/" + obj + "/" + ver},
                 to: {ref: wstar + "/" + objtar}},
                callback,
                function(result) {//failed
                    error_callback(result.error.message);
                }
           );
        }
    };

    this._get_time_stamp = function() {
        var d = new Date();
        var width2 = function(s) {
            if (s.length == 1) {
                return "0" + s;
            }
            return s;
        }
        return  d.getUTCFullYear().toString() +
                width2((d.getUTCMonth() + 1).toString()) +
                width2(d.getUTCDate().toString()) +
                width2(d.getUTCHours().toString()) +
                width2(d.getUTCMinutes().toString()) +
                width2(d.getUTCSeconds().toString())

    };

    // Examine a narrative object's metadata and return all its dependencies
    // The inputs are based on the numeric workspace id and numeric objid, which
    // are using the narrative URLs. Alternative you can just pass in a string
    // like "ws.637.obj.1" in the fq_id (fully qualified id) and this will parse
    // it out into the necessary components.
    // what is returns is a dictionary with the following keys
    // fq_id - the fully qualified id of the form "ws.####.obj.###"
    // description - the description of the narrative
    // name - the name of the narrative
    // deps - a dictionary keyed on the dependencies names (based on ws.####.obj.### format)
    //        where the subkeys are:
    //        name - textual name
    //        type - textual type of the object
    //        overwrite - a boolean that indicates if there is already
    this.get_narrative_deps = function (p_in) {
        var def_params = { callback : undefined,
                project_id : undefined, // numeric workspace id
                narrative_id : undefined, //numeric object id
                fq_id : undefined, // string of the form "ws.{numeric ws id}.obj.{numeric obj id}",
                callback: undefined,
                error_callback: error_handler };
        var p = $.extend( def_params, p_in);
        this._parse_object_id_string(p);
        var self = this;
        var metadata_fn = ws_client.get_object_info([{wsid: p.project_id, objid : p.narrative_id}], 1);
        $.when( metadata_fn).then( function( obj_info) {
            if (obj_info.length != 1) {
                p.error_callback( "Error: narrative ws." + p.project_id +
                        ".obj." + p.narrative_id + " not found");
            } else {
                var oi = {obj_info: obj_info[0],
                          callback: p.callback
                          };
                self._get_narrative_deps_from_obj_info(oi)
            }
        });
    };

    this._get_narrative_deps_from_obj_info = function(p) {
        var res = {};
        var obj_info = p.obj_info;
        var meta = obj_info[10]
        res.fq_id = "ws." + obj_info[6] + ".obj." + obj_info[0];
        res.description = meta.description;
        res.name = meta.name;
        var temp = $.parseJSON(meta.data_dependencies);
        //deps should really be stored as an id, not a name, since names can change
        var deps = temp.reduce( function(prev,curr,index) {
            var dep = curr.split(" ");
            prev[dep[1]] = {};
            prev[dep[1]].type = dep[0];
            prev[dep[1]].name = dep[1];
            prev[dep[1]].overwrite = false;
            return(prev);
        },{});
        var home = USER_ID + ":home";
        var proms = []
        //TODO use has_objects when it exists rather than multiple get_objects calls
        $.each(deps, function(key, val) {
            proms.push(ws_client.get_object_info(
                    [{workspace: home, name: key}], 0,
                    function(result) { //success, the object exists
                        deps[key].overwrite = true;
                    },
                    function(result) { //fail
                        console.log(result.error.message);
                        if (!/^No object with .+ exists in workspace [:\w]+$/
                                .test(result.error.message)) {
                            //p.error_callback(result.error.message);
                        }
                        //everything's cool, the object doesn't exist in home
                    })
            );

        });
        //this wraps all the promise objects in another promise object
        //that forces all the original POs to resolve in a $.when()
        proms = $.map(proms, function(p) {
            var dfd = $.Deferred();
            p.always(function() { dfd.resolve(); });
            return dfd.promise();
        });
        res.deps = deps;
        $.when.apply($, proms).done(function() {
                p.callback(res);
        });
    };

    // Create an empty narrative object with the specified name. The name
    // *must" conform to the future workspace rules about object naming,
    // the it can only contain [a-zA-Z0-9_]. We show the error prompt
    // if it fails to conform
    this.new_narrative = function( p_in ) {
        var def_params = { project_id : USER_ID+":home",
                           narrative_id : undefined,
                           description : "A KBase narrative" };

        var p = $.extend( def_params, p_in);

        var nar = $.extend(true,{},empty_narrative);
        nar.data.metadata.ws_name = p.project_id;
        nar.name = p.narrative_id;
        nar.data.metadata.name = p.narrative_id;
        nar.data.metadata.creator = USER_ID;

        var ws_fn = ws_client.save_objects( {workspace: p.project_id, objects: [nar]});
        return $.when( ws_fn ).then( function(obj_meta) {
                      return obj_meta_dict(obj_meta);
                  });
    };

}


text:
package org.openid4java.message;

import org.openid4java.OpenIDException;

/**
 * @author Marius Scurtescu, Johnny Bufu
 */
public class MessageException extends OpenIDException
{
    public MessageException(String message)
    {
        super(message, MESSAGE_ERROR);
    }

    public MessageException(String message, int code)
    {
        super(message, code);
    }

    public MessageException(Throwable cause)
    {
        super(MESSAGE_ERROR, cause);
    }

    public MessageException(int code, Throwable cause)
    {
        super(code, cause);
    }

    public MessageException(String message, Throwable cause)
    {
        super(message, MESSAGE_ERROR, cause);
    }

    public MessageException(String message, int code, Throwable cause)
    {
        super(message, code, cause);
    }
}


text:
#include <stdio.h>
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winerror.h>
#include <wincrypt.h>

#include "wine/test.h"

static BOOL (WINAPI *pCryptProtectData)(DATA_BLOB*,LPCWSTR,DATA_BLOB*,PVOID,CRYPTPROTECT_PROMPTSTRUCT*,DWORD,DATA_BLOB*);
static BOOL (WINAPI *pCryptUnprotectData)(DATA_BLOB*,LPWSTR*,DATA_BLOB*,PVOID,CRYPTPROTECT_PROMPTSTRUCT*,DWORD,DATA_BLOB*);

static char  secret[]     = "I am a super secret string that no one can see!";
static char  secret2[]    = "I am a super secret string indescribable string";
static char  key[]        = "Wibble wibble wibble";
static const WCHAR desc[] = {'U','l','t','r','a',' ','s','e','c','r','e','t',' ','t','e','s','t',' ','m','e','s','s','a','g','e',0};
static BOOL protected = FALSE; /* if true, the unprotect tests can run */
static DATA_BLOB cipher;
static DATA_BLOB cipher_entropy;
static DATA_BLOB cipher_no_desc;

static void test_cryptprotectdata(void)
{
    LONG r;
    DATA_BLOB plain;
    DATA_BLOB entropy;

    plain.pbData=(void*)secret;
    plain.cbData=strlen(secret)+1;

    entropy.pbData=(void*)key;
    entropy.cbData=strlen(key)+1;

    SetLastError(0xDEADBEEF);
    protected = pCryptProtectData(NULL,desc,NULL,NULL,NULL,0,&cipher);
    ok(!protected, "Encrypting without plain data source.\n");
    r = GetLastError();
    ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);

    SetLastError(0xDEADBEEF);
    protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,NULL);
    ok(!protected, "Encrypting without cipher destination.\n");
    r = GetLastError();
    ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);

    cipher.pbData=NULL;
    cipher.cbData=0;

    /* without entropy */
    SetLastError(0xDEADBEEF);
    protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,&cipher);
    ok(protected ||
     broken(!protected), /* Win9x/NT4 */
     "Encrypting without entropy.\n");
    if (protected)
    {
        r = GetLastError();
        ok(r == ERROR_SUCCESS ||
           r == ERROR_IO_PENDING, /* win2k */
           "Expected ERROR_SUCCESS or ERROR_IO_PENDING, got %d\n",r);
    }

    cipher_entropy.pbData=NULL;
    cipher_entropy.cbData=0;

    /* with entropy */
    SetLastError(0xDEADBEEF);
    protected = pCryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
    ok(protected ||
     broken(!protected), /* Win9x/NT4 */
     "Encrypting with entropy.\n");

    cipher_no_desc.pbData=NULL;
    cipher_no_desc.cbData=0;

    /* with entropy but no description */
    plain.pbData=(void*)secret2;
    plain.cbData=strlen(secret2)+1;
    SetLastError(0xDEADBEEF);
    protected = pCryptProtectData(&plain,NULL,&entropy,NULL,NULL,0,&cipher_no_desc);
    if (!protected)
    {
        /* fails in win2k */
        ok(GetLastError() == ERROR_INVALID_PARAMETER,
           "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    }
}

static void test_cryptunprotectdata(void)
{
    LONG r;
    DATA_BLOB plain;
    DATA_BLOB entropy;
    BOOL okay;
    WCHAR * data_desc;

    entropy.pbData=(void*)key;
    entropy.cbData=strlen(key)+1;

    /* fails in win2k */
    if (!protected)
    {
        skip("CryptProtectData failed to run\n");
        return;
    }

    plain.pbData=NULL;
    plain.cbData=0;

    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
    ok(!okay,"Decrypting without destination\n");
    r = GetLastError();
    ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);

    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
    ok(!okay,"Decrypting without source\n");
    r = GetLastError();
    ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);

    plain.pbData=NULL;
    plain.cbData=0;

    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
    ok(!okay,"Decrypting without needed entropy\n");
    r = GetLastError();
    ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);

    plain.pbData=NULL;
    plain.cbData=0;
    data_desc=NULL;

    /* without entropy */
    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
    ok(okay,"Decrypting without entropy\n");

    ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
    ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
    ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
    ok(data_desc!=NULL,"Description not allocated\n");
    ok(!lstrcmpW(data_desc,desc),"Description does not match\n");

    LocalFree(plain.pbData);
    LocalFree(data_desc);

    plain.pbData=NULL;
    plain.cbData=0;
    data_desc=NULL;

    /* with wrong entropy */
    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
    ok(!okay,"Decrypting with wrong entropy\n");
    r = GetLastError();
    ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);

    /* with entropy */
    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
    ok(okay,"Decrypting with entropy\n");

    ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
    ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
    ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
    ok(data_desc!=NULL,"Description not allocated\n");
    ok(!lstrcmpW(data_desc,desc),"Description does not match\n");

    LocalFree(plain.pbData);
    LocalFree(data_desc);

    plain.pbData=NULL;
    plain.cbData=0;
    data_desc=NULL;

    /* with entropy but no description */
    SetLastError(0xDEADBEEF);
    okay = pCryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
    ok(okay,"Decrypting with entropy and no description\n");

    ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
    ok(plain.cbData==strlen(secret2)+1,"Plain DATA_BLOB wrong length\n");
    ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
    ok(data_desc!=NULL,"Description not allocated\n");
    ok(data_desc[0]=='\0',"Description not empty\n");

    LocalFree(data_desc);
    LocalFree(plain.pbData);

    plain.pbData=NULL;
    plain.cbData=0;
}

static void test_simpleroundtrip(const char *plaintext)
{
    DATA_BLOB input;
    DATA_BLOB encrypted;
    DATA_BLOB output;
    int res;
    WCHAR emptyW[1];

    emptyW[0] = 0;
    input.pbData = (unsigned char *)plaintext;
    input.cbData = strlen(plaintext);
    res = pCryptProtectData(&input, emptyW, NULL, NULL, NULL, 0, &encrypted);
    ok(res != 0 || broken(!res), "can't protect\n");
    if (!res)
    {
        /* Fails on Win9x, NT4 */
        win_skip("CryptProtectData failed\n");
        return;
    }

    res = pCryptUnprotectData(&encrypted, NULL, NULL, NULL, NULL, 0, &output);
    ok(res != 0, "can't unprotect; last error %u\n", GetLastError());
    ok(output.cbData == strlen(plaintext), "output wrong length %d for input '%s', wanted %d\n", output.cbData, plaintext, lstrlenA(plaintext));
    ok(!memcmp(plaintext, (char *)output.pbData, output.cbData), "output wrong contents for input '%s'\n", plaintext);
    LocalFree(output.pbData);
    LocalFree(encrypted.pbData);
}

START_TEST(protectdata)
{
    HMODULE hCrypt32 = GetModuleHandleA("crypt32.dll");
    pCryptProtectData = (void*)GetProcAddress(hCrypt32, "CryptProtectData");
    pCryptUnprotectData = (void*)GetProcAddress(hCrypt32, "CryptUnprotectData");
    if (!pCryptProtectData || !pCryptUnprotectData)
    {
        win_skip("Crypt(Un)ProtectData() is not available\n");
        return;
    }

    protected=FALSE;
    test_cryptprotectdata();
    test_cryptunprotectdata();
    test_simpleroundtrip("");
    test_simpleroundtrip("hello");

    /* deinit globals here */
    if (cipher.pbData) LocalFree(cipher.pbData);
    if (cipher_entropy.pbData) LocalFree(cipher_entropy.pbData);
    if (cipher_no_desc.pbData) LocalFree(cipher_no_desc.pbData);
}


text:
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CDAPDatasource = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var CDAPDatasource = exports.CDAPDatasource = function () {
  function CDAPDatasource(instanceSettings, $q, backendSrv, templateSrv) {
    _classCallCheck(this, CDAPDatasource);

    this.url = instanceSettings.url;
    this.q = $q;
    this.backendSrv = backendSrv;

    // A memo-table for search results, because searching can be painfully slow.
    // (This doesn't cache actual metric data, just tags and metric names.)
    this._metricsSearchTable = {};
  }

  _createClass(CDAPDatasource, [{
    key: 'testDatasource',
    value: function testDatasource() {
      // For now, just try searching for some tags. If that works, then assume we're good.
      return this.searchForTags().then(function () {
        return { status: 'success', message: 'Data source is working', title: 'Success' };
      });
    }
  }, {
    key: 'metricFindQuery',
    value: function metricFindQuery(query) {
      // Convert each metric name into an object with "text" and "expandable" fields.
      return this.searchForMetrics().then(function (metrics) {
        return _lodash2.default.map(metrics, function (x) {
          return { text: x, expandable: false };
        });
      });
    }
  }, {
    key: 'searchForTags',
    value: function searchForTags(tags) {
      // CDAP returns a list of objects with "name" and "value" fields. We want to
      // concatenate them with ":" in the middle. But we also want to add tags with
      // "*" for the value, like "app:*".
      return this._metricsSearch('tag', tags).then(function (objs) {
        var names = _lodash2.default.sortBy(_lodash2.default.uniq(_lodash2.default.map(objs, function (x) {
          return x.name;
        })));
        var found = _lodash2.default.filter(names, function (x) {
          return x !== 'namespace';
        });
        var stars = _lodash2.default.map(found, function (x) {
          return { name: x, value: '*' };
        });
        return _lodash2.default.concat(stars, objs);
      }).then(function (objs) {
        return _lodash2.default.map(objs, function (x) {
          return x.name + ':' + x.value;
        });
      }).then(function (strs) {
        return _lodash2.default.sortBy(strs);
      });
    }
  }, {
    key: 'searchForMetrics',
    value: function searchForMetrics(tags) {
      // The results are probably already sorted. But better safe than sorry.
      return this._metricsSearch('metric', tags).then(function (strs) {
        return _lodash2.default.sortBy(strs);
      });
    }
  }, {
    key: 'query',
    value: function query(options) {
      // If a target is missing a refId field, then use this name.
      var defaultRefId = 'data';

      // Similarly, if a target is missing metric names, then use this one.
      var defaultMetric = 'system.dataset.store.reads';

      // Utility function to guess the resolution we should request from CDAP.
      // (The "ms" comes from options.intervalMs.)
      var guessResolution = function guessResolution(ms) {
        if (ms < 1000 * 60) {
          return '1s';
        } else if (ms < 1000 * 60 * 60) {
          return '1m';
        } else {
          return '1h';
        }
      };

      // Grafana provides timestamps that Date.parse can understand. So use that.
      // It returns the number of milliseconds since the epoch, but CDAP wants it
      // in seconds. So divide by 1000 and convert to an integer.
      var parseTime = function parseTime(x) {
        return Date.parse(x) / 1000 | 0;
      };

      // All of the targets use the same time range, so we can define this now.
      var timeRange = {
        resolution: guessResolution(options.intervalMs),
        start: parseTime(options.range.from),
        end: parseTime(options.range.to)
      };

      // Create a new object. The keys are the refId names of each target. Map
      // each target a CDAP subquery object. The subquery object has the fields
      // that CDAP requires: tags, metrics, groupBy, and timeRange.
      var data = _lodash2.default.fromPairs(_lodash2.default.map(options.targets, function (target) {
        var metrics = target.metrics || [];
        return [target.refId || defaultRefId, {
          tags: _lodash2.default.fromPairs(_lodash2.default.map(target.tags, function (str) {
            return str.split(':');
          })),
          metrics: metrics.length ? metrics : [defaultMetric],
          groupBy: target.groups || [],
          timeRange: target.aggregate ? { aggregate: "true" } : timeRange
        }];
      }));

      // Count how often each metric appears across all our targets. This tells
      // us if we need to include a target's refId in our labels. (We omit the
      // refId unless it's required to disambiguate the results.)
      var metricCounts = {};
      _lodash2.default.forEach(options.targets, function (target) {
        _lodash2.default.forEach(_lodash2.default.uniq(target.metrics), function (metric) {
          if (!metricCounts[metric]) {
            metricCounts[metric] = 1;
          } else {
            metricCounts[metric] += 1;
          }
        });
      });

      // Request the metric data. Map each target to the corresponding response data.
      return this._cdapRequest('/v3/metrics/query', {}, data).then(function (response) {
        return { data: _lodash2.default.flatMap(options.targets, function (target) {
            var refId = target.refId || defaultRefId;
            var convertTime = function convertTime(t) {
              // When you request the aggregate value, CDAP returns one data point
              // at time=0. Grafana doesn't like that. So in this case, shove the
              // data point to the end of our time range.
              var secs = target.aggregate && t === 0 ? timeRange.end : t;

              // Also, CDAP returns times in seconds since the epoch. Grafana wants
              // the times in milliseconds.
              return secs * 1000;
            };
            return _lodash2.default.map(response.data[refId].series, function (found) {
              // We might need to prefix our metric name with some context.
              var prefix = metricCounts[found.metricName] > 1 ? refId + ' ' : '';

              // For each group in our target, prefix our label with the group's value.
              _lodash2.default.forEach(target.groups, function (group) {
                prefix += found.grouping[group] + ' ';
              });

              // The "target" seems to be the label that appears in the panel for
              // this particular timeseries.
              return {
                target: prefix + found.metricName,
                datapoints: _lodash2.default.map(found.data, function (pt) {
                  return [pt.value, convertTime(pt.time)];
                })
              };
            });
          }) };
      });
    }

    // Searches for tags or metrics (indicated by the target argument). Returns
    // a promise of the response data.

  }, {
    key: '_metricsSearch',
    value: function _metricsSearch(target, tags) {
      // Check the memo-table. (Searching for tags and metrics can be slow.)
      var table = this._metricsSearchTable;
      var key = 'metrics,' + target + ',' + (tags ? tags.join(',') : '');
      if (table[key]) {
        return table[key];
      }

      var params = { 'target': target };
      if (tags && tags.length > 0) {
        params.tag = tags;
      }
      var request = this._cdapRequest('/v3/metrics/search', params).then(function (response) {
        return response.data;
      });
      table[key] = request;
      return request;
    }
  }, {
    key: '_cdapRequest',

    // Sends a request to CDAP. Returns a promise of the response. (Note that the
    // "data" argument is optional and may be omitted.)
    value: function _cdapRequest(path, params, data) {
      return this.backendSrv.datasourceRequest({
        method: 'POST',
        url: this.url + path,
        params: params,
        data: data
      });
    }
  }]);

  return CDAPDatasource;
}();


text:
require 'spec_helper'

RSpec.describe Gitlab::Ci::Build::AutoRetry do
  let(:auto_retry) { described_class.new(build) }

  describe '#allowed?' do
    using RSpec::Parameterized::TableSyntax

    let(:build) { create(:ci_build) }

    subject { auto_retry.allowed? }

    where(:description, :retry_count, :options, :failure_reason, :result) do
      "retries are disabled" | 0 | { max: 0 } | nil | false
      "max equals count" | 2 | { max: 2 } | nil | false
      "max is higher than count" | 1 | { max: 2 } | nil | true
      "max is a string" | 1 | { max: '2' } | nil | true
      "matching failure reason" | 0 | { when: %w[api_failure], max: 2 } | :api_failure | true
      "not matching with always" | 0 | { when: %w[always], max: 2 } | :api_failure | true
      "not matching reason" | 0 | { when: %w[script_error], max: 2 } | :api_failure | false
      "scheduler failure override" | 1 | { when: %w[scheduler_failure], max: 1 } | :scheduler_failure | false
      "default for scheduler failure" | 1 | {} | :scheduler_failure | true
    end

    with_them do
      before do
        allow(build).to receive(:retries_count) { retry_count }

        build.options[:retry] = options
        build.failure_reason = failure_reason
        allow(build).to receive(:retryable?).and_return(true)
      end

      it { is_expected.to eq(result) }
    end

    context 'when build is not retryable' do
      before do
        allow(build).to receive(:retryable?).and_return(false)
      end

      specify { expect(subject).to eq(false) }
    end
  end

  describe '#options_retry_max' do
    subject(:result) { auto_retry.send(:options_retry_max) }

    context 'with retries max config option' do
      let(:build) { create(:ci_build, options: { retry: { max: 1 } }) }

      context 'when build_metadata_config is set' do
        before do
          stub_feature_flags(ci_build_metadata_config: true)
        end

        it 'returns the number of configured max retries' do
          expect(result).to eq 1
        end
      end

      context 'when build_metadata_config is not set' do
        before do
          stub_feature_flags(ci_build_metadata_config: false)
        end

        it 'returns the number of configured max retries' do
          expect(result).to eq 1
        end
      end
    end

    context 'without retries max config option' do
      let(:build) { create(:ci_build) }

      it 'returns nil' do
        expect(result).to be_nil
      end
    end

    context 'when build is degenerated' do
      let(:build) { create(:ci_build, :degenerated) }

      it 'returns nil' do
        expect(result).to be_nil
      end
    end

    context 'with integer only config option' do
      let(:build) { create(:ci_build, options: { retry: 1 }) }

      it 'returns the number of configured max retries' do
        expect(result).to eq 1
      end
    end
  end

  describe '#options_retry_when' do
    subject(:result) { auto_retry.send(:options_retry_when) }

    context 'with retries when config option' do
      let(:build) { create(:ci_build, options: { retry: { when: ['some_reason'] } }) }

      it 'returns the configured when' do
        expect(result).to eq ['some_reason']
      end
    end

    context 'without retries when config option' do
      let(:build) { create(:ci_build) }

      it 'returns always array' do
        expect(result).to eq ['always']
      end
    end

    context 'with integer only config option' do
      let(:build) { create(:ci_build, options: { retry: 1 }) }

      it 'returns always array' do
        expect(result).to eq ['always']
      end
    end
  end
end


text:
  La Rochelle-Normande est
       un village
       situé dans le département de Manche en Basse-Normandie. Elle totalisait 248 habitants en 2008.

Le nombre de logements, à La Rochelle-Normande, se décomposait en 2011 en deux appartements et 150 maisons soit
 un marché relativement équilibré.

À coté de La Rochelle-Normande sont situées les villes de
La Haye-Pesnel localisée à 4 km, 1 365 habitants,
Les Chambres à 3 km, 111 habitants,
Montviron à 3 km, 307 habitants,
Lolif localisée à 4 km, 565 habitants,
Angey située à 4 km, 208 habitants,
Champcervon située à 3 km, 203 habitants,
 entre autres. De plus, La Rochelle-Normande est située à seulement quatorze km de Granville.

Si vous pensez demenager à La Rochelle-Normande, vous pourrez facilement trouver une maison à acheter.


text:
package org.kuali.kfs.sec.document;

import java.util.HashMap;

import org.kuali.kfs.sec.businessobject.SecurityDefinition;
import org.kuali.kfs.sec.businessobject.SecurityModelMember;
import org.kuali.kfs.sec.businessobject.SecurityPrincipal;
import org.kuali.kfs.sec.businessobject.SecurityPrincipalDefinition;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.rice.core.api.membership.MemberType;
import org.kuali.rice.kew.api.exception.WorkflowException;
import org.kuali.rice.kim.api.role.Role;
import org.kuali.rice.kim.api.role.RoleMember;
import org.kuali.rice.kim.api.role.RoleService;
import org.kuali.rice.kim.api.services.KimApiServiceLocator;
import org.kuali.rice.kns.document.MaintenanceDocument;
import org.kuali.rice.krad.bo.DocumentHeader;
import org.kuali.rice.krad.service.DocumentService;
import org.kuali.rice.krad.util.KRADConstants;

/**
 * Maintainable implementation for the Security Principal maintenance document. Hooks into Post processing to create the KIM permissions for the principal and assign security role
 * members
 */
public class SecurityPrincipalMaintainableImpl extends AbstractSecurityModuleMaintainable {
    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SecurityPrincipalMaintainableImpl.class);

    /**
     * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#doRouteStatusChange(org.kuali.rice.krad.bo.DocumentHeader)
     */
    @Override
    public void doRouteStatusChange(DocumentHeader documentHeader) {
        super.doRouteStatusChange(documentHeader);

        if (documentHeader.getWorkflowDocument().isProcessed()) {
            DocumentService documentService = SpringContext.getBean(DocumentService.class);
            try {
                MaintenanceDocument document = (MaintenanceDocument) documentService.getByDocumentHeaderId(documentHeader.getDocumentNumber());
                SecurityPrincipal oldSecurityPrincipal = (SecurityPrincipal) document.getOldMaintainableObject().getBusinessObject();
                SecurityPrincipal newSecurityPrincipal = (SecurityPrincipal) document.getNewMaintainableObject().getBusinessObject();

                boolean newMaintenanceAction = getMaintenanceAction().equalsIgnoreCase(KRADConstants.MAINTENANCE_NEW_ACTION) || getMaintenanceAction().equalsIgnoreCase(KRADConstants.MAINTENANCE_COPY_ACTION);

                assignOrUpdatePrincipalMembershipToDefinitionRoles(oldSecurityPrincipal, newSecurityPrincipal, newMaintenanceAction);
                assignOrUpdatePrincipalModelRoles(newSecurityPrincipal);
            }
            catch (WorkflowException e) {
                LOG.error("caught exception while handling handleRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
                throw new RuntimeException("caught exception while handling handleRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
            }
        }
    }

    /**
     * Iterates through the principal definition list and assigns the principal to the definition role if necessary or updates the current member assignment
     *
     * @param oldSecurityPrincipal SecurityPrincipal before updates
     * @param newSecurityPrincipal SecurityPrincipal which contains the definition list and principal
     * @param newMaintenanceAction boolean indicating whether this is a new record (old side will not contain data)
     */
    protected void assignOrUpdatePrincipalMembershipToDefinitionRoles(SecurityPrincipal oldSecurityPrincipal, SecurityPrincipal newSecurityPrincipal, boolean newMaintenanceAction) {
        RoleService roleService = KimApiServiceLocator.getRoleService();

        String principalId = newSecurityPrincipal.getPrincipalId();

        for (SecurityPrincipalDefinition securityPrincipalDefinition : newSecurityPrincipal.getPrincipalDefinitions()) {
            SecurityDefinition securityDefinition = securityPrincipalDefinition.getSecurityDefinition();

            Role definitionRoleInfo = roleService.getRole(securityDefinition.getRoleId());

            RoleMember principalMembershipInfo = null;
            if (!newMaintenanceAction) {
                SecurityPrincipalDefinition oldPrincipalDefinition = null;
                for (SecurityPrincipalDefinition principalDefinition : oldSecurityPrincipal.getPrincipalDefinitions()) {
                   if ((principalDefinition.getPrincipalDefinitionId() != null) && principalDefinition.getPrincipalDefinitionId().equals(securityPrincipalDefinition.getPrincipalDefinitionId())) {
                       oldPrincipalDefinition = principalDefinition;
                   }
                }

                if (oldPrincipalDefinition != null) {
                    principalMembershipInfo = getRoleMembershipForMemberType(definitionRoleInfo.getId(), principalId, MemberType.PRINCIPAL.getCode(), getRoleQualifiersFromSecurityModelDefinition(oldPrincipalDefinition));
                }
            }

            // only create membership if principal definition record is active
            boolean membershipActive = securityPrincipalDefinition.isActive();

            // if membership already exists, need to remove if the principal record is now inactive or the qualifications need updated
            if (principalMembershipInfo != null) {
                boolean qualificationsMatch = doMembershipQualificationsMatchValues(principalMembershipInfo.getAttributes(), securityPrincipalDefinition.getConstraintCode(), securityPrincipalDefinition.getOperatorCode(), securityPrincipalDefinition.getAttributeValue());
                if (!membershipActive || !qualificationsMatch) {
                    roleService.removePrincipalFromRole(principalMembershipInfo.getMemberId(), definitionRoleInfo.getNamespaceCode(), definitionRoleInfo.getName(), principalMembershipInfo.getAttributes());
                }
            }

            // create of update role if membership should be active
            if (membershipActive) {
                if ( principalMembershipInfo == null ) {
                    principalMembershipInfo = roleService.assignPrincipalToRole( principalId, definitionRoleInfo.getNamespaceCode(), definitionRoleInfo.getName(), getRoleQualifiersFromSecurityModelDefinition(securityPrincipalDefinition));
                } else {
                    RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(principalMembershipInfo);
                    updatedRoleMember.setAttributes(getRoleQualifiersFromSecurityModelDefinition(securityPrincipalDefinition));
                    updatedRoleMember.setMemberId(principalId);
                    roleService.updateRoleMember(updatedRoleMember.build());
                }
            }
        }
    }

    /**
     * Iterates through the principal model list and assigns the principal to the model role or updates the membership
     *
     * @param securityPrincipal SecurityPrincipal which contains the model list and principal
     */
    protected void assignOrUpdatePrincipalModelRoles(SecurityPrincipal securityPrincipal) {
        RoleService roleService = KimApiServiceLocator.getRoleService();
        String principalId = securityPrincipal.getPrincipalId();

        for (SecurityModelMember principalModel : securityPrincipal.getPrincipalModels()) {
            Role modelRole = roleService.getRole(principalModel.getSecurityModel().getRoleId());
            updateSecurityModelRoleMember(modelRole, principalModel, MemberType.PRINCIPAL.getCode(), principalId, new HashMap<String, String>(0));
        }
    }

}