eletrotupi / tcc/ commit / a6fd253

api/insights: daily energy reflects today only

The energy insight averaged a rolling seven days while labelling it today,
so it described neither. It now averages only today's mood entries and
stores today's start of day as its period
Pedro Lucas Porcellis porcellis@eletrotupi.com · 12 days ago a6fd2536de855fa5edadf135809eb4944828c6ab Browse files
Parents 5f12877
Changed files
2
Additions
+12
Deletions
-10
api/src/lib/queue/processors/insights/dailyEnergy.ts
1 1
import { prisma } from "@app/lib/prisma";
2 2
import { InsightType, InsightPeriod } from "@prisma/client";
3 3
import { InsightPeriodPayload } from "@app/lib/queue/types";
4 4
import { avg } from "@app/utils/calc";
5 +
import { appDayWindow } from "@app/lib/queue/processors/insights/utils";
5 6

6 7
export async function processDailyEnergy({
7 8
  userId,
8 9
  periodStart,
9 -
  periodEnd,
10 10
}: InsightPeriodPayload): Promise<void> {
11 -
  const start = new Date(periodStart);
12 -
  const end = new Date(periodEnd);
11 +
  const { start, end } = appDayWindow(new Date(periodStart));
13 12

14 13
  const moods = await prisma.mood.findMany({
15 -
    where: { userId, moment: { gte: start, lte: end } },
14 +
    where: { userId, moment: { gte: start, lt: end } },
16 15
    orderBy: { moment: "asc" },
17 16
    select: {
18 17
      energyLevel: true,
19 18
      stressLevel: true,
20 19
      anxietyLevel: true,
98 97
    return `Energia ${level} hoje, com melhora ao longo do dia.`;
99 98
  if (dayTrend < -10)
100 99
    return `Energia ${level} hoje, com queda ao longo do dia.`;
101 100
  return `Energia ${level} e estável ao longo do dia.`;
102 101
}
api/src/services/mood.service.ts
1 1
import { prisma } from "@app/lib/prisma";
2 2
import { Mood } from "@prisma/client";
3 -
import { rollingPeriod } from "@app/lib/queue/processors/insights/utils";
3 +
import {
4 +
  rollingPeriod,
5 +
  appDayPeriod,
6 +
} from "@app/lib/queue/processors/insights/utils";
4 7
import { InsightJobName } from "@app/lib/queue/types";
5 8
import { getQueue } from "@app/lib/queue";
6 9

7 10
import { CreateMoodInput } from "@app/schemas";
8 11

116 119

117 120
export const enqueueOnDemandInsights = async (
118 121
  userId: number,
119 122
): Promise<void> => {
120 123
  const queue = getQueue("insights");
121 -
  const period = rollingPeriod(7);
122 -
  const payload = { userId, ...period };
124 +
  const week = { userId, ...rollingPeriod(7) };
125 +
  const day = { userId, ...appDayPeriod() };
123 126

124 127
  await queue.addBulk([
125 128
    {
126 129
      name: InsightJobName.MoodTrend,
127 -
      data: payload,
130 +
      data: week,
128 131
      opts: {
129 132
        jobId: `mood-trend-${userId}-${new Date().toDateString()}`,
130 133
        removeOnComplete: true,
131 134
        removeOnFail: true,
132 135
      },
133 136
    },
134 137
    {
135 138
      name: InsightJobName.EnergySleep,
136 -
      data: payload,
139 +
      data: week,
137 140
      opts: {
138 141
        jobId: `energy-sleep-${userId}-${new Date().toDateString()}`,
139 142
        removeOnComplete: true,
140 143
        removeOnFail: true,
141 144
      },
142 145
    },
143 146
    {
144 147
      name: InsightJobName.DailyEnergy,
145 -
      data: payload,
148 +
      data: day,
146 149
      opts: {
147 150
        jobId: `daily-energy-${userId}-${new Date().toDateString()}`,
148 151
        removeOnComplete: true,
149 152
        removeOnFail: true,
150 153
      },
151 154
    },
152 155
  ]);
153 156
};