def testLogCommandEventMetrics_noInvocationTiming(
      self, event_type_metric, command_timing_metric):
    _, request_id, _, command_id = self.command.key.flat()
    invocation_completed_event = command_event_test_util.CreateTestCommandEvent(
        request_id, command_id, "0000000", "InvocationCompleted")

    command_event_handler.LogCommandEventMetrics(
        command=self.command, event=invocation_completed_event)

    expected_metric_fields = {
        metric.METRIC_FIELD_HOSTNAME: "hostname",
        metric.METRIC_FIELD_TYPE: "InvocationCompleted"
    }
    event_type_metric.Increment.assert_called_once_with(expected_metric_fields)
    command_timing_metric.assert_not_called()
  def testLogCommandEventMetrics_badEvent(
      self, event_type_metric, command_timing_metric):
    _, request_id, _, command_id = self.command.key.flat()
    invocation_completed_event = command_event_test_util.CreateTestCommandEvent(
        request_id, command_id, "0000000", "InvocationCompleted",
        data={"fetch_build_time_millis": "abcd"})  # Not a number

    # ValueError should be caught and this should not crash
    command_event_handler.LogCommandEventMetrics(
        command=self.command, event=invocation_completed_event)

    expected_metric_fields = {
        metric.METRIC_FIELD_HOSTNAME: "hostname",
        metric.METRIC_FIELD_TYPE: "InvocationCompleted"
    }
    event_type_metric.Increment.assert_called_once_with(expected_metric_fields)
    command_timing_metric.assert_not_called()
  def testLogCommandEventMetrics_withSetupTiming(
      self, event_type_metric, command_timing_metric):
    _, request_id, _, command_id = self.command.key.flat()
    invocation_completed_event = command_event_test_util.CreateTestCommandEvent(
        request_id, command_id, "0000000", "InvocationCompleted",
        data={"setup_time_millis": "10500"})  # 10.5 seconds

    command_event_handler.LogCommandEventMetrics(
        command=self.command, event=invocation_completed_event)

    expected_metric_fields = {
        metric.METRIC_FIELD_HOSTNAME: "hostname",
        metric.METRIC_FIELD_TYPE: "InvocationCompleted",
        metric.METRIC_FIELD_HOSTNAME: "hostname"
    }
    event_type_metric.Increment.assert_called_once_with(expected_metric_fields)
    command_timing_metric.assert_called_once_with(
        cluster_id="foobar",
        run_target="foo",
        command_action=metric.CommandAction.INVOCATION_SETUP,
        hostname="hostname",
        latency_secs=10.5)
  def testLogCommandEventMetrics_logInvocationComplete(
      self, event_type_metric, command_timing_metric):
    _, request_id, _, command_id = self.command.key.flat()
    self.command.start_time = TIMESTAMP
    self.command.put()
    invocation_completed_event = command_event_test_util.CreateTestCommandEvent(
        request_id, command_id, "0000000", "InvocationCompleted")

    command_event_handler.LogCommandEventMetrics(
        command=self.command, event=invocation_completed_event)

    expected_metric_fields = {
        metric.METRIC_FIELD_HOSTNAME: "hostname",
        metric.METRIC_FIELD_TYPE: "InvocationCompleted",
    }
    event_type_metric.Increment.assert_called_once_with(expected_metric_fields)
    command_timing_metric.assert_called_once_with(
        cluster_id="foobar",
        run_target="foo",
        command_action=metric.CommandAction.INVOCATION_COMPLETED,
        hostname="hostname",
        create_timestamp=TIMESTAMP)