Skip to content

Commit

Permalink
fix: merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
PongDev committed Jan 25, 2023
2 parents f9f6a5c + 8a3c870 commit d05d214
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 117 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"rules": {
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
"react/display-name": "off"
}
},
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ Thumbs.db
**/*.sqlite

google_application_credentials.json
# cgr-api log
log/
4 changes: 3 additions & 1 deletion apps/api/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ JWT_SECRET=<insert_jwt_secret>

ADMIN_TOKEN=<insert_admin_token>

CLIENT_LOGGER_URL=http://192.0.2.255/gelf
ELASTIC_URL=https://localhost:9200
ELASTIC_USERNAME=cgr-api
ELASTIC_PASSWORD=<password>

COMPUTATION_BACKEND_URL=http://localhost:8000
COMPUTATION_BACKEND_AUTHTOKEN=<Authelia Token, Optional for Connecting to Computation Prod from outside>
Expand Down
19 changes: 12 additions & 7 deletions apps/api/src/clientlogging/clientlogging.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ class ClientLogDto {
deviceId: string
sessionId: string
additionalData?: Record<string, string>
localTimestamp: string
}

const clientLogDtoArraySchema = {
type: 'array',
items: {
type: 'object',
properties: {
localTimestamp: {
type: 'string',
},
kind: {
type: 'string',
},
Expand Down Expand Up @@ -78,16 +82,17 @@ export class ClientLoggingController {
const logEntry: GelfLogEntry & Record<string, string> = {
short_message: dto.message,
full_message: dto.detail,
_kind: dto.kind,
_app: 'frontend-client',
_source_ip: req.ip,
_user_id: accessToken?._id || undefined,
_session_id: dto.sessionId,
_device_id: dto.deviceId,
kind: dto.kind,
app: 'frontend-client',
source_ip: req.ip,
user_id: accessToken?._id || undefined,
session_id: dto.sessionId,
device_id: dto.deviceId,
local_timestamp: dto.localTimestamp,
}

if (dto.additionalData)
Object.entries(dto.additionalData).forEach(([k, v]) => (logEntry[`_a_${k}`] = v))
Object.entries(dto.additionalData).forEach(([k, v]) => (logEntry[`a_${k}`] = v))

await this.loggingService.sendLogEntry(logEntry)
}
Expand Down
26 changes: 21 additions & 5 deletions apps/api/src/clientlogging/clientlogging.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { HttpModule } from '@nestjs/axios'
import { Module, forwardRef } from '@nestjs/common'
import { Module, forwardRef, Provider } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Client } from "@opensearch-project/opensearch"

import { AuthModule } from '../auth/auth.module'
import { ClientLoggingController } from './clientlogging.controller'
import { ClientLoggingService } from './clientlogging.service'

const elasticProvider: Provider<Client> = {
provide: Client,
useFactory: (configService: ConfigService) => new Client({
node: configService.get("elasticUrl"),
auth: {
username: configService.get("elasticUsername"),
password: configService.get("elasticPassword"),
},
ssl: {
rejectUnauthorized: false,
}
}),
inject: [ConfigService],
}

@Module({
providers: [ClientLoggingService],
providers: [ClientLoggingService, elasticProvider],
controllers: [ClientLoggingController],
imports: [HttpModule, forwardRef(() => AuthModule)],
imports: [forwardRef(() => AuthModule)],
exports: [ClientLoggingService],
})
export class ClientLoggingModule {}
export class ClientLoggingModule { }
51 changes: 19 additions & 32 deletions apps/api/src/clientlogging/clientlogging.service.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
import { HttpService } from '@nestjs/axios'
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Client } from '@opensearch-project/opensearch'

import * as http from 'http'
import * as https from 'https'
import { hostname } from 'os'

export interface GelfLogEntry {
short_message: string
full_message?: string
_kind: string
_app: 'frontend-client' | 'backend'
kind: string
app: 'frontend-client' | 'backend'
}

const CLIENTLOGGING_INDEX = "cgr-clientlogging"

@Injectable()
export class ClientLoggingService {
httpAg: http.Agent
httpsAg: https.Agent

constructor(readonly configService: ConfigService, readonly httpService: HttpService) {
this.httpAg = new http.Agent({
keepAlive: true,
})
this.httpsAg = new https.Agent({
keepAlive: true,
})
}
constructor(readonly configService: ConfigService, private readonly elasticClient: Client) {}

async sendLogEntry(entry: GelfLogEntry & Record<string, string>) {
try {
const res = await this.httpService
.post(
this.configService.get('clientLoggerUrl'),
{
...entry,
version: '1.1',
host: hostname(),
},
{
httpAgent: this.httpAg,
httpsAgent: this.httpsAg,
}
)
.toPromise()
return res.data
try {
const record = {
...entry,
timestamp: new Date(),
host: hostname(),
}

await this.elasticClient.index({
index: CLIENTLOGGING_INDEX,
body: record,
refresh: true
})
} catch (e) {
Logger.warn('Failed to send log to log collector', entry)
Logger.error("Failed to send log to logging service", e)
throw new InternalServerErrorException("Can't send log to log collector")
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/common/pipes/keyvalue.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from

@Injectable()
export class ParseKeyValuePipe implements PipeTransform<string, Record<string, string>> {
transform(value: string, metadata: ArgumentMetadata): Record<string, string> {
transform(value: string, _metadata: ArgumentMetadata): Record<string, string> {
const pairs = value.split('&').map((pair) => pair.split('='))

// there exists a pair that doesn't follow 'key=value' format
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/computation/computation.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CourseRecommendationRequest, CourseRecommendationResponse } from '../gr
export class ComputationResolver {
private metadata: grpc.Metadata

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private courseRecommendation: any

constructor(configService: ConfigService) {
Expand All @@ -37,6 +38,7 @@ export class ComputationResolver {
)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.courseRecommendation = new (descriptor.CourseRecommendation as any)(...clientArgs)
}

Expand Down
10 changes: 9 additions & 1 deletion apps/api/src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export interface Configuration {
computationBackendAuthToken: string
slackWebhookUrl: string
env: string
elasticUrl: string
elasticUsername: string
elasticPassword: string
}

export const configuration = (): Configuration => {
Expand All @@ -32,6 +35,9 @@ export const configuration = (): Configuration => {
computationBackendAuthToken: process.env.COMPUTATION_BACKEND_AUTHTOKEN,
slackWebhookUrl: process.env.SLACK_WEBHOOK_URL,
env: process.env.ENV || 'development',
elasticUrl: process.env.ELASTIC_URL,
elasticUsername: process.env.ELASTIC_USERNAME,
elasticPassword: process.env.ELASTIC_PASSWORD,
}
}

Expand All @@ -40,9 +46,11 @@ const requiredConfigs = [
'googleOAuthSecret',
'jwtSecret',
'adminToken',
'clientLoggerUrl',
'computationBackendUrl',
'backendPublicUrl',
'elasticUrl',
'elasticUsername',
'elasticPassword',
]

export function validateConfig(configService: ConfigService<Configuration>): void {
Expand Down
65 changes: 0 additions & 65 deletions apps/api/src/logger.ts

This file was deleted.

21 changes: 19 additions & 2 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@ import { Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory } from '@nestjs/core'
import { NestExpressApplication } from '@nestjs/platform-express'
import { WinstonModule } from "nest-winston"
import * as winston from "winston"

import * as cookieParser from 'cookie-parser'

import { AppModule } from './app/app.module'
import { Configuration, validateConfig } from './config/configuration'
import { GelfLogger } from './logger'

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: new GelfLogger(),
logger: WinstonModule.createLogger({
handleExceptions: true,
handleRejections: true,
defaultMeta: {
app: "cgr-api",
},
transports: [
new winston.transports.Console({format: winston.format.combine(
winston.format.colorize({all: true}),
winston.format.simple(),
)}),
new winston.transports.File({filename: "log/app.log", format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
)}),
]
})
})

const configService = app.get<ConfigService<Configuration>>(ConfigService)
Expand Down
13 changes: 11 additions & 2 deletions apps/api/src/review/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ export class ReviewService {
})
return reviews
.map((rawReview) => this.transformReview(rawReview, userId))
.sort((reviewA, reviewB) => (reviewB.isOwner ? 1 : 0) - (reviewA.isOwner ? 1 : 0))
.sort((reviewA, reviewB) => {
if (reviewA.isOwner) {
return -1
}
if (reviewB.isOwner) {
return 1
}
// Sort by _id descending
return reviewB._id.localeCompare(reviewA._id)
})
}

async getPending(): Promise<Review[]> {
Expand Down Expand Up @@ -218,7 +227,7 @@ export class ReviewService {
interaction.userId.equals(userId)
)?.type
return {
_id: rawReview._id,
_id: rawReview._id.toString(),
rating: rawReview.rating,
courseNo: rawReview.courseNo,
semester: rawReview.semester,
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/services/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function collectLogEvent(event: LogEvent) {
deviceId: deviceId,
sessionId: sessionId,
accessToken: userStore.accessToken || undefined,
localTimestamp: new Date().toISOString(),
}
backlogLog.push(log)
if (backlogLog.length >= 5) sendCollectedLog()
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/services/logging/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface ClientLogDTO {
deviceId: string
sessionId: string
additionalData?: Record<string, string>
localTimestamp: string
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@nestjs/platform-express": "^9.2.1",
"@nestjs/schedule": "1.0.2",
"@nrwl/next": "15.0.4",
"@opensearch-project/opensearch": "^2.1.0",
"@react-hook/google-optimize": "^1.2.1",
"@slack/webhook": "6.0.0",
"@thinc-org/chula-courses": "^2.3.0",
Expand Down Expand Up @@ -85,6 +86,7 @@
"mobx-react": "^7.6.0",
"mobx-utils": "6.0.5",
"mongoose": "^6.8.2",
"nest-winston": "^1.8.0",
"next": "13.0.0",
"next-seo": "4.29.0",
"next-sitemap": "^3.1.43",
Expand Down Expand Up @@ -114,6 +116,7 @@
"styled-components": "^5.3.6",
"tslib": "^2.4.1",
"uuid": "^8.3.2",
"winston": "^3.8.2",
"zustand": "^4.1.5"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit d05d214

Please sign in to comment.