Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions supabase/functions/_backend/utils/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const TRACK_DEVICE_USAGE_CACHE_MAX_AGE_SECONDS = 29 * 24 * 60 * 60 // 29 days
* @param device_id - Unique device identifier
* @param app_id - Application identifier
* @param org_id - Organization identifier (optional, defaults to empty string)
* @param platform - Device platform ('ios' or 'android')
*/
export async function trackDeviceUsageCF(c: Context, device_id: string, app_id: string, org_id: string) {
export async function trackDeviceUsageCF(c: Context, device_id: string, app_id: string, org_id: string, platform: string) {
if (!c.env.DEVICE_USAGE)
return

Expand All @@ -67,9 +68,13 @@ export async function trackDeviceUsageCF(c: Context, device_id: string, app_id:
}
}

// Platform: 0 = android, 1 = ios
const platformValue = platform?.toLowerCase() === 'ios' ? 1 : 0

// Write to Analytics Engine
c.env.DEVICE_USAGE.writeDataPoint({
blobs: [device_id, org_id],
doubles: [platformValue],
indexes: [app_id],
})

Expand All @@ -79,9 +84,12 @@ export async function trackDeviceUsageCF(c: Context, device_id: string, app_id:
}
}
catch {
// Platform: 0 = android, 1 = ios
const platformValue = platform?.toLowerCase() === 'ios' ? 1 : 0
// On error, still try to write to Analytics Engine without caching
c.env.DEVICE_USAGE.writeDataPoint({
blobs: [device_id, org_id],
doubles: [platformValue],
indexes: [app_id],
})
}
Expand Down Expand Up @@ -810,7 +818,7 @@ export interface DevicesByPlatform {
}

export async function readLastMonthDevicesByPlatformCF(c: Context): Promise<DevicesByPlatform> {
if (!c.env.DEVICE_INFO) {
if (!c.env.DEVICE_USAGE) {
return { total: 0, ios: 0, android: 0 }
}

Expand All @@ -820,7 +828,7 @@ export async function readLastMonthDevicesByPlatformCF(c: Context): Promise<Devi
COUNT(DISTINCT blob1) AS total,
COUNT(DISTINCT CASE WHEN double1 = 1 THEN blob1 END) AS ios,
COUNT(DISTINCT CASE WHEN double1 = 0 THEN blob1 END) AS android
FROM device_info
FROM device_usage
WHERE timestamp >= toDateTime('${formatDateCF(oneMonthAgo)}')
AND timestamp < now()`

Expand Down
4 changes: 2 additions & 2 deletions supabase/functions/_backend/utils/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { countDevicesSB, getAppsFromSB, getUpdateStatsSB, readBandwidthUsageSB,
import { DEFAULT_LIMIT } from './types.ts'
import { backgroundTask } from './utils.ts'

export function createStatsMau(c: Context, device_id: string, app_id: string, org_id: string) {
export function createStatsMau(c: Context, device_id: string, app_id: string, org_id: string, platform: string) {
const lowerDeviceId = device_id
if (!c.env.DEVICE_USAGE)
return trackDeviceUsageSB(c, lowerDeviceId, app_id, org_id)
return trackDeviceUsageCF(c, lowerDeviceId, app_id, org_id)
return trackDeviceUsageCF(c, lowerDeviceId, app_id, org_id, platform)
}

export async function onPremStats(c: Context, app_id: string, action: string, device: DeviceWithoutCreatedAt) {
Expand Down
2 changes: 1 addition & 1 deletion supabase/functions/_backend/utils/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export async function updateWithPG(
return simpleError200(c, 'missing_info', 'Cannot find device_id or app_id')
}

await backgroundTask(c, createStatsMau(c, device_id, app_id, appOwner.owner_org))
await backgroundTask(c, createStatsMau(c, device_id, app_id, appOwner.owner_org, platform))

cloudlog({ requestId: c.get('requestId'), message: 'vals', platform, device })

Expand Down
Loading