|
|
@@ -49,6 +49,13 @@ class EventsRepository
|
|
|
*/
|
|
|
private $tableName;
|
|
|
|
|
|
+ /**
|
|
|
+ * Очередь событий для вставки
|
|
|
+ *
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ private $bulkQueue = [];
|
|
|
+
|
|
|
/**
|
|
|
* EventsRepository constructor.
|
|
|
*
|
|
|
@@ -108,50 +115,76 @@ QUERY;
|
|
|
*
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
- public function generateEventsForCompanies(MongoCursor $cursor, int $needCount = 256): void
|
|
|
+ public function generateEventsForCompanies(MongoCursor $cursor, int $needCount = 3200): void
|
|
|
{
|
|
|
- $this->createTableIfNeeded();
|
|
|
+ //$this->createTableIfNeeded();
|
|
|
$eventTypeCount = \count(CompanyEvent::keys());
|
|
|
$this->clickhouse->database($this->databaseName);
|
|
|
|
|
|
+ $threshold = 100000;
|
|
|
+ $eachCount = 0;
|
|
|
+
|
|
|
foreach ($cursor as $company) {
|
|
|
$eventCount = random_int($needCount - 10, $needCount + 10);
|
|
|
$eventCount = $eventCount > 0 ? $eventCount : 10;
|
|
|
for ($count = 1; $count <= $eventCount; $count++) {
|
|
|
$randomDate = $this->getRandomDate($this->startDate, $this->endDate);
|
|
|
$randomEventKey = random_int(1, $eventTypeCount);
|
|
|
- $this->saveEvent($company->_id, $randomDate, new CompanyEvent($randomEventKey));
|
|
|
+ $this->addEventToQueue($company->_id, $randomDate, new CompanyEvent($randomEventKey));
|
|
|
+
|
|
|
+ $eachCount++;
|
|
|
+ if ($eachCount % $threshold === 0) {
|
|
|
+ $this->insertEventsFromQueue();
|
|
|
+ gc_collect_cycles();
|
|
|
+ // echo " + 100k\n";
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+ $this->insertEventsFromQueue();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Сохранение события
|
|
|
+ * Добавление события в очередь на сохранение
|
|
|
*
|
|
|
* @param ObjectId $companyId
|
|
|
* @param \DateTime $datetime
|
|
|
* @param CompanyEvent $event
|
|
|
*/
|
|
|
- public function saveEvent(ObjectId $companyId, \DateTime $datetime, CompanyEvent $event): void
|
|
|
+ public function addEventToQueue(ObjectId $companyId, \DateTime $datetime, CompanyEvent $event): void
|
|
|
{
|
|
|
$idChunks = $this->convertObjectIdToInts($companyId);
|
|
|
|
|
|
+ $this->bulkQueue[] = [
|
|
|
+ $datetime->format('Y-m-d'),
|
|
|
+ $datetime->getTimestamp(),
|
|
|
+ $event->getKey(),
|
|
|
+ $idChunks[0],
|
|
|
+ $idChunks[1],
|
|
|
+ $idChunks[2]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Вставка всех событий из очереди
|
|
|
+ */
|
|
|
+ public function insertEventsFromQueue(): void
|
|
|
+ {
|
|
|
+ if (!$this->bulkQueue || \count($this->bulkQueue)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
$this->clickhouse->insert(
|
|
|
$this->tableName,
|
|
|
[
|
|
|
- [
|
|
|
- $datetime->format('Y-m-d'),
|
|
|
- $datetime->getTimestamp(),
|
|
|
- $event->getKey(),
|
|
|
- $idChunks[0],
|
|
|
- $idChunks[1],
|
|
|
- $idChunks[2]
|
|
|
- ]
|
|
|
+ $this->bulkQueue
|
|
|
],
|
|
|
[
|
|
|
'event_date', 'event_time', 'event_type', 'id_1', 'id_2', 'id_3'
|
|
|
]
|
|
|
);
|
|
|
+ $this->bulkQueue = [];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -185,12 +218,11 @@ QUERY;
|
|
|
private function convertObjectIdToInts(ObjectId $objectId): array
|
|
|
{
|
|
|
$idString = (string) $objectId;
|
|
|
- $idChunks = [
|
|
|
+
|
|
|
+ return [
|
|
|
hexdec(mb_substr($idString, 0, 8)),
|
|
|
hexdec(mb_substr($idString, 8, 8)),
|
|
|
hexdec(mb_substr($idString, 16))
|
|
|
];
|
|
|
-
|
|
|
- return $idChunks;
|
|
|
}
|
|
|
}
|