Skip to content

Commit

Permalink
cleanup: query follows satistics design; refactor: DailyStats to SatS…
Browse files Browse the repository at this point in the history
…ummary
  • Loading branch information
Soxasora committed Dec 23, 2024
1 parent 057fc59 commit d1663ee
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 33 deletions.
21 changes: 11 additions & 10 deletions api/resolvers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pushSubscriptionSchema, validateSchema } from '@/lib/validate'
import { replyToSubscription } from '@/lib/webPush'
import { getSub } from './sub'
import { GqlAuthenticationError, GqlInputError } from '@/lib/error'
import { msatsToSats } from '@/lib/format'

export default {
Query: {
Expand Down Expand Up @@ -361,12 +362,12 @@ export default {
LIMIT ${LIMIT})`
)

if (meFull.noteDailyStats) {
if (meFull.noteSatSummary) {
queries.push(
`(SELECT CONCAT('stats_', date_trunc('day', t))::text as id,
t AS "sortTime",
NULL as "earnedSats",
'DailyStats' AS type
'SatSummary' AS type
FROM user_stats_days
WHERE "user_stats_days"."id" = $1
AND t >= date_trunc('day', CURRENT_DATE - INTERVAL '1 day')
Expand Down Expand Up @@ -502,27 +503,27 @@ export default {
return res.length ? res[0].type : null
}
},
DailyStats: {
SatSummary: {
date: async (n, args, { models }) => {
return new Date(n.id.replace('stats_', ''))
},
stacked: async (n, args, { me, models }) => {
const res = await models.$queryRaw`
SELECT ((msats_stacked+msats_rewards)::float)/1000.0 as sats_stacked
stacked: async (n, args, { me, models }) => { // msats_rewards is already counted in msats_stacked
const [{ stacked }] = await models.$queryRaw`
SELECT sum(msats_stacked) as stacked
FROM user_stats_days
WHERE id = ${Number(me.id)}
AND t = ${new Date(n.id.replace('stats_', ''))}::timestamp
`
return res.length ? res[0].sats_stacked : 0
return (stacked && msatsToSats(stacked)) || 0
},
spent: async (n, args, { me, models }) => {
const res = await models.$queryRaw`
SELECT (msats_spent::float)/1000.0 as sats_spent
const [{ spent }] = await models.$queryRaw`
SELECT sum(msats_spent) as spent
FROM user_stats_days
WHERE id = ${Number(me.id)}
AND t = ${new Date(n.id.replace('stats_', ''))}::timestamp
`
return res.length ? res[0].sats_spent : 0
return (spent && msatsToSats(spent)) || 0
}
},
Earn: {
Expand Down
6 changes: 3 additions & 3 deletions api/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,14 @@ export default {
}
}

if (user.noteDailyStats) {
const dailyStats = await models.dailyStats.findFirst({
if (user.noteSatSummary) {
const satSummary = await models.satSummary.findFirst({
where: {
id: me.id
}
})

if (dailyStats) {
if (satSummary) {
foundNotes()
return true
}
Expand Down
4 changes: 2 additions & 2 deletions api/typeDefs/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default gql`
type: String!
}
type DailyStats {
type SatSummary {
id: ID!
sortTime: Date!
date: Date!
Expand Down Expand Up @@ -163,7 +163,7 @@ export default gql`
union Notification = Reply | Votification | Mention
| Invitification | Earn | JobChanged | InvoicePaid | WithdrawlPaid | Referral
| Streak | DailyStats | FollowActivity | ForwardedVotification | Revenue | SubStatus
| Streak | SatSummary | FollowActivity | ForwardedVotification | Revenue | SubStatus
| TerritoryPost | TerritoryTransfer | Reminder | ItemMention | Invoicification
| ReferralReward
Expand Down
4 changes: 2 additions & 2 deletions api/typeDefs/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default gql`
nostrPubkey: String
nostrRelays: [String!]
noteAllDescendants: Boolean!
noteDailyStats: Boolean
noteSatSummary: Boolean
noteCowboyHat: Boolean!
noteDeposits: Boolean!,
noteWithdrawals: Boolean!,
Expand Down Expand Up @@ -169,7 +169,7 @@ export default gql`
nostrPubkey: String
nostrRelays: [String!]
noteAllDescendants: Boolean!
noteDailyStats: Boolean!
noteSatSummary: Boolean!
noteCowboyHat: Boolean!
noteDeposits: Boolean!
noteWithdrawals: Boolean!
Expand Down
6 changes: 3 additions & 3 deletions components/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function Notification ({ n, fresh }) {
(type === 'WithdrawlPaid' && <WithdrawlPaid n={n} />) ||
(type === 'Referral' && <Referral n={n} />) ||
(type === 'Streak' && <Streak n={n} />) ||
(type === 'DailyStats' && <DailyStats n={n} />) ||
(type === 'SatSummary' && <SatSummary n={n} />) ||
(type === 'Votification' && <Votification n={n} />) ||
(type === 'ForwardedVotification' && <ForwardedVotification n={n} />) ||
(type === 'Mention' && <Mention n={n} />) ||
Expand Down Expand Up @@ -164,7 +164,7 @@ const defaultOnClick = n => {
if (type === 'Referral') return { href: '/referrals/month' }
if (type === 'ReferralReward') return { href: '/referrals/month' }
if (type === 'Streak') return {}
if (type === 'DailyStats') return {}
if (type === 'SatSummary') return {}
if (type === 'TerritoryTransfer') return { href: `/~${n.sub.name}` }

if (!n.item) return {}
Expand Down Expand Up @@ -203,7 +203,7 @@ function Streak ({ n }) {
)
}

function DailyStats ({ n }) { // WIP
function SatSummary ({ n }) { // WIP
// stacked and spent
return (
<div className='d-flex'>
Expand Down
2 changes: 1 addition & 1 deletion fragments/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const NOTIFICATIONS = gql`
days
type
}
... on DailyStats {
... on SatSummary {
id
sortTime
date
Expand Down
2 changes: 1 addition & 1 deletion fragments/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const SETTINGS_FIELDS = gql`
noteWithdrawals
noteInvites
noteJobIndicator
noteDailyStats
noteSatSummary
noteCowboyHat
noteForwardedSats
hideInvoiceDesc
Expand Down
2 changes: 1 addition & 1 deletion lib/apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getClient (uri) {
'WithdrawlPaid',
'Referral',
'Streak',
'DailyStats',
'SatSummary',
'FollowActivity',
'ForwardedVotification',
'Revenue',
Expand Down
4 changes: 2 additions & 2 deletions lib/webPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const createUserFilter = (tag) => {
EARN: 'noteEarning',
DEPOSIT: 'noteDeposits',
WITHDRAWAL: 'noteWithdrawals',
DAILY_STATS: 'noteDailyStats',
DAILY_STATS: 'noteSatSummary',
STREAK: 'noteCowboyHat'
}
const key = tagMap[tag.split('-')[0]]
Expand Down Expand Up @@ -393,7 +393,7 @@ export async function notifyStreakLost (userId, streak) {
}
}

export async function notifyDailyStats (userId, stats) { // WIP
export async function notifySatSummary (userId, stats) { // WIP
try {
await sendUserNotification(userId, {
title: 'WIP sats stats',
Expand Down
4 changes: 2 additions & 2 deletions pages/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default function Settings ({ ssrData }) {
noteInvites: settings?.noteInvites,
noteJobIndicator: settings?.noteJobIndicator,
noteCowboyHat: settings?.noteCowboyHat,
noteDailyStats: settings?.noteDailyStats,
noteSatSummary: settings?.noteSatSummary,
noteForwardedSats: settings?.noteForwardedSats,
hideInvoiceDesc: settings?.hideInvoiceDesc,
autoDropBolt11s: settings?.autoDropBolt11s,
Expand Down Expand Up @@ -333,7 +333,7 @@ export default function Settings ({ ssrData }) {
/>
<Checkbox
label='daily stacked and spent sats summary is available'
name='noteDailyStats'
name='noteSatSummary'
groupClassName='mb-0'
/>
<Checkbox
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-- AlterTable
ALTER TABLE "users" ADD COLUMN "noteDailyStats" BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE "users" ADD COLUMN "noteSatSummary" BOOLEAN NOT NULL DEFAULT false;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ model User {
nostrCrossposting Boolean @default(false)
slashtagId String? @unique(map: "users.slashtagId_unique")
noteCowboyHat Boolean @default(true)
noteDailyStats Boolean @default(false)
noteSatSummary Boolean @default(false)
streak Int?
gunStreak Int?
horseStreak Int?
Expand Down
8 changes: 4 additions & 4 deletions worker/satsummary.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { notifyDailyStats } from '@/lib/webPush'
import { notifySatSummary } from '@/lib/webPush'
export async function summarizeDailySats ({ data: { userId }, models }) {
let dailyStats
let satSummary
try {
dailyStats = await models.dailyStats.findUnique({ where: { id: userId } })
satSummary = await models.satSummary.findUnique({ where: { id: userId } })
} catch (err) {
console.error('failed to lookup daily stats by user', err)
}
try {
await notifyDailyStats({ userId, dailyStats })
await notifySatSummary({ userId, satSummary })
} catch (err) {
console.error('failed to send push notification for daily stats', err)
}
Expand Down

0 comments on commit d1663ee

Please sign in to comment.