From 543bbd0603864df4d1e74ef58f0d3d3e661ac1f4 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 15:15:35 +0100 Subject: [PATCH 01/17] Added workflows --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ .github/workflows/create-release.yml | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/create-release.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e3f8137 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: ci +on: + workflow_dispatch: + push: + branches: + - develop + pull_request: + - ready_for_review +permissions: + contents: read +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true +jobs: + ci: + name: ci + uses: statikbe/.github/.github/workflows/ci.yml@main + with: + craft_version: '4' + jobs: '["ecs", "phpstan"]' + secrets: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml new file mode 100644 index 0000000..b96abe3 --- /dev/null +++ b/.github/workflows/create-release.yml @@ -0,0 +1,24 @@ +# Creates a new GitHub Release whenever the Craft Plugin Store +# is notified of a new version tag. + +name: Create Release +run-name: Create release for ${{ github.event.client_payload.version }} + +on: + repository_dispatch: + types: + - craftcms/new-release + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: ncipollo/release-action@v1 + with: + body: ${{ github.event.client_payload.notes }} + makeLatest: ${{ github.event.client_payload.latest }} + name: ${{ github.event.client_payload.version }} + prerelease: ${{ github.event.client_payload.prerelease }} + tag: ${{ github.event.client_payload.tag }} From 381ac79b2a8c191840b2d5b402cd7e4bd65b8145 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 15:17:30 +0100 Subject: [PATCH 02/17] Fixed CI action path --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3f8137..7e93771 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ concurrency: jobs: ci: name: ci - uses: statikbe/.github/.github/workflows/ci.yml@main + uses: statikbe/.github/.github/workflows/craftcms/ci.yml@main with: craft_version: '4' jobs: '["ecs", "phpstan"]' From 5831240718224e21d49f2b3136b9955f53df3d68 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 15:22:37 +0100 Subject: [PATCH 03/17] Fixed CI path --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7e93771..e3f8137 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ concurrency: jobs: ci: name: ci - uses: statikbe/.github/.github/workflows/craftcms/ci.yml@main + uses: statikbe/.github/.github/workflows/ci.yml@main with: craft_version: '4' jobs: '["ecs", "phpstan"]' From 12bc768aff88fce43caad8b86b41e20e0cb9ed8d Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 15:38:25 +0100 Subject: [PATCH 04/17] Change pr action condition --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3f8137..fabc97b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,6 @@ on: branches: - develop pull_request: - - ready_for_review permissions: contents: read concurrency: From 6c15be79ce68812bc933ee4ddf681db25fd2a4e6 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 15:42:09 +0100 Subject: [PATCH 05/17] =?UTF-8?q?Only=20run=20action=20on=20open=20PR?= =?UTF-8?q?=E2=80=99s=20that=20change=20PHP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fabc97b..7ac9c3d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,10 @@ on: branches: - develop pull_request: + types: + - opened + paths: + - '**.php' permissions: contents: read concurrency: From b55393cbc47871f454330463befe20195a8e1058 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 16:00:44 +0100 Subject: [PATCH 06/17] =?UTF-8?q?Run=20CI=20against=20PR=E2=80=99s=20for?= =?UTF-8?q?=20develop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ac9c3d..b08b854 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,8 +5,7 @@ on: branches: - develop pull_request: - types: - - opened + branches: [ develop ] paths: - '**.php' permissions: From 6aa88563d2bf1979e889f174eeb609751cda2037 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 14 Feb 2024 16:29:55 +0100 Subject: [PATCH 07/17] More actions testing --- .github/workflows/ci.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b08b854..aad5ea0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,12 +2,11 @@ name: ci on: workflow_dispatch: push: - branches: - - develop + branches: [ develop ] + paths: + - '**.php' + push: pull_request: - branches: [ develop ] - paths: - - '**.php' permissions: contents: read concurrency: From 087ca6eb328bb4ccedfa711524c5aa2975c30582 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 15 Feb 2024 09:52:33 +0100 Subject: [PATCH 08/17] Remove path check --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aad5ea0..d963cf4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,8 +3,6 @@ on: workflow_dispatch: push: branches: [ develop ] - paths: - - '**.php' push: pull_request: permissions: From d2f0334843a8e2bcb77ae5bae85b80bec65c83b3 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 15 Feb 2024 12:32:52 +0100 Subject: [PATCH 09/17] Workflow? --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d963cf4..1c4357a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,8 +3,7 @@ on: workflow_dispatch: push: branches: [ develop ] - push: - pull_request: + pull_request: permissions: contents: read concurrency: From 8b477cb6729bdcf1627431ac958980614018983a Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 15 Feb 2024 12:38:04 +0100 Subject: [PATCH 10/17] ECS fixes --- src/CarbonTracker.php | 6 +++--- src/console/controllers/TestController.php | 2 -- src/jobs/CarbonStatsJob.php | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/CarbonTracker.php b/src/CarbonTracker.php index cc74cde..8e324ff 100644 --- a/src/CarbonTracker.php +++ b/src/CarbonTracker.php @@ -56,7 +56,7 @@ public function init(): void } // Defer most setup tasks until Craft is fully initialized - Craft::$app->onInit(function () { + Craft::$app->onInit(function() { $this->attachEventHandlers(); }); } @@ -77,7 +77,7 @@ protected function settingsHtml(): ?string private function attachEventHandlers(): void { Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, - function (ModelEvent $event) { + function(ModelEvent $event) { /** @var Entry $entry */ $entry = $event->sender; if (!ElementHelper::isDraftOrRevision($entry) && $entry->getUrl()) { @@ -92,7 +92,7 @@ function (ModelEvent $event) { Event::on( Entry::class, Entry::EVENT_DEFINE_SIDEBAR_HTML, - function (DefineHtmlEvent $event) { + function(DefineHtmlEvent $event) { /** @var Entry $entry */ $entry = $event->sender; try { diff --git a/src/console/controllers/TestController.php b/src/console/controllers/TestController.php index 2d427e6..e9f120f 100644 --- a/src/console/controllers/TestController.php +++ b/src/console/controllers/TestController.php @@ -14,6 +14,4 @@ public function actionIndex(int $id): void $data = CarbonTracker::getInstance()->api->getSite($entry); dd($data); } - - } diff --git a/src/jobs/CarbonStatsJob.php b/src/jobs/CarbonStatsJob.php index 8d1f5a6..0fb6155 100644 --- a/src/jobs/CarbonStatsJob.php +++ b/src/jobs/CarbonStatsJob.php @@ -29,8 +29,8 @@ protected function defaultDescription(): string */ public function execute($queue): void { - $entry = Entry::findOne(['id' => $this->entryId,'siteId' => $this->siteId]); - if($entry) { + $entry = Entry::findOne(['id' => $this->entryId, 'siteId' => $this->siteId]); + if ($entry) { CarbonTracker::getInstance()->stats->upsertDataForEntry($entry); } } From 5dd285f61cef44675bc996d17865e04bc2b7fad1 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 7 Mar 2024 13:14:41 +0100 Subject: [PATCH 11/17] Migration to add siteId col --- src/migrations/Install.php | 1 + .../m240307_121231_addSiteIdCol.php | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/migrations/m240307_121231_addSiteIdCol.php diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 7ea1111..65d55d3 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -15,6 +15,7 @@ public function safeUp(): bool [ 'id' => $this->primaryKey(), 'entryId' => $this->integer()->notNull(), + 'siteId' => $this->integer()->notNull(), 'url' => $this->string()->notNull(), 'green' => $this->boolean()->defaultValue(0), 'bytes' => $this->integer(), diff --git a/src/migrations/m240307_121231_addSiteIdCol.php b/src/migrations/m240307_121231_addSiteIdCol.php new file mode 100644 index 0000000..938c16b --- /dev/null +++ b/src/migrations/m240307_121231_addSiteIdCol.php @@ -0,0 +1,36 @@ +addColumn( + SiteStatisticsRecord::tableName(), + 'siteId', + $this->integer()->notNull()->after('entryId') + ); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown(): bool + { + echo "m240307_121231_addSiteIdCol cannot be reverted.\n"; + return false; + } +} From ec1d8d13983aaa23934f8c2ee44494beb7a5fd02 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Fri, 8 Mar 2024 14:26:48 +0100 Subject: [PATCH 12/17] Track siteId for each stats record --- src/CarbonTracker.php | 4 +- .../m240307_121231_addSiteIdCol.php | 1 - src/models/SiteStatisticsModel.php | 5 ++- src/records/SiteStatisticsRecord.php | 1 + src/services/ApiService.php | 43 +++++++++++-------- src/services/StatsService.php | 18 +++++--- 6 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/CarbonTracker.php b/src/CarbonTracker.php index 8e324ff..ec5b4ee 100644 --- a/src/CarbonTracker.php +++ b/src/CarbonTracker.php @@ -31,7 +31,7 @@ */ class CarbonTracker extends Plugin { - public string $schemaVersion = '1.0.0'; + public string $schemaVersion = '1.1.0'; public bool $hasCpSettings = true; /** @@ -80,7 +80,7 @@ private function attachEventHandlers(): void function(ModelEvent $event) { /** @var Entry $entry */ $entry = $event->sender; - if (!ElementHelper::isDraftOrRevision($entry) && $entry->getUrl()) { + if (!ElementHelper::isDraftOrRevision($entry) && $entry->getUrl() && !$entry->propagating) { Queue::push(new CarbonStatsJob([ 'entryId' => $entry->id, 'siteId' => $entry->siteId, diff --git a/src/migrations/m240307_121231_addSiteIdCol.php b/src/migrations/m240307_121231_addSiteIdCol.php index 938c16b..fca1849 100644 --- a/src/migrations/m240307_121231_addSiteIdCol.php +++ b/src/migrations/m240307_121231_addSiteIdCol.php @@ -2,7 +2,6 @@ namespace statikbe\carbontracker\migrations; -use Craft; use craft\db\Migration; use statikbe\carbontracker\records\SiteStatisticsRecord; diff --git a/src/models/SiteStatisticsModel.php b/src/models/SiteStatisticsModel.php index 02b77ef..1c550c4 100644 --- a/src/models/SiteStatisticsModel.php +++ b/src/models/SiteStatisticsModel.php @@ -10,6 +10,7 @@ class SiteStatisticsModel extends Model { public int $entryId; + public int $siteId; public string $url = "https://"; public bool $green = false; public int $bytes = 0; @@ -23,8 +24,8 @@ class SiteStatisticsModel extends Model public function rules(): array { return [ - [['entryId', 'url', 'green', 'bytes', 'cleanerThan', 'rating', 'dateUpdated'], 'required'], - [['entryId', 'url', 'green', 'bytes', 'cleanerThan', 'rating', 'dateUpdated'], 'safe'], + [['entryId', 'siteId', 'url', 'green', 'bytes', 'cleanerThan', 'rating', 'dateUpdated'], 'required'], + [['entryId', 'siteId', 'url', 'green', 'bytes', 'cleanerThan', 'rating', 'dateUpdated'], 'safe'], ]; } } diff --git a/src/records/SiteStatisticsRecord.php b/src/records/SiteStatisticsRecord.php index aedecd5..bc959b6 100644 --- a/src/records/SiteStatisticsRecord.php +++ b/src/records/SiteStatisticsRecord.php @@ -6,6 +6,7 @@ /** * @property int $entryId entryId + * @property int $siteId siteId * @property string $url url * @property boolean $green green * @property float $cleanerThan cleanerThan diff --git a/src/services/ApiService.php b/src/services/ApiService.php index dd8a817..2690097 100644 --- a/src/services/ApiService.php +++ b/src/services/ApiService.php @@ -5,6 +5,7 @@ use craft\base\Component; use craft\elements\Entry; use GuzzleHttp\Client; +use statikbe\carbontracker\CarbonTracker; use statikbe\carbontracker\models\SiteStatisticsModel; class ApiService extends Component @@ -22,7 +23,7 @@ public function init(): void parent::init(); } - public function getSite(Entry $entry): SiteStatisticsModel + public function getSite(Entry $entry): SiteStatisticsModel|bool { $model = new SiteStatisticsModel(); @@ -32,26 +33,32 @@ public function getSite(Entry $entry): SiteStatisticsModel $url = $entry->getUrl(); } - $data = $this->makeRequest('/site', [ - 'query' => [ - 'url' => $url, - ], - ]); + try { + $data = $this->makeRequest('/site', [ + 'query' => [ + 'url' => $url, + ], + ]); - if (empty($data)) { - return $model; - } + if (empty($data)) { + return $model; + } - $model->setAttributes([ - 'entryId' => $entry->id, - 'url' => $data['url'], - 'green' => $data['green'], - 'bytes' => $data['bytes'], - 'cleanerThan' => $data['cleanerThan'], - 'rating' => $data['rating'], - ]); + $model->setAttributes([ + 'entryId' => $entry->id, + 'siteId' => $entry->siteId, + 'url' => $data['url'], + 'green' => $data['green'], + 'bytes' => $data['bytes'], + 'cleanerThan' => $data['cleanerThan'], + 'rating' => $data['rating'], + ]); - return $model; + return $model; + } catch (\Exception $e) { + \Craft::error($e->getMessage(), CarbonTracker::class); + return false; + } } /** diff --git a/src/services/StatsService.php b/src/services/StatsService.php index 6978333..c961639 100644 --- a/src/services/StatsService.php +++ b/src/services/StatsService.php @@ -31,6 +31,7 @@ public function upsertDataForEntry(Entry $entry): void $date = DateTimeHelper::currentUTCDateTime()->modify('-1 day'); $record = SiteStatisticsRecord::find() ->where(['=', 'entryId', $entry->id]) + ->andWhere(['=', 'siteId', $entry->siteId]) ->andWhere(['>=', 'dateCreated', $date->format('c')]) ->one(); @@ -41,12 +42,15 @@ public function upsertDataForEntry(Entry $entry): void } $stats = CarbonTracker::getInstance()->api->getSite($entry); - $record = new SiteStatisticsRecord(); - $record->entryId = $stats->entryId; - $record->url = $stats->url; - $record->green = $stats->green; - $record->cleanerThan = $stats->cleanerThan; - $record->rating = $stats->rating; - $record->save(); + if ($stats) { + $record = new SiteStatisticsRecord(); + $record->entryId = $stats->entryId; + $record->siteId = $stats->siteId; + $record->url = $stats->url; + $record->green = $stats->green; + $record->cleanerThan = $stats->cleanerThan; + $record->rating = $stats->rating; + $record->save(); + } } } From e50faf50757bf892ee7384efbae14259e5559253 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Fri, 8 Mar 2024 14:37:06 +0100 Subject: [PATCH 13/17] Added ci shortcut --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e5c5a0a..9d8473e 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "scripts": { "check-cs": "ecs check --ansi", "fix-cs": "ecs check --ansi --fix", - "phpstan": "phpstan --memory-limit=1G" + "phpstan": "phpstan --memory-limit=1G", + "ci": "ecs check --ansi --fix && phpstan --memory-limit=1G" }, "config": { "sort-packages": true, From 2a614604b3218f95a06fd77ee48e48468d8c0b7f Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Fri, 8 Mar 2024 14:40:19 +0100 Subject: [PATCH 14/17] Changelog for 4.1.0 --- CHANGELOG.md | 7 +++++-- composer.json | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5fe7eb..c6ff086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for carbon-tracker +## 4.1.0 - 2024-03-08 +### Added +- Added multisite support ([#3](https://github.com/statikbe/craft-carbon-tracker/issues/3)) + ## 4.0.3 - 2024-01-12 ### Fixed - Fix typo in sidebar template @@ -8,10 +12,9 @@ ### Fixed - Only run our job for enabled entries, because we need to be able to crawl them. - ## 4.0.1 - 2024-01-04 ### Fixed - Fixed issue with failing jobs on multi-site installs ## 4.0.0 - 2024-01-02 -- Initial release 🚀 +- Initial release 🚀 \ No newline at end of file diff --git a/composer.json b/composer.json index 9d8473e..4dbfaef 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "statikbe/craft-carbon-tracker", "description": "Carbon tracker aims to raise awareness of the carbon emissions created by webpages, by displaying these insights along side the content in Craft's control panel.", "type": "craft-plugin", - "version": "4.0.3", + "version": "4.1.0", "license": "mit", "support": { "email": "support@statik.be", From 222428036a62f2823c463fa41e9bcc956bf30467 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Mon, 11 Mar 2024 15:57:36 +0100 Subject: [PATCH 15/17] Better fallback data --- src/services/ApiService.php | 71 +++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/services/ApiService.php b/src/services/ApiService.php index 2690097..bc17324 100644 --- a/src/services/ApiService.php +++ b/src/services/ApiService.php @@ -28,37 +28,62 @@ public function getSite(Entry $entry): SiteStatisticsModel|bool $model = new SiteStatisticsModel(); if (getenv('CRAFT_ENVIRONMENT') === 'dev') { - $url = self::READ_MORE_LINK; + $json_data = '{ + "url": "https://www.wholegraindigital.com/", + "green": true, + "rating": "A+", + "bytes": 443854, + "cleanerThan": 0.83, + "statistics": { + "adjustedBytes": 335109.77, + "energy": 0.0005633320052642376, + "co2": { + "grid": { + "grams": 0.26758270250051286, + "litres": 0.14882949913078525 + }, + "renewable": { + "grams": 0.24250694721722435, + "litres": 0.13488236404222018 + } + } + } + }'; + + $data = json_decode($json_data, true); } else { - $url = $entry->getUrl(); - } - try { - $data = $this->makeRequest('/site', [ - 'query' => [ - 'url' => $url, - ], - ]); - if (empty($data)) { - return $model; + try { + + $url = $entry->getUrl(); + $data = $this->makeRequest('/site', [ + 'query' => [ + 'url' => $url, + ], + ]); + } catch (\Exception $e) { + \Craft::error($e->getMessage(), CarbonTracker::class); + return false; } + } - $model->setAttributes([ - 'entryId' => $entry->id, - 'siteId' => $entry->siteId, - 'url' => $data['url'], - 'green' => $data['green'], - 'bytes' => $data['bytes'], - 'cleanerThan' => $data['cleanerThan'], - 'rating' => $data['rating'], - ]); + if (empty($data)) { return $model; - } catch (\Exception $e) { - \Craft::error($e->getMessage(), CarbonTracker::class); - return false; } + + $model->setAttributes([ + 'entryId' => $entry->id, + 'siteId' => $entry->siteId, + 'url' => $data['url'], + 'green' => $data['green'], + 'bytes' => $data['bytes'], + 'cleanerThan' => $data['cleanerThan'], + 'rating' => $data['rating'], + ]); + + return $model; } /** From 84b0800ea60451e37ca1afaac0fb0bd18e6f4d60 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Mon, 11 Mar 2024 16:02:48 +0100 Subject: [PATCH 16/17] Search for stats witouth a siteId as fallback --- src/services/StatsService.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/services/StatsService.php b/src/services/StatsService.php index c961639..db3081a 100644 --- a/src/services/StatsService.php +++ b/src/services/StatsService.php @@ -13,11 +13,14 @@ class StatsService extends Component { public function getDataForEntry(Entry $entry): SiteStatisticsModel|bool { - $record = SiteStatisticsRecord::findOne(['entryId' => $entry->id]); - + $record = SiteStatisticsRecord::findOne(['entryId' => $entry->id, 'siteId' => $entry->siteId]); + if (!$record) { - // What if we don't have any data yet? - return false; + // Search again without the siteId + $record = SiteStatisticsRecord::findOne(['entryId' => $entry->id]); + if(!$record) { + return false; + } } $model = new SiteStatisticsModel(); From 87aa47972b91b694d9bcb92137c031012c6d58c5 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Mon, 11 Mar 2024 16:03:44 +0100 Subject: [PATCH 17/17] ECS fixes --- src/services/ApiService.php | 3 --- src/services/StatsService.php | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/services/ApiService.php b/src/services/ApiService.php index bc17324..c66f395 100644 --- a/src/services/ApiService.php +++ b/src/services/ApiService.php @@ -52,10 +52,7 @@ public function getSite(Entry $entry): SiteStatisticsModel|bool $data = json_decode($json_data, true); } else { - - try { - $url = $entry->getUrl(); $data = $this->makeRequest('/site', [ 'query' => [ diff --git a/src/services/StatsService.php b/src/services/StatsService.php index db3081a..88add60 100644 --- a/src/services/StatsService.php +++ b/src/services/StatsService.php @@ -18,7 +18,7 @@ public function getDataForEntry(Entry $entry): SiteStatisticsModel|bool if (!$record) { // Search again without the siteId $record = SiteStatisticsRecord::findOne(['entryId' => $entry->id]); - if(!$record) { + if (!$record) { return false; } }