2022-06-10 19:20:33 +00:00
|
|
|
const PLACEHOLDERS_NUM = 616
|
|
|
|
|
|
|
|
export default {
|
|
|
|
Query: {
|
|
|
|
registrationGrowth: async (parent, args, { models }) => {
|
|
|
|
return await models.$queryRaw(
|
2022-07-01 18:26:34 +00:00
|
|
|
`SELECT date_trunc('month', created_at) AS time, count("inviteId") as invited, count(*) - count("inviteId") as organic
|
2022-06-10 19:20:33 +00:00
|
|
|
FROM users
|
|
|
|
WHERE id > ${PLACEHOLDERS_NUM} AND date_trunc('month', now_utc()) <> date_trunc('month', created_at)
|
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`)
|
|
|
|
},
|
|
|
|
activeGrowth: async (parent, args, { models }) => {
|
|
|
|
return await models.$queryRaw(
|
|
|
|
`SELECT date_trunc('month', created_at) AS time, count(DISTINCT "userId") as num
|
|
|
|
FROM "ItemAct"
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', created_at)
|
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`)
|
|
|
|
},
|
|
|
|
itemGrowth: async (parent, args, { models }) => {
|
|
|
|
return await models.$queryRaw(
|
2022-06-24 15:38:00 +00:00
|
|
|
`SELECT date_trunc('month', created_at) AS time, count("parentId") as comments,
|
|
|
|
count("subName") as jobs, count(*)-count("parentId")-count("subName") as posts
|
2022-06-10 19:20:33 +00:00
|
|
|
FROM "Item"
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', created_at)
|
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`)
|
|
|
|
},
|
|
|
|
spentGrowth: async (parent, args, { models }) => {
|
2022-06-24 15:38:00 +00:00
|
|
|
// add up earn for each month
|
|
|
|
// add up non-self votes/tips for posts and comments
|
|
|
|
|
2022-06-10 19:20:33 +00:00
|
|
|
return await models.$queryRaw(
|
2022-06-24 15:38:00 +00:00
|
|
|
`SELECT date_trunc('month', "ItemAct".created_at) AS time,
|
2022-09-02 19:05:44 +00:00
|
|
|
sum(CASE WHEN act = 'STREAM' THEN "ItemAct".sats ELSE 0 END) as jobs,
|
|
|
|
sum(CASE WHEN act IN ('VOTE', 'POLL') AND "Item"."userId" = "ItemAct"."userId" THEN "ItemAct".sats ELSE 0 END) as fees,
|
|
|
|
sum(CASE WHEN act = 'BOOST' THEN "ItemAct".sats ELSE 0 END) as boost,
|
|
|
|
sum(CASE WHEN act = 'TIP' THEN "ItemAct".sats ELSE 0 END) as tips
|
2022-06-10 19:20:33 +00:00
|
|
|
FROM "ItemAct"
|
2022-06-24 15:38:00 +00:00
|
|
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', "ItemAct".created_at)
|
2022-06-10 19:20:33 +00:00
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`)
|
2022-06-22 21:13:51 +00:00
|
|
|
},
|
2022-06-24 15:38:00 +00:00
|
|
|
earnerGrowth: async (parent, args, { models }) => {
|
2022-06-22 21:13:51 +00:00
|
|
|
return await models.$queryRaw(
|
|
|
|
`SELECT time, count(distinct user_id) as num
|
|
|
|
FROM
|
|
|
|
((SELECT date_trunc('month', "ItemAct".created_at) AS time, "Item"."userId" as user_id
|
|
|
|
FROM "ItemAct"
|
|
|
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id AND "Item"."userId" <> "ItemAct"."userId"
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', "ItemAct".created_at))
|
|
|
|
UNION ALL
|
|
|
|
(SELECT date_trunc('month', created_at) AS time, "userId" as user_id
|
|
|
|
FROM "Earn"
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', created_at))) u
|
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`)
|
2022-06-24 15:38:00 +00:00
|
|
|
},
|
|
|
|
stackedGrowth: async (parent, args, { models }) => {
|
|
|
|
return await models.$queryRaw(
|
2022-07-01 18:26:34 +00:00
|
|
|
`SELECT time, sum(airdrop) as rewards, sum(post) as posts, sum(comment) as comments
|
2022-06-24 15:38:00 +00:00
|
|
|
FROM
|
|
|
|
((SELECT date_trunc('month', "ItemAct".created_at) AS time, 0 as airdrop,
|
2022-09-02 19:05:44 +00:00
|
|
|
CASE WHEN "Item"."parentId" IS NULL THEN 0 ELSE "ItemAct".sats END as comment,
|
|
|
|
CASE WHEN "Item"."parentId" IS NULL THEN "ItemAct".sats ELSE 0 END as post
|
2022-06-24 15:38:00 +00:00
|
|
|
FROM "ItemAct"
|
|
|
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id AND "Item"."userId" <> "ItemAct"."userId"
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', "ItemAct".created_at) AND
|
|
|
|
"ItemAct".act IN ('VOTE', 'TIP'))
|
|
|
|
UNION ALL
|
|
|
|
(SELECT date_trunc('month', created_at) AS time, msats / 1000 as airdrop, 0 as post, 0 as comment
|
|
|
|
FROM "Earn"
|
|
|
|
WHERE date_trunc('month', now_utc()) <> date_trunc('month', created_at))) u
|
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`)
|
|
|
|
},
|
|
|
|
registrationsWeekly: async (parent, args, { models }) => {
|
2022-06-24 18:59:02 +00:00
|
|
|
return await models.user.count({
|
2022-06-24 15:38:00 +00:00
|
|
|
where: {
|
|
|
|
createdAt: {
|
|
|
|
gte: new Date(new Date().setDate(new Date().getDate() - 7))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
activeWeekly: async (parent, args, { models }) => {
|
|
|
|
const [{ active }] = await models.$queryRaw(
|
|
|
|
`SELECT count(DISTINCT "userId") as active
|
|
|
|
FROM "ItemAct"
|
|
|
|
WHERE created_at >= now_utc() - interval '1 week'`
|
|
|
|
)
|
|
|
|
return active
|
|
|
|
},
|
|
|
|
earnersWeekly: async (parent, args, { models }) => {
|
|
|
|
const [{ earners }] = await models.$queryRaw(
|
|
|
|
`SELECT count(distinct user_id) as earners
|
|
|
|
FROM
|
|
|
|
((SELECT "Item"."userId" as user_id
|
|
|
|
FROM "ItemAct"
|
|
|
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id AND "Item"."userId" <> "ItemAct"."userId"
|
|
|
|
WHERE "ItemAct".created_at >= now_utc() - interval '1 week')
|
|
|
|
UNION ALL
|
|
|
|
(SELECT "userId" as user_id
|
|
|
|
FROM "Earn"
|
|
|
|
WHERE created_at >= now_utc() - interval '1 week')) u`)
|
|
|
|
return earners
|
|
|
|
},
|
|
|
|
itemsWeekly: async (parent, args, { models }) => {
|
|
|
|
const [stats] = await models.$queryRaw(
|
|
|
|
`SELECT json_build_array(
|
|
|
|
json_build_object('name', 'comments', 'value', count("parentId")),
|
2022-06-24 18:59:02 +00:00
|
|
|
json_build_object('name', 'jobs', 'value', count("subName")),
|
2022-06-24 15:38:00 +00:00
|
|
|
json_build_object('name', 'posts', 'value', count(*)-count("parentId")-count("subName"))) as array
|
|
|
|
FROM "Item"
|
|
|
|
WHERE created_at >= now_utc() - interval '1 week'`)
|
|
|
|
|
|
|
|
return stats?.array
|
|
|
|
},
|
|
|
|
spentWeekly: async (parent, args, { models }) => {
|
|
|
|
const [stats] = await models.$queryRaw(
|
|
|
|
`SELECT json_build_array(
|
2022-09-02 19:05:44 +00:00
|
|
|
json_build_object('name', 'jobs', 'value', sum(CASE WHEN act = 'STREAM' THEN "ItemAct".sats ELSE 0 END)),
|
|
|
|
json_build_object('name', 'fees', 'value', sum(CASE WHEN act in ('VOTE', 'POLL') AND "Item"."userId" = "ItemAct"."userId" THEN "ItemAct".sats ELSE 0 END)),
|
|
|
|
json_build_object('name', 'boost', 'value', sum(CASE WHEN act = 'BOOST' THEN "ItemAct".sats ELSE 0 END)),
|
|
|
|
json_build_object('name', 'tips', 'value', sum(CASE WHEN act = 'TIP' THEN "ItemAct".sats ELSE 0 END))) as array
|
2022-06-24 15:38:00 +00:00
|
|
|
FROM "ItemAct"
|
|
|
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id
|
|
|
|
WHERE "ItemAct".created_at >= now_utc() - interval '1 week'`)
|
|
|
|
|
|
|
|
return stats?.array
|
|
|
|
},
|
|
|
|
stackedWeekly: async (parent, args, { models }) => {
|
|
|
|
const [stats] = await models.$queryRaw(
|
|
|
|
`SELECT json_build_array(
|
2022-07-01 18:26:34 +00:00
|
|
|
json_build_object('name', 'rewards', 'value', sum(airdrop)),
|
2022-06-24 15:38:00 +00:00
|
|
|
json_build_object('name', 'posts', 'value', sum(post)),
|
|
|
|
json_build_object('name', 'comments', 'value', sum(comment))
|
|
|
|
) as array
|
|
|
|
FROM
|
|
|
|
((SELECT 0 as airdrop,
|
2022-09-02 19:05:44 +00:00
|
|
|
CASE WHEN "Item"."parentId" IS NULL THEN 0 ELSE "ItemAct".sats END as comment,
|
|
|
|
CASE WHEN "Item"."parentId" IS NULL THEN "ItemAct".sats ELSE 0 END as post
|
2022-06-24 15:38:00 +00:00
|
|
|
FROM "ItemAct"
|
|
|
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id AND "Item"."userId" <> "ItemAct"."userId"
|
|
|
|
WHERE "ItemAct".created_at >= now_utc() - interval '1 week' AND
|
|
|
|
"ItemAct".act IN ('VOTE', 'TIP'))
|
|
|
|
UNION ALL
|
|
|
|
(SELECT msats / 1000 as airdrop, 0 as post, 0 as comment
|
|
|
|
FROM "Earn"
|
|
|
|
WHERE created_at >= now_utc() - interval '1 week')) u`)
|
|
|
|
|
|
|
|
return stats?.array
|
2022-06-10 19:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|