Verified Commit beca0ad0 authored by Korko's avatar Korko
Browse files

Rework metrics

parent 21989493
Pipeline #381823 failed with stages
in 1 minute and 16 seconds
......@@ -9,7 +9,6 @@ use App\Models\DearSanta;
use App\Models\Mail as MailModel;
use App\Models\Participant;
use App\Traits\UpdatesMailDelivery;
use Metrics;
class DearSantaController extends Controller
{
......@@ -57,6 +56,8 @@ class DearSantaController extends Controller
$dearSanta->mail()->associate($mail);
$dearSanta->save();
$participant->createMetric('dearsanta');
$this->sendMail($dearSanta);
$message = trans('message.sent');
......@@ -72,8 +73,6 @@ class DearSantaController extends Controller
protected function sendMail(DearSanta $dearSanta)
{
Metrics::increment('dearsanta');
SendMail::dispatch($dearSanta->sender->santa, new DearSantaEmail($dearSanta));
}
}
......@@ -12,7 +12,6 @@ use App\Models\Participant;
use Csv;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Metrics;
class OrganizerController extends Controller
{
......@@ -58,10 +57,14 @@ class OrganizerController extends Controller
{
if ($participant->email === $request->input('email')) {
$message = trans('message.sent');
$participant->createMetric('resend_email');
} else {
$participant->email = $request->input('email');
$participant->save();
$participant->createMetric('change_email');
$message = trans('organizer.up_and_sent');
}
......@@ -76,13 +79,13 @@ class OrganizerController extends Controller
protected function sendEmail(Participant $participant)
{
Metrics::increment('email');
SendMail::dispatch($participant, new TargetDrawn($participant));
}
public function csvInit(Draw $draw)
{
$draw->createMetric('csv_initial_download');
return response(
"\xEF\xBB\xBF".// UTF-8 BOM
$draw->participants
......@@ -99,6 +102,8 @@ class OrganizerController extends Controller
abort_unless($draw->expired, 403, 'Cet évènement n\'est pas encore terminé');
abort_unless($draw->next_solvable, 404, 'Cet évènement ne permet pas cette génération');
$draw->createMetric('csv_final_download');
return response(
"\xEF\xBB\xBF".// UTF-8 BOM
$draw->participants
......@@ -115,6 +120,8 @@ class OrganizerController extends Controller
{
$draw->delete();
$draw->createMetric('delete');
return response()->json([
'message' => trans('organizer.deleted'),
]);
......
......@@ -6,8 +6,10 @@ use App\Casts\EncryptedString;
use DateInterval;
use DateTime;
use DB;
use exussum12\xxhash\Ffi\V32 as xxHash;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Metrics;
class Draw extends Model
{
......@@ -69,4 +71,17 @@ class Draw extends Model
{
return $this->expires_at->addWeeks(self::WEEKS_BEFORE_DELETION);
}
public function getMetricIdAttribute()
{
return (new xxHash($this->id))->hash($this->created_at);
}
public function createMetric($name)
{
return Metrics::create($name)
->setTags([
'draw' => $this->metricId
]);
}
}
......@@ -4,9 +4,11 @@ namespace App\Models;
use App\Casts\EncryptedString;
use App\Collections\ParticipantsCollection;
use exussum12\xxhash\Ffi\V32 as xxHash;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Metrics;
class Participant extends Model
{
......@@ -108,4 +110,19 @@ class Participant extends Model
{
return new ParticipantsCollection($models);
}
public function getMetricIdAttribute()
{
return (new xxHash($this->draw->id))->hash($this->id);
}
public function createMetric($name, $value = 1)
{
return Metrics::create($name, $value)
->setTags([
'draw' => $this->draw->metricId,
'participant' => $this->metricId,
'is_organizer' => $this->is($this->draw->organizer)
]);
}
}
......@@ -10,7 +10,6 @@ use App\Mail\OrganizerRecap;
use App\Mail\TargetDrawn;
use App\Models\Participant;
use Arr;
use Metrics;
use Solver;
class DrawHandler
......@@ -37,10 +36,10 @@ class DrawHandler
public function sendMail($title, $body): void
{
$draw = $this->createDraw($title, $body, $this->expirationDate ?: date('Y-m-d', strtotime('+2 days')));
Metrics::increment('draws');
$this->createParticipants($draw, $this->participants, $this->hat);
Metrics::increment('participants', count($this->participants));
$draw->createMetric('new_draw')
->addExtra('participants', count($this->participants));
if (! $this->isNextDrawSolvable($draw)) {
$draw->next_solvable = false;
......@@ -117,8 +116,6 @@ class DrawHandler
public function sendParticipantEmail(Participant $participant)
{
Metrics::increment('email');
SendMail::dispatch($participant, new TargetDrawn($participant));
}
}
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b89fb84864d4590c3be41c8dbad86573",
"content-hash": "c96ad7f127aaaf91777cb8fe3de77b19",
"packages": [
{
"name": "anhskohbo/no-captcha",
......@@ -676,6 +676,50 @@
},
"time": "2017-07-23T21:35:13+00:00"
},
{
"name": "exussum12/xxhash",
"version": "1.3",
"source": {
"type": "git",
"url": "https://github.com/exussum12/xxhash.git",
"reference": "f5567ec5739ffee27aa3469e5001b4759b1b9e0d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/exussum12/xxhash/zipball/f5567ec5739ffee27aa3469e5001b4759b1b9e0d",
"reference": "f5567ec5739ffee27aa3469e5001b4759b1b9e0d",
"shasum": ""
},
"require": {
"ext-bcmath": "*",
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "^7.1|^8.0|^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"exussum12\\xxhash\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-2-Clause"
],
"authors": [
{
"name": "Scott Dutton",
"email": "scott@exussum.co.uk"
}
],
"description": "xxHash implementation in Pure PHP",
"support": {
"issues": "https://github.com/exussum12/xxhash/issues",
"source": "https://github.com/exussum12/xxhash/tree/1.3"
},
"time": "2020-11-30T09:04:53+00:00"
},
{
"name": "facade/ignition-contracts",
"version": "1.0.2",
......@@ -1273,6 +1317,68 @@
},
"time": "2020-11-26T19:24:33+00:00"
},
{
"name": "influxdb/influxdb-php",
"version": "1.15.2",
"source": {
"type": "git",
"url": "https://github.com/influxdata/influxdb-php.git",
"reference": "d6e59f4f04ab9107574fda69c2cbe36671253d03"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/influxdata/influxdb-php/zipball/d6e59f4f04ab9107574fda69c2cbe36671253d03",
"reference": "d6e59f4f04ab9107574fda69c2cbe36671253d03",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "^6.0|^7.0",
"php": "^5.5 || ^7.0 || ^8.0"
},
"require-dev": {
"dms/phpunit-arraysubset-asserts": "^0.2.1",
"phpunit/phpunit": "^9.5"
},
"suggest": {
"ext-curl": "Curl extension, needed for Curl driver",
"stefanotorresi/influxdb-php-async": "An asyncronous client for InfluxDB, implemented via ReactPHP."
},
"type": "library",
"autoload": {
"psr-4": {
"InfluxDB\\": "src/InfluxDB"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Stephen Hoogendijk",
"email": "stephen@tca0.nl"
},
{
"name": "Daniel Martinez",
"email": "danimartcas@hotmail.com"
},
{
"name": "Gianluca Arbezzano",
"email": "gianarb92@gmail.com"
}
],
"description": "InfluxDB client library for PHP",
"keywords": [
"client",
"influxdata",
"influxdb",
"influxdb class",
"influxdb client",
"influxdb library",
"time series"
],
"time": "2020-12-26T17:45:17+00:00"
},
{
"name": "korko/validator",
"version": "dev-master",
......@@ -1850,60 +1956,6 @@
],
"time": "2021-01-18T20:58:21+00:00"
},
{
"name": "league/statsd",
"version": "1.5.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/statsd.git",
"reference": "c6290ef6c7528b7b739b26ce6aedf81ee6a4a2ac"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/statsd/zipball/c6290ef6c7528b7b739b26ce6aedf81ee6a4a2ac",
"reference": "c6290ef6c7528b7b739b26ce6aedf81ee6a4a2ac",
"shasum": ""
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.5"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"League\\StatsD\\Laravel5\\Provider\\StatsdServiceProvider"
],
"aliases": {
"Statsd": "League\\StatsD\\Laravel5\\Facade\\StatsdFacade"
}
}
},
"autoload": {
"psr-4": {
"League\\StatsD\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Marc Qualie",
"email": "marc@marcqualie.com",
"homepage": "http://marcqualie.com",
"role": "Developer"
}
],
"description": "A simple library for working with StatsD in PHP.",
"homepage": "https://github.com/thephpleague/statsd",
"keywords": [
"graphite",
"library",
"statsd"
],
"time": "2018-10-09T16:02:46+00:00"
},
{
"name": "monolog/monolog",
"version": "2.2.0",
......@@ -3883,6 +3935,63 @@
],
"time": "2020-12-04T08:36:05+00:00"
},
{
"name": "stechstudio/laravel-metrics",
"version": "1.3.1",
"source": {
"type": "git",
"url": "https://github.com/stechstudio/laravel-metrics.git",
"reference": "27f941dd25514d7d1811efa895e4392479ab660e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/stechstudio/laravel-metrics/zipball/27f941dd25514d7d1811efa895e4392479ab660e",
"reference": "27f941dd25514d7d1811efa895e4392479ab660e",
"shasum": ""
},
"require": {
"illuminate/support": "^5.6|^6.0|^7.0|^8.0",
"influxdb/influxdb-php": "^1.14"
},
"require-dev": {
"aws/aws-sdk-php": "^3.133",
"mockery/mockery": "^1.0",
"orchestra/testbench": "~5.0",
"phpunit/phpunit": "^9.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"STS\\Metrics\\MetricsServiceProvider"
],
"aliases": {
"Metrics": "STS\\Metrics\\MetricsFacade"
}
}
},
"autoload": {
"psr-4": {
"STS\\Metrics\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Joseph Szobody",
"email": "joseph@stechstudio.com"
},
{
"name": "Rob 'Bubba' Hines",
"email": "bubba@stechstudio.com"
}
],
"description": "Easily track metrics from Laravel events, or on your own",
"time": "2020-10-05T14:19:12+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.2.5",
......@@ -9915,5 +10024,5 @@
"php": "^7.3|^8.0"
},
"platform-dev": [],
"plugin-api-version": "1.1.0"
"plugin-api-version": "2.0.0"
}
......@@ -181,7 +181,6 @@ return [
* Package Service Providers...
*/
Korko\Validator\ValidatorServiceProvider::class,
League\StatsD\Laravel5\Provider\StatsdServiceProvider::class,
/*
* Application Service Providers...
......@@ -235,7 +234,6 @@ return [
'Lang' => Illuminate\Support\Facades\Lang::class,
'Log' => Illuminate\Support\Facades\Log::class,
'Mail' => Illuminate\Support\Facades\Mail::class,
'Metrics' => Facades\League\StatsD\Client::class,
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
'Notification' => Illuminate\Support\Facades\Notification::class,
'Password' => Illuminate\Support\Facades\Password::class,
......
......@@ -27,6 +27,7 @@
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="METRICS_BACKEND" value="null"/>
<server name="APP_CONFIG_CACHE" value="bootstrap/cache/config.phpunit.php"/>
<server name="APP_SERVICES_CACHE" value="bootstrap/cache/services.phpunit.php"/>
<server name="APP_PACKAGES_CACHE" value="bootstrap/cache/packages.phpunit.php"/>
......
......@@ -11,7 +11,6 @@ use App\Models\Exclusion;
use App\Models\Participant;
use DrawHandler;
use Exception;
use Metrics;
use Mail;
use Tests\TestCase;
......@@ -25,9 +24,6 @@ class DrawHandlerTest extends TestCase
Mail::fake();
Mail::assertNothingSent();
Metrics::shouldReceive('increment')
->never();
$this->assertEquals(0, Draw::count());
$this->assertEquals(0, Participant::count());
......@@ -49,21 +45,6 @@ class DrawHandlerTest extends TestCase
public function testClassic(): void
{
Metrics::shouldReceive('increment')
->once()
->with('draws')
->andReturn(true);
Metrics::shouldReceive('increment')
->once()
->with('participants', 3)
->andReturn(true);
Metrics::shouldReceive('increment')
->times(3)
->with('email')
->andReturn(true);
Mail::fake();
$this->assertEquals(0, Draw::count());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment