add code for fake data

This commit is contained in:
CounterFire2023 2024-05-08 15:07:15 +08:00
parent e5d2e653e8
commit 6b2bde5938
7 changed files with 350 additions and 0 deletions

View File

@ -0,0 +1,3 @@
[
""
]

View File

@ -17,6 +17,7 @@
"repairredis": "ts-node -r tsconfig-paths/register src/repairredis.ts",
"checkredis": "ts-node -r tsconfig-paths/register src/checkredis.ts",
"additem": "ts-node -r tsconfig-paths/register src/addboxdata.ts",
"fixdata": "ts-node -r tsconfig-paths/register src/fixdata.ts",
"test:watch": "jest --watch",
"test": "jest"
},

View File

@ -29,3 +29,45 @@ export const formatNumShow = (num: number) => {
return num.toFixed(1)
}
}
export const randomUserAgent = () => {
// 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.97 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
let n1 = Math.floor(Math.random() * 15) + 70
let n2 = Math.floor(Math.random() * 100)
let osName = Math.random() > 0.5 ? 'Windows NT 10.0' : 'Macintosh; Intel Mac OS X 10_15_7'
let s0 = `Mozilla/5.0 (${osName}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/`
let s2 = `.0.3029.`
let s3 = 'Safari/537.46'
return `${s0}${n1}${s2}${n2}${s3}`
}
export const randomIp = () => {
let n1 = Math.floor(Math.random() * 254)
let n2 = Math.floor(Math.random() * 254)
let n3 = Math.floor(Math.random() * 254)
let n4 = Math.floor(Math.random() * 254)
let ip1 = `${n1}.${n2}.${n3}.${n4}`
let ip2 = Math.random() > 0.5 ? '10.0.2.69' : '10.0.0.80'
return `${ip1}, ${ip2}`
}
// 0xd1c7bad0033678f3e347076e5db7290f990d2fcd69369a95918db6d8c6d87c3021fa2334da8cdacb99bdf311ff2087e1b7f5c8f4d8fdb0a653bfeae0a9ebbda41c
// get 132 random hex string with 0x prefix
export const randomSign = () => {
let hex = '0x'
for (let i = 0; i < 64; i++) {
hex += Math.floor(Math.random() * 16).toString(16)
hex += Math.floor(Math.random() * 16).toString(16)
}
hex += Math.random() > 0.5 ? '1b' : '1c'
return hex
}

68
src/fixdata.ts Normal file
View File

@ -0,0 +1,68 @@
import * as dotenv from 'dotenv'
const envFile = process.env.NODE_ENV && process.env.NODE_ENV === 'production' ? `.env.production` : '.env.development'
dotenv.config({ path: envFile })
console.log(process.env.DB_MAIN)
import mongoose from 'mongoose'
import { CheckIn } from 'models/chain/CheckIn'
import { toEIP55 } from 'zutils/utils/chain.util'
import { inertUser, insertWalletLoginLog } from 'services/fix.svr'
const dbMain = mongoose.createConnection(process.env.DB_MAIN)
const dbChain = mongoose.createConnection(process.env.DB_CHAIN)
const dbCtrl = mongoose.createConnection(process.env.DB_CTRL)
const ACTIVITY = 'uaw_activity'
const insertLoginRecord = async () => {
console.log('insertLoginRecord')
}
const insertCheckinRecord = async () => {
console.log('insertCheckinRecord')
}
const insertExploreRecord = async () => {
console.log('insertExploreRecord')
}
const insertOpenBoxRecord = async () => {
console.log('insertOpenBoxRecord')
}
const insertTaskRecord = async () => {
console.log('insertTaskRecord')
}
const parseOneRecord = async (record: any) => {
let addressEip55 = toEIP55(record.from)
console.log('parseOneRecord', addressEip55)
const user = await dbCtrl.collection('users_test').findOne({ address: addressEip55 })
if (!user) {
// 如果没有用户记录,插入用户记录, 并插入一条钱包登录记录(在插入用户记录之前)
const { uid, timestamp } = await inertUser(dbCtrl, addressEip55, record.blockTime)
await insertWalletLoginLog(dbCtrl, addressEip55, uid, timestamp - Math.floor(Math.random() * 2))
}
// 插入签到记录
}
;(async () => {
try {
let beginBlocktime = 1713583747
// for await (const doc of User.find().cursor()) {
// console.log(doc.name);
// }
let idx = 0
await CheckIn.find({ blockTime: { $gt: beginBlocktime } })
.sort({ _id: 1 })
.cursor()
.eachAsync(parseOneRecord)
console.log('done', idx)
// await dbMain.collection('test1').insertOne({ name: 'test' })
// await dbChain.collection('test2').insertOne({ name: 'test' })
} catch (e) {
console.log(e)
}
console.log('end')
process.exit(0)
})()

View File

@ -0,0 +1,32 @@
import { getModelForClass, index, modelOptions, prop, Severity } from '@typegoose/typegoose'
import { dbconn } from 'decorators/dbconn'
import { BaseModule } from '../Base'
@dbconn('ctrl')
@index({ address: 1 }, { unique: true })
@index({ uid: 1 }, { unique: true })
@modelOptions({
schemaOptions: { collection: 'fix_info', timestamps: true },
options: { allowMixed: Severity.ALLOW },
})
export class FixInfoClass extends BaseModule {
@prop({ required: true })
public address: string
@prop({ required: true })
public uid: string
@prop()
public wallet: string
@prop()
public ip: string
@prop()
public user_agent: string
@prop({ default: 0 })
public version: number
}
export const FixInfo = getModelForClass(FixInfoClass, { existingConnection: FixInfoClass.db })

View File

