Skip to content

Commit

Permalink
feat(server): 完成 event/issue/user 表
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyao27 committed Jun 1, 2022
1 parent 4c4c157 commit 7fe6b2a
Show file tree
Hide file tree
Showing 30 changed files with 445 additions and 333 deletions.
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,39 @@
"dev:web": "nr docker & pnpm --parallel --filter web --filter web run dev",
"dev:server": "nr docker & pnpm --parallel --filter server --filter config run dev",
"setup": "prisma migrate dev && prisma db seed",
"studio": "prisma studio",
"lint": "eslint . --fix",
"prepublishOnly": "nr build",
"test": "pnpm -r run test"
},
"dependencies": {
"@ohbug/browser": "^2.0.1",
"@ohbug/core": "^2.0.1",
"@ohbug/react": "^2.0.1",
"@ohbug/types": "^2.0.1",
"@ohbug/vue": "^2.0.1",
"@ohbug/browser": "^2.0.2",
"@ohbug/core": "^2.0.2",
"@ohbug/react": "^2.0.2",
"@ohbug/types": "^2.0.2",
"@ohbug/vue": "^2.0.2",
"@prisma/client": "^3.11.1",
"dotenv": "^16.0.1"
"dotenv": "^16.0.1",
"source-map-trace": "^0.1.3"
},
"devDependencies": {
"@antfu/ni": "^0.16.2",
"@babel/types": "^7.18.0",
"@babel/types": "^7.18.4",
"@changesets/cli": "^2.22.0",
"@chenyueban/eslint-config": "^1.0.9",
"@chenyueban/tsconfig": "^2.1.0",
"@commitlint/cli": "^17.0.0",
"@types/node": "^17.0.35",
"@types/node": "^17.0.36",
"commitizen": "^4.2.4",
"cross-env": "^7.0.3",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.16.0",
"esno": "^0.16.3",
"husky": "^8.0.1",
"lint-staged": "^12.4.0",
"lint-staged": "^12.4.3",
"prisma": "^3.11.1",
"rimraf": "^3.0.2",
"tsup": "^5.12.8",
"tsup": "^6.0.1",
"typescript": "^4.6.3"
},
"prisma": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.5",
"source-map-trace": "^0.1.3",
"types": "workspace:*",
"winston": "^3.7.2"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/server/src/api/report/report.core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EventTypes } from '@ohbug/core'

