Code Explainers

Code explainers tagged #aggregation

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
python
import re
from collections import defaultdict
from pathlib import Path
 

Summarizing log files by date in Python

regex parsing aggregation
Intermediate 7 steps
php
<?php
 
namespace App\Services;
 

Caching tenant dashboard metrics in Laravel

caching multi-tenancy aggregation
Intermediate 7 steps
python
import csv
from collections import defaultdict
from dataclasses import dataclass, field
from decimal import Decimal, InvalidOperation

Aggregating CSV sales by category in Python

dataclasses csv-parsing defaultdict
Intermediate 8 steps
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) {
    return orders.stream()
            .collect(Collectors.groupingBy(Order::getCustomerId));
}

Grouping streams with Java Collectors

streams grouping collectors
Intermediate 5 steps
typescript
interface Order {
  id: number;
  customerId: string;
  total: number;

A type-safe groupBy in TypeScript

generics reduce type-inference
Intermediate 6 steps
rust
use std::collections::HashMap;
 
#[derive(Debug, Clone)]
struct Order {

Aggregating Rust data with fold and entry

fold hashmap ownership
Intermediate 6 steps
python
from django.db.models import Sum, Count, F, DecimalField
from django.db.models.functions import TruncMonth, Coalesce
 
from .models import Order

Build a monthly revenue report in Django

aggregation orm group-by
Advanced 7 steps