-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActivePaidAccessesMeasurement, fix measurements graph keys
remp/novydenik#1241
- Loading branch information
Matus Kalafut
committed
Jun 20, 2024
1 parent
d1e86ae
commit 608ee9d
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Crm\FamilyModule\Measurements; | ||
|
||
use Crm\ApplicationModule\Models\Measurements\BaseMeasurement; | ||
use Crm\ApplicationModule\Models\Measurements\Criteria; | ||
use Crm\ApplicationModule\Models\Measurements\Point; | ||
use Crm\ApplicationModule\Models\Measurements\Series; | ||
|
||
class ActivePaidAccessesMeasurement extends BaseMeasurement | ||
{ | ||
public const CODE = 'subscriptions.active_paid_accesses'; | ||
|
||
public function calculate(Criteria $criteria): Series | ||
{ | ||
$series = $criteria->getEmptySeries(); | ||
|
||
$date = clone $criteria->getFrom(); | ||
while ($date <= $criteria->getTo()) { | ||
$next = $criteria->getAggregation()->nextDate($date); | ||
|
||
$total = $this->countActivePayingSubscribers($date, $next) + $this->countUnusedFamilyRequests($date, $next); | ||
|
||
$point = new Point($criteria->getAggregation(), $total, clone $date); | ||
$series->setPoint($point); | ||
|
||
$date = $next; | ||
} | ||
return $series; | ||
} | ||
|
||
private function countActivePayingSubscribers($date, $next): int | ||
{ | ||
$query = " | ||
SELECT is_paid, COUNT(DISTINCT subscriptions.user_id) AS count | ||
FROM subscriptions | ||
JOIN users ON users.id = subscriptions.user_id | ||
WHERE ? | ||
GROUP BY is_paid | ||
"; | ||
|
||
$rows = $this->db()->query( | ||
$query, | ||
[ | ||
'start_time <' => $next, | ||
'end_time >=' => $date, | ||
'users.active' => 1, | ||
'subscription_type_id NOT' => $this->db()::literal( | ||
'IN (SELECT master_subscription_type_id FROM family_subscription_types)' | ||
), | ||
], | ||
); | ||
|
||
foreach ($rows as $row) { | ||
if ($row->is_paid === 1) { | ||
return $row->count; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private function countUnusedFamilyRequests($date, $next): int | ||
{ | ||
$query = " | ||
SELECT COUNT(*) AS count | ||
FROM family_requests | ||
JOIN subscriptions ON family_requests.master_subscription_id = subscriptions.id | ||
WHERE ? | ||
"; | ||
|
||
$row = $this->db()->fetch( | ||
$query, | ||
[ | ||
'subscriptions.start_time <' => $next, | ||
'subscriptions.end_time >=' => $date, | ||
'family_requests.created_at <' => $next, | ||
$this->db()::literal('?or', [ | ||
'accepted_at' => null, | ||
'accepted_at >' => $next, | ||
]), | ||
$this->db()::literal('?or', [ | ||
'canceled_at' => null, | ||
'canceled_at <' => $next, | ||
]), | ||
], | ||
); | ||
|
||
return $row->count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Crm\FamilyModule\Seeders; | ||
|
||
use Crm\ApplicationModule\Repositories\MeasurementsRepository; | ||
use Crm\ApplicationModule\Seeders\ISeeder; | ||
use Crm\ApplicationModule\Seeders\MeasurementsTrait; | ||
use Crm\FamilyModule\Measurements\ActivePaidAccessesMeasurement; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class MeasurementsSeeder implements ISeeder | ||
{ | ||
use MeasurementsTrait; | ||
|
||
private MeasurementsRepository $measurementsRepository; | ||
|
||
public function __construct(MeasurementsRepository $measurementsRepository) | ||
{ | ||
$this->measurementsRepository = $measurementsRepository; | ||
} | ||
|
||
public function seed(OutputInterface $output) | ||
{ | ||
$this->addMeasurement( | ||
$output, | ||
ActivePaidAccessesMeasurement::CODE, | ||
'family.measurements.active_paid_accesses.title', | ||
'family.measurements.active_paid_accesses.description', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters