-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2105eb2
commit f3261f9
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ponder } from "@/generated"; | ||
import { count, countDistinct, eq, gte } from "@ponder/core"; | ||
|
||
/** | ||
* Get the overall system stats | ||
*/ | ||
ponder.get("/stats/overall", async (ctx) => { | ||
// Get all the product ids for this admin | ||
const { InteractionEvent, ProductInteractionContract, Product } = | ||
ctx.tables; | ||
|
||
// Get the total nbr of user who performed an interaction | ||
const totalInteractions = await ctx.db | ||
.select({ | ||
count: countDistinct(InteractionEvent.user), | ||
}) | ||
.from(InteractionEvent); | ||
const totalPerType = await ctx.db | ||
.select({ | ||
name: InteractionEvent.type, | ||
count: countDistinct(InteractionEvent.user), | ||
}) | ||
.from(InteractionEvent) | ||
.groupBy(InteractionEvent.type); | ||
const totalPerProduct = await ctx.db | ||
.select({ | ||
name: Product.name, | ||
count: countDistinct(InteractionEvent.user), | ||
}) | ||
.from(InteractionEvent) | ||
.innerJoin( | ||
ProductInteractionContract, | ||
eq(InteractionEvent.interactionId, ProductInteractionContract.id) | ||
) | ||
.innerJoin( | ||
Product, | ||
eq(ProductInteractionContract.productId, Product.id) | ||
) | ||
.groupBy(Product.id); | ||
|
||
// Get the min time for the WAU and DAU | ||
const wauMinTime = BigInt(Date.now() - 7 * 24 * 60 * 60 * 1000) / 1000n; | ||
const dauMinTime = BigInt(Date.now() - 30 * 60 * 60 * 1000) / 1000n; | ||
|
||
// Get the WAU and DAU | ||
const wauInteractions = await ctx.db | ||
.select({ | ||
count: countDistinct(InteractionEvent.user), | ||
}) | ||
.from(InteractionEvent) | ||
.where(gte(InteractionEvent.timestamp, wauMinTime)); | ||
const dauInteractions = await ctx.db | ||
.select({ | ||
count: countDistinct(InteractionEvent.user), | ||
}) | ||
.from(InteractionEvent) | ||
.where(gte(InteractionEvent.timestamp, dauMinTime)); | ||
|
||
// Total number of product registered | ||
const totalProducts = await ctx.db.select({ count: count() }).from(Product); | ||
|
||
return ctx.json({ | ||
interactions: { | ||
total: totalInteractions[0]?.count, | ||
wau: wauInteractions[0]?.count, | ||
dau: dauInteractions[0]?.count, | ||
totalPerType, | ||
totalPerProduct, | ||
}, | ||
products: totalProducts[0]?.count, | ||
}); | ||
}); |