private function addReport($options, $connection)
 {
     $report = new Report();
     $report->setDescription(self::generateDescription());
     $report->setTitle(implode(', ', array_keys($options['queries'])));
     $report->save();
     foreach ($options['queries'] as $key => $value) {
         $query = new Query();
         $query->setQuery($key);
         $query->save();
         $report_query = new ReportQuery();
         $report_query->setQuery($query);
         $report_query->setTitle($key);
         $report_query->setReport($report);
         $report_query->save();
         $date_start = strtotime($options['start_date']);
         $date_end = strtotime($options['end_date']);
         $start = $value['rand_start'];
         while ($date_start < $date_end) {
             if (rand(0, 100) > $value['rand']) {
                 $query_result = new QueryResultBulk();
                 $query_result->setQuery($query);
                 $start += rand($value['rand_min'], $value['rand_max']) * $value['rand_diff'];
                 $query_result->setResultSize($start);
                 $query_result->setCreatedAt($date_start);
                 $query_result->save();
             }
             $date_start = strtotime(date('Y-m-d', $date_start) . ' +1 days');
         }
         $sql = "DELETE FROM %s where date(%s) = '%s' and %s = %s";
         $sql = sprintf($sql, QueryResultPeer::TABLE_NAME, QueryResultPeer::RESULT_DATE, date('Y-m-d'), QueryResultPeer::QUERY_ID, $query->getId());
         $statement = $connection->prepareStatement($sql);
         $statement->executeQuery();
     }
     $tags = self::generateTags();
     foreach ($tags as $tag) {
         $tag->setReport($report);
         $tag->save();
     }
 }
Example #2
0
function ajax_edit_query()
{
    if ($_POST['qid']) {
        $query = new Query($_POST['qid']);
    } else {
        $query = new Query();
    }
    $query->name = $_POST['name'];
    if (!empty($_POST['tags'])) {
        $query->tags = $_POST['tags'];
    }
    if (!empty($_POST['users'])) {
        $query->users = $_POST['users'];
    }
    if (!empty($_POST['tracks'])) {
        $query->tracks = $_POST['tracks'];
    }
    $query->save();
    print json_encode($query);
}
Example #3
0
 public function submitQuery()
 {
     $validator = Validator::make(Input::all(), array('query' => array('required', 'query'), 'servers' => array('required', 'array'), 'type' => array('required', 'type')));
     if ($validator->fails()) {
         return Response::json(array('success' => false, 'error' => $validator->messages()->first()));
     }
     // Validate rate limit
     $ip = Request::getClientIp();
     $querylimit = Config::get('lowendping.ratelimit.queries');
     if (!empty($querylimit)) {
         $limit = RateLimit::find($ip);
         if ($limit) {
             $time = time();
             $expiration = (int) ($limit->time + Config::get('lowendping.ratelimit.timespan'));
             if ($expiration > $time) {
                 if ($limit->hits >= $querylimit) {
                     $reset = ($expiration - $time) / 60;
                     if ($reset <= 1) {
                         return Response::json(array('success' => false, 'error' => 'Rate limit exceeded, please try again in 1 minute.'));
                     }
                     return Response::json(array('success' => false, 'error' => 'Rate limit exceeded, please try again in ' . $reset . ' minutes.'));
                 }
                 $limit->hits++;
                 $limit->save();
             } else {
                 $limit->time = time();
                 $limit->hits = 1;
                 $limit->save();
             }
         } else {
             $limit = new RateLimit();
             $limit->ip = $ip;
             $limit->hits = 1;
             $limit->time = time();
             $limit->save();
         }
     }
     $servers = Config::get('lowendping.servers');
     $serverIds = array();
     // Validate servers
     foreach (Input::get('servers') as $id => $val) {
         if (!isset($servers[$id])) {
             return Response::json(array('success' => false, 'error' => 'Invalid server ' . $id));
         }
         $serverIds[] = $id;
     }
     // Process the query
     $query = Input::get('query');
     $q = new Query();
     $q->query = $query;
     $q->servers = serialize($serverIds);
     $q->save();
     Queue::push('QueryJob', array('id' => $q->id, 'query' => $query, 'type' => Input::get('type'), 'servers' => $serverIds));
     $response = array('success' => true, 'queryid' => $q->id, 'serverCount' => count($serverIds));
     if (Config::get('lowendping.websocket.enabled', false)) {
         $response['websocket'] = websocket_url(Config::get('lowendping.websocket.path', ''), Config::get('lowendping.websocket.proxied', false) ? false : Config::get('lowendping.websocket.port', 8080));
     }
     if (Config::get('lowendping.archive.enabled', false)) {
         $response['resultLink'] = action('HomeController@showResult', array('query' => $q->id));
     }
     return Response::json($response);
 }
