Skip to main content

Get Dashboard Statistics

curl -X GET https://api.sociapp.com/stats \
  -H "Authorization: Bearer YOUR_TOKEN"
{
  "socios": 156,
  "noSocios": 23,
  "actividades": 42,
  "proyectos": 18,
  "proyectosActivos": 12,
  "proyectosPendientes": 3,
  "trabajadores": 8
}

Endpoint

GET /stats

Authentication

Requires JWT authentication token with monitor or admin role. Required Roles: monitor, admin

Response Fields

socios
number
Total number of active members (users with socio = 1)
noSocios
number
Total number of non-members (users with socio = 2)
actividades
number
Total number of activities registered in the system
proyectos
number
Total number of projects (all statuses)
proyectosActivos
number
Number of projects with status “Activo”
proyectosPendientes
number
Number of projects with status “Pendiente”
trabajadores
number
Total number of workers (users with category “Trabajador”)

Caching

This endpoint implements intelligent caching to optimize performance:
  • Cache Key: dashboard_stats
  • TTL (Time To Live): 5 minutes (300,000 milliseconds)
  • Cache Behavior:
    • First request fetches data from the database and stores it in cache
    • Subsequent requests within 5 minutes return cached data
    • After 5 minutes, cache expires and next request refreshes the data
Benefits:
  • Reduces database load for frequently accessed statistics
  • Improves response time for dashboard loads
  • Ensures data is reasonably fresh (updated every 5 minutes)

Database Queries

The endpoint executes a single optimized aggregated query:
SELECT
  SUM(CASE WHEN socio = 1 THEN 1 ELSE 0 END) AS "socios",
  SUM(CASE WHEN socio = 2 THEN 1 ELSE 0 END) AS "noSocios",
  (SELECT COUNT(*) FROM actividades) AS "actividades",
  (SELECT COUNT(*) FROM proyectos) AS "proyectos",
  (SELECT COUNT(*) FROM proyectos WHERE estado = 'Activo') AS "proyectosActivos",
  (SELECT COUNT(*) FROM proyectos WHERE estado = 'Pendiente') AS "proyectosPendientes",
  (SELECT COUNT(*) FROM usuarios WHERE categoria = "Trabajador") AS "trabajadores"
FROM usuarios;

Security

  • Protected Endpoint: Only authenticated users can access
  • Role-Based Access: Requires monitor or admin role
  • JWT Guards: Implements both JwtAuthGuard and RolesGuard
  • Data Privacy: Statistics are aggregated counts with no personal information exposed

Use Cases

  1. Dashboard Loading: Display key metrics on the admin dashboard
  2. Reporting: Generate quick summary reports for board meetings
  3. Monitoring: Track growth trends (members, projects, activities)
  4. Health Checks: Verify system activity and engagement levels

Performance Notes

  • Optimized Query: Single aggregated query reduces database round trips
  • Cache Strategy: 5-minute cache significantly reduces database load
  • Scalability: Suitable for associations with thousands of members
  • Response Time: Typically under 50ms (cached) or under 200ms (uncached)

Example Usage

// Fetch and display dashboard stats
async function loadDashboard() {
  try {
    const response = await fetch('https://api.sociapp.com/stats', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });
    
    if (!response.ok) {
      throw new Error('Failed to fetch stats');
    }
    
    const stats = await response.json();
    
    // Update UI
    document.getElementById('members-count').textContent = stats.socios;
    document.getElementById('projects-count').textContent = stats.proyectos;
    document.getElementById('activities-count').textContent = stats.actividades;
    document.getElementById('workers-count').textContent = stats.trabajadores;
    
    console.log('Dashboard loaded:', stats);
  } catch (error) {
    console.error('Error loading dashboard:', error);
  }
}

Build docs developers (and LLMs) love