/** * Persist changes to a metric's sample * * @param Data $data */ public function persist(Data $data) { Assertion::lessOrEqualThan($data->getDatetime()->diffInDays(), static::BACKFILL_LIMIT_DAYS, sprintf('Data samples can only be backfilled as far back as %d days', static::BACKFILL_LIMIT_DAYS)); $this->driver->post('pages/{page}/metrics/{metric}/data.json', ['metric' => $this->metricId], ['data' => ['timestamp' => $data->getDatetime()->getTimestamp(), 'value' => $data->getValue()]]); }
/** * Combine and submit the total of the samples to the metric * * @param int $resolution Resolution (defaults to 30 seconds) */ public function submitTotals($resolution = self::SAMPLES_INTERVAL) { Assertion::greaterOrEqualThan($resolution, self::SAMPLES_INTERVAL, sprintf('The minimum resolution is %d seconds', self::SAMPLES_INTERVAL)); if (sizeof($this->samples) == 0) { return; } $totals = []; ksort($this->samples); foreach ($this->samples as $timestamp => $samples) { foreach ($samples as $sample) { $key = $timestamp - $timestamp % $resolution; if (!isset($totals[$key])) { $totals[$key] = 0; } $totals[$key] += $sample; } } // Submit resulting samples to the metric foreach ($totals as $timestamp => $value) { $this->endpoint->persist(Data::fromUnixTimestamp($timestamp, $value)); } $this->reset(); }