1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright Mautic, Inc |
||
5 | * @author Mautic, Inc |
||
6 | * |
||
7 | * @link http://mautic.com |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\WebhookBundle\Entity; |
||
13 | |||
14 | use Mautic\CoreBundle\Entity\CommonRepository; |
||
15 | |||
16 | class LogRepository extends CommonRepository |
||
17 | { |
||
18 | /** |
||
19 | * Retains a rolling number of log records for a webhook id. |
||
20 | * |
||
21 | * @param int $webhookId |
||
22 | * @param int $logMax how many recent logs should remain, the rest will be deleted |
||
23 | * |
||
24 | * @return int |
||
25 | */ |
||
26 | public function removeOldLogs($webhookId, $logMax) |
||
27 | { |
||
28 | // if the hook was deleted then return a count of 0 |
||
29 | if (!$webhookId) { |
||
30 | return false; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
31 | } |
||
32 | |||
33 | $qb = $this->_em->getConnection()->createQueryBuilder(); |
||
34 | |||
35 | $count = $qb->select('count(id) as log_count') |
||
36 | ->from(MAUTIC_TABLE_PREFIX.'webhook_logs', $this->getTableAlias()) |
||
37 | ->where('webhook_id = '.$webhookId) |
||
38 | ->execute()->fetch(); |
||
39 | |||
40 | if ((int) $count['log_count'] >= (int) $logMax) { |
||
41 | $qb = $this->_em->getConnection()->createQueryBuilder(); |
||
42 | |||
43 | $id = $qb->select('id') |
||
44 | ->from(MAUTIC_TABLE_PREFIX.'webhook_logs', $this->getTableAlias()) |
||
45 | ->where('webhook_id = '.$webhookId) |
||
46 | ->orderBy('date_added', 'ASC')->setMaxResults(1) |
||
47 | ->execute()->fetch(); |
||
48 | |||
49 | $qb = $this->_em->getConnection()->createQueryBuilder(); |
||
50 | |||
51 | $qb->delete(MAUTIC_TABLE_PREFIX.'webhook_logs') |
||
52 | ->where($qb->expr()->in('id', $id)) |
||
53 | ->execute(); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Lets assume that all HTTP status codes 2** are a success. |
||
59 | * This method will count the latest success codes until the $limit |
||
60 | * and divide them with the all requests until the limit. |
||
61 | * |
||
62 | * 0 = 100% responses failed |
||
63 | * 1 = 100% responses are successful |
||
64 | * null = no log rows yet |
||
65 | * |
||
66 | * @param int $webhookId |
||
67 | * @param int $limit |
||
68 | * |
||
69 | * @return float|null |
||
70 | */ |
||
71 | public function getSuccessVsErrorStatusCodeRatio($webhookId, $limit) |
||
72 | { |
||
73 | // Generate query to select last X = $limit rows |
||
74 | $selectqb = $this->_em->getConnection()->createQueryBuilder(); |
||
75 | $selectqb->select('*') |
||
76 | ->from(MAUTIC_TABLE_PREFIX.'webhook_logs', $this->getTableAlias()) |
||
77 | ->where($this->getTableAlias().'.webhook_id = :webhookId') |
||
78 | ->setFirstResult(0) |
||
79 | ->setMaxResults($limit) |
||
80 | ->orderBy($this->getTableAlias().'.date_added', 'DESC'); |
||
81 | |||
82 | // Count all responses |
||
83 | $countAllQb = $this->_em->getConnection()->createQueryBuilder(); |
||
84 | $countAllQb->select('COUNT('.$this->getTableAlias().'.id) AS thecount') |
||
85 | ->from(sprintf('(%s)', $selectqb->getSQL()), $this->getTableAlias()) |
||
86 | ->setParameter('webhookId', $webhookId); |
||
87 | |||
88 | $result = $countAllQb->execute()->fetch(); |
||
89 | |||
90 | if (isset($result['thecount'])) { |
||
91 | $allCount = (int) $result['thecount']; |
||
92 | } else { |
||
93 | return null; |
||
94 | } |
||
95 | |||
96 | // Count successful responses |
||
97 | $countSuccessQb = $this->_em->getConnection()->createQueryBuilder(); |
||
98 | $countSuccessQb->select('COUNT('.$this->getTableAlias().'.id) AS thecount') |
||
99 | ->from(sprintf('(%s)', $selectqb->getSQL()), $this->getTableAlias()) |
||
100 | ->andWhere($countSuccessQb->expr()->gte($this->getTableAlias().'.status_code', 200)) |
||
101 | ->andWhere($countSuccessQb->expr()->lt($this->getTableAlias().'.status_code', 300)) |
||
102 | ->setParameter('webhookId', $webhookId); |
||
103 | |||
104 | $result = $countSuccessQb->execute()->fetch(); |
||
105 | |||
106 | if (isset($result['thecount'])) { |
||
107 | $successCount = (int) $result['thecount']; |
||
108 | } |
||
109 | |||
110 | if (!empty($allCount) && isset($successCount)) { |
||
111 | return $successCount / $allCount; |
||
112 | } |
||
113 | |||
114 | return null; |
||
115 | } |
||
116 | } |
||
117 |