Code Explainers

Code explainers tagged #aggregation

ruby
class LogAggregator
  BUCKET_FORMAT = "%Y-%m-%dT%H:%M"
 
  def initialize(entries)

Bucketing log entries by the minute in Ruby

aggregation hashing enumerable
Intermediate 5 steps
ruby
class WeeklySignupsReport
  DEFAULT_WEEKS = 12
 
  def initialize(weeks: DEFAULT_WEEKS, source: User.all)

Building a weekly signups report in Rails

service object aggregation group by
Intermediate 7 steps
php
<?php
 
namespace App\Services;
 

Building a cached daily leaderboard in Laravel

caching aggregation eager-loading
Intermediate 9 steps
python
from django.db import models
from django.db.models import F, Sum, DecimalField
from django.db.models.functions import Coalesce
 

Recomputing order totals in Django

annotations aggregation custom-manager
Intermediate 9 steps
php
<?php
 
namespace App\Reports;
 

Bucketing spending by time period in Laravel

aggregation date-bucketing collections
Intermediate 9 steps
php
<?php
 
namespace App\Services\Analytics;
 

Building a signup chart with Laravel's query builder

aggregation date-ranges query-builder
Intermediate 6 steps
php
class Cart extends Model
{
    protected $fillable = ['user_id'];
 

Modeling a shopping cart in Laravel

eloquent relationships domain-model
Intermediate 7 steps
ruby
class ReportingQuery
  def initialize(scope = ApplicationRecord)
    @scope = scope
  end

Routing heavy reports to a read replica in Rails

read-replica database-roles query-object
Intermediate 6 steps
php
<?php
 
namespace App\Services\Reports;
 

Streaming a monthly revenue CSV in Laravel

csv-export lazy-collections eloquent-query
Intermediate 8 steps
php
public function customerTotals(Request $request): Collection
{
    return DB::table('orders')
        ->join('customers', 'customers.id', '=', 'orders.customer_id')

Building an aggregate report with Laravel's query builder

query builder aggregation conditional queries
Intermediate 8 steps
python
from collections import defaultdict
from datetime import datetime
from openpyxl import load_workbook
 

Summarizing an Excel sheet with openpyxl

excel-parsing aggregation type-dispatch
Intermediate 7 steps
ruby
def build_pivot_table(sales)
  pivot = sales.each_with_object(Hash.new { |h, k| h[k] = Hash.new(0.0) }) do |record, table|
    region = record.fetch(:region)
    quarter = record.fetch(:quarter)

Building a pivot table in Ruby

aggregation nested-hashes default-values
Intermediate 9 steps
java
package com.example.orders.repository;
 
import com.example.orders.domain.Order;
import org.springframework.data.jpa.repository.JpaRepository;

Projection interfaces in Spring Data JPA

projections jpql aggregation
Intermediate 7 steps
python
from django.core.cache import cache
from django.db.models import Count, Sum, Q
from django.utils import timezone
 

Caching a Django dashboard aggregate query

caching aggregation conditional-queries
Intermediate 7 steps
java
import java.util.List;
import java.util.IntSummaryStatistics;
 
public class OrderReport {

Aggregating stats with IntSummaryStatistics

streams aggregation records
Intermediate 5 steps
java
public List<String> collectTagsFromArticles(List<Article> articles) {
    return articles.stream()
        .flatMap(article -> article.getTags().stream())
        .map(String::trim)

Flattening nested data with Java streams

streams flatmap collectors
Intermediate 7 steps
rust
use std::collections::HashMap;
 
#[derive(Debug, Clone)]
struct Record {

Batched aggregation with fold in Rust

iterators fold hashmap
Intermediate 9 steps
ruby
class ReportGenerator
  def initialize(account, period)
    @account = account
    @period = period

Memoized report metrics in Ruby in Rails

memoization query-composition service-object
Intermediate 7 steps