Skip to content

Instantly share code, notes, and snippets.

@macedd
Created June 22, 2022 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macedd/a337e0739bc2a821c4176de49ff4b3f1 to your computer and use it in GitHub Desktop.
Save macedd/a337e0739bc2a821c4176de49ff4b3f1 to your computer and use it in GitHub Desktop.
Laravel test for aggregating analytical data on software
<?php
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
/**
* Laravel command to calculate and cache logs statistics
*/
class CalculateStatisticsCommand extends Command {
public function handle() {
// accumulator of the counts batches
$all_counts = collect([]);
// loop log items (in batch) in order to count each country code
// todo: possibly can be optimized to aggregate the json data in mysql itself (done in laravel for experimentation purpose)
DB::table('logs')
->orderBy('id')
->chunk(500, function ($logs) use (&$all_counts) {
// parses each log row and aggregates by country code
$counts = $logs->countBy(function($log) {
// todo: could be optimized to get countryCode from regex instead of parsing the whole json
$params = json_decode($log->params);
return $params->countryCode;
});
// stores aggregation count for this batch in the accumulator variable
$all_counts->mergeRecursive($counts);
});
// sums aggregated values for final result (not sure the data format applies for mapWithKeys, would be sure by testing with a dataset :))
$final_counts = $all_counts->mapWithKeys(function ($item, $key) {
if (is_array($item)) {
$sum = array_sum($item);
} else {
$sum = $item;
}
return [$key => $sum];
});
// stores the resulting data in the cache store
Cache::put('statistics-logs-countryCode', $final_counts);
}
}
<?php
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
class StatisticsController extends Controller {
public function dashboard() {
$countryCode = Cache::get('statistics-logs-countryCode');
return view('statistics.dashboard', [
'statistics' => [
'countryCode' => $countryCode,
],
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment