-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visitor table and poll clicks from Memphis (#599)
* wip: add tracker configuration * feat: created dockerfile for tracker ms * fix: prettier * feat: remove unused deps --------- Co-authored-by: orig <[email protected]>
- Loading branch information
Showing
39 changed files
with
1,059 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ProducerService } from '@reduced.to/queue-manager'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
|
||
const SHORTENER_PRODUCER_NAME = 'shortener'; | ||
const SHORTENER_QUEUE_NAME = 'stats'; | ||
|
||
@Injectable() | ||
export class ShortenerProducer extends ProducerService { | ||
constructor() { | ||
super(SHORTENER_PRODUCER_NAME, SHORTENER_QUEUE_NAME); | ||
constructor(config: AppConfigService) { | ||
super(SHORTENER_PRODUCER_NAME, config.getConfig().tracker.stats.queueName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
node_modules | ||
dist | ||
npm-debug.log | ||
CONTRIBUTE.MD | ||
README.md | ||
.gitignore | ||
LICENSE | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -------------------------------------------- | ||
# Dependencies Stage | ||
# -------------------------------------------- | ||
FROM node:19.2-alpine3.15 as dependencies | ||
WORKDIR /app | ||
|
||
COPY package*.json ./ | ||
|
||
RUN apk add --update python3 make g++\ | ||
&& rm -rf /var/cache/apk/* | ||
|
||
RUN npm ci | ||
|
||
# -------------------------------------------- | ||
# Build Stage | ||
# -------------------------------------------- | ||
# Intermediate docker image to build the bundle in and install dependencies | ||
FROM node:19.2-alpine3.15 as build | ||
WORKDIR /app | ||
|
||
COPY . . | ||
COPY --from=dependencies /app/node_modules ./node_modules | ||
|
||
# Run prisma generate & build the bundle in production mode | ||
RUN npx nx build tracker --prod --skip-nx-cache | ||
|
||
# -------------------------------------------- | ||
# Production Stage | ||
# -------------------------------------------- | ||
FROM node:19.2-alpine3.15 as production | ||
WORKDIR /app | ||
|
||
COPY --from=build /app/dist/apps/tracker ./tracker | ||
COPY --from=build /app/node_modules ./node_modules | ||
COPY --from=build /app/libs/ ./libs | ||
|
||
EXPOSE 3001 | ||
|
||
# Start the application | ||
CMD sh -c "npx nx migrate-deploy prisma && node backend/main.js" | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'tracker', | ||
preset: '../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/apps/tracker', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{ | ||
"name": "tracker", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "apps/tracker/src", | ||
"projectType": "application", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/webpack:webpack", | ||
"outputs": ["{options.outputPath}"], | ||
"defaultConfiguration": "production", | ||
"options": { | ||
"target": "node", | ||
"compiler": "tsc", | ||
"outputPath": "dist/apps/tracker", | ||
"main": "apps/tracker/src/main.ts", | ||
"tsConfig": "apps/tracker/tsconfig.app.json", | ||
"isolatedConfig": true, | ||
"webpackConfig": "apps/tracker/webpack.config.js" | ||
}, | ||
"configurations": { | ||
"development": {}, | ||
"production": {} | ||
} | ||
}, | ||
"serve": { | ||
"executor": "@nx/js:node", | ||
"defaultConfiguration": "development", | ||
"options": { | ||
"buildTarget": "tracker:build" | ||
}, | ||
"configurations": { | ||
"development": { | ||
"buildTarget": "tracker:build:development" | ||
}, | ||
"production": { | ||
"buildTarget": "tracker:build:production" | ||
} | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["apps/tracker/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "apps/tracker/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
}, | ||
"docker-build": { | ||
"dependsOn": ["build"], | ||
"command": "docker build -f apps/tracker/Dockerfile . -t tracker" | ||
}, | ||
"push-image-to-registry": { | ||
"dependsOn": ["docker-build"], | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"commands": [ | ||
"docker image tag tracker ghcr.io/{args.repository}/tracker:master", | ||
"docker push ghcr.io/{args.repository}/tracker:master" | ||
], | ||
"parallel": false | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { AppConfigModule } from '@reduced.to/config'; | ||
import { AppLoggerModule } from '@reduced.to/logger'; | ||
import { StatsModule } from '../stats/stats.module'; | ||
|
||
@Global() | ||
@Module({ | ||
imports: [AppConfigModule, AppLoggerModule, StatsModule], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { VersioningType } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { NestExpressApplication } from '@nestjs/platform-express'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
import { AppModule } from './app/app.module'; | ||
import { AppLoggerSerivce } from '@reduced.to/logger'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create<NestExpressApplication>(AppModule); | ||
|
||
app.enableVersioning({ | ||
type: VersioningType.URI, | ||
defaultVersion: '1', | ||
prefix: 'api/v', | ||
}); | ||
|
||
app.enableCors({ origin: true, credentials: true }); | ||
|
||
const port = app.get(AppConfigService).getConfig().general.trackerPort; | ||
const logger = app.get(AppLoggerSerivce); | ||
|
||
await app.listen(port); | ||
|
||
logger.log(`Starting tracker on port ${port}`); | ||
} | ||
|
||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConsumerService } from '@reduced.to/queue-manager'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
import { AppLoggerSerivce } from '@reduced.to/logger'; | ||
import { StatsService } from './stats.service'; | ||
import * as geoip from 'geoip-lite'; | ||
import { createHash } from 'node:crypto'; | ||
import { Message } from 'memphis-dev/*'; | ||
|
||
@Injectable() | ||
export class StatsConsumer extends ConsumerService { | ||
constructor(config: AppConfigService, private readonly loggerService: AppLoggerSerivce, private readonly statsService: StatsService) { | ||
super('tracker', config.getConfig().tracker.stats.queueName); | ||
} | ||
|
||
async onMessage(message: Message): Promise<void> { | ||
const { ip, userAgent, key, url } = message.getDataAsJson() as { ip: string; userAgent: string; key: string; url: string }; | ||
message.ack(); | ||
|
||
const hashedIp = createHash('sha256').update(ip).digest('hex'); | ||
const isUniqueVisit = await this.statsService.isUniqueVisit(key, hashedIp); | ||
|
||
if (!isUniqueVisit) { | ||
return; | ||
} | ||
|
||
const geo = geoip.lookup(ip); | ||
await this.statsService.addVisit(key, { | ||
hashedIp, | ||
ua: userAgent, | ||
geo, | ||
}); | ||
|
||
this.loggerService.log(`Added unique visit for ${key}`); | ||
} | ||
} |
Oops, something went wrong.