/**
  * @args {"description": "Delay: Stats of questionnaire"}
  */
 public function perform()
 {
     $args = $this->args;
     //Get date from args or today
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     $stats = QuestionnaireLog::getStats($dateStr);
     $statsRows = [];
     foreach ($stats as $stat) {
         $questionnaireId = $stat['_id']['questionnaireId'];
         $accountId = $stat['_id']['accountId'];
         //if $dailyStats exists, update it; else , create
         $dailyStats = ModelStatsQuestionnaireDaily::getByQuestionnaireAndDate($accountId, $questionnaireId, $dateStr);
         if (empty($dailyStats)) {
             $statsRows[] = ['accountId' => $accountId, 'questionnaireId' => $questionnaireId, 'date' => $dateStr, 'count' => $stat['count']];
         } else {
             $dailyStats->count = $stat['count'];
             //catch exception to avoid block batch insert
             try {
                 $dailyStats->save(true, ['count']);
             } catch (Exception $e) {
                 LogUtil::error(['Update StatsQuestionnaireDaily error' => $e->getMessage(), 'StatsQuestionnaireDaily' => $dailyStats]);
                 continue;
             }
         }
     }
     ModelStatsQuestionnaireDaily::batchInsert($statsRows);
     return true;
 }
 /**
  * @args {"description": "Delay: Stats of StatsMemberGrowthMonthly every day"}
  * @author Rex Chen
  */
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m', $datetime);
     $startTime = new \MongoDate(strtotime($dateStr . '-01'));
     $endTime = new \MongoDate(strtotime($dateStr . '-01 +1 month'));
     $accounts = Account::findAll(['enabledMods' => 'member']);
     foreach ($accounts as $account) {
         $accountId = $account->_id;
         $totalMember = Member::countByAccount($accountId, null, $endTime);
         $totalActive = MemberLogs::getTotalActiveByAccount($accountId, $startTime, $endTime);
         $totalNew = Member::countByAccount($accountId, $startTime, $endTime);
         $statsGrowth = ModelStatsMemberGrowthMonthly::getByMonthAndAccount($accountId, $dateStr);
         if (empty($statsGrowth)) {
             $statsGrowth = new ModelStatsMemberGrowthMonthly();
             $statsGrowth->accountId = $accountId;
             $statsGrowth->month = $dateStr;
         }
         $statsGrowth->totalNew = $totalNew;
         $statsGrowth->totalActive = $totalActive;
         $statsGrowth->totalInactive = $totalMember - $totalActive;
         try {
             $statsGrowth->save();
         } catch (Exception $e) {
             ResqueUtil::log(['Update StatsMemberGrowthMonthly error' => $e->getMessage(), 'StatsMemberGrowthMonthly' => $statsGrowth]);
             continue;
         }
     }
     return true;
 }
