From cf2dad3ab8872d3ff50a702a8bfaa93cc3ff794d Mon Sep 17 00:00:00 2001 From: Matt DeMoss Date: Tue, 29 Mar 2022 19:50:07 -0400 Subject: [PATCH] Reducer (retrying after failed tests) (#2273) --- bridges/FeedReducerBridge.php | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 bridges/FeedReducerBridge.php diff --git a/bridges/FeedReducerBridge.php b/bridges/FeedReducerBridge.php new file mode 100644 index 00000000..1963b53b --- /dev/null +++ b/bridges/FeedReducerBridge.php @@ -0,0 +1,60 @@ + array( + 'name' => 'Feed URI', + 'exampleValue' => 'https://lorem-rss.herokuapp.com/feed?length=42', + 'required' => true + ), + 'percentage' => array( + 'name' => 'percentage', + 'type' => 'number', + 'exampleValue' => 50, + 'required' => true + ) + )); + const CACHE_TIMEOUT = 3600; + + public function collectData(){ + if(preg_match('#^http(s?)://#i', $this->getInput('url'))) { + $this->collectExpandableDatas($this->getInput('url')); + } else { + throw new Exception('URI must begin with http(s)://'); + } + } + + public function getItems(){ + $filteredItems = array(); + $intPercentage = (int)preg_replace('/[^0-9]/', '', $this->getInput('percentage')); + + foreach ($this->items as $thisItem) { + // The URL is included in the hash: + // - so you can change the output by adding a local-part to the URL + // - so items with the same URI in different feeds won't be correlated + + // $pseudoRandomInteger will be a 16 bit unsigned int mod 100. + // This won't be uniformly distributed 1-100, but should be close enough. + + $pseudoRandomInteger = unpack( + 'S', // unsigned 16-bit int + hash( 'sha256', $thisItem['uri'] . '::' . $this->getInput('url'), true ) + )[1] % 100; + + if ($pseudoRandomInteger < $intPercentage) { + $filteredItems[] = $thisItem; + } + } + + return $filteredItems; + } + + public function getName(){ + $trimmedPercentage = preg_replace('/[^0-9]/', '', $this->getInput('percentage')); + return parent::getName() . ' [' . $trimmedPercentage . '%]'; + } +}