All files / admin analytics-growth.js

100% Statements 15/15
100% Branches 2/2
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81              1x 1x 1x 1x         1x   1x 1x   1x 1x             18x       6x 6x     6x                         6x                                                           1x  
/**
 * Platform growth analytics (totals, daily trends, monthly active users)
 * GET /v1/platform-admin/analytics/growth
 *
 * @implements UC-ADMIN-002.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { getRecentMonths, getActiveUsersByMonth } = require('../utils/activeUsers')
const {
  getApproximateItemCount,
  getRecentDays,
  getDailyGrowth
} = require('../utils/platformGrowth')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const TREND_DAYS = 30
const ACTIVE_USERS_MONTHS = 12
 
/**
 * The stored snapshots stop at yesterday: today's point is the live total shown
 * in the tiles. A null total (no table count available) leaves the day out.
 */
function withToday(totalByDay, today, total) {
  return total === null ? totalByDay : { ...totalByDay, [today]: total }
}
 
async function analyticsGrowthHandler() {
  const days = getRecentDays(TREND_DAYS)
  const today = days[days.length - 1]
 
  const [totalUsers, totalFlows, totalEntities, totalItems, growth, activeUsersByMonth] =
    await Promise.all([
      getApproximateItemCount(client, process.env.USERS_TABLE_NAME),
      getApproximateItemCount(client, process.env.FLOWS_TABLE_NAME),
      getApproximateItemCount(client, process.env.ENTITIES_TABLE_NAME),
      getApproximateItemCount(client, process.env.ITEMS_TABLE_NAME),
      getDailyGrowth(docClient, process.env.PLATFORM_GROWTH_DAILY_TABLE_NAME, days),
      getActiveUsersByMonth(
        docClient,
        process.env.ACTIVE_USERS_MONTHLY_TABLE_NAME,
        getRecentMonths(ACTIVE_USERS_MONTHS)
      )
    ])
 
  return {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*'
    },
    body: JSON.stringify({
      totals: {
        users: totalUsers,
        flows: totalFlows,
        entities: totalEntities,
        items: totalItems
      },
      trend: {
        days: TREND_DAYS,
        signupsByDay: growth.signups,
        usersDeletedByDay: growth.usersDeleted,
        totalUsersByDay: withToday(growth.totalUsers, today, totalUsers),
        flowsCreatedByDay: growth.flowsCreated,
        flowsDeletedByDay: growth.flowsDeleted,
        totalFlowsByDay: withToday(growth.totalFlows, today, totalFlows),
        entitiesCreatedByDay: growth.entitiesCreated,
        entitiesDeletedByDay: growth.entitiesDeleted,
        totalEntitiesByDay: withToday(growth.totalEntities, today, totalEntities),
        activeUsersByMonth
      }
    })
  }
}
 
exports.handler = requirePlatformAdmin(analyticsGrowthHandler)