function updateMessage($uid, $rid, $msg, $type, $pmid = null) { global $wpdb; $messages_table_name = $wpdb->prefix . "gwpm_messages"; $user_table_name = $wpdb->prefix . "users"; if ($type == 'int' && $pmid == null) { $queryString = "SELECT msgtab.unread msgcount FROM " . $messages_table_name . " msgtab, " . $user_table_name . " usertab " . " WHERE msgtab.pmid=-1 AND msgtab.user_id=%d AND msgtab.recipient_id=%d AND usertab.id=%d"; $preparedSql = $wpdb->prepare($queryString, $uid, $rid, $rid); appendLog($preparedSql); $result = $wpdb->get_results($preparedSql); $obj = $result[0]; $datetime = date('Y-m-d H:i:s'); $endVal = -1; if (isset($obj) && $obj != null && $obj->msgcount > 0) { if ($obj->msgcount < GWPM_MAX_USER_MESSAGES) { $new_val = $obj->msgcount++; appendLog("new value : " + $new_val); $endVal = $wpdb->update($messages_table_name, array("unread" => $obj->msgcount++, "created" => $datetime), array("pmid" => '-1', "user_id" => $uid, "recipient_id" => $rid)); } } else { $wpdb->insert($messages_table_name, array("pmid" => '-1', "created" => $datetime, "user_id" => $uid, "recipient_id" => $rid, "user_message" => $msg, "folder" => 'sent', "unread" => '1'), array("%s", "%s", "%d", "%d", "%s", "%s", "%d")); $endVal = 0; } } return $endVal; }
function updateUser($userObj) { global $gwpm_activity_model; $isGwpmUser = get_user_meta($userObj->userId, 'gwpm_user'); if (isset($isGwpmUser) && sizeof($isGwpmUser) > 0) { } else { appendLog(add_user_meta($userObj->userId, 'gwpm_user', true, true)); } $processKeys = array_keys(get_class_vars(get_class($userObj))); $dynaKeys = getDynamicFieldKeys(); foreach ($dynaKeys as $__keys) { array_push($processKeys, $__keys); } foreach ($processKeys as $key) { appendLog($key); if ($key == "gwpm_profile_photo") { $photoObj = $userObj->{$key}; if ($photoObj["size"] != 0) { $value = savePhotoToUploadFolder($userObj->{$key}, $userObj->userId); $gwpm_activity_model->addActivityLog("profile", "Updated Profile Image", $userObj->userId); } else { continue; } } elseif (!is_array($userObj->{$key})) { $value = trim($userObj->{$key}); } else { $value = $userObj->{$key}; } if ($key != 'userId' && $key != 'dynamicFields' && $key != 'dynamicFieldsValidation') { update_user_meta($userObj->userId, $key, $value); } } $gwpm_activity_model->addActivityLog("profile", "Updated Profile", $userObj->userId); }
/** * @param Payment $payment * * @return float */ protected function sanitizeAmount(Payment $payment) { if ($payment->currency === 'USD') { return (double) $payment->amount; } try { $rate = CurrencyQuery::query($payment->currency); return $rate * $payment->amount; } catch (\InvalidArgumentException $e) { appendLog(sprintf('currency[%s] rate fail: %s', $payment->currency, $e->getMessage())); } return 0.0; }
function getUnreadMessages() { global $wpdb; $userId = get_current_user_id(); print_r($userId); $messages_table_name = $wpdb->prefix . "gwpm_messages"; $queryString = "SELECT SUM(unread) counts " . "FROM " . $messages_table_name . " WHERE recipient_id =%d and unread > 0"; $preparedSql = $wpdb->prepare($queryString, $userId); appendLog('Unread notification sql: '); appendLog($preparedSql); $result = $wpdb->get_results($preparedSql); $resultVal = $result[0]; return $resultVal->counts; }
function saveDynamicFields($gwpmFieldLabels, $gwpmFieldTypes, $gwpmFieldValue, $earlierCount) { global $wpdb; $totalFields = get_option(GWPM_DYNA_FIELD_COUNT); appendLog(print_r($gwpmFieldLabels, true)); $keys = array_keys($gwpmFieldLabels); // $deletedObjs = explode(',', $deletedItems) ; $save_options = null; if ($totalFields == false) { $totalFields = 0; } appendLog($earlierCount . " - " . $totalFields); if ($earlierCount == $totalFields) { foreach ($keys as $mKey) { $save_options = null; appendLog("\nK: " . $mKey . "\n"); $save_options['gwpm_dyna_field_label'] = $gwpmFieldLabels[$mKey]; $save_options['gwpm_dyna_field_type'] = $gwpmFieldTypes[$mKey]; if ($gwpmFieldTypes[$mKey] == "select") { appendLog("Select Type"); $gwpm_field_values = $gwpmFieldValue[$mKey]; appendLog("zKey: " . $mKey . ' - ' . $gwpm_field_values); $itr = 1; $save_options_values = null; foreach ($gwpm_field_values as $vKey) { appendLog("vKey: " . $vKey . "<br />"); $save_options_values[$itr] = $vKey; $itr++; } $save_options['gwpm_dyna_field_values'] = $save_options_values; } else { if ($gwpmFieldTypes[$mKey] == 'yes_no') { appendLog("Yes/No Type"); } else { appendLog("Text Type"); } } $totalFields++; appendLog(print_r($save_options, true)); $result = update_option(GWPM_DYNA_KEY_PREFIX . $totalFields, $save_options); if ($result == 1) { update_option(GWPM_DYNA_FIELD_COUNT, $totalFields); } } } else { throw new GwpmCommonException("Invalid Request"); } }
/** * @param array $groupedUidList * * @return int */ public function updateES(array $groupedUidList) { appendLog(sprintf('%s start with memory usage %s', __METHOD__, PHP_Timer::resourceUsage())); $count = array_sum(array_map(function (array $list) { return count($list); }, $groupedUidList)); if ($count === 0) { appendLog(sprintf('%s: have 0 user to sync', __METHOD__)); return 0; } $start = microtime(true); $groupedDetail = $this->dataProvider->generate($groupedUidList); $delta = microtime(true) - $start; $totalUidCount = array_sum(array_map(function (array $uidList) { return count($uidList); }, $groupedUidList)); appendLog(sprintf('Total %d uids, read detail cost %s', $totalUidCount, PHP_Timer::secondsToTimeString($delta))); $bufferSyncAction = function (array $users) { $count = count($users); appendLog(sprintf('ES updater have %d user to sync', $count)); $deltaList = $this->indexer->batchUpdate($users, function ($userCount, $delta) { appendLog(sprintf('on ES batch update of %d users cost %s', $userCount, PHP_Timer::secondsToTimeString($delta))); }); $totalDelta = array_sum($deltaList); appendLog(sprintf('Sync %d users to ES cost %s with average cost %s', $count, PHP_Timer::secondsToTimeString($totalDelta), PHP_Timer::secondsToTimeString($totalDelta / $count))); }; $buffer = new WriteBuffer($bufferSyncAction, 500); foreach ($groupedDetail as $payload) { $shardId = $payload['shardId']; $shardUserList = $payload['dataSet']; $count = count($shardUserList); if ($count === 0) { continue; } appendLog(sprintf('%s have %d user to sync', $shardId, $count)); foreach ($shardUserList as $userInfo) { $buffer->add($userInfo); } } $buffer->sync(); }
<?php /** * Created by PhpStorm. * User: Jiang Yu * Date: 2015/07/07 * Time: 11:29 AM */ require __DIR__ . '/../../bootstrap.php'; $options = getopt('', ['gv:']); $gameVersion = isset($options['gv']) ? $options['gv'] : 'tw'; appendLog('game version: ' . $gameVersion); $shardList = \Environment\PlatformFactory::make($gameVersion)->getMySQLShards(); foreach ($shardList as $shard) { appendLog($shard); }
$stepGenerator = WorkRoundGenerator::generate($now, $quitTimestamp, $interval, false); foreach ($stepGenerator as $timestamp) { $msg = $myself . ': ' . date('c', $timestamp) . ' run with ts ' . date('c', $timestamp); appendLog($msg); $date = $specifiedDate ? $specifiedDate : date('Y-m-d'); $groupedUidList = $installUidProvider->generate($date, function ($shardId, $userCount, $delta) { if ($userCount === 0) { return; } appendLog(sprintf('%s install(%d) cost %s', $shardId, $userCount, PHP_Timer::secondsToTimeString($delta))); }); $queue->push($groupedUidList); $deltaList = $installUidProvider->getDeltaList(); $totalCount = 0; foreach ($groupedUidList as $shardId => $shardUidList) { $shardCount = count($shardUidList); if ($shardCount === 0) { continue; } $totalCount += $shardCount; } appendLog(sprintf('%s: %s found %d install user, cost %s', $myself, date('c'), $totalCount, PHP_Timer::secondsToTimeString(array_sum($deltaList)))); $logFile = call_user_func($logFileGetter, $gameVersion, $date); file_put_contents($logFile, date('c') . ' have ' . $totalCount . PHP_EOL . print_r($groupedUidList, true)); if ($specifiedDate) { dump(date('c')); dump($groupedUidList); dump($myself . ': quit'); break; } }
/** * @param Platform $platform * @param DeAuthorizedUserQuery $query * * @return array * @throws Exception */ protected function findDeAuthorizedUser(Platform $platform, DeAuthorizedUserQuery $query) { $resultSet = []; $shardList = $platform->getMySQLShards(); foreach ($shardList as $shardConfig) { $dbName = $shardConfig['database']; appendLog('on database ' . $dbName); PHP_Timer::start(); $snsidList = $this->onShard($shardConfig, $query); $delta = PHP_Timer::stop(); appendLog('cost on database ' . $dbName . ': ' . PHP_Timer::secondsToTimeString($delta)); dump(sprintf('Memory: %4.2fMb', memory_get_peak_usage(true) / 1048576)); $resultSet = array_merge($resultSet, $snsidList); } return $resultSet; }
}); $options = getopt('v', ['gv:', 'es:', 'bs:', 'interval:', 'round:', 'repeatTimes:', 'waitTime:']); $verbose = isset($options['v']); $gameVersion = null; if (defined('GAME_VERSION')) { $gameVersion = GAME_VERSION; } else { assert(isset($options['gv']), 'game version not defined'); $gameVersion = trim($options['gv']); } $esHost = isset($options['es']) ? $options['es'] : '52.19.73.190'; $backStep = isset($options['bs']) ? $options['bs'] : 1; $interval = isset($options['interval']) ? $options['interval'] : 20; $round = isset($options['round']) ? $options['round'] : 100; $maxRepeatTimes = isset($options['repeatTimes']) ? $options['repeatTimes'] : 100; $longestWaitTime = isset($options['waitTime']) ? $options['waitTime'] : 10 * 60; $lastActiveTimestamp = time() - $backStep; $quitTimestamp = time() + $round * $interval; if ($verbose) { $msg = sprintf('game version: %s, ES host: %s, backStep=%d, interval=%d, round=%d, start at: %s, quit at: %s', $gameVersion, $esHost, $backStep, $interval, $round, date('H:i:s', $lastActiveTimestamp), date('H:i:s', $quitTimestamp)); dump($msg); dump(sprintf('max repeat times: %d, max wait time: %d', $maxRepeatTimes, $longestWaitTime)); } $myself = basename(__FILE__); $stepGenerator = WorkRoundGenerator::generate($lastActiveTimestamp, $quitTimestamp, $interval, $verbose); foreach ($stepGenerator as $timestamp) { $msg = $myself . ': ' . date('c', $timestamp) . ' run with ts ' . $timestamp; dump($msg); appendLog($msg); (new SyncMachine($gameVersion, $esHost))->run($maxRepeatTimes, $longestWaitTime); }
$newInstallCount = array_sum($distribution); appendLog(sprintf('Total %d new install on %s', $newInstallCount, $calendarDay)); $loginDistribution[$calendarDay] = $newInstallCount; $start = microtime(true); $groupedDetail = $userDetailProvider->generate($groupedUidList); $delta = microtime(true) - $start; appendLog(sprintf('Total %d new install on %s, read detail cost %s', $newInstallCount, $calendarDay, PHP_Timer::secondsToTimeString($delta))); $esUpdateQueue = []; foreach ($groupedDetail as $payload) { $shardId = $payload['shardId']; $shardUserList = $payload['dataSet']; // error_log(print_r($shardUserList, true), 3, CONFIG_DIR.'/aaa'); $count = count($shardUserList); if ($count === 0) { continue; } appendLog(sprintf('%s have %d user to sync', $shardId, $count)); $esUpdateQueue = array_merge($esUpdateQueue, $shardUserList); $queueLength = count($esUpdateQueue); if ($queueLength >= $magicNumber) { appendLog(sprintf('%s: flush ES update queue: %d user on date %s to sync %s', date('c'), $queueLength, $calendarDay, PHP_Timer::resourceUsage())); call_user_func($esUpdateHandler, $indexer, $esUpdateQueue); $esUpdateQueue = []; } } call_user_func($esUpdateHandler, $indexer, $esUpdateQueue); $calendarMarker->mark($markerDate); } appendLog(sprintf('%s Total %d user processed, cost %s', date('c'), $loginDistribution, PHP_Timer::resourceUsage())); appendLog($loginDistribution); sleep(3600 * 10);
<?php $deletedItems = ""; $formSubmitted = $_POST['formSubmitted']; if ($formSubmitted != null) { $deletedItems = $_POST['deletedItems']; $gwpmFieldLabels = $_POST['gwpm_dyna_field_label']; $gwpmFieldTypes = $_POST['gwpm_dyna_field_type']; $gwpmFieldValue = $_POST['gwpm_dyna_field_value']; $earlierCount = $_POST['earlierCount']; if (isset($gwpmFieldLabels) && $gwpmFieldLabels != null) { appendLog(print_r($gwpmFieldLabels, true)); appendLog(print_r($gwpmFieldTypes, true)); appendLog(print_r($gwpmFieldValue, true)); $adminModel = new GwpmAdminModel(); $adminModel->saveDynamicFields($gwpmFieldLabels, $gwpmFieldTypes, $gwpmFieldValue, $earlierCount); } } $existingRecords = get_option(GWPM_DYNA_FIELD_COUNT); if (!isset($existingRecords) || $existingRecords == null || $existingRecords == false) { $existingRecords = 0; } ?> <br /> <div id="gwpm_resultBox" class="gwpm_message_box" ></div> <form method="post" > <div id="oldDynamicFieldHolder"> <?php $totalFields = $existingRecords; $displayText = ""; $alternateClass = "";
/** * @param string $dsn * @param array $options * * @return false|PDO */ private function connect($dsn, array $options) { try { $pdo = new PDO($dsn, $options['username'], $options['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 1, PDO::ATTR_CASE => PDO::CASE_NATURAL]); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $pdo; } catch (\PDOException $e) { appendLog(sprintf('Error on dsn[%s]: %s', $dsn, $e->getMessage())); return false; } }
function getGravatarImageForUser($userid, $isExplicit = null) { $profileImgName = get_user_meta($userid, "gwpm_profile_photo", true); appendLog("profileImgName: " . print_r($profileImgName, true)); if (isset($profileImgName['thumb_name'])) { $imageName = $profileImgName['thumb_name']; $imageURL = GWPM_GALLERY_URL . URL_S . $userid . URL_S . $imageName; appendLog("image url : " . $imageURL); } else { $imageURL = GWPM_PUBLIC_IMG_URL . URL_S . 'gwpm_icon.png'; } if (isset($isExplicit)) { return '<img width="48" height="48" class="avatar avatar-48 photo" src="' . $imageURL . '" alt="Profile PIC">'; } else { return $imageURL; } }
$elasticaHelper = new ElasticaHelper($gameVersion, ELASTIC_SEARCH_INDEX, $magicNumber); $elasticaHelper->setVerbose($verbose); $localeCount = 0; $processedCount = 0; QueryHelperFactory::setVerbose($verbose); $userDetailProvider = new \DataProvider\User\UserDetailProvider($gameVersion, $pdoPool); $shardIdList = $pdoPool->listShardId(); foreach ($shardIdList as $shardId) { $pdo = $pdoPool->getByShardId($shardId); $queryHelper = QueryHelperFactory::make($pdo); PHP_Timer::start(); $uidLocalePairs = $queryHelper->listLocale(); $localeCount += count($uidLocalePairs); appendLog(sprintf('%s have %d locale users cost %s', $shardId, count($uidLocalePairs), PHP_Timer::secondsToTimeString(PHP_Timer::stop()))); file_put_contents(call_user_func($logFileGetter, $gameVersion, $shardId), print_r($uidLocalePairs, true)); $userDetailGenerator = $userDetailProvider->generate([$shardId => array_keys($uidLocalePairs)]); foreach ($userDetailGenerator as $payload) { $userInfoList = $payload['dataSet']; $syncCount = count($userInfoList); $processedCount += $syncCount; appendLog(sprintf('read user info get %d users cost %s', $syncCount, PHP_Timer::resourceUsage())); PHP_Timer::start(); $elasticaHelper->update($userInfoList, function ($snsidList) { appendLog(sprintf('Total %d user sync failed', count($snsidList))); appendLog('failed snsid: ' . implode(',', $snsidList)); }); appendLog(sprintf('Sync %d users to ES cost %s, %s', $syncCount, PHP_Timer::secondsToTimeString(PHP_Timer::stop()), PHP_Timer::resourceUsage())); } } appendLog(sprintf('Total %d locale, %d user processed, cost %s', $localeCount, $processedCount, PHP_Timer::resourceUsage()));
function GwpmCommonException($message = '') { echo '<b> GwpmCommonException: </b>' . $message; // throw new Exception($message) ; appendLog($message); }
/** * @param string $esHost * @param string $gameVersion * @param int $lastActiveTimestamp */ function main($esHost, $gameVersion, $lastActiveTimestamp) { $dsnList = mysqlDsnGenerator($gameVersion); $totalUserCount = 0; foreach ($dsnList as $mysqlOptions) { $userList = onShard($mysqlOptions, $lastActiveTimestamp); if (count($userList) === 0) { continue; } $totalUserCount += count($userList); PHP_Timer::start(); batchUpdateES($esHost, $gameVersion, $userList); appendLog('ES update[' . count($userList) . '] cost: ' . PHP_Timer::secondsToTimeString(PHP_Timer::stop())); } appendLog(sprintf('%s total user count [%d], from %s', PHP_Timer::resourceUsage(), $totalUserCount, date('Y/m/d H:i:s', $lastActiveTimestamp))); }
private function getUserIds($filters, $args, $userList, $wpdb) { $resultList = array(); $query = "SELECT DISTINCT {$wpdb->usermeta}.user_id FROM {$wpdb->usermeta} WHERE " . $filters; appendLog('-----------'); appendLog(print_r($userList, true)); if ($userList != null) { if (sizeof($userList) > 0) { $userList = esc_sql($userList); $userList = implode(', ', $userList); $query .= "OR " . user_id . " IN ( {$userList} ) "; } else { // $query .= "AND " . user_id . " IN ( ) "; } } appendLog($query); appendLog(print_r($args, true)); $preparedSql = $wpdb->prepare($query, $args); $result = $wpdb->get_results($preparedSql); $fetchedList = array(); foreach ($result as $obj) { appendLog($obj->user_id); $tempObj = $obj->user_id; if (isset($tempObj) && $tempObj != null) { array_push($fetchedList, $tempObj); } } return $fetchedList; }
/** * @param int[] $uidList * @param int[] $uninstalledUidList [uid => value, uid => value] * @param string $columns * * @return array[] */ public function readUserInfo(array $uidList, array $uninstalledUidList, $columns = '*') { $pdo = $this->pdo; $placeHolderList = array_pad([], count($uidList), '?'); $sql = sprintf('/* %s */SELECT %s FROM tbl_user WHERE uid IN (%s)', __METHOD__, $columns, implode(',', $placeHolderList)); $statement = $pdo->prepare($sql); if ($statement === false) { throw new \RuntimeException(json_encode($pdo->errorInfo())); } $statement->execute($uidList); $allRows = $statement->fetchAll(PDO::FETCH_ASSOC); $resultSet = []; foreach ($allRows as $row) { $uid = (int) $row['uid']; if (array_key_exists($uid, $uninstalledUidList)) { $activeLoginTime = (int) $row['logintime']; $uninstallTime = $uninstalledUidList[$uid]['uninstallTime']; if ($activeLoginTime <= $uninstallTime) { $row['status'] = 0; if ($this->verbose) { appendLog(sprintf('%s(%d) => uninstalled', $row['snsid'], $uid)); } } } $resultSet[$uid] = $row; } return $resultSet; }
function gwpm_get_avatar($avatar, $id_or_email, $size, $default) { global $wpdb; appendLog($id_or_email); if (strpos($default, GWPM_AVATAR) !== false) { $imageURL = GWPM_PUBLIC_IMG_URL . URL_S . 'gwpm_icon.png'; appendLog("isAdmin: " . is_admin()); if (!is_admin()) { appendLog("Not Admin page"); if (is_object($id_or_email)) { if ($id_or_email->ID) { $id_or_email = $id_or_email->ID; } else { if ($id_or_email->user_id) { $id_or_email = $id_or_email->user_id; } else { if ($id_or_email->comment_author_email) { $id_or_email = $id_or_email->comment_author_email; } } } } if (is_numeric($id_or_email)) { $userid = (int) $id_or_email; } else { if (is_string($id_or_email)) { $userid = (int) $wpdb->get_var("SELECT ID FROM {$wpdb->users} WHERE user_email = '" . mysql_escape_string($id_or_email) . "'"); } } $imageURL = getGravatarImageForUser($userid); } appendLog("imageURL: " . $imageURL); $doc = new DOMDocument(); $doc->loadHTML($avatar); $imageTags = $doc->getElementsByTagName('img'); foreach ($imageTags as $tag) { appendLog($tag->getAttribute('src')); $imgSrc = $tag->getAttribute('src'); $tag->setAttribute("src", $imageURL); $avatar = $tag->ownerDocument->saveXML($tag); appendLog("altered avatar: "); } } return $avatar; }
$totalUser += $newInstallCount; $start = microtime(true); $groupedDetail = $userDetailProvider->generate($groupedUidList); $delta = microtime(true) - $start; appendLog(sprintf('Total %d new install on %s, read detail cost %s', $newInstallCount, $calendarDay, PHP_Timer::secondsToTimeString($delta))); $esUpdateQueue = []; foreach ($groupedDetail as $payload) { $shardId = $payload['shardId']; $shardUserList = $payload['dataSet']; $count = count($shardUserList); if ($count === 0) { continue; } appendLog(sprintf('%s have %d user to sync', $shardId, $count)); $esUpdateQueue = array_merge($esUpdateQueue, $shardUserList); $queueLength = count($esUpdateQueue); if ($queueLength >= $magicNumber) { appendLog(sprintf('%s: flush ES update queue: %d user on date %s to sync %s', date('c'), $queueLength, $calendarDay, PHP_Timer::resourceUsage())); call_user_func($esUpdateHandler, $indexer, $esUpdateQueue); $esUpdateQueue = []; } } call_user_func($esUpdateHandler, $indexer, $esUpdateQueue); $calendarMarker->mark($markerDate); appendLog(sprintf('Total %d user processed, cost %s', $totalUser, PHP_Timer::resourceUsage())); ++$processedRound; if ($processedRound >= $safeRound) { appendLog(sprintf('Safe round finished, cost %s', PHP_Timer::resourceUsage())); break; } }
/** * @param array $groupedUidList * @param int $repeatTimes * @param int $waitTime * * @return array */ private function aggregate(array $groupedUidList, $repeatTimes, $waitTime) { $now = time(); $beforeTs = $now - $waitTime; appendLog(sprintf('%s: on time %s(%d) with repeat>=%d or waitTime>=%d(%s)', __METHOD__, date('c', $now), $now, $repeatTimes, $waitTime, date('c', $beforeTs))); array_walk($groupedUidList, function (array $uidList, $shardId) use($now) { $this->aggregator->add($shardId, $uidList, $now); }); $rawData = $this->aggregator->filter($repeatTimes, $beforeTs); $groupedUidList = []; array_walk($rawData, function (array $uidList, $shardId) use(&$groupedUidList) { $groupedUidList[$shardId] = array_keys($uidList); }); return $groupedUidList; }