Skip to content

Commit 2bb3b15

Browse files
committed
2 parents 57d0901 + 0f1c092 commit 2bb3b15

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

src/Jobs/RecordMetric.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function handle(): void
3939
);
4040

4141
/** @var \Illuminate\Database\Eloquent\Model $model */
42-
$model = new DatabaseMetricManager::$model;
42+
$model = transform($metric->model() ?? DatabaseMetricManager::$model, fn (string $model) => new $model);
4343

4444
$model->getConnection()->transaction(function () use ($metric, $value, $model) {
4545
$instance = $model->newQuery()->firstOrCreate([

src/JsonMeasurableEncoder.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class JsonMeasurableEncoder implements MeasurableEncoder
1212
*/
1313
public function encode(Measurable $metric): string
1414
{
15-
$model = $metric->measurable();
15+
$measurable = $metric->measurable();
1616

1717
return json_encode([
1818
'name' => Enum::value($metric->name()),
@@ -21,9 +21,10 @@ public function encode(Measurable $metric): string
2121
'month' => $metric->month(),
2222
'day' => $metric->day(),
2323
'hour' => $metric->hour(),
24-
'measurable' => $model ? get_class($model) : null,
25-
'measurable_key' => $model?->getKeyName() ?? null,
26-
'measurable_id' => $model?->getKey() ?? null,
24+
'model' => $metric->model(),
25+
'measurable' => $measurable ? get_class($measurable) : null,
26+
'measurable_key' => $measurable?->getKeyName() ?? null,
27+
'measurable_id' => $measurable?->getKey() ?? null,
2728
'additional' => $metric->additional(),
2829
]);
2930
}
@@ -36,12 +37,12 @@ public function decode(string $key, int $value): Measurable
3637
$attributes = json_decode($key, true);
3738

3839
if ($attributes['measurable'] && class_exists($attributes['measurable'])) {
39-
/** @var \Illuminate\Database\Eloquent\Model $model */
40-
$model = (new $attributes['measurable'])->newFromBuilder([
40+
/** @var \Illuminate\Database\Eloquent\Model $measurable */
41+
$measurable = (new $attributes['measurable'])->newFromBuilder([
4142
$attributes['measurable_key'] => $attributes['measurable_id'],
4243
]);
4344
} else {
44-
$model = null;
45+
$measurable = null;
4546
}
4647

4748
$date = CarbonImmutable::create(
@@ -56,9 +57,10 @@ public function decode(string $key, int $value): Measurable
5657
category: $attributes['category'],
5758
value: $value,
5859
date: $date,
59-
measurable: $model,
60+
measurable: $measurable,
6061
additional: $attributes['additional'] ?? [],
6162
hourly: $attributes['hour'] ?? false,
63+
model: $attributes['model'] ?? null,
6264
);
6365
}
6466
}

src/Measurable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function day(): int;
4242
*/
4343
public function hour(): ?int;
4444

45+
/**
46+
* Get the model class of the metric.
47+
*/
48+
public function model(): ?string;
49+
4550
/**
4651
* Get the measurable model of the metric.
4752
*/

src/MetricData.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
protected ?Model $measurable = null,
2424
protected array $additional = [],
2525
protected bool $hourly = false,
26+
protected ?string $model = null,
2627
) {
2728
$this->date ??= new CarbonImmutable;
2829
}
@@ -83,6 +84,14 @@ public function hour(): ?int
8384
return $this->hourly ? $this->date->hour : null;
8485
}
8586

87+
/**
88+
* {@inheritDoc}
89+
*/
90+
public function model(): ?string
91+
{
92+
return $this->model;
93+
}
94+
8695
/**
8796
* {@inheritDoc}
8897
*/

src/PendingMetric.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class PendingMetric
2828
*/
2929
protected ?Model $measurable = null;
3030

31+
/**
32+
* The model to use for the metric.
33+
*/
34+
protected ?string $model = null;
35+
3136
/**
3237
* Whether to track hourly metrics.
3338
*/
@@ -86,6 +91,16 @@ public function measurable(Model $measurable): self
8691
return $this;
8792
}
8893

94+
/**
95+
* Set the metric model to use.
96+
*/
97+
public function model(string $model): self
98+
{
99+
$this->model = $model;
100+
101+
return $this;
102+
}
103+
89104
/**
90105
* Enable hourly tracking for the metric.
91106
*/
@@ -130,7 +145,8 @@ public function toMetricData(int $value): Measurable
130145
$this->date,
131146
$this->measurable,
132147
$this->additional,
133-
$this->trackHourly
148+
$this->trackHourly,
149+
$this->model,
134150
);
135151
}
136152
}

tests/PendingMetricTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
->and($data->day())->toBe($date->day);
4343
});
4444

45+
it('can set model', function () {
46+
$pending = PendingMetric::make('page_views')->model('CustomMetric');
47+
48+
$data = $pending->toMetricData(1);
49+
50+
expect($data->model())->toBe('CustomMetric');
51+
});
52+
4553
it('can set measurable', function () {
4654
$user = createUser();
4755

0 commit comments

Comments
 (0)