Skip to content

Commit 0e4222d

Browse files
committed
✨ Optimize chat archive range fetching and caching logic πŸ—„οΈ
πŸ”§ Refactor to improve performance by checking cached ranges before querying the database. πŸ” Add methods for matching ranges and verifying chat existence in archives to handle overlaps effectively. πŸ› οΈ Update type casting for threshold values in bot action commands for consistency.
1 parent 3ba01ec commit 0e4222d

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

β€Žlhc_web/lib/core/lhchat/lhchatarchive.phpβ€Ž

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,37 @@ public static function setArchiveAttribute(array & $chatsId)
7878

7979
foreach ($archivedChats as $archiveChatId) {
8080

81-
if (empty($archivesRanges) || ($archiveId = self::isInRange($archiveChatId, $archivesRanges)) === null) {
82-
$stmt = $db->prepare('SELECT id,first_id,last_id FROM lh_chat_archive_range WHERE :chat_id_1 <= last_id && :chat_id_2 >= first_id LIMIT 1');
83-
$stmt->bindValue(':chat_id_1', $archiveChatId);
84-
$stmt->bindValue(':chat_id_2', $archiveChatId);
81+
$archiveId = null;
82+
83+
// Collect all matching ranges from cache first
84+
$matchingRanges = self::getMatchingRanges($archiveChatId, $archivesRanges);
85+
86+
// If no cached ranges match, query the database
87+
if (empty($matchingRanges)) {
88+
$stmt = $db->prepare('SELECT id, first_id, last_id FROM lh_chat_archive_range WHERE :chat_id <= last_id AND :chat_id >= first_id');
89+
$stmt->bindValue(':chat_id', $archiveChatId);
8590
$stmt->execute();
86-
$dataArchive = $stmt->fetch(PDO::FETCH_ASSOC);
91+
$matchingRanges = $stmt->fetchAll(PDO::FETCH_ASSOC);
92+
}
8793

88-
if (is_array($dataArchive)) {
94+
// If multiple ranges match (overlap), verify by checking the archive table
95+
if (count($matchingRanges) > 1) {
96+
foreach ($matchingRanges as $dataArchive) {
97+
if (self::chatExistsInArchive($db, $dataArchive['id'], $archiveChatId)) {
98+
$archiveId = $dataArchive['id'];
99+
100+
if (! self::isRangeCached($dataArchive['id'], $archivesRanges)) {
101+
$archivesRanges[] = $dataArchive;
102+
}
103+
break;
104+
}
105+
}
106+
} elseif (count($matchingRanges) === 1) {
107+
$dataArchive = reset($matchingRanges);
108+
$archiveId = $dataArchive['id'];
109+
110+
if (! self::isRangeCached($dataArchive['id'], $archivesRanges)) {
89111
$archivesRanges[] = $dataArchive;
90-
$archiveId = $dataArchive['id'];
91-
} else {
92-
$archiveId = null;
93112
}
94113
}
95114

@@ -113,4 +132,42 @@ public static function isInRange($chatId, $ranges)
113132

114133
return null;
115134
}
135+
136+
/**
137+
* Returns all cached ranges that could contain the given chat ID.
138+
* When ranges overlap, multiple ranges may match.
139+
*/
140+
private static function getMatchingRanges($chatId, $ranges)
141+
{
142+
$matched = array();
143+
foreach ($ranges as $range) {
144+
if ($chatId >= $range['first_id'] && $chatId <= $range['last_id']) {
145+
$matched[] = $range;
146+
}
147+
}
148+
return $matched;
149+
}
150+
151+
private static function isRangeCached($rangeId, $ranges)
152+
{
153+
foreach ($ranges as $r) {
154+
if ($r['id'] == $rangeId) {
155+
return true;
156+
}
157+
}
158+
return false;
159+
}
160+
161+
/**
162+
* Verifies whether a chat actually exists in a specific archive table.
163+
* This resolves ambiguity when archive ranges overlap.
164+
*/
165+
private static function chatExistsInArchive($db, $archiveRangeId, $chatId)
166+
{
167+
$tableName = 'lh_chat_archive_' . (int)$archiveRangeId;
168+
$stmt = $db->prepare("SELECT 1 FROM `{$tableName}` WHERE id = :chat_id LIMIT 1");
169+
$stmt->bindValue(':chat_id', $chatId, PDO::PARAM_INT);
170+
$stmt->execute();
171+
return (bool)$stmt->fetchColumn();
172+
}
116173
}

β€Žlhc_web/lib/core/lhgenericbot/actionTypes/lhgenericbotactioncommand.phpβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public static function process($chat, $action, $trigger, $params)
434434

435435
$messagesGroupFieldAll = $action['content']['payload_arg_val_sum'];
436436
$messagesGroupFieldValueScore = isset($action['content']['payload_arg_val_field']) ? (string)$action['content']['payload_arg_val_field'] : '';
437-
$messagesThresholdValue = isset($action['content']['payload_arg_val_trshl']) ? (double)$action['content']['payload_arg_val_trshl'] : 0;
437+
$messagesThresholdValue = isset($action['content']['payload_arg_val_trshl']) ? (float)$action['content']['payload_arg_val_trshl'] : 0;
438438

439439
$counterTotal = 0;
440440
$counterRequired = 0;

β€Žlhc_web/lib/core/lhgenericbot/actionTypes/lhgenericbotactionrestapi.phpβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ public static function makeRequest($host, $methodSettings, $paramsCustomer)
18731873
// Sometimes object already exists
18741874
}
18751875
}
1876-
curl_close($ch);
1876+
// curl_close($ch);
18771877
}
18781878

18791879
if ($logRequest === true) {

0 commit comments

Comments
Β (0)