export async function getProfilesStats()

in src/app/functions/server/onerep.ts [542:587]


export async function getProfilesStats(
  from?: Date,
  to?: Date,
): Promise<ProfileStats | undefined> {
  const queryParams = new URLSearchParams();
  if (from) queryParams.set("from", from.toISOString().substring(0, 10));
  if (to) queryParams.set("to", to.toISOString().substring(0, 10));
  const queryParamsString = queryParams.toString();

  // check for cache map first
  if (profileStatsCache.has(queryParamsString))
    return profileStatsCache.get(queryParamsString);

  const response: Response = await onerepFetch(
    `/stats/profiles?${queryParamsString}`,
    {
      method: "GET",
    },
  );
  if (!response.ok) {
    logger.error(
      `Failed to fetch OneRep profile stats: [${response.status}] [${response.statusText}]`,
    );
    // throw new Error(
    //   `Failed to fetch OneRep profile stats: [${response.status}] [${response.statusText}]`,
    // );
  }

  try {
    const profileStats: ProfileStats = await response.json();

    // cache results in map, with a flush hack to keep the size low
    if (profileStatsCache.size > 5) profileStatsCache.clear();
    profileStatsCache.set(queryParamsString, profileStats);
    return profileStats;
  } catch (e) {
    if (e instanceof Error) {
      logger.error("failed_fetching_stats", {
        stack: e.stack,
        message: e.message,
      });
    } else {
      logger.error("failed_fetching_stats", { e });
    }
  }
}