Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ArrayMetricRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function add(Measurable $metric): void
),
$metric->measurable(),
$metric->additional(),
$existing->hour() !== null
$existing->hour() !== null,
$metric->databaseModel(),
);
} else {
$this->metrics[$key] = $metric;
Expand Down
3 changes: 2 additions & 1 deletion src/Jobs/RecordMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public function handle(): void
fn (Measurable $metric) => $metric->value()
);

$modelClass = $metric->databaseModel();
/** @var \Illuminate\Database\Eloquent\Model $model */
$model = new DatabaseMetricManager::$model;
$model = new $modelClass;

$model->getConnection()->transaction(function () use ($metric, $value, $model) {
$instance = $model->newQuery()->firstOrCreate([
Expand Down
1 change: 1 addition & 0 deletions src/JsonMeasurableEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function decode(string $key, int $value): Measurable
measurable: $model,
additional: $attributes['additional'] ?? [],
hourly: $attributes['hour'] ?? false,
databaseModel: $attributes['databaseModel'] ?? DatabaseMetricManager::$model,
);
}
}
7 changes: 7 additions & 0 deletions src/Measurable.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ public function measurable(): ?Model;
* Get the additional attributes of the metric.
*/
public function additional(): array;

/**
* Get the database model of the metric.
*
* @return class-string<\Illuminate\Database\Eloquent\Model>
*/
public function databaseModel(): string;
}
10 changes: 10 additions & 0 deletions src/MetricData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function __construct(
protected ?Model $measurable = null,
protected array $additional = [],
protected bool $hourly = false,
/** @var @var class-string<\Illuminate\Database\Eloquent\Model> */
protected string $databaseModel = DatabaseMetricManager::$model,
) {
$this->date ??= new CarbonImmutable;
}
Expand Down Expand Up @@ -98,4 +100,12 @@ public function additional(): array
{
return $this->additional;
}

/**
* {@inheritDoc}
*/
public function databaseModel(): string
{
return $this->databaseModel;
}
}
21 changes: 20 additions & 1 deletion src/PendingMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class PendingMetric
*/
protected bool $trackHourly = false;

/**
* The database model of the metric.
* @var class-string<\Illuminate\Database\Eloquent\Model>
*/
protected string $databaseModel = DatabaseMetricManager::$model;

/**
* Additional attributes to store with the metric.
*
Expand Down Expand Up @@ -96,6 +102,18 @@ public function hourly(): self
return $this;
}

/**
* Set the measurable model of the metric.
*
* @param class-string<\Illuminate\Database\Eloquent\Model> $model
*/
public function databaseModel(string $databaseModel): self
{
$this->databaseModel = $databaseModel;

return $this;
}

/**
* Set additional attributes to store with the metric.
*
Expand Down Expand Up @@ -130,7 +148,8 @@ public function toMetricData(int $value): Measurable
$this->date,
$this->measurable,
$this->additional,
$this->trackHourly
$this->trackHourly,
$this->databaseModel,
);
}
}