/** * Run the database seeds. * * @return void */ public function run() { DB::table('counters')->delete(); Counter::create(['name' => 'total_views', 'count' => 0]); Counter::create(['name' => 'logged_in_views', 'count' => 0]); Counter::create(['name' => 'task_deletes', 'count' => 0]); Counter::create(['name' => 'task_creates', 'count' => 0]); }
/** * Execute the console command. * * @return mixed */ public function handle() { $addValue = $this->option('add'); if ($addValue > 0) { $count = Counter::where('name', 'total_views')->first(); $count->count += $this->option('add'); $count->save(); $this->info("Done! Feels good, doesn't it?"); } else { if ($addValue == 0) { $this->info("Can't add 0 views..."); } else { $this->info("Can't add negative views... nice try!"); } } }
public function combineCommonUnits($results, $units_set, $urls_count) { $lemmatizator = new Lemmatizator(); $combined_result = []; $entries_sums = []; //is neccessary for bodies counting $zones = $this->allZones(); $queries = array_keys($results); $counter = new Counter(); foreach ($zones as $zone) { foreach ($units_set as $unit) { foreach ($queries as $query) { if (isset($results[$query][$zone][$unit])) { $combined_result[$zone][$unit]['entries'][] = $results[$query][$zone][$unit]; //count sums for bodies counting if ($zone != 'title') { //titles are not included in bodies counting foreach ($results[$query][$zone][$unit] as $url_num => $entry_count) { $entries_sums[$unit][$query][$url_num] = isset($entries_sums[$unit][$query][$url_num]) ? $entries_sums[$unit][$query][$url_num] + $entry_count : $entry_count; } } //get the averages array only for those sets which has been realy counted for entries $combined_result[$zone][$unit]['average'][] = $counter->getAverage($results[$query][$zone][$unit]); } else { //make the false sets of entries for those which have not been realy counted for entries $combined_result[$zone][$unit]['entries'][] = array_fill(0, $urls_count[$query], 0); } } } } // correct the extra FORMs //======================== //get the list of 'asis' units from units list //use the fact that we need those which have not no FORMs form right after the asis form $asis_to_correct_set = []; foreach ($units_set as $key => $unit) { if (strpos($unit, 'FORMS') === false && (!isset($units_set[$key + 1]) || strpos($units_set[$key + 1], 'FORMS') === false)) { $asis_to_correct_set[] = $unit; } } //get the list of FORMs units that should be corrected //to do this proceed each from asis list with makeUnitNormal function $forms_to_correct_set = []; foreach ($asis_to_correct_set as $asis) { $asis_normal = $lemmatizator->makeUnitNormal($asis); $asis_normal .= " - FORMS"; $forms_to_correct_set[$asis_normal][] = $asis; } //make the array FORMs-unit => all it's asis units foreach (array_keys($forms_to_correct_set) as $asis_normal) { $more_key = array_search($asis_normal, $units_set); array_unshift($forms_to_correct_set[$asis_normal], $units_set[$more_key - 1]); } //prepare factors and divider data for each FORMs-unit (with zone) $correct_factors = []; foreach (array_keys($forms_to_correct_set) as $form) { foreach ($zones as $zone) { $correct_factors[$zone][$form]['divider'] = count($combined_result[$zone][$form]['average']); foreach ($forms_to_correct_set[$form] as $asis) { $correct_factors[$zone][$form]['factors'][$asis] = $correct_factors[$zone][$form]['divider'] - count($combined_result[$zone][$asis]['average']); } } } //not the end of correctness procedure -> to be continued //======================================================= //start the combine averages procedure foreach ($zones as $zone) { foreach ($units_set as $unit) { $entries_flattened = []; foreach ($combined_result[$zone][$unit]['entries'] as $arr) { $entries_flattened = array_merge($entries_flattened, $arr); } // file_put_contents(storage_path("tmp/results.txt"), print_r($combined_result['plain']['КОШКА - FORMS'], true),FILE_APPEND);//FOR TESTING !!! $combined_result[$zone][$unit]['average'] = $counter->getAverage($combined_result[$zone][$unit]['average']); $combined_result[$zone][$unit]['entries'] = $entries_flattened; } } //end of combine averages procedure //================================== //continue the correctness procedure //get the amendment for each zone-FORMSunit foreach (array_keys($forms_to_correct_set) as $form) { foreach ($zones as $zone) { $dividend = 0; foreach ($forms_to_correct_set[$form] as $asis) { $summand = $correct_factors[$zone][$form]['factors'][$asis] * $combined_result[$zone][$asis]['average']; $dividend += $summand; } $correct_factors[$zone][$form]['amendment'] = -1.0 * round($dividend / $correct_factors[$zone][$form]['divider'], 0, PHP_ROUND_HALF_UP); } } //correct the averages in combined result foreach ($correct_factors as $zone => $form_array) { foreach ($form_array as $form => $factors_array) { $combined_result[$zone][$form]['average'] += $factors_array['amendment']; if ($combined_result[$zone][$form]['average'] < 0) { $combined_result[$zone][$form]['average'] = 0; } } } //start the bodies count procedure //================================ $bodies = []; foreach ($entries_sums as $unit => $query_sums) { $bodies[$unit] = []; foreach ($query_sums as $query => $sums) { $bodies[$unit] = array_merge($bodies[$unit], $sums); } $bodies[$unit] = $counter->getAverage($bodies[$unit]); // $body_amendment = 0; foreach ($zones as $zone) { if ($zone != 'title') { // $bodies[$unit] = $bodies[$unit]; $bodies[$unit] = $bodies[$unit] - $combined_result[$zone][$unit]['average']; } } //get the average amendment from all zones foreach ($correct_factors as $zone => $form_array) { if (isset($correct_factors[$zone][$unit]) && $zone != 'title') { $body_amendment = isset($body_amendment) ? $body_amendment += $correct_factors[$zone][$unit]['amendment'] : $correct_factors[$zone][$unit]['amendment']; } } // $body_amendment = $body_amendment / 4.0; if (isset($body_amendment)) { $body_amendment = round($body_amendment, 0, PHP_ROUND_HALF_UP); //average for 4 zones w/o title $bodies[$unit] += $body_amendment; if ($bodies[$unit] < 0) { $bodies[$unit] = 0; } } //correct the body amount with average amendment } return [$combined_result, $bodies]; }
/** * Destroy the given task. * * @param Request $request * @param Task $task * @return Response */ public function destroy(Request $request, Counter $counter) { $counter->delete(); $request->session()->flash('alert-success', 'Counter was successful deleted!'); $page = $request->session()->get('counter_page'); return redirect('/counters?page=' . $page); }
public static function taskDeletesCount() { return Counter::where('name', 'task_deletes')->first(); }
public function storeResultsAdmin($result_for_storing, $bodies, $sample_combined_result, $project, $words, $words_sample) { //names for two different files - for admin usage and simple for client usage $filename = 'results_admin'; $client_filename = 'results'; $storage_file = $project->storage_path() . $filename; if (file_exists($storage_file)) { unlink($storage_file); } //пустые массивы для организации исходных данных с плоской структурой пригодной для записи в таблицы $data_for_storing = []; //admin usage data with each entry at each handled url $client_for_storing = []; //simplified results for client usage foreach ($result_for_storing as $zone => $units_array) { foreach ($units_array as $unit => $data_array) { $data_for_storing[$unit][0] = $unit; $client_for_storing[$unit][0] = $unit; $data_for_storing[$unit]['body'] = $bodies[$unit]; $client_for_storing[$unit]['body'] = $bodies[$unit]; $data_for_storing[$unit][$zone] = $result_for_storing[$zone][$unit]['average']; $client_for_storing[$unit][$zone] = $result_for_storing[$zone][$unit]['average']; //add the each url entries - only for admin usage foreach ($result_for_storing[$zone][$unit]['entries'] as $key => $entry_value) { $data_for_storing[$unit][substr($zone, 0, 1) . "-" . $key] = $result_for_storing[$zone][$unit]['entries'][$key]; } } } foreach ($sample_combined_result as $zone => $units) { foreach ($units as $unit => $val) { $data_for_storing[$unit][$zone . "-diff"] = $data_for_storing[$unit][$zone] - $val; $data_for_storing[$unit][$zone . "-sample"] = $val; $client_for_storing[$unit][$zone . "-diff"] = $data_for_storing[$unit][$zone] - $val; $client_for_storing[$unit][$zone . "-sample"] = $val; } } //remove from client version the strings containing only zeros foreach ($client_for_storing as $unit => $zone_entries) { //$zone_entries[0] is a string and may begin with digital $remove_pos = true; foreach ($zone_entries as $zone_entry) { if ($zone_entry != $unit && $zone_entry != 0) { $remove_pos = false; break; } } if ($remove_pos) { unset($client_for_storing[$unit]); } } //prepare words row for storing $words_admin = ['words count', ""]; $words_client = ['words count', ""]; $counter = new Counter(); foreach ($words as $zone => $res_arr) { $zone_words_avg[$zone] = $counter->getAverage(array_values($res_arr)); $words_admin[] = $zone_words_avg[$zone]; $words_client[] = $zone_words_avg[$zone]; foreach ($res_arr as $word_count) { $words_admin[] = $word_count; } } foreach ($words_sample as $zone => $value) { $words_admin[] = $zone_words_avg[$zone] - $value; $words_admin[] = $value; $words_client[] = $zone_words_avg[$zone] - $value; $words_client[] = $value; } //prepare urls row for storing $refs_lines = file(storage_path("tmp/" . $project->dir . "/reports/Handled_referencies.txt"), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $query = array_shift($refs_lines); $refs = []; $refs[$query] = []; while (true) { $line = array_shift($refs_lines); if (is_null($line)) { break; } if (strpos($line, 'http') !== false) { $refs[$query][] = $line; } else { $query = $line; $refs[$query] = []; } } $refs_admin = ["", "", ""]; foreach ($refs as $query => $urls_arr) { $refs_per_query[$query] = count($urls_arr); $refs_admin = array_merge($refs_admin, $urls_arr); } $refs_admin_add = array_slice($refs_admin, 2); $refs_admin = array_merge($refs_admin, $refs_admin_add, $refs_admin_add, $refs_admin_add, $refs_admin_add); // get as_is info $asis_info = $this->get_asis_info($client_for_storing, $refs_per_query, $project); // apply as_is info to client_for_storing info if (isset($asis_info['dops']) && !empty($asis_info['dops'])) { $asis_info['push_to'] = isset($asis_info['push_to']) ? array_merge($asis_info['push_to'], $asis_info['dops']) : $asis_info['dops']; } if (isset($asis_info['push_to'])) { foreach ($asis_info['push_to'] as $query) { if (isset($client_for_storing[$query])) { $client_for_storing[$query]['plain'] += 1; $client_for_storing[$query]['plain-diff'] += 1; } else { // $client_for_storing = array_merge( [$query=>['0'=>$query, // 'body' => 0, // 'plain' => 1, // 'fragment' => 0, // 'a' => 0, // 'h1' => 0, // 'title' => 0, // 'plain-diff' => 1, // 'plain-sample' => 0, // 'fragment-diff' => 0, // 'fragment-sample' => 0, // 'a-diff' => 0, // 'a-sample' => 0, // 'h1-diff' => 0, // 'h1-sample' => 0, // 'title-diff' => 0, // 'title-sample' => 0 // ]], $client_for_storing); $client_for_storing = [$query => ['0' => $query, 'body' => 0, 'plain' => 1, 'fragment' => 0, 'a' => 0, 'h1' => 0, 'title' => 0, 'plain-diff' => 1, 'plain-sample' => 0, 'fragment-diff' => 0, 'fragment-sample' => 0, 'a-diff' => 0, 'a-sample' => 0, 'h1-diff' => 0, 'h1-sample' => 0, 'title-diff' => 0, 'title-sample' => 0]] + $client_for_storing; } } } foreach ($asis_info['units'] as $unit) { $client_for_storing[$unit[0]][$unit[1]] += -1; if ($unit[1] == 'plain') { $client_for_storing[$unit[0]]['plain-diff'] += -1; } } //delete from client-report units with only "0" values //which can appear after subtractions foreach ($client_for_storing as $unit => $unit_arr) { if (array_unique(array_values($unit_arr)) == array($unit, 0)) { unset($client_for_storing[$unit]); } } //store to admin file $excel = \App::make('excel'); $storagefile = Excel::create($filename, function ($excel) use($data_for_storing, $project, $words_admin, $refs_admin, $refs_per_query, $asis_info) { $excel->sheet("Admin", function ($sheet) use($data_for_storing, $project, $words_admin, $refs_admin, $refs_per_query, $asis_info) { $sheet->getStyle("C1:C999")->getAlignment()->setWrapText(true); $sheet->freezeFirstRowAndColumn(); $sheet->fromArray($data_for_storing, null, 'A1', false, true); $sheet->prependRow(array($project->dir)); $row_num = 1; $sheet->setHeight($row_num, 20); $row_num++; $sheet->setHeight($row_num, 20); foreach ($data_for_storing as $unit => $arr) { $row_num++; $sheet->setHeight($row_num, 20); } $sheet->appendRow($row_num + 1, $words_admin); $sheet->setHeight($row_num + 1, 20); $sheet->appendRow($row_num + 2, $refs_admin); $sheet->setHeight($row_num + 2, 200); $sheet->row($row_num + 2, function ($row) { $row->setTextRotation(90); }); //merge cells for queries and prepare array for inserting $curr_column_index = 3; $query_admin = ["", "", ""]; for ($i = 1; $i <= 5; $i++) { //the same operation for five zones foreach ($refs_per_query as $query => $merge_length) { $start_cell_column = $sheet->getCellByColumnAndRow($curr_column_index, $row_num + 3)->getColumn(); $end_cell_column = $sheet->getCellByColumnAndRow($curr_column_index + $merge_length - 1, $row_num + 3)->getColumn(); $sheet->mergeCells($start_cell_column . ($row_num + 3) . ":" . $end_cell_column . ($row_num + 3)); $query_admin[] = $query; if ($merge_length - 1 > 0) { // to avoid E_WARNING: array_fill(): Number of elements must be positive - restrictment for php < 5.6 $query_admin = array_merge($query_admin, array_fill(0, $merge_length - 1, "")); } else { $query_admin = array_merge($query_admin, []); } $curr_column_index += $merge_length; } $curr_column_index++; $query_admin[] = ""; } $sheet->appendRow($row_num + 3, $query_admin); $sheet->setHeight($row_num + 3, 20); if (!empty($asis_info['push_to'])) { $sheet->prependRow(1, ["In addition it is strongly recommended" . "\n" . "to add to plain text 'as is' form" . "\n" . "of following queries:" . "\n" . "\n" . implode("\n", $asis_info['push_to'])]); $sheet->cell('A1', function ($cell) { $cell->setFontColor('#ff0000'); }); $sheet->setHeight(1, 70); } }); })->store('xls', $project->storage_path() . "/reports/", true); //store to client file $storagefile = Excel::create($client_filename, function ($excel) use($client_for_storing, $project, $words_client, $refs_per_query, $asis_info) { $excel->sheet("Admin", function ($sheet) use($client_for_storing, $project, $words_client, $refs_per_query, $asis_info) { $sheet->getStyle("C1:C999")->getAlignment()->setWrapText(true); $sheet->freezeFirstRowAndColumn(); $sheet->fromArray($client_for_storing, null, 'A1', false, true); //set fonts to red color for 'dop'-queries //to set red color also for forcibly added native queries //toggle comments from 'dops' to 'push_to' in 'if' and 'foreach' if (isset($asis_info['dops'])) { // if( isset($asis_info['push_to']) ){ foreach ($asis_info['dops'] as $query) { // foreach ($asis_info['push_to'] as $query) { $new_row_num = array_search($query, array_keys($client_for_storing)) + 2; if ($new_row_num !== false) { //вдруг удалили строку где-то по дороге $sheet->cell('A' . $new_row_num, function ($cell) { $cell->setFontColor('#ff0000'); }); $sheet->cell('C' . $new_row_num, function ($cell) { $cell->setFontColor('#ff0000'); }); $sheet->cell('H' . $new_row_num, function ($cell) { $cell->setFontColor('#ff0000'); }); } } } $sheet->prependRow(array($project->dir)); //подсветка измененных данных по юнитам in client report //uncomment to enable // if( isset($asis_info['units']) ){ // $column_zone = [ // 'body'=>'B', // 'body-diff'=>'B', // 'plain'=>'C', // 'fragment'=>'D', // 'a'=>'E', // 'h1'=>'F', // 'title'=>'G', // 'plain-diff'=>'H', // 'fragment-diff'=>'J', // 'a-diff'=>'L', // 'h1-diff'=>'N', // 'title-diff'=>'P', // ]; // foreach($asis_info['units'] as $unit){ // $new_row_num = array_search( $unit[0], array_keys($client_for_storing) ) + 3; // // file_put_contents("/var/www/test.txt", "C".$new_row_num."\n", FILE_APPEND); // $sheet->cell($column_zone[$unit[1]].$new_row_num, function($cell) { // $cell->setBackground('#f4904c'); // }); // $sheet->cell($column_zone[$unit[1].'-diff'].$new_row_num, function($cell) { // $cell->setBackground('#f4904c'); // }); // } // } $row_num = 1; $sheet->setHeight($row_num, 20); $row_num++; $sheet->setHeight($row_num, 20); foreach ($client_for_storing as $unit => $arr) { $row_num++; $sheet->setHeight($row_num, 20); } $sheet->appendRow($row_num + 1, $words_client); $sheet->setHeight($row_num + 1, 20); $queries = implode("\n", array_keys($refs_per_query)); $sheet->prependRow(1, [$queries]); $sheet->setHeight(1, 70); }); })->store('xls', $project->storage_path() . "/reports/", true); }