Skip to main content

Overview

The Statistics API provides aggregated data for dashboard displays, including counts of PTEs, OTEs, production records, and total production volumes.

Get Statistics

Retrieve dashboard statistics. Endpoint: GET /operaciones/api/estadisticas/
curl -X GET "https://api.sascop-bme-subtec.com/operaciones/api/estadisticas/" \
  -H "Cookie: sessionid=your_session_id"

Response Fields

total_ptes
integer
Total count of all PTEs in the system
total_otes
integer
Total count of all OTEs (work orders) in the system
total_produccion
integer
Total count of production records
volumen_total
number
Aggregated sum of all production volumes across all records

Response Example

{
  "total_ptes": 45,
  "total_otes": 128,
  "total_produccion": 1543,
  "volumen_total": 25678.50
}

Source Code

URL Configuration (operaciones/urls.py:177)
path('api/estadisticas/', api.api_estadisticas, name='api_estadisticas'),
View Implementation (operaciones/views/api.py:7-24)
@login_required(login_url='/accounts/login/')
def api_estadisticas(request):
    """API para estadísticas del dashboard"""
    total_ptes = PTEHeader.objects.count()
    total_otes = OTE.objects.count()
    total_produccion = Produccion.objects.count()
    
    from django.db.models import Sum
    volumen_total = Produccion.objects.aggregate(
        total=Sum('volumen_produccion')
    )['total'] or 0
    
    return JsonResponse({
        'total_ptes': total_ptes,
        'total_otes': total_otes,
        'total_produccion': total_produccion,
        'volumen_total': float(volumen_total)
    })

Implementation Details

Database Queries

The endpoint executes the following queries:
  1. Count PTEs: PTEHeader.objects.count()
  2. Count OTEs: OTE.objects.count()
  3. Count Production: Produccion.objects.count()
  4. Sum Volumes: Produccion.objects.aggregate(total=Sum('volumen_produccion'))

Performance Considerations

This endpoint performs 4 database queries each time it’s called. Consider caching the results for frequently accessed dashboards.
Recommended Caching Strategy:
from django.core.cache import cache

def api_estadisticas(request):
    cache_key = 'dashboard_stats'
    stats = cache.get(cache_key)
    
    if stats is None:
        # Compute statistics
        stats = {
            'total_ptes': PTEHeader.objects.count(),
            'total_otes': OTE.objects.count(),
            'total_produccion': Produccion.objects.count(),
            'volumen_total': float(Produccion.objects.aggregate(
                total=Sum('volumen_produccion')
            )['total'] or 0)
        }
        
        # Cache for 5 minutes
        cache.set(cache_key, stats, 300)
    
    return JsonResponse(stats)

Use Cases

Dashboard Display

React Component
import { useEffect, useState } from 'react';
import axios from 'axios';

function DashboardStats() {
  const [stats, setStats] = useState(null);
  const [loading, setLoading] = useState(true);
  
  useEffect(() => {
    const fetchStats = async () => {
      try {
        const response = await axios.get('/operaciones/api/estadisticas/', {
          withCredentials: true
        });
        setStats(response.data);
      } catch (error) {
        console.error('Failed to fetch statistics:', error);
      } finally {
        setLoading(false);
      }
    };
    
    fetchStats();
    
    // Refresh every 5 minutes
    const interval = setInterval(fetchStats, 300000);
    return () => clearInterval(interval);
  }, []);
  
  if (loading) return <div>Loading...</div>;
  
  return (
    <div className="stats-grid">
      <StatCard title="PTEs" value={stats.total_ptes} />
      <StatCard title="OTEs" value={stats.total_otes} />
      <StatCard title="Production Records" value={stats.total_produccion} />
      <StatCard 
        title="Total Volume" 
        value={stats.volumen_total.toFixed(2)} 
      />
    </div>
  );
}

CLI Tool

Python CLI
import requests
import argparse
from tabulate import tabulate

def get_statistics(session_id):
    cookies = {'sessionid': session_id}
    response = requests.get(
        'https://api.sascop-bme-subtec.com/operaciones/api/estadisticas/',
        cookies=cookies
    )
    
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"Failed to fetch statistics: {response.status_code}")

def main():
    parser = argparse.ArgumentParser(description='Get SASCOP statistics')
    parser.add_argument('--session-id', required=True, help='Session ID')
    args = parser.parse_args()
    
    stats = get_statistics(args.session_id)
    
    table = [
        ['Metric', 'Value'],
        ['Total PTEs', stats['total_ptes']],
        ['Total OTEs', stats['total_otes']],
        ['Production Records', stats['total_produccion']],
        ['Total Volume', f"{stats['volumen_total']:.2f}"]
    ]
    
    print(tabulate(table, headers='firstrow', tablefmt='grid'))

if __name__ == '__main__':
    main()

Excel Report Generator

import requests
import openpyxl
from datetime import datetime

def generate_stats_report(session):
    # Fetch statistics
    response = session.get(
        'https://api.sascop-bme-subtec.com/operaciones/api/estadisticas/'
    )
    stats = response.json()
    
    # Create Excel workbook
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = 'Statistics'
    
    # Headers
    ws['A1'] = 'SASCOP BME SubTec - Statistics Report'
    ws['A2'] = f'Generated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}'
    
    # Data
    ws['A4'] = 'Metric'
    ws['B4'] = 'Value'
    
    ws['A5'] = 'Total PTEs'
    ws['B5'] = stats['total_ptes']
    
    ws['A6'] = 'Total OTEs'
    ws['B6'] = stats['total_otes']
    
    ws['A7'] = 'Production Records'
    ws['B7'] = stats['total_produccion']
    
    ws['A8'] = 'Total Volume'
    ws['B8'] = stats['volumen_total']
    
    # Save
    filename = f'stats_report_{datetime.now().strftime("%Y%m%d_%H%M%S")}.xlsx'
    wb.save(filename)
    print(f'Report saved: {filename}')

# Usage
session = requests.Session()
# ... login ...
generate_stats_report(session)

PTEHeader

Technical execution projects

OTE

Work orders

Produccion

Production records

Build docs developers (and LLMs) love