Esempio n. 3
0
 /**
  * @args {"description": "Delay: Stats of StatsMemberDaily", "runNextJob": true}
  * @author Rex Chen
  */
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     if (!empty(WECONNECT_DOMAIN)) {
         $channelNameMap = $this->_getChannelNameMap();
     }
     $start = new \MongoDate($datetime);
     $end = new \MongoDate(strtotime('+1 day', $datetime));
     $memberStats = Member::getNewMemberStats($start, $end);
     $rowStats = [];
     foreach ($memberStats as $stats) {
         $accountId = $stats['_id']['accountId'];
         $origin = $stats['_id']['origin'];
         $socialAccountId = $stats['_id']['socialAccountId'];
         $originName = empty($channelNameMap[$socialAccountId]) ? '' : $channelNameMap[$socialAccountId];
         $total = $stats['total'];
         $statsMember = ModelStatsMemberDaily::getByDateAndOriginInfo($dateStr, $origin, $originName, $accountId);
         if (!empty($statsMember)) {
             $statsMember->total = $total;
             try {
                 $statsMember->save(true, ['total']);
             } catch (Exception $e) {
                 ResqueUtil::log(['Update StatsMemberDaily error' => $e->getMessage(), 'StatsMemberDaily' => $statsMember]);
                 continue;
             }
         } else {
             $rowStats[] = ['date' => $dateStr, 'origin' => $origin, 'originName' => $originName, 'total' => $total, 'accountId' => $accountId];
         }
     }
     ModelStatsMemberDaily::batchInsert($rowStats);
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m', $datetime);
     $monthData = ModelStatsMemberDaily::getMonthData($dateStr);
     $rowsMonthly = [];
     foreach ($monthData as $item) {
         $origin = $item['_id']['origin'];
         $originName = $item['_id']['originName'];
         $accountId = $item['_id']['accountId'];
         $total = $item['total'];
         $statsMemberMonthly = ModelStatsMemberMonthly::getByDateAndOriginInfo($dateStr, $origin, $originName, $accountId);
         if (!empty($statsMemberMonthly)) {
             $statsMemberMonthly->total = $total;
             try {
                 $statsMemberMonthly->save(true, ['total']);
             } catch (Exception $e) {
                 ResqueUtil::log(['Update StatsMemberMonthly error' => $e->getMessage(), 'StatsMemberMonthly' => $statsMemberMonthly]);
                 continue;
             }
         } else {
             $rowsMonthly[] = ['month' => $dateStr, 'origin' => $origin, 'originName' => $originName, 'accountId' => $accountId, 'total' => $total];
         }
     }
     ModelStatsMemberMonthly::batchInsert($rowsMonthly);
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $accountId = $args['accountId'];
     return ModelStatsCampaignProductCodeQuarterly::generateByYearAndQuarter($accountId, $datetime);
 }
 /**
  * @args {"description": "Direct: Stats of coupon"}
  */
 public function perform()
 {
     $args = $this->args;
     //Get date from args or today
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     $stats = CouponLog::getStats($dateStr);
     if (!empty($stats)) {
         self::createStatsCouponLog($dateStr, $stats);
     }
     return true;
 }
 /**
  * @args {"description": "Delay: Stats of questionnaire answer"}
  */
 public function perform()
 {
     $args = $this->args;
     //Get date from args or today
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     //in case of too much data, get stats by questionnaire
     $skip = 0;
     $limit = 100;
     $query = Questionnaire::find()->orderBy(['_id' => SORT_ASC]);
     $query = $query->offset($skip)->limit($limit);
     $questionnaires = $query->all();
     while (!empty($questionnaires)) {
         $statsRows = [];
         foreach ($questionnaires as $questionnaire) {
             $stats = QuestionnaireLog::getAnswerStats($questionnaire->_id, $dateStr);
             //group stats by questionId
             $rows = [];
             foreach ($stats as $stat) {
                 $questionIdStr = (string) $stat['_id']['questionId'];
                 $optionValue = $stat['_id']['value'];
                 $rows[$questionIdStr][] = ['option' => $optionValue, 'count' => $stat['count']];
             }
             //format stats data, and save it
             foreach ($rows as $questionIdStr => $answerStats) {
                 $questionId = new MongoId($questionIdStr);
                 $statsAnswerDaily = ModelStatsQuestionnaireAnswerDaily::getByQuestionIdAndDate($questionId, $dateStr);
                 if (empty($statsAnswerDaily)) {
                     $statsRows[] = ['questionId' => $questionId, 'stats' => $answerStats, 'date' => $dateStr, 'accountId' => $questionnaire->accountId];
                 } else {
                     $statsAnswerDaily->stats = $answerStats;
                     try {
                         $statsAnswerDaily->save();
                     } catch (Exception $e) {
                         ResqueUtil::log(['Update StatsQuestionnaireAnswerDaily error' => $e->getMessage(), 'StatsQuestionnaireAnswerDaily' => $statsAnswerDaily]);
                         continue;
                     }
                 }
             }
         }
         ModelStatsQuestionnaireAnswerDaily::batchInsert($statsRows);
         $skip += $limit;
         $query = $query->offset($skip)->limit($limit);
         //free $questionnaires
         unset($questionnaires);
         $questionnaires = $query->all();
     }
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $accountId = $args['accountId'];
     //Assume that the subChannel is the secode element in properties
     $propertyKey = $args['properties'][1];
     $memberProperty = MemberProperty::findOne(['propertyId' => $propertyKey]);
     if ($memberProperty != null) {
         return ModelStatsMemberPropTradeCodeQuarterly::generateByYearAndQuarter((string) $memberProperty['_id'], $accountId, $datetime);
     } else {
         ResqueUtil::log("Fail to get memberProperty with propertyId {$propertyKey}");
     }
     return false;
 }
 public function perform()
 {
     //accountId, properties are required fileds
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['properties'])) {
         ResqueUtil::log('Missing required arguments accountId or properties!');
         return false;
     }
     $date = empty($args['date']) ? '' : $args['date'];
     $datetime = TimeUtil::getDatetime($date);
     $dateStr = date('Y-m-d', $datetime);
     $start = new MongoDate($datetime);
     $end = new MongoDate(strtotime('+1 day', $datetime));
     $accountId = new MongoId($args['accountId']);
     $condition = ['accountId' => $accountId, 'createdAt' => ['$gte' => $start, '$lt' => $end]];
     $campaignLogs = self::getCampaignLog($condition);
     if (empty($campaignLogs)) {
         LogUtil::info(['date' => date('Y-m-d H:i:s'), 'message' => $dateStr . ': campaignLogs is empty,no need to store data'], 'resque');
         return true;
     }
     //get all member info
     $memberInfos = self::getMemberInfo($condition);
     //Get all the property mongo id for comparison
     $condition = ['propertyId' => ['$in' => $args['properties']], 'accountId' => $accountId];
     $propertyIdStrs = self::getPropertyIds($condition);
     //Generate the meta data for inserting
     $statsRows = [];
     foreach ($campaignLogs as $campaignLog) {
         $campaignLog = $campaignLog['_id'];
         $redeemTime = self::getRedeemTime($campaignLog);
         //check the redeem time whether exists
         $condition = ['code' => $campaignLog['code'], 'productId' => $campaignLog['productId'], 'month' => date('Y-m', $redeemTime)];
         $memberCampaignLogDaily = ModelStatsMemberCampaignLogDaily::findOne($condition);
         if (empty($memberCampaignLogDaily)) {
             $memProperty = self::getProperty((string) $campaignLog['member']['id'], $memberInfos, $propertyIdStrs);
             $statsRows[] = ['memberId' => $campaignLog['member']['id'], 'memProperty' => $memProperty, 'productId' => $campaignLog['productId'], 'code' => $campaignLog['code'], 'year' => date('Y', $redeemTime), 'month' => date('Y-m', $redeemTime), 'quarter' => TimeUtil::getQuarter($redeemTime), 'accountId' => $accountId, 'createdAt' => new MongoDate(strtotime('+1 day', $datetime) - 1)];
         }
     }
     ModelStatsMemberCampaignLogDaily::batchInsert($statsRows);
     unset($statsRows, $memberCampaignLogDaily, $condition, $memProperty, $memberInfos);
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['properties'][0])) {
         ResqueUtil::log('Missing required arguments accountId or properties!');
         return false;
     }
     $accountId = new MongoId($args['accountId']);
     $property = $args['properties'][0];
     $memberProperty = MemberProperty::getByPropertyId($accountId, $property);
     if (empty($memberProperty)) {
         ResqueUtil::log('Can not find member property with propertyId:' . $property);
         return false;
     }
     $date = empty($args['date']) ? '' : $args['date'];
     $date = TimeUtil::getDatetime($date);
     $year = date('Y', $date);
     $quarter = TimeUtil::getQuarter($date);
     self::generateData($memberProperty, $property, $year, $quarter, $accountId);
     return true;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['properties'][0])) {
         ResqueUtil::log('Missing required arguments accountId or properties!');
         return false;
     }
     $accountId = new \MongoId($args['accountId']);
     $property = $args['properties'][0];
     $memberProperty = MemberProperty::findOne(['propertyId' => $property, 'accountId' => $accountId]);
     if (empty($memberProperty)) {
         ResqueUtil::log('Can not find member property with propertyId:' . $property);
         return false;
     }
     $date = empty($args['date']) ? '' : $args['date'];
     $date = TimeUtil::getDatetime($date);
     $month = date('Y-m', $date);
     $startDate = strtotime($month);
     $endDate = strtotime(date('Y-m', strtotime('+1 month', $date)));
     $raw = Member::getCollection()->aggregate([['$unwind' => '$properties'], ['$match' => ['createdAt' => ['$gte' => new \MongoDate($startDate), '$lt' => new \MongoDate($endDate)], 'properties.id' => $memberProperty->_id, 'accountId' => $accountId]], ['$group' => ['_id' => '$properties.value', 'total' => ['$sum' => 1]]]]);
     foreach ($raw as $item) {
         $total = $item['total'];
         $propValue = $item['_id'];
         // save the stats member property monthly
         $statsMemberPropMonthly = ModelStatsMemberPropMonthly::findOne(['propId' => $property, 'propValue' => $propValue, 'month' => $month, 'accountId' => $accountId]);
         if (empty($statsMemberPropMonthly)) {
             $statsMemberPropMonthly = new ModelStatsMemberPropMonthly();
             $statsMemberPropMonthly->propId = $property;
             $statsMemberPropMonthly->propValue = $propValue;
             $statsMemberPropMonthly->month = $month;
             $statsMemberPropMonthly->accountId = $accountId;
         }
         $statsMemberPropMonthly->total = $total;
         ResqueUtil::log($statsMemberPropMonthly->attributes);
         $statsMemberPropMonthly->save();
     }
     return true;
 }