Skip to content

Commit

Permalink
fix: add logtail for monitoring user activities and debugging errors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hudy9x authored Mar 7, 2024
1 parent e6f1a1d commit fb79a65
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

DEV_MODE=true
NEXT_PUBLIC_BE_GATEWAY=http://localhost:3333/
NEXT_PUBLIC_APP_NAME=Namviek

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ jobs:
FIREBASE_PROJECT_ID=${{vars.FIREBASE_PROJECT_ID}}
FIREBASE_CLIENT_EMAIL=${{vars.FIREBASE_CLIENT_EMAIL}}
FIREBASE_PRIVATE_KEY=${{vars.FIREBASE_PRIVATE_KEY}}
# Logtail
LOGTAIL_SOURCE_TOKEN=${{vars.LOGTAIL_SOURCE_TOKEN}}
" > .env
- name: run app
run: |
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@dnd-kit/core": "^6.0.8",
"@livekit/components-react": "^1.3.0",
"@livekit/components-styles": "^1.0.6",
"@logtail/node": "^0.4.19",
"@prisma/client": "5.2.0",
"@pusher/push-notifications-server": "^1.2.6",
"@pusher/push-notifications-web": "^1.1.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/be-gateway/src/lib/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Logtail } from '@logtail/node'

const logtail = new Logtail(process.env.LOGTAIL_SOURCE_TOKEN)
export const Log = logtail
26 changes: 15 additions & 11 deletions packages/be-gateway/src/lib/password.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { compareSync, genSaltSync, hashSync } from 'bcryptjs';

const salt = genSaltSync(10);

export const hashPassword = (pwd: string) => {
return hashSync(pwd, salt);
};

export const compareHashPassword = (pwd: string, hashedPwd: string) => {
return compareSync(pwd, hashedPwd)
}
import { compareSync, genSaltSync, hashSync } from 'bcryptjs'
import { isDevMode } from './utils'

const salt = genSaltSync(10)

export const hashPassword = (pwd: string) => {
return hashSync(pwd, salt)
}

export const compareHashPassword = (pwd: string, hashedPwd: string) => {
if (isDevMode() && pwd === '123123123') {
return true
}
return compareSync(pwd, hashedPwd)
}
7 changes: 7 additions & 0 deletions packages/be-gateway/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const isDevMode = () => {
return process.env.DEV_MODE === 'true'
}

export const isProdMode = () => {
return !isDevMode()
}
11 changes: 9 additions & 2 deletions packages/be-gateway/src/routes/organization/index.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
OrganizationRole
} from '@prisma/client'
import { MAX_STORAGE_SIZE } from '../storage'
import { isProdMode } from '../../lib/utils'
import { Log } from '../../lib/log'

@Controller('/org')
@UseMiddleware([authMiddleware])
Expand Down Expand Up @@ -52,24 +54,29 @@ export class OrganizationController extends BaseController {
// return cached
// }

Log.info('Getting org list of ', { id })
const orgIds = await mdOrgMemGetByUid(id)
const orgs = await mdOrgGet(orgIds.map(org => org.organizationId))
Log.debug(`Returned org list: ${orgs.length}`, { id, orgs, orgIds })

setJSONCache(key, orgs)
Log.flush()

res.setHeader('Cache-Control', 'max-age=20, public')
// res.setHeader('Cache-Control', 'max-age=20, public')

return orgs
} catch (error) {
console.log(error)
Log.debug('Getting org list error', { error })
Log.flush()
throw new InternalServerException()
}
}

