Skip to content

Commit

Permalink
fix type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pselge-5anker committed Mar 9, 2024
1 parent fd4624e commit a2cb96a
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/Prometheus/Storage/LaravelCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,37 @@ protected function collectHistograms(array $histograms): array
$data['samples'][] = [
'name' => $metaData['name'] . '_bucket',
'labelNames' => ['le'],
'labelValues' => array_merge($decodedLabelValues,
[$bucket]),
'labelValues' => array_merge(
$decodedLabelValues,
[$bucket]
),
'value' => $acc,
];
} else {
$acc += $histogramBuckets[$labelValues][$bucket];
$data['samples'][] = [
'name' => $metaData['name'].'_'.'bucket',
'name' => $metaData['name'] . '_' . 'bucket',
'labelNames' => ['le'],
'labelValues' => array_merge($decodedLabelValues,
[$bucket]),
'labelValues' => array_merge(
$decodedLabelValues,
[$bucket]
),
'value' => $acc,
];
}
}

// Add the count
$data['samples'][] = [
'name' => $metaData['name'].'_count',
'name' => $metaData['name'] . '_count',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => $acc,
];

// Add the sum
$data['samples'][] = [
'name' => $metaData['name'].'_sum',
'name' => $metaData['name'] . '_sum',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => $histogramBuckets[$labelValues]['sum'],
Expand All @@ -156,9 +160,11 @@ protected function collectHistograms(array $histograms): array
}

/**
* @param mixed[] $summary
*
* @return MetricFamilySamples[]
*/
protected function collectSummaries($summaries): array
protected function collectSummaries(array $summaries): array

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.0

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.0

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.0

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.0

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

PHPDoc tag @param references unknown parameter: $summary

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Method Prometheus\Storage\LaravelCacheAdapter::collectSummaries() has parameter $summaries with no value type specified in iterable type array.

Check failure on line 167 in src/Prometheus/Storage/LaravelCacheAdapter.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

PHPDoc tag @param references unknown parameter: $summary
{
$math = new Math();
$output = [];
Expand All @@ -174,17 +180,19 @@ protected function collectSummaries($summaries): array
'samples' => [],
];

foreach ($summary['samples'] as $key => &$values) {
foreach ($summary['samples'] as $key => $values) {
$parts = explode(':', $key);
$labelValues = $parts[2];
$decodedLabelValues = $this->decodeLabelValues($labelValues);

// Remove old data
$values = array_filter($values,
$values = array_filter(
$values,
function (array $value) use ($data): bool {
return time() - $value['time']
<= $data['maxAgeSeconds'];
});
}
);
if (count($values) === 0) {
unset($summary['samples'][$key]);
continue;
Expand All @@ -202,24 +210,28 @@ function (array $value) use ($data): bool {
$data['samples'][] = [
'name' => $metaData['name'],
'labelNames' => ['quantile'],
'labelValues' => array_merge($decodedLabelValues,
[$quantile]),
'value' => $math->quantile(array_column($values,
'value'), $quantile),
'labelValues' => array_merge(
$decodedLabelValues,
[$quantile]
),
'value' => $math->quantile(array_column(
$values,
'value'
), $quantile),
];
}

// Add the count
$data['samples'][] = [
'name' => $metaData['name'].'_count',
'name' => $metaData['name'] . '_count',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => count($values),
];

// Add the sum
$data['samples'][] = [
'name' => $metaData['name'].'_sum',
'name' => $metaData['name'] . '_sum',
'labelNames' => [],
'labelValues' => $decodedLabelValues,
'value' => array_sum(array_column($values, 'value')),
Expand Down Expand Up @@ -306,7 +318,8 @@ public function updateHistogram(array $data): void
}

$bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease);
if (array_key_exists($bucketKey, $histograms[$metaKey]['samples'])
if (
array_key_exists($bucketKey, $histograms[$metaKey]['samples'])
=== false
) {
$histograms[$metaKey]['samples'][$bucketKey] = 0;
Expand Down Expand Up @@ -334,7 +347,8 @@ public function updateSummary(array $data): void
}

$valueKey = $this->valueKey($data);
if (array_key_exists($valueKey, $summaries[$metaKey]['samples'])
if (
array_key_exists($valueKey, $summaries[$metaKey]['samples'])
=== false
) {
$summaries[$metaKey]['samples'][$valueKey] = [];
Expand Down Expand Up @@ -363,7 +377,8 @@ public function updateGauge(array $data): void
'samples' => [],
];
}
if (array_key_exists($valueKey, $gauges[$metaKey]['samples'])
if (
array_key_exists($valueKey, $gauges[$metaKey]['samples'])
=== false
) {
$gauges[$metaKey]['samples'][$valueKey] = 0;
Expand Down Expand Up @@ -392,7 +407,8 @@ public function updateCounter(array $data): void
'samples' => [],
];
}
if (array_key_exists($valueKey, $counters[$metaKey]['samples'])
if (
array_key_exists($valueKey, $counters[$metaKey]['samples'])
=== false
) {
$counters[$metaKey]['samples'][$valueKey] = 0;
Expand Down Expand Up @@ -469,8 +485,10 @@ protected function metaData(array $data): array
protected function sortSamples(array &$samples): void
{
usort($samples, function ($a, $b): int {
return strcmp(implode("", $a['labelValues']),
implode("", $b['labelValues']));
return strcmp(
implode("", $a['labelValues']),
implode("", $b['labelValues'])
);
});
}

Expand Down Expand Up @@ -511,7 +529,7 @@ protected function decodeLabelValues(string $values): array
/**
* @param string $type
*
* @return array<int,array<string,mixed>>
* @return mixed[]
* @throws InvalidArgumentException
*/
protected function fetch(string $type): array
Expand All @@ -521,7 +539,7 @@ protected function fetch(string $type): array

/**
* @param string $type
* @param array<int,array<string,mixed>> $data
* @param mixed[] $data
*
* @return void
*/
Expand All @@ -532,6 +550,6 @@ protected function push(string $type, array $data): void

protected function cacheKey(string $type): string
{
return static::CACHE_KEY_PREFIX.$type.static::CACHE_KEY_SUFFIX;
return static::CACHE_KEY_PREFIX . $type . static::CACHE_KEY_SUFFIX;
}
}

0 comments on commit a2cb96a

Please sign in to comment.