Skip to content

Commit

Permalink
Growth support y Growth opportunities graph
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMendozaPrado committed Jun 3, 2024
1 parent 6faa5d1 commit 04dfbd7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
45 changes: 17 additions & 28 deletions app/(base)/projects/[projectId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getUserRole } from "@/services/user";
import {
getOverallStatistics,
getDetailedProjectStatistics,
getGrowthData, // Importar el nuevo servicio
} from "@/services/sprintSurvey";

import ChevronDownIcon from "@/components/icons/ChevronDownIcon";
Expand Down Expand Up @@ -56,6 +57,11 @@ const Project = ({ params }: { params: { projectId: string } }) => {
queryKey: ["project-detailed-statistics", projectId],
queryFn: () => getDetailedProjectStatistics(projectId),
});

const { data: growthData, isLoading: isLoadingGrowthData } = useQuery({
queryKey: ["project-growth-data", projectId],
queryFn: () => getGrowthData(projectId), // Consumir el nuevo servicio
});
const radarData = statistics
? [
{
Expand All @@ -80,33 +86,15 @@ const Project = ({ params }: { params: { projectId: string } }) => {
},
]
: [];
const areaData = [
{
month: "Jan",
growthSupport: 89,
growthOportunities: 70,
},
{
month: "Feb",
growthSupport: 76,
growthOportunities: 82,
},
{
month: "Mar",
growthSupport: 95,
growthOportunities: 89,
},
{
month: "Apr",
growthSupport: 72,
growthOportunities: 85,
},
{
month: "May",
growthSupport: 83,
growthOportunities: 78,
},
];
const areaData = growthData
? growthData.growthSupportData.map((item, index) => ({
month: item.month,
growthSupport: item.averageAnswer,
growthOportunities:
growthData.growthOpportunitiesData[index]?.averageAnswer || 0,
}))
: [];

const gaugeData = detailedStatistics
? [
{
Expand Down Expand Up @@ -140,7 +128,8 @@ const Project = ({ params }: { params: { projectId: string } }) => {
if (
isLoadingStatistics ||
isLoadingDetailedStatistics ||
isLoadingProjectData
isLoadingProjectData ||
isLoadingGrowthData // Agregar el estado de carga de los datos de crecimiento
) {
return (
<div className="h-[80dvh]">
Expand Down
3 changes: 3 additions & 0 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ export const finalSurveyAnswer = pgTable(
}),
questionId: integer("question_id").references(() => question.id),
answer: integer("answer"),
answeredAt: date("answered_at", { mode: "date" }).default(
sql`CURRENT_TIMESTAMP::date`,
), // Nueva columna
comment: text("comment"),
},
// composite primary key on (userId, finalSurveyId)
Expand Down
68 changes: 67 additions & 1 deletion services/sprintSurvey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
projectMember,
finalSurveyAnswer,
} from "@/db/schema";
import { eq, or, sql, and } from "drizzle-orm";
import { eq, or, sql, and, inArray } from "drizzle-orm";

import {
Questions,
Expand Down Expand Up @@ -320,3 +320,69 @@ export async function getDetailedProjectStatistics(projectId: number) {
resourcesSatisfaction,
};
}

export const getGrowthData = async (projectId: number) => {
const growthSupportQuestions = [27, 37, 38]; // Preguntas relacionadas con soporte de crecimiento
const growthOpportunitiesQuestions = [37, 38]; // Preguntas relacionadas con oportunidades de crecimiento

// Obtener los datos de soporte de crecimiento
const growthSupportDataResults = await db
.select({
month: sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`.as(
"month",
),
averageAnswer: sql`AVG(${finalSurveyAnswer.answer})`.as("averageAnswer"),
})
.from(finalSurveyAnswer)
.innerJoin(
projectMember,
eq(finalSurveyAnswer.userId, projectMember.userId),
)
.where(
and(
eq(projectMember.projectId, projectId),
inArray(finalSurveyAnswer.questionId, growthSupportQuestions),
),
)
.groupBy(sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`)
.execute();

const growthSupportData = growthSupportDataResults.map((result) => ({
month: result.month,
averageAnswer: result.averageAnswer,
}));

// Obtener los datos de oportunidades de crecimiento
const growthOpportunitiesDataResults = await db
.select({
month: sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`.as(
"month",
),
averageAnswer: sql`AVG(${finalSurveyAnswer.answer})`.as("averageAnswer"),
})
.from(finalSurveyAnswer)
.innerJoin(
projectMember,
eq(finalSurveyAnswer.userId, projectMember.userId),
)
.where(
and(
eq(projectMember.projectId, projectId),
inArray(finalSurveyAnswer.questionId, growthOpportunitiesQuestions),
),
)
.groupBy(sql`EXTRACT(MONTH FROM ${finalSurveyAnswer.answeredAt})`)
.execute();

const growthOpportunitiesData = growthOpportunitiesDataResults.map(
(result) => ({
month: result.month,
averageAnswer: result.averageAnswer,
}),
);

return {
growthSupportData,
growthOpportunitiesData,
};
};

0 comments on commit 04dfbd7

Please sign in to comment.