ajustes en las estadisticas
This commit is contained in:
@@ -127,6 +127,8 @@ export class TrainingService {
|
||||
genderResult,
|
||||
municipalityDistribution,
|
||||
parishDistribution,
|
||||
surveyStatusDistribution,
|
||||
isExportingDistribution,
|
||||
] = await Promise.all([
|
||||
// 1. Total OSPs
|
||||
this.drizzle
|
||||
@@ -307,6 +309,26 @@ export class TrainingService {
|
||||
.leftJoin(parishes, eq(trainingSurveys.parish, parishes.id))
|
||||
.where(whereCondition)
|
||||
.groupBy(parishes.name),
|
||||
|
||||
// 19. Distribución por Estatus de la Encuesta (Completada / Borrador)
|
||||
this.drizzle
|
||||
.select({
|
||||
name: trainingSurveys.surveyStatus,
|
||||
value: sql<number>`count(*)`,
|
||||
})
|
||||
.from(trainingSurveys)
|
||||
.where(whereCondition)
|
||||
.groupBy(trainingSurveys.surveyStatus),
|
||||
|
||||
// 20. Distribución por Exportación
|
||||
this.drizzle
|
||||
.select({
|
||||
name: sql<string>`case when ${trainingSurveys.isExporting} then 'Sí' else 'No' end`,
|
||||
value: sql<number>`count(*)`,
|
||||
})
|
||||
.from(trainingSurveys)
|
||||
.where(whereCondition)
|
||||
.groupBy(trainingSurveys.isExporting),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -374,6 +396,14 @@ export class TrainingService {
|
||||
...item,
|
||||
value: Number(item.value),
|
||||
})),
|
||||
surveyStatusDistribution: surveyStatusDistribution.map((item) => ({
|
||||
...item,
|
||||
value: Number(item.value),
|
||||
})),
|
||||
isExportingDistribution: isExportingDistribution.map((item) => ({
|
||||
...item,
|
||||
value: Number(item.value),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ export const StatisticsItems: NavItem[] = [
|
||||
url: '#', // Placeholder as there is no direct link for the parent
|
||||
icon: 'chartColumn',
|
||||
isActive: true,
|
||||
role: ['admin', 'superadmin', 'autoridad'],
|
||||
role: ['admin', 'superadmin', 'autoridad', 'manager'],
|
||||
|
||||
items: [
|
||||
// {
|
||||
@@ -82,7 +82,7 @@ export const StatisticsItems: NavItem[] = [
|
||||
shortcut: ['s', 's'],
|
||||
url: '/dashboard/estadisticas/socioproductiva',
|
||||
icon: 'blocks',
|
||||
role: ['admin', 'superadmin', 'autoridad'],
|
||||
role: ['admin', 'superadmin', 'autoridad', 'manager'],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -62,18 +62,31 @@ export function TrainingStatistics() {
|
||||
const { data: dataMunicipality } = useMunicipalityQuery(stateId);
|
||||
const { data: dataParish } = useParishQuery(municipalityId);
|
||||
|
||||
const stateOptions = dataState?.data || [{ id: 0, name: 'Sin estados' }];
|
||||
const municipalityOptions =
|
||||
Array.isArray(dataMunicipality?.data) && dataMunicipality.data.length > 0
|
||||
? dataMunicipality.data
|
||||
: [{ id: 0, stateId: 0, name: 'Sin Municipios' }];
|
||||
const parishOptions =
|
||||
Array.isArray(dataParish?.data) && dataParish.data.length > 0
|
||||
? dataParish.data
|
||||
: [{ id: 0, stateId: 0, name: 'Sin Parroquias' }];
|
||||
const ALL_OPTION = [{ value: 'all', label: 'Todos' }];
|
||||
|
||||
const stateOptions = [
|
||||
...ALL_OPTION,
|
||||
...(dataState?.data || []).map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
})),
|
||||
];
|
||||
const municipalityOptions = [
|
||||
...ALL_OPTION,
|
||||
...(Array.isArray(dataMunicipality?.data) ? dataMunicipality.data : []).map(
|
||||
(item) => ({ value: item.id.toString(), label: item.name }),
|
||||
),
|
||||
];
|
||||
const parishOptions = [
|
||||
...ALL_OPTION,
|
||||
...(Array.isArray(dataParish?.data) ? dataParish.data : []).map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
})),
|
||||
];
|
||||
|
||||
// Query with Filters
|
||||
const { data, isLoading, refetch } = useTrainingStatsQuery({
|
||||
const { data, isLoading } = useTrainingStatsQuery({
|
||||
startDate: startDate || undefined,
|
||||
endDate: endDate || undefined,
|
||||
stateId: stateId || undefined,
|
||||
@@ -135,25 +148,55 @@ export function TrainingStatistics() {
|
||||
);
|
||||
}
|
||||
|
||||
const toList = (value: unknown): any[] =>
|
||||
Array.isArray(value) ? value : [];
|
||||
|
||||
const raw = data ?? {};
|
||||
const totalOsps = raw.totalOsps ?? 0;
|
||||
const totalProducts = raw.totalProducts ?? 0;
|
||||
|
||||
const {
|
||||
totalOsps,
|
||||
// totalProducers,
|
||||
statusDistribution,
|
||||
activityDistribution,
|
||||
typeDistribution,
|
||||
stateDistribution,
|
||||
yearDistribution,
|
||||
ecoSectorDistribution,
|
||||
productiveSectorDistribution,
|
||||
centralActivityDistribution,
|
||||
mainActivityDistribution,
|
||||
structureTypeDistribution,
|
||||
isOpenSpaceDistribution,
|
||||
hasTransportDistribution,
|
||||
genderDistribution,
|
||||
municipalityDistribution,
|
||||
parishDistribution,
|
||||
} = data;
|
||||
statusDistribution: statusRaw,
|
||||
activityDistribution: activityRaw,
|
||||
typeDistribution: typeRaw,
|
||||
stateDistribution: stateRaw,
|
||||
yearDistribution: yearRaw,
|
||||
ecoSectorDistribution: ecoRaw,
|
||||
productiveSectorDistribution: prodRaw,
|
||||
centralActivityDistribution: centralRaw,
|
||||
mainActivityDistribution: mainRaw,
|
||||
structureTypeDistribution: structureRaw,
|
||||
isOpenSpaceDistribution: openRaw,
|
||||
hasTransportDistribution: transportRaw,
|
||||
genderDistribution: genderRaw,
|
||||
municipalityDistribution: municipalityRaw,
|
||||
parishDistribution: parishRaw,
|
||||
surveyStatusDistribution: surveyRaw,
|
||||
isExportingDistribution: exportingRaw,
|
||||
} = raw;
|
||||
|
||||
const statusDistribution = toList(statusRaw);
|
||||
const activityDistribution = toList(activityRaw);
|
||||
const typeDistribution = toList(typeRaw);
|
||||
const stateDistribution = toList(stateRaw);
|
||||
const yearDistribution = toList(yearRaw);
|
||||
const ecoSectorDistribution = toList(ecoRaw);
|
||||
const productiveSectorDistribution = toList(prodRaw);
|
||||
const centralActivityDistribution = toList(centralRaw);
|
||||
const mainActivityDistribution = toList(mainRaw);
|
||||
const structureTypeDistribution = toList(structureRaw);
|
||||
const isOpenSpaceDistribution = toList(openRaw);
|
||||
const hasTransportDistribution = toList(transportRaw);
|
||||
const genderDistribution = toList(genderRaw);
|
||||
const municipalityDistribution = toList(municipalityRaw);
|
||||
const parishDistribution = toList(parishRaw);
|
||||
const surveyStatusDistribution = toList(surveyRaw);
|
||||
const isExportingDistribution = toList(exportingRaw);
|
||||
|
||||
const totalCompleted =
|
||||
surveyStatusDistribution.find((s) => s.name === 'COMPLETADA')?.value ?? 0;
|
||||
const totalDrafts =
|
||||
surveyStatusDistribution.find((s) => s.name === 'BORRADOR')?.value ?? 0;
|
||||
|
||||
const COLORS = [
|
||||
'#0088FE',
|
||||
@@ -192,49 +235,44 @@ export function TrainingStatistics() {
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">Estado</label>
|
||||
<SelectSearchable
|
||||
options={stateOptions.map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
}))}
|
||||
onValueChange={(value: any) => {
|
||||
setStateId(Number(value));
|
||||
options={stateOptions}
|
||||
onValueChange={(value) => {
|
||||
setStateId(value === 'all' || value === null ? 0 : Number(value));
|
||||
setMunicipalityId(0); // Reset municipality
|
||||
setParishId(0); // Reset parish
|
||||
}}
|
||||
placeholder="Selecciona un estado"
|
||||
defaultValue={stateId ? stateId.toString() : ''}
|
||||
value={stateId ? stateId.toString() : 'all'}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">Municipio</label>
|
||||
<SelectSearchable
|
||||
options={municipalityOptions.map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
}))}
|
||||
onValueChange={(value: any) => {
|
||||
setMunicipalityId(Number(value));
|
||||
options={municipalityOptions}
|
||||
onValueChange={(value) => {
|
||||
setMunicipalityId(
|
||||
value === 'all' || value === null ? 0 : Number(value),
|
||||
);
|
||||
setParishId(0);
|
||||
}}
|
||||
placeholder="Selecciona municipio"
|
||||
defaultValue={municipalityId ? municipalityId.toString() : ''}
|
||||
value={municipalityId ? municipalityId.toString() : 'all'}
|
||||
disabled={!stateId || stateId === 0}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">Parroquia</label>
|
||||
<SelectSearchable
|
||||
options={parishOptions.map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
}))}
|
||||
onValueChange={(value: any) => setParishId(Number(value))}
|
||||
options={parishOptions}
|
||||
onValueChange={(value) =>
|
||||
setParishId(value === 'all' || value === null ? 0 : Number(value))
|
||||
}
|
||||
placeholder="Selecciona parroquia"
|
||||
defaultValue={parishId ? parishId.toString() : ''}
|
||||
value={parishId ? parishId.toString() : 'all'}
|
||||
disabled={!municipalityId || municipalityId === 0}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
{/* <div className="space-y-2">
|
||||
<label className="text-sm font-medium">Tipo de OSP</label>
|
||||
<Select value={ospType} onValueChange={setOspType}>
|
||||
<SelectTrigger>
|
||||
@@ -249,7 +287,7 @@ export function TrainingStatistics() {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div> */}
|
||||
<div className="flex items-end gap-2">
|
||||
<Button variant="outline" onClick={handleClearFilters}>
|
||||
Limpiar Filtros
|
||||
@@ -268,36 +306,88 @@ export function TrainingStatistics() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Statistics Cards */}
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
{/* Summary Cards */}
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Total de OSP Registradas
|
||||
Registradas
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{totalOsps}</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Organizaciones Socioproductivas
|
||||
OSP registradas
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* <Card>
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">
|
||||
Total de Productores
|
||||
</CardTitle>
|
||||
<CardTitle className="text-sm font-medium">Completadas</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{totalProducers}</div>
|
||||
<div className="text-2xl font-bold text-green-600">
|
||||
{totalCompleted}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Productores asociados
|
||||
Encuestas completadas
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card> */}
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">Borrador</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold text-orange-500">
|
||||
{totalDrafts}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Encuestas en borrador
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">Productos</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{totalProducts}</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Total de productos registrados
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Statistics Cards */}
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium">Mano de obra</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">
|
||||
{(() => {
|
||||
const mujeres =
|
||||
genderDistribution.find((g) => g.name === 'Mujeres')?.value ??
|
||||
0;
|
||||
const hombres =
|
||||
genderDistribution.find((g) => g.name === 'Hombres')?.value ??
|
||||
0;
|
||||
if (mujeres > hombres) return 'Mujeres';
|
||||
if (hombres > mujeres) return 'Hombres';
|
||||
return 'Empate';
|
||||
})()}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Fuerza laboral predominante
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card className="col-span-full">
|
||||
<CardHeader>
|
||||
@@ -635,8 +725,8 @@ export function TrainingStatistics() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* OPEN SPACE AND TRANSPORT (PIE CHARTS) */}
|
||||
<div className="col-span-full grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{/* OPEN SPACE, TRANSPORT AND EXPORTING (PIE CHARTS) */}
|
||||
<div className="col-span-full grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Espacio Abierto</CardTitle>
|
||||
@@ -700,6 +790,38 @@ export function TrainingStatistics() {
|
||||
</ResponsiveContainer>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Exportación</CardTitle>
|
||||
<CardDescription>¿Exportan sus productos?</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="h-[300px]">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<PieChart>
|
||||
<Pie
|
||||
data={isExportingDistribution}
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
innerRadius={60}
|
||||
outerRadius={80}
|
||||
fill="#8884d8"
|
||||
paddingAngle={5}
|
||||
dataKey="value"
|
||||
>
|
||||
{isExportingDistribution.map((entry, index) => (
|
||||
<Cell
|
||||
key={`cell-${index}`}
|
||||
fill={COLORS[(index + 6) % COLORS.length]}
|
||||
/>
|
||||
))}
|
||||
</Pie>
|
||||
<Tooltip wrapperStyle={{ color: '#000' }} />
|
||||
<Legend />
|
||||
</PieChart>
|
||||
</ResponsiveContainer>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,34 +4,43 @@ export const statisticsItemSchema = z.object({
|
||||
name: z
|
||||
.string()
|
||||
.nullable()
|
||||
.optional()
|
||||
.transform((val) => val || 'Sin Información'),
|
||||
value: z.number(),
|
||||
value: z.number().nullable().optional().default(0),
|
||||
});
|
||||
|
||||
const distributionSchema = z
|
||||
.array(statisticsItemSchema)
|
||||
.nullable()
|
||||
.optional()
|
||||
.default([]);
|
||||
|
||||
export const trainingStatisticsSchema = z.object({
|
||||
totalOsps: z.number(),
|
||||
totalOsps: z.number().nullable().optional().default(0),
|
||||
// totalProducers: z.number(),
|
||||
totalProducts: z.number(),
|
||||
statusDistribution: z.array(statisticsItemSchema),
|
||||
activityDistribution: z.array(statisticsItemSchema),
|
||||
typeDistribution: z.array(statisticsItemSchema),
|
||||
stateDistribution: z.array(statisticsItemSchema),
|
||||
yearDistribution: z.array(statisticsItemSchema),
|
||||
ecoSectorDistribution: z.array(statisticsItemSchema),
|
||||
productiveSectorDistribution: z.array(statisticsItemSchema),
|
||||
centralActivityDistribution: z.array(statisticsItemSchema),
|
||||
mainActivityDistribution: z.array(statisticsItemSchema),
|
||||
structureTypeDistribution: z.array(statisticsItemSchema),
|
||||
isOpenSpaceDistribution: z.array(statisticsItemSchema),
|
||||
hasTransportDistribution: z.array(statisticsItemSchema),
|
||||
genderDistribution: z.array(statisticsItemSchema),
|
||||
municipalityDistribution: z.array(statisticsItemSchema),
|
||||
parishDistribution: z.array(statisticsItemSchema),
|
||||
totalProducts: z.number().nullable().optional().default(0),
|
||||
statusDistribution: distributionSchema,
|
||||
activityDistribution: distributionSchema,
|
||||
typeDistribution: distributionSchema,
|
||||
stateDistribution: distributionSchema,
|
||||
yearDistribution: distributionSchema,
|
||||
ecoSectorDistribution: distributionSchema,
|
||||
productiveSectorDistribution: distributionSchema,
|
||||
centralActivityDistribution: distributionSchema,
|
||||
mainActivityDistribution: distributionSchema,
|
||||
structureTypeDistribution: distributionSchema,
|
||||
isOpenSpaceDistribution: distributionSchema,
|
||||
hasTransportDistribution: distributionSchema,
|
||||
genderDistribution: distributionSchema,
|
||||
municipalityDistribution: distributionSchema,
|
||||
parishDistribution: distributionSchema,
|
||||
surveyStatusDistribution: distributionSchema,
|
||||
isExportingDistribution: distributionSchema,
|
||||
});
|
||||
|
||||
export type TrainingStatisticsData = z.infer<typeof trainingStatisticsSchema>;
|
||||
|
||||
export const trainingStatisticsResponseSchema = z.object({
|
||||
message: z.string(),
|
||||
data: trainingStatisticsSchema,
|
||||
message: z.string().nullable().optional(),
|
||||
data: trainingStatisticsSchema.nullable().optional(),
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ function SelectSearchable({
|
||||
onValueChange,
|
||||
placeholder,
|
||||
defaultValue,
|
||||
value,
|
||||
disabled,
|
||||
enableNoneOption = false, // New prop to enable or disable "Ninguno"
|
||||
}: {
|
||||
@@ -18,6 +19,7 @@ function SelectSearchable({
|
||||
onValueChange: (value: string | null) => void;
|
||||
placeholder?: string;
|
||||
defaultValue?: string;
|
||||
value?: string;
|
||||
disabled?: boolean;
|
||||
enableNoneOption?: boolean;
|
||||
}) {
|
||||
@@ -33,7 +35,7 @@ function SelectSearchable({
|
||||
return (
|
||||
<SelectPrimitive.Root
|
||||
onValueChange={(value) => onValueChange(value === 'null' ? null : value)}
|
||||
defaultValue={defaultValue}
|
||||
{...(value !== undefined ? { value } : { defaultValue })}
|
||||
disabled={disabled}
|
||||
>
|
||||
<SelectPrimitive.Trigger
|
||||
|
||||
Reference in New Issue
Block a user