code: func (c *ReplicaCalculator) GetExternalPerPodMetricReplicas(currentReplicas int32, targetUtilizationPerPod int64, metricName, namespace string, metricSelector *metav1.LabelSelector) (replicaCount int32, utilization int64, timestamp time.Time, err error) {
  	metricLabelSelector, err := metav1.LabelSelectorAsSelector(metricSelector)
  	if err != nil {
  		return 0, 0, time.Time{}, err
  	}
  	metrics, timestamp, err := c.metricsClient.GetExternalMetric(metricName, namespace, metricLabelSelector)
  	if err != nil {
  		return 0, 0, time.Time{}, fmt.Errorf("unable to get external metric %s/%s/%+v: %s", namespace, metricName, metricSelector, err)
  	}
  	utilization = 0
  	for _, val := range metrics {
  		utilization = utilization + val
  	}
  
  	replicaCount = currentReplicas
  	usageRatio := float64(utilization) / (float64(targetUtilizationPerPod) * float64(replicaCount))
  	if math.Abs(1.0-usageRatio) > c.tolerance {
  		// update number of replicas if the change is large enough
  		replicaCount = int32(math.Ceil(float64(utilization) / float64(targetUtilizationPerPod)))
  	}
  	utilization = int64(math.Ceil(float64(utilization) / float64(currentReplicas)))
  	return replicaCount, utilization, timestamp, nil
  }
docs: // GetExternalPerPodMetricReplicas calculates the desired replica count based on a
  // target metric value per pod (as a milli-value) for the external metric in the
  // given namespace, and the current replica count.