@ -0,0 +1,27 @@
import { getModelForClass, index, modelOptions, mongoose, prop, ReturnModelType, Severity } from '@typegoose/typegoose'
import { dbconn } from 'decorators/dbconn'
import { BaseModule } from '../Base'
@dbconn('ctrl')
@index({ address: 1 }, { unique: false })
@index({ address: 1, action: 1 }, { unique: false })
@index({ address: 1, action: 1, dataId: 1 }, { unique: false })
@modelOptions({
schemaOptions: { collection: 'fix_record', timestamps: true },
options: { allowMixed: Severity.ALLOW },
})
export class FixRecordClass extends BaseModule {
@prop({ required: true })
public address: string
@prop()
public action: string
@prop()
public dataId: string
@prop({ type: mongoose.Schema.Types.Mixed })
public outData: any
}
export const FixRecord = getModelForClass(FixRecordClass, { existingConnection: FixRecordClass.db })

177
src/services/fix.svr.ts Normal file
View File

@ -0,0 +1,177 @@
import { BASE52_ALPHABET } from 'common/Constants'
import { randomIp, randomSign, randomUserAgent } from 'common/Utils'
import { FixInfo } from 'models/ctrl/FixInfo'
import { convert } from 'zutils/utils/number.util'
const ACTIVITY = 'uaw_activity'
const { ObjectId } = require('mongodb')
export const generateShareCode = id => {
// 取ObjectId的time和inc段,
// 将time段倒序(倒序后, 如果以0开始, 则移除0, 随机拼接一个hex字符), 然后拼接inc段, 再转换成52进制
let timeStr = id.slice(0, 8).split('').reverse().join('')
if (timeStr.indexOf('0') === 0) {
let randomStr = convert({ numStr: ((Math.random() * 51) | (0 + 1)) + '', base: 10, to: 52 })
timeStr = randomStr + timeStr.slice(1)
}
let shortId = timeStr + id.slice(-6)
return convert({ numStr: shortId, base: 16, to: 52, alphabet: BASE52_ALPHABET })
}
export const generateObjectId = (timestamp: number) => {
// get last 3 bytes of tmp objectid
const tmp = new ObjectId().toString().slice(-6)
return new ObjectId(timestamp.toString(16) + 'ea158e7e49' + tmp)
}
export const inertUser = async (db: any, address: string, timestamp: number) => {
// 根据blockTime, 随机往前推1小时内的时间, 作为用户记录创建时间
const createTimestamp = timestamp - Math.floor(Math.random() * 3600)
const id = generateObjectId(createTimestamp)
const shareCode = generateShareCode(id.toString())
const createdAt = new Date(createTimestamp * 1000 + Math.floor(Math.random() * 1000))
await db.collection('users_test').insertOne({
_id: id,
activity: ACTIVITY,
address,
__v: 0,
boost: 1,
createdAt,
inWhiteList: false,
locked: false,
taskProgress: [
{
id: 'e2yhq2lj30vwcpedv7p',
task: 'TwitterConnect',
status: 0,
},
{
id: 'e2fclylj30vwcpe0szl',
task: 'TwitterFollow',
status: 0,
},
{
id: 'e2far3lj30vwcpe0mh7',
task: 'DiscordConnect',
status: 0,
},
{
id: 'e2far3lj30vwcpe0mf8',
task: 'DiscordJoin',
status: 0,
},
{
id: 'e2fuah0j30vwcpe0my7',
task: 'TwitterRetweet',
status: 0,
},
{
id: 'e2fuah0j30vwcpe0my9',
task: 'TwitterLike',
status: 0,
},
],
updatedAt: createdAt,
inviteCode: shareCode,
lastLogin: createdAt,
})
return { uid: id, timestamp: createTimestamp }
}
export const insertWalletLoginLog = async (db: any, address: string, uid: string, timestamp: number) => {
let record = await FixInfo.findOne({
address: address.toLowerCase(),
})
if (!record) {
record = new FixInfo({
address: address.toLowerCase(),
uid,
ip: randomIp(),
user_agent: randomUserAgent(),
wallet: Math.random() > 0.5 ? 'okx' : 'metamask',
version: 0,
})
await record.save()
}
let nonce = generateObjectId(timestamp - Math.floor(Math.random() * 30)).toString()
const params = {
message: {
domain: 'treasure.counterfire.games',
address,
chainId: 214,
uri: 'https://treasure.counterfire.games',
version: '1',
statement: 'CF UAW',
nonce,
issuedAt: new Date(timestamp * 1000).toISOString(),
},
signature: randomSign(),
activity: ACTIVITY,
}
const timestamp2 = timestamp - Math.floor(Math.random() * 20)
const createdAt = new Date(timestamp2 * 1000)
await db.collection('user_login_record').insertOne({
_id: generateObjectId(timestamp + Math.floor(Math.random() * 10)),
user: uid,
wallet: record.wallet,
activity: ACTIVITY,
user_agent: record.user_agent,
ip: record.ip,
createdAt: createdAt,
updatedAt: createdAt,
_v: 0,
})
return insertUserLog({
db,
user: '',
timestamp,
name: 'wallet_login',
method: 'POST',
path: '/api/wallet/login',
referer: '',
userAgent: record.user_agent,
ip: record.ip,
params,
})
}
export const insertUserLog = async ({
db,
user,
timestamp,
name,
method,
path,
referer,
userAgent,
ip,
params,
}: {
db: any
user: string
timestamp: number
name: string
method: string
path: string
referer: string
userAgent: string
ip: string
params: any
}) => {
const id = generateObjectId(timestamp)
const createdAt = new Date(timestamp * 1000 + Math.floor(Math.random() * 1000))
await db.collection('user_logs').insertOne({
_id: id,
user,
name,
method,
path,
referer,
user_agent: userAgent,
ip,
params,
createdAt,
updatedAt: createdAt,
__v: 0,
})
}