Skip to content

Commit fc0f8fc

Browse files
committed
QueryCountMiddleware now counts 4 different types
* initiated * successful * errored * slow (configurable)
1 parent d295435 commit fc0f8fc

File tree

7 files changed

+338
-40
lines changed

7 files changed

+338
-40
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
"react/dns": "^0.4.17 || ^1.0.0",
2222
"roave/better-reflection": "^3.5",
2323
"voryx/pgasync": "^2.0",
24+
"wyrihaximus/constants": "^1.1",
2425
"wyrihaximus/doctrine-annotation-autoloader": "^1.0",
2526
"wyrihaximus/iterator-or-array-to-array": "^1.1"
2627
},
2728
"require-dev": {
2829
"robmorgan/phinx": "^0.10.7 || ^0.11.0",
30+
"thecodingmachine/safe": "^0.1.16",
2931
"vlucas/phpdotenv": "^3.3",
3032
"wyrihaximus/async-test-utilities": "^1.1"
3133
},

composer.lock

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infection.json.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
"WyriHaximus\\React\\SimpleORM\\Repository::translateFieldName"
3434
]
3535
},
36+
"Increment": {
37+
"ignore": [
38+
"WyriHaximus\\React\\SimpleORM\\Middleware\\QueryCountMiddleware::query"
39+
]
40+
},
3641
"OneZeroInteger": {
3742
"ignore": [
3843
"WyriHaximus\\React\\SimpleORM\\Repository::count",

src/Middleware/QueryCountMiddleware.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,74 @@
55
use PgAsync\Client as PgClient;
66
use Plasma\SQL\QueryBuilder;
77
use React\Promise\PromiseInterface;
8+
use Rx\Observable;
9+
use Throwable;
810
use WyriHaximus\React\SimpleORM\MiddlewareInterface;
11+
use function React\Promise\reject;
912
use function React\Promise\resolve;
1013

1114
final class QueryCountMiddleware implements MiddlewareInterface
1215
{
1316
private const ZERO = 0;
1417

1518
/** @var int */
16-
private $count = self::ZERO;
19+
private $initiatedCount = self::ZERO;
20+
21+
/** @var int */
22+
private $successfulCount = self::ZERO;
23+
24+
/** @var int */
25+
private $erroredCount = self::ZERO;
26+
27+
/** @var int */
28+
private $slowCount = self::ZERO;
29+
30+
/** @var int */
31+
private $slowQueryTime;
32+
33+
public function __construct(int $slowQueryTime)
34+
{
35+
$this->slowQueryTime = $slowQueryTime;
36+
}
1737

1838
public function query(QueryBuilder $query, callable $next): PromiseInterface
1939
{
20-
$this->count++;
40+
$this->initiatedCount++;
41+
42+
$startTime = hrtime()[0];
43+
44+
return resolve($next($query))->then(function (Observable $observable) use ($startTime): PromiseInterface {
45+
$this->successfulCount++;
46+
47+
if (hrtime()[0] - $startTime > $this->slowQueryTime) {
48+
$this->slowCount++;
49+
}
50+
51+
return resolve($observable);
52+
}, function (Throwable $throwable) use ($startTime): PromiseInterface {
53+
$this->erroredCount++;
54+
55+
if (hrtime()[0] - $startTime > $this->slowQueryTime) {
56+
$this->slowCount++;
57+
}
2158

22-
return resolve($next($query))->then();
59+
return reject($throwable);
60+
});
2361
}
2462

25-
public function getCount(): int
63+
public function getCounters(): iterable
2664
{
27-
return $this->count;
65+
yield 'initiated' => $this->initiatedCount;
66+
yield 'successful' => $this->successfulCount;
67+
yield 'errored' => $this->erroredCount;
68+
yield 'slow' => $this->slowCount;
2869
}
2970

30-
public function resetCount(): void
71+
public function resetCounters(): void
3172
{
32-
$this->count = 0;
73+
$this->initiatedCount = self::ZERO;
74+
$this->successfulCount = self::ZERO;
75+
$this->erroredCount = self::ZERO;
76+
$this->slowCount = self::ZERO;
3377
}
3478
}

src/MiddlewareRunner.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Plasma\SQL\QueryBuilder;
66
use Psr\Http\Message\ServerRequestInterface;
77
use React\Promise\PromiseInterface;
8+
use const WyriHaximus\Constants\Numeric\ONE;
9+
use const WyriHaximus\Constants\Numeric\ZERO;
810

911
/**
1012
* @internal
@@ -25,18 +27,18 @@ public function __construct(MiddlewareInterface ...$middleware)
2527

2628
public function query(QueryBuilder $query, callable $last): PromiseInterface
2729
{
28-
return $this->call($query, 0, $last);
30+
return $this->call($query, ZERO, $last);
2931
}
3032

3133
private function call(QueryBuilder $query, int $position, callable $last): PromiseInterface
3234
{
3335
// final request handler will be invoked without hooking into the promise
34-
if (!array_key_exists($position + 1, $this->middleware)) {
36+
if (!array_key_exists($position + ONE, $this->middleware)) {
3537
return $this->middleware[$position]->query($query, $last);
3638
}
3739

3840
return $this->middleware[$position]->query($query, function (QueryBuilder $query) use ($position, $last) {
39-
return $this->call($query, $position + 1, $last);
41+
return $this->call($query, $position + ONE, $last);
4042
});
4143
}
4244
}

0 commit comments

Comments
 (0)