Example #4
0
 public function actionAjax()
 {
     //this function does one of three things depending on payload
     //would be better in hindsight to send an "action" command
     //if we want to save the query
     if (isset($_POST['save'])) {
         //if id is set, there we are updating, else it's a new query
         if (isset($_POST['query_id']) && (int) $_POST['query_id']) {
             //we are updating
             $Query = Query::model()->findByPk($_POST['query_id']);
         } else {
             $Query = new Query();
             $Query->created = date("Y-m-d H:i:s");
         }
         // Is this an invite query?
         $Query->invite = isset($_POST['invite']) ? 1 : 0;
         $Query->user_id = Yii::app()->user->id;
         $Query->name = $_POST['Query']['name'];
         $Query->description = $_POST['Query']['description'];
         $Query->JSON = $this->getQueryJSON();
         if ($Query->save()) {
             if (!$_POST['query_id']) {
                 // Creating a Query or an Invite?
                 if ($Query->invite) {
                     // Create a campaign to go with this Query - it has to have one
                     $Campaign = new Campaign();
                     $Campaign->name = $Query->name;
                     $Campaign->description = $Query->description;
                     $Campaign->query_id = $Query->id;
                     $Campaign->status = Campaign::STATUS_NOT_RUN;
                     $Campaign->processing = 0;
                     if ($Campaign->save()) {
                         $errorOccured = false;
                         // Everything is saved ok. Now run the query and get all the contacts
                         $queryResults = $Query->runInviteContactQuery();
                         // loop array and add each one to invite table
                         foreach ($queryResults['rows'] as $contact) {
                             // Create a new Invite model
                             $Invite = new Invite();
                             $Invite->contact_warehouse_id = $contact['contact_warehouse_id'];
                             $Invite->store2contact_id = $contact['store2contact_id'];
                             $Invite->store_id = $contact['store_id'];
                             $Invite->organisation_id = $contact['origin_organisation_id'];
                             $Invite->hash = sha1($contact['contact_warehouse_id'] . $contact['store2contact_id'] . $contact['origin_organisation_id'] . microtime(true) . SHASALT);
                             $Invite->date = date('Y-m-d H:i:s');
                             $Invite->query_id = $Campaign->query_id;
                             $Invite->campaign_id = $Campaign->id;
                             $Invite->status = Invite::STATUS_UNSENT;
                             $Invite->processing = 0;
                             if (!$Invite->save()) {
                                 $errorOccured = true;
                                 $errors = print_r($Invite->errors, true);
                                 Yii::log('Error saving Invite model: ' . $errors, 'error');
                             } else {
                             }
                             unset($Invite);
                         }
                         if ($errorOccured) {
                             mail('*****@*****.**', 'Website Error', 'Invite attempted Invite model could not be saved. See Application logs.');
                         }
                         $Query->num_contacts = sizeof($queryResults['rows']);
                         $Query->save(true, array('num_contacts'));
                         // new. set flash then return request to redirect.
                         Yii::app()->user->setFlash('success', 'The new invite has been created successfully.');
                         $array = array('success' => true, 'redirect' => $this->createUrl('invite/index'));
                     } else {
                         throw new CHttpException('500', 'Error saving campaign');
                     }
                 } else {
                     // new. set flash then return request to redirect.
                     //Run query to get count to save.
                     $queryResults = $Query->runCampaignCountQuery();
                     $Query->num_contacts = $queryResults['count'];
                     $Query->save(true, array('num_contacts'));
                     Yii::app()->user->setFlash('success', 'The new query has been created successfully.');
                     $array = array('success' => true, 'redirect' => $this->createUrl('query/update', array('id' => $Query->id)));
                 }
             } else {
                 $queryResults = $Query->runCampaignCountQuery();
                 $Query->num_contacts = $queryResults['count'];
                 $Query->save(true, array('num_contacts'));
                 $array = array("success" => true, 'id' => $Query->id, 'resultsTotal' => number_format($queryResults['count']));
             }
         } else {
             $array = array("errors" => $Query->getErrors());
         }
         header('Content-Type: application/json');
         print CJSON::encode($array);
     } else {
         if (isset($_POST['new-row'])) {
             $rowNumber = time();
             $Question = QueryQuestion::model()->findByPk($_POST['new']['query_choice']);
             $QueryQuestions = QueryQuestion::model()->findAll(array('order' => 'type,id'));
             header('Content-Type: application/json');
             print json_encode(array('html' => $this->renderPartial('_row', array('Question' => $Question, 'QueryQuestions' => $QueryQuestions, 'and_choice' => $_POST['new']['and_choice'], 'bool_choice' => $_POST['new']['bool_choice'], 'query_choice' => $_POST['new']['query_choice'], 'query_number' => $_POST['new']['query_number'], 'query_option' => $_POST['new']['query_option'], 'rowNumber' => $rowNumber), true)));
         } else {
             if (isset($_POST['render'])) {
                 //just render the question options
                 //get the query question with that id
                 $Question = QueryQuestion::model()->findByPk($_POST['id']);
                 //render partial
                 $this->renderPartial('_options', array('Question' => $Question, 'rowNumber' => $_POST['rowNumber']));
             } elseif (isset($_POST['results'])) {
                 if ($_POST['query_id']) {
                     $Query = Query::model()->findByPk($_POST['query_id']);
                     if (is_null($Query)) {
                         throw new CHttpException(404, 'Not found');
                     }
                     $Query->JSON = $this->getQueryJSON();
                 } else {
                     $Query = new Query();
                     $Query->JSON = $this->getQueryJSON();
                 }
                 if (isset($_POST['invite'])) {
                     $queryResults = $Query->runInviteCountQuery();
                 } else {
                     $queryResults = $Query->runCampaignCountQuery();
                 }
                 header('Content-Type: application/json');
                 $queryResults['results'] = number_format($queryResults['count']);
                 unset($queryResults['rows']);
                 // Don't need rows on query page, just extra HTTP traffic
                 print json_encode($queryResults);
             } else {
                 throw new CHttpException(404, 'The requested page does not exist.');
             }
         }
     }
 }
Example #5
0
 public function save()
 {
     $query = new Query();
     $query->save($this);
 }