import type {
AggregationDataAndMetaData,
OhbugEventDetail,
Expand Down
7 changes: 4 additions & 3 deletions packages/server/src/api/report/report.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import type { ReactErrorDetail } from '@ohbug/react'
import type { VueErrorDetail } from '@ohbug/vue'
import { Prisma } from '@prisma/client'
import { OhbugEventLike } from '~/types'
import { OhbugEventLike } from 'types'

export type OhbugEventDetail = UncaughtErrorDetail &
UnhandledrejectionErrorDetail &
Expand All @@ -35,8 +35,9 @@ export interface AggregationDataAndMetaData {
metaData: Prisma.InputJsonObject
}

export interface CreateOrUpdateIssueByIntroParams {
export interface CreateDataParams {
event: OhbugEventLike
intro: string
issueIntro: string
userIntro: string
metaData: Prisma.InputJsonObject
}
58 changes: 46 additions & 12 deletions packages/server/src/api/report/report.processor.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
import { Process, Processor } from '@nestjs/bull'
import type { Job } from 'bull'
import { CreateOrUpdateIssueByIntroParams } from './report.interface'
import type { Prisma } from '@prisma/client'
import type { CreateDataParams } from './report.interface'
import { ForbiddenException, PrismaService } from '~/common'

@Processor('document')
export class ReportProcessor {
constructor(private readonly prisma: PrismaService) {}

async CreateOrUpdateIssueByIntro({
async CreateData({
event,
intro,
issueIntro,
userIntro,
metaData,
}: CreateOrUpdateIssueByIntroParams) {
}: CreateDataParams) {
try {
const result = await this.prisma.event.create({
return await this.prisma.event.create({
data: {
...event,
apiKey: event.apiKey,
appVersion: event.appVersion,
appType: event.appType,
releaseStage: event.releaseStage,
timestamp: event.timestamp,
category: event.category,
type: event.type,
sdk: event.sdk as unknown as Prisma.InputJsonObject,
detail: event.detail as Prisma.InputJsonValue,
device: event.device as Prisma.InputJsonObject,
user: event.user as Prisma.InputJsonObject,
actions: event.actions as unknown as Prisma.InputJsonArray,
metaData: event.metaData as Prisma.InputJsonObject,
issue: {
connectOrCreate: {
where: { intro },
where: { id: issueIntro },
create: {
intro,
id: issueIntro,
apiKey: event.apiKey,
type: event.type,
metaData,
users: {
connectOrCreate: {
where: {
issueId_userId: {
issueId: issueIntro,
userId: userIntro,
},
},
create: {
user: {
connectOrCreate: {
where: { id: userIntro },
create: {
...event.user,
id: userIntro,
ipAddress: event.user.ipAddress!,
},
},
},
},
},
},
},
},
},
},
})

return result
}
catch (error) {
throw new ForbiddenException(400400, error)
Expand All @@ -48,11 +82,11 @@ export class ReportProcessor {
@Process('event')
async handleEvent(job: Job) {
try {
const data = job.data as CreateOrUpdateIssueByIntroParams
const data = job.data as CreateDataParams

if (data) {
// 1. 创建 issue/event (postgres)
await this.CreateOrUpdateIssueByIntro(data)
await this.CreateData(data)

// // 2. 根据 apiKey 拿到对应的 notification 配置
// const notification = await getNotificationByApiKey(issue.apiKey)
Expand Down
33 changes: 17 additions & 16 deletions packages/server/src/api/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Injectable } from '@nestjs/common'
import type { OhbugEvent } from '@ohbug/types'
import type { Queue } from 'bull'
import { InjectQueue } from '@nestjs/bull'
import type { OhbugEventLike } from 'types'
import {
getMd5FromAggregationData,
switchErrorDetailAndGetAggregationDataAndMetaData,
} from './report.core'
import type { CreateDataParams } from './report.interface'
import { ForbiddenException } from '~/common'

@Injectable()
Expand Down Expand Up @@ -47,20 +49,17 @@ export class ReportService {
}

/**
* 将可能会变的字段转为 string
* error: `detail` `actions` `metaData`
* performance: `data`
* 补充 event 中的信息
*
* @param event
* @param ip
* @param ipAddress
*/
transferEvent(event: OhbugEvent<any>, ip: string, intro: string) {
transferEvent(event: OhbugEvent<any>, ipAddress: string): OhbugEventLike {
return Object.assign(event, {
user: {
...(event.user ?? {}),
ip,
ipAddress,
},
intro,
})
}

Expand All @@ -70,13 +69,14 @@ export class ReportService {
*
* @param event
*/
aggregation(event: OhbugEvent<any>) {
aggregation(event: OhbugEventLike) {
try {
const { type, detail, apiKey } = event
const { agg, metaData }
= switchErrorDetailAndGetAggregationDataAndMetaData(type, detail)
const intro = getMd5FromAggregationData(apiKey, ...agg)
return { intro, metaData }
const issueIntro = getMd5FromAggregationData(apiKey, ...agg)
const userIntro = getMd5FromAggregationData(apiKey, ...Object.values(event.user))
return { issueIntro, userIntro, metaData }
}
catch (error) {
throw new ForbiddenException(4001001, error)
Expand All @@ -93,15 +93,16 @@ export class ReportService {
try {
if (typeof event === 'string') event = JSON.parse(event)
const filteredEvent = this.filterEvent(event as OhbugEvent<any>)
const aggregationEvent = this.aggregation(filteredEvent)
const eventLike = this.transferEvent(filteredEvent, ip, aggregationEvent.intro)
const eventLike = this.transferEvent(filteredEvent, ip)
const aggregationEvent = this.aggregation(eventLike)
const createDataParams: CreateDataParams = {
event: eventLike,
...aggregationEvent,
}

this.documentQueue.add(
'event',
{
event: eventLike,
...aggregationEvent,
},
createDataParams,
{
delay: 3000,
removeOnComplete: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpException } from '@nestjs/common'
import { ErrorShowType } from 'types'
import { status } from '../constants'
import { ErrorShowType } from '~/types'

export class ForbiddenException extends HttpException {
public readonly code: number
Expand Down
3 changes: 1 addition & 2 deletions packages/server/src/common/filters/all-exceptions.filter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Catch, HttpException, HttpStatus, Logger } from '@nestjs/common'
import type { ArgumentsHost } from '@nestjs/common'
import { BaseExceptionFilter } from '@nestjs/core'

import type { ResponseStructure } from '~/types'
import type { ResponseStructure } from 'types'

@Catch()
export class AllExceptionsFilter extends BaseExceptionFilter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Catch, HttpException, HttpStatus, Logger } from '@nestjs/common'
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common'

import type { ResponseStructure } from 'types'
import { ForbiddenException } from '../exceptions'
import type { ResponseStructure } from '~/types'

@Catch(HttpException)
export class ForbiddenExceptionFilter implements ExceptionFilter {
Expand Down
24 changes: 0 additions & 24 deletions packages/server/src/types/interfaces/event.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "types",
"private": true,
"types": "./src/index.ts",
"files": [
"src"
],
"sideEffects": false
}
File renamed without changes.
18 changes: 18 additions & 0 deletions packages/types/src/interfaces/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { OhbugEvent, OhbugUser } from '@ohbug/types'
import type { Result } from 'source-map-trace/dist/interfaces'

export interface OhbugEventLike extends Omit<OhbugEvent<any>, 'user'> {
user: OhbugUser
}

export type { OhbugUser }
interface Document {
id: string
index: string
}
export interface EventInAPP<T> extends OhbugEvent<T> {
// source
source?: Result
next?: Document
previous?: Document
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum ErrorShowType {
SILENT = 0,
WARN_MESSAGE = 1,
ERROR_MESSAGE = 2,
MODAL = 3,
NOTIFICATION = 4,
REDIRECT = 9,
}
Expand Down
4 changes: 4 additions & 0 deletions packages/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src"]
}
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"react-dom": "18.1.0"
},
"devDependencies": {
"@types/node": "^17.0.35",
"@types/node": "^17.0.36",
"@types/react": "18.0.9",
"@types/react-dom": "18.0.5",
"babel-plugin-superjson-next": "^0.4.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/web/src/components/issueList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Issue } from '@prisma/client'
import type { FC } from 'react'

interface Props {
data: Issue[]
}
const IssueList: FC<Props> = ({ data }) => {
return (
<div>{
data.map(issue => (
<div key={issue.id}>
<div>
{issue.type}
</div>
</div>
))
}
</div>
)
}

export default IssueList
Loading

0 comments on commit 7fe6b2a

Please sign in to comment.