Skip to content

Commit

Permalink
Track siteId for each stats record
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Mar 8, 2024
1 parent 5dd285f commit ec1d8d1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/CarbonTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
class CarbonTracker extends Plugin
{
public string $schemaVersion = '1.0.0';
public string $schemaVersion = '1.1.0';
public bool $hasCpSettings = true;

/**
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/migrations/m240307_121231_addSiteIdCol.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace statikbe\carbontracker\migrations;

use Craft;
use craft\db\Migration;
use statikbe\carbontracker\records\SiteStatisticsRecord;

Expand Down
5 changes: 3 additions & 2 deletions src/models/SiteStatisticsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'],
];
}
}
1 change: 1 addition & 0 deletions src/records/SiteStatisticsRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @property int $entryId entryId
* @property int $siteId siteId
* @property string $url url
* @property boolean $green green
* @property float $cleanerThan cleanerThan
Expand Down
43 changes: 25 additions & 18 deletions src/services/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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;
}
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/services/StatsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
}
}
}

0 comments on commit ec1d8d1

Please sign in to comment.