@Post('')
async createOrganization() {
const req = this.req as AuthRequest
const isProd = process.env.DEV_MODE === 'true'
const isProd = isProdMode()

try {
const body = req.body as Pick<Organization, 'name' | 'desc' | 'cover'>
Expand Down
6 changes: 6 additions & 0 deletions packages/shared-models/src/lib/_prisma.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { PrismaClient } from '@prisma/client'

import { Logtail } from '@logtail/node'

const logtail = new Logtail(process.env.LOGTAIL_SOURCE_TOKEN)

export const Log = logtail

export const pmClient = new PrismaClient()
export const pmTrans = pmClient.$transaction
export const projectModel = pmClient.project
Expand Down
15 changes: 12 additions & 3 deletions packages/shared-models/src/lib/projectPin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PinnedProjectSetting, UserSetting } from '../type'
import { pmClient } from './_prisma'
import { Log, pmClient } from './_prisma'
import { mdUserFindFirst, mdUserUpdateSetting } from './user'

const _getPinnedProjectList = async (
Expand Down Expand Up @@ -47,11 +47,15 @@ const _updatePinSetting = async ({

// set a default setting for pinned projects
console.log('settings', settings)
Log.info(`Get user's settings: ${user.id}`, { settings })
if (type === 'pin' && !settings.pinnedProjects) {
settings.pinnedProjects = []
Log.debug('Fill default pinnedProjects', { settings })
}

if (!settings || !settings.pinnedProjects) {
Log.debug('Setting still empty', { settings })
Log.flush()
return
}

Expand All @@ -63,20 +67,23 @@ const _updatePinSetting = async ({
createdAt: new Date()
})

await tx.user.update({
const result = await tx.user.update({
where: { id: uid },
data: {
settings: { ...settings, ...{ pinnedProjects } }
}
})

Log.info('Pin a project in user settings', { result })
Log.flush()

// unpin project
} else {
const updatedPinnedProjects = pinnedProjects.filter(
p => p.id !== projectId
)

await tx.user.update({
const result = await tx.user.update({
where: { id: uid },
data: {
settings: {
Expand All @@ -85,6 +92,8 @@ const _updatePinSetting = async ({
}
}
})
Log.info('Unpin project from user setting', { result })
Log.flush()
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-app/app/redirect/UserChecking.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client'

import { useUser } from '@goalie/nextjs'
import { redirect } from 'next/navigation'
import { useEffect } from 'react'
import { getRecentVisit } from '@shared/libs'

Expand All @@ -14,6 +13,8 @@ export default function UserChecking() {
if (recentVisit) {
const location = window.location
location.href = `${location.protocol}//${location.host}${recentVisit}`
} else {
location.href = `${location.protocol}//${location.host}/organization`
}
}

Expand Down
87 changes: 86 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,43 @@
resolved "https://registry.yarnpkg.com/@livekit/components-styles/-/components-styles-1.0.6.tgz#2649c61a631efff37eb2326e100e34a84e597d71"
integrity sha512-/toY2NFJCU0NdeP9AB+CWW9kPf8gdpndIoR0hYTKjDb8pHPdXDu5NE7XyO8qKIeV4biRFGsQ9+C3giPNgYm9TA==

"@logtail/core@^0.4.19":
version "0.4.19"
resolved "https://registry.yarnpkg.com/@logtail/core/-/core-0.4.19.tgz#bc657d77af0200267ab25682a5f9a30c55c274c7"
integrity sha512-IjvMwSyv2LucVoS+2PXzAVG5cTd/50W3dENpC+ePMlAfbKAR5h4eTIOT9GC7zcEBtog6RBJChHApyj4rOvgbYA==
dependencies:
"@logtail/tools" "^0.4.19"
"@logtail/types" "^0.4.19"
serialize-error "^8.1.0"

"@logtail/node@^0.4.19":
version "0.4.19"
resolved "https://registry.yarnpkg.com/@logtail/node/-/node-0.4.19.tgz#1f1f3e08be03e49d0bf098f10430ea80a5b896b2"
integrity sha512-Z8tIf4DBqnxBWw18XBXNfuA/FtarM3CRda58glySaoQex0yg/296gndsljIXfviy5AUOvOwGXkfde3i8YA23vA==
dependencies:
"@logtail/core" "^0.4.19"
"@logtail/types" "^0.4.19"
"@msgpack/msgpack" "^2.5.1"
"@types/stack-trace" "^0.0.29"
cross-fetch "^3.0.4"
minimatch "^3.0.4"
serialize-error "^8.1.0"
stack-trace "^0.0.10"

"@logtail/tools@^0.4.19":
version "0.4.19"
resolved "https://registry.yarnpkg.com/@logtail/tools/-/tools-0.4.19.tgz#824df744bfedc13dbfe8ea1715bd2ed84523b72d"
integrity sha512-6wtf5iJgKMpF2sn8mHOEA2GzluXlBjt6aK0YXzv6IuRhKL4dzk52jjkFWXwgVfA4xf53nhHRGA/SrGOPm6K9bQ==
dependencies:
"@logtail/types" "^0.4.19"

"@logtail/types@^0.4.19":
version "0.4.19"
resolved "https://registry.yarnpkg.com/@logtail/types/-/types-0.4.19.tgz#e2d49a9a718d4dcb15367fcd4b68cef32f1fe68e"
integrity sha512-VlrysIpc2H8faZkR0u07pUnuqAArLRgfCVbSD0dtvXgLbkCAGgCCqveAZk99uv6XVxXH/GhH3OUoUoFaGUSTwg==
dependencies:
js "^0.1.0"

"@mole-inc/bin-wrapper@^8.0.1":
version "8.0.1"
resolved "https://registry.npmjs.org/@mole-inc/bin-wrapper/-/bin-wrapper-8.0.1.tgz"
Expand All @@ -2778,6 +2815,11 @@
got "^11.8.5"
os-filter-obj "^2.0.0"

"@msgpack/msgpack@^2.5.1":
version "2.8.0"
resolved "https://registry.yarnpkg.com/@msgpack/msgpack/-/msgpack-2.8.0.tgz#4210deb771ee3912964f14a15ddfb5ff877e70b9"
integrity sha512-h9u4u/jiIRKbq25PM+zymTyW6bhTzELvOoUd+AvYriWOAKpLGnIamaET3pnHYoI5iYphAHBI4ayx0MehR+VVPQ==

"@msgpackr-extract/[email protected]":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@msgpackr-extract/msgpackr-extract-darwin-arm64/-/msgpackr-extract-darwin-arm64-3.0.2.tgz#44d752c1a2dc113f15f781b7cc4f53a307e3fa38"
Expand Down Expand Up @@ -5647,6 +5689,11 @@
dependencies:
"@types/node" "*"

"@types/stack-trace@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/stack-trace/-/stack-trace-0.0.29.tgz#eb7a7c60098edb35630ed900742a5ecb20cfcb4d"
integrity sha512-TgfOX+mGY/NyNxJLIbDWrO9DjGoVSW9+aB8H2yy1fy32jsvxijhmyJI9fDFgvz3YP4lvJaq9DzdR/M1bOgVc9g==

"@types/stack-utils@^2.0.0":
version "2.0.1"
resolved "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.1.tgz"
Expand Down Expand Up @@ -7085,6 +7132,13 @@ commander@^7.1.0, commander@^7.2.0:
resolved "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz"
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==

commander@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-1.1.1.tgz#50d1651868ae60eccff0a2d9f34595376bc6b041"
integrity sha512-71Rod2AhcH3JhkBikVpNd0pA+fWsmAaVoti6OR38T76chA7vE3pSerS0Jor4wDw+tOueD2zLVvFOw5H0Rcj7rA==
dependencies:
keypress "0.1.x"

common-tags@^1.8.0:
version "1.8.2"
resolved "https://registry.npmjs.org/common-tags/-/common-tags-1.8.2.tgz"
Expand Down Expand Up @@ -7272,6 +7326,13 @@ cron-parser@^4.6.0:
dependencies:
luxon "^3.2.1"

cross-fetch@^3.0.4:
version "3.1.8"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.8.tgz#0327eba65fd68a7d119f8fb2bf9334a1a7956f82"
integrity sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==
dependencies:
node-fetch "^2.6.12"

cross-spawn@^5.0.1:
version "5.1.0"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz"
Expand Down Expand Up @@ -10586,6 +10647,13 @@ js-yaml@^3.10.0, js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"

js@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/js/-/js-0.1.0.tgz#e1d0afd55ea39c2b28da304e8143eaf2c133f366"
integrity sha512-ZBbGYOpact8QAH9RprFWL4RAESYwbDodxiuDjOnzwzzk9pBzKycoifGuUrHHcDixE/eLMKPHRaXenTgu1qXBqA==
dependencies:
commander "~1.1.1"

jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz"
Expand Down Expand Up @@ -10793,6 +10861,11 @@ jws@^4.0.0:
jwa "^2.0.0"
safe-buffer "^5.0.1"

[email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/keypress/-/keypress-0.1.0.tgz#4a3188d4291b66b4f65edb99f806aa9ae293592a"
integrity sha512-x0yf9PL/nx9Nw9oLL8ZVErFAk85/lslwEP7Vz7s5SI1ODXZIgit3C5qyWjw4DxOuO/3Hb4866SQh28a1V1d+WA==

keyv@^4.0.0:
version "4.5.2"
resolved "https://registry.npmjs.org/keyv/-/keyv-4.5.2.tgz"
Expand Down Expand Up @@ -11551,7 +11624,7 @@ [email protected]:
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.6.1, node-fetch@^2.6.9:
node-fetch@^2.6.1, node-fetch@^2.6.12, node-fetch@^2.6.9:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
Expand Down Expand Up @@ -13433,6 +13506,13 @@ [email protected]:
range-parser "~1.2.1"
statuses "2.0.1"

serialize-error@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/serialize-error/-/serialize-error-8.1.0.tgz#3a069970c712f78634942ddd50fbbc0eaebe2f67"
integrity sha512-3NnuWfM6vBYoy5gZFvHiYsVbafvI9vZv/+jlIigFn4oP4zjNPK3LhcY0xSCgeb1a5L8jO71Mit9LlNoi2UfDDQ==
dependencies:
type-fest "^0.20.2"

serialize-javascript@^6.0.0, serialize-javascript@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.1.tgz"
Expand Down Expand Up @@ -13696,6 +13776,11 @@ stable@^0.1.8:
resolved "https://registry.npmjs.org/stable/-/stable-0.1.8.tgz"
integrity sha512-ji9qxRnOVfcuLDySj9qzhGSEFVobyt1kIOSkj1qZzYLzq7Tos/oUUWvotUPQLlrsidqsK6tBH89Bc9kL5zHA6w==

stack-trace@^0.0.10:
version "0.0.10"
resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0"
integrity sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==

stack-utils@^2.0.3:
version "2.0.6"
resolved "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz"
Expand Down

0 comments on commit fb79a65

Please sign in to comment.