diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1c4357a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,20 @@ +name: ci +on: + workflow_dispatch: + push: + branches: [ develop ] + pull_request: +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 }} 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 e5c5a0a..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", @@ -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, diff --git a/src/CarbonTracker.php b/src/CarbonTracker.php index cc74cde..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; /** @@ -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,10 +77,10 @@ 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()) { + if (!ElementHelper::isDraftOrRevision($entry) && $entry->getUrl() && !$entry->propagating) { Queue::push(new CarbonStatsJob([ 'entryId' => $entry->id, 'siteId' => $entry->siteId, @@ -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); } } 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..fca1849 --- /dev/null +++ b/src/migrations/m240307_121231_addSiteIdCol.php @@ -0,0 +1,35 @@ +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; + } +} 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..c66f395 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,21 +23,48 @@ public function init(): void parent::init(); } - public function getSite(Entry $entry): SiteStatisticsModel + 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 { + $url = $entry->getUrl(); + $data = $this->makeRequest('/site', [ + 'query' => [ + 'url' => $url, + ], + ]); + } catch (\Exception $e) { + \Craft::error($e->getMessage(), CarbonTracker::class); + return false; + } } - $data = $this->makeRequest('/site', [ - 'query' => [ - 'url' => $url, - ], - ]); if (empty($data)) { return $model; @@ -44,6 +72,7 @@ public function getSite(Entry $entry): SiteStatisticsModel $model->setAttributes([ 'entryId' => $entry->id, + 'siteId' => $entry->siteId, 'url' => $data['url'], 'green' => $data['green'], 'bytes' => $data['bytes'], diff --git a/src/services/StatsService.php b/src/services/StatsService.php index 6978333..88add60 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(); @@ -31,6 +34,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 +45,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(); + } } }