feat: add user interaction metrix collection

This commit is contained in:
Eunwoo Choi 2023-06-28 15:12:54 +09:00
parent 6c2a621880
commit 9e76c11313
No known key found for this signature in database
GPG Key ID: ADB4D90A2C8C2E13
5 changed files with 25 additions and 8 deletions

View File

@ -12,7 +12,7 @@ import { ParsedUrlQuery } from 'querystring'
import { Bot, ResponseProps, Theme, User } from '@types' import { Bot, ResponseProps, Theme, User } from '@types'
import { git, KoreanbotsEndPoints, reportCats, Status } from '@utils/Constants' import { git, KoreanbotsEndPoints, reportCats, Status } from '@utils/Constants'
import { get } from '@utils/Query' import {get, put} from '@utils/Query'
import Day from '@utils/Day' import Day from '@utils/Day'
import { ReportSchema } from '@utils/Yup' import { ReportSchema } from '@utils/Yup'
import Fetch from '@utils/Fetch' import Fetch from '@utils/Fetch'
@ -344,6 +344,9 @@ export const getServerSideProps = async (ctx: Context) => {
data data
} }
} }
await put.viewBot(ctx.query.id)
const desc = await get.botDescSafe(data.id) const desc = await get.botDescSafe(data.id)
const user = await get.Authorization(parsed?.token) const user = await get.Authorization(parsed?.token)
if((checkBotFlag(data.flags, 'trusted') || checkBotFlag(data.flags, 'partnered')) && data.vanity && data.vanity !== ctx.query.id) { if((checkBotFlag(data.flags, 'trusted') || checkBotFlag(data.flags, 'partnered')) && data.vanity && data.vanity !== ctx.query.id) {

View File

@ -11,7 +11,7 @@ import { ParsedUrlQuery } from 'querystring'
import { Server, Theme, User } from '@types' import { Server, Theme, User } from '@types'
import { DiscordEnpoints, DSKR_BOT_ID, KoreanbotsEndPoints } from '@utils/Constants' import { DiscordEnpoints, DSKR_BOT_ID, KoreanbotsEndPoints } from '@utils/Constants'
import { get, safeImageHost } from '@utils/Query' import {get, put, safeImageHost} from '@utils/Query'
import Day from '@utils/Day' import Day from '@utils/Day'
import Fetch from '@utils/Fetch' import Fetch from '@utils/Fetch'
import { checkBotFlag, checkServerFlag, checkUserFlag, formatNumber, parseCookie } from '@utils/Tools' import { checkBotFlag, checkServerFlag, checkUserFlag, formatNumber, parseCookie } from '@utils/Tools'
@ -280,6 +280,9 @@ export const getServerSideProps = async (ctx: Context) => {
data data
} }
} }
await put.viewServer(ctx.query.id)
const desc = safeImageHost(data.desc) const desc = safeImageHost(data.desc)
const user = await get.Authorization(parsed?.token) const user = await get.Authorization(parsed?.token)
if((checkServerFlag(data.flags, 'trusted') || checkServerFlag(data.flags, 'partnered')) && data.vanity && data.vanity !== ctx.query.id) { if((checkServerFlag(data.flags, 'trusted') || checkServerFlag(data.flags, 'partnered')) && data.vanity && data.vanity !== ctx.query.id) {

View File

@ -1,14 +1,16 @@
import { NextPage, GetServerSideProps } from 'next' import { NextPage, GetServerSideProps } from 'next'
import NotFound from 'pages/404' import NotFound from 'pages/404'
import { get } from '@utils/Query' import { get } from '@utils/Query'
import { Servers } from '@utils/Mongo'
import { getYYMMDD } from '@utils/Tools'
import { DiscordEnpoints } from '@utils/Constants' import { DiscordEnpoints } from '@utils/Constants'
const Join: NextPage = () => <NotFound /> const Join: NextPage = () => <NotFound />
export const getServerSideProps: GetServerSideProps = async (ctx) => { export const getServerSideProps: GetServerSideProps = async (ctx) => {
const data = await get.server.load(ctx.query.id as string) const data = await get.server.load(ctx.query.id as string)
if(!data) return { props: {} } if(!data) return { props: {} }
// // const record = await Bots.updateOne({ _id: data.id, 'inviteMetrix.day': getYYMMDD() }, { $inc: { 'inviteMetrix.$.count': 1 } }) const record = await Servers.updateOne({ _id: data.id, 'joinMetrix.day': getYYMMDD() }, { $inc: { 'joinMetrix.$.count': 1 } })
// if(record.n === 0) await Bots.findByIdAndUpdate(data.id, { $push: { inviteMetrix: { count: 1 } } }, { upsert: true }) if(record.matchedCount === 0) await Servers.findByIdAndUpdate(data.id, { $push: { joinMetrix: { count: 1 } } }, { upsert: true })
ctx.res.statusCode = 307 ctx.res.statusCode = 307
ctx.res.setHeader('Location', DiscordEnpoints.ServerInvite(data.invite)) ctx.res.setHeader('Location', DiscordEnpoints.ServerInvite(data.invite))
return { return {

View File

@ -22,7 +22,10 @@ const botSchema = new mongoose.Schema({
const serverSchema = new mongoose.Schema({ const serverSchema = new mongoose.Schema({
_id: String, _id: String,
data: {} data: {},
viewMetrix: [ metrix ],
voteMetrix: [ { ...metrix, increasement: { type: Number, default: 1 } } ],
joinMetrix: [ metrix ],
}) })
type ModelType<TSchema extends Schema> = Model< type ModelType<TSchema extends Schema> = Model<

View File

@ -411,8 +411,8 @@ async function voteServer(userID: string, serverID: string): Promise<number|bool
data[key] = date data[key] = date
await knex('servers').where({ id: serverID }).increment('votes', 1) await knex('servers').where({ id: serverID }).increment('votes', 1)
await knex('users').where({ id: userID }).update({ votes: JSON.stringify(data) }) await knex('users').where({ id: userID }).update({ votes: JSON.stringify(data) })
// const record = await Servers.updateOne({ _id: serverID, 'voteMetrix.day': getYYMMDD() }, { $inc: { 'voteMetrix.$.increasement': 1, 'voteMetrix.$.count': 1 } }) const record = await Servers.updateOne({ _id: serverID, 'voteMetrix.day': getYYMMDD() }, { $inc: { 'voteMetrix.$.increasement': 1, 'voteMetrix.$.count': 1 } })
// if(record.n === 0) await Servers.findByIdAndUpdate(serverID, { $push: { voteMetrix: { count: (await knex('servers').where({ id: serverID }))[0].votes } } }, { upsert: true }) if(record.matchedCount === 0) await Servers.findByIdAndUpdate(serverID, { $push: { voteMetrix: { count: (await knex('servers').where({ id: serverID }))[0].votes } } }, { upsert: true })
return true return true
} }
/** /**
@ -754,6 +754,11 @@ async function viewBot(id: string) {
if(record.matchedCount === 0) await Bots.findByIdAndUpdate(id, { $push: { viewMetrix: { count: 0 } } }, { upsert: true }) if(record.matchedCount === 0) await Bots.findByIdAndUpdate(id, { $push: { viewMetrix: { count: 0 } } }, { upsert: true })
} }
async function viewServer(id: string) {
const record = await Servers.updateOne({ _id: id, 'viewMetrix.day': getYYMMDD() }, { $inc: { 'viewMetrix.$.count': 1 } })
if(record.matchedCount === 0) await Servers.findByIdAndUpdate(id, { $push: { viewMetrix: { count: 0 } } }, { upsert: true })
}
export const get = { export const get = {
discord: { discord: {
user: new DataLoader( user: new DataLoader(
@ -911,7 +916,8 @@ export const put = {
voteServer, voteServer,
submitBot, submitBot,
submitServer, submitServer,
viewBot viewBot,
viewServer
} }
export const remove = { export const remove = {