Skip to content

Commit

Permalink
Merge pull request #3 from MoeidHeidari/feature/log-parser
Browse files Browse the repository at this point in the history
Feature/log parser
  • Loading branch information
MoeidHeidari committed Jun 22, 2022
2 parents b67c65d + f6989db commit b5b730c
Show file tree
Hide file tree
Showing 28 changed files with 1,013 additions and 773 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@
# Table of Contents

- [Table of Contents](#table-of-contents)

- [Overview](#overview)

- [Provided Commands](#provided-commands)

- [Code architecture](#code-architecture)

- [source code](#source-code)

- [Service build information](#service-build-information)

- [Regular user](#regular-user)

- [Documentation](#documentation)

- [Example](#example)

- [ToDo list](#todo-list)

## Overview

Log parser takes a log input file and tries to parse it to extract useful information. It also respects [General Data Protection Regulation (GDPR)](https://gdpr-info.eu/)

You can see take a look at the Full documentation [here](https://github.com/MoeidHeidari/log-parser/blob/main/full-documentation.md)
You can see take a look at the Full documentation [here](https://github.com/MoeidHeidari/log-parser/blob/main/full-documentation.md)


---

Expand Down Expand Up @@ -74,8 +84,20 @@ cd log-parser
npm install
npm run build
npm run test
npm run lint
npm start:{dev || debug || prod}
```

### using runner.sh

```bash
bash scripts/runner.sh
...
Done! 😍
now you can use the application by following commmand
node dist/cli.js -h
```

test result

```bash
Expand Down Expand Up @@ -123,8 +145,7 @@ Run Log parser

```bash
npm run build
node dist/cli.js --input input.log --output log.json --log-level error,debug
Success 😀. output is written in log.json
ode dist/cli.js --input input.log --output log.json --log-level error,debug
```

output.json
Expand Down Expand Up @@ -187,6 +208,4 @@ output.json
- [ ] connect it to logstash
- [ ] implement elastic search

##


##
8 changes: 8 additions & 0 deletions log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"timestamp": 1628475171259,
"logLevel": "error",
"transactionId": "9abc55b2-807b-4361-9dbe-aa88b1b2e978",
"err": "Cannot find user orders list"
}
]
7 changes: 7 additions & 0 deletions scripts/runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
npm install
npm run build
npm test
npm run lint
echo 'Done! 😍'
echo 'now you can use the application by following commmand'
echo 'node dist/cli.js -h'
8 changes: 3 additions & 5 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { Module } from '@nestjs/common';
import { CommandModule } from 'nestjs-command';
import { ParserModule } from '../parser/parser.module';



@Module({
imports: [CommandModule,ParserModule,],
providers: []
imports: [CommandModule, ParserModule],
providers: [],
})
export class AppModule {}
export class AppModule {}
29 changes: 13 additions & 16 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { NestFactory } from '@nestjs/core'
import { CommandModule, CommandService } from 'nestjs-command'
import { AppModule } from './app/app.module'
import { NestFactory } from '@nestjs/core';
import { CommandModule, CommandService } from 'nestjs-command';
import { AppModule } from './app/app.module';

/**
* Main application entry point
*/
async function bootstrap () {
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule, {
logger: ['error'],
})
});

try {
await app
.select(CommandModule)
.get(CommandService)
.exec()
await app.close()
await app.select(CommandModule).get(CommandService).exec();
await app.close();
} catch (error) {
console.error(error)
await app.close()
process.exit(1)
console.error(error);
await app.close();
process.exit(1);
}
}
/**
* Bootstraps the application
*/
bootstrap()
* Bootstraps the application
*/
bootstrap();
38 changes: 19 additions & 19 deletions src/parser/command/parser.command.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Command, Positional, Option } from 'nestjs-command'
import { Injectable } from '@nestjs/common'
import { ParserService } from '../service/parser.service'
import { ParserCommandDTO } from '../dtos/parser-command-dto'
import { validateDTO } from '../common'
import { Command, Option } from 'nestjs-command';
import { Injectable } from '@nestjs/common';
import { ParserService } from '../service/parser.service';
import { ParserCommandDTO } from '../dtos/parser-command-dto';
import { validateDTO } from '../common';

/**
* Log parser command
*/
@Injectable()
export class ParserCommand {
/**
* Constructs the Log parser command class
* @param parserService Injection of parser service
*/
constructor (private readonly parserService: ParserService) {}
/**
* Constructs the Log parser command class
* @param parserService Injection of parser service
*/
constructor(private readonly parserService: ParserService) {}

/**
* Parser command that takes the input log file, parses and writes the result to the output file as ajson content.
Expand All @@ -26,7 +26,7 @@ export class ParserCommand {
command: '*',
describe: 'parses a log file',
})
async parse (
async parse(
@Option({
name: 'input',
describe: 'input log file (ex: "input.log")',
Expand Down Expand Up @@ -55,23 +55,23 @@ export class ParserCommand {
log_level: string,
) {
try {
let logFilter = log_level.split(',')
let command = new ParserCommandDTO({
const logFilter = log_level.split(',');
const command = new ParserCommandDTO({
input: input,
output: output,
logLevel: logFilter,
})
await validateDTO(command)
});
await validateDTO(command);
return this.parserService
.parse(command)
.then(() => {
console.log(`Success 😀. output is written in ${output}`)
console.log(`Success 😀. output is written in ${output}`);
})
.catch(error => {
.catch((error) => {
console.error(error);
})
});
} catch (error) {
console.log(error)
console.log(error);
}
}
}
2 changes: 1 addition & 1 deletion src/parser/common/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './utils'
export * from './utils';
18 changes: 8 additions & 10 deletions src/parser/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { NotAcceptableException } from "@nestjs/common";
import { validate } from "class-validator";
import { validate } from 'class-validator';

/**
* validates dto and returns bad request if it is wrong
* @param dto dto
* @param httpResponseGenerator http response service
*/
export async function validateDTO(dto: any): Promise<any> {
const errors = await validate(dto);

if (errors.length) throw new Error(`InvalidArgumentExcpetion ${errors}`);

return dto;
}

export async function validateDTO(dto: any): Promise<any> {
const errors = await validate(dto);

if (errors.length) throw new Error(`InvalidArgumentExcpetion ${errors}`);

return dto;
}
78 changes: 42 additions & 36 deletions src/parser/dtos/parser-command-dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { IsDefined, IsEnum, IsNotEmpty, IsOptional, IsString } from "class-validator";
import { LogLevel } from "../enum";
import {
IsDefined,
IsEnum,
IsNotEmpty,
IsOptional,
IsString,
} from 'class-validator';
import { LogLevel } from '../enum';

/**
* list of allowed properties
Expand All @@ -8,38 +14,38 @@ const allowedProperties = ['input', 'output', 'logLevel'];
/**
* Log parser command DTO
*/
export class ParserCommandDTO{
export class ParserCommandDTO {
/**
* addres of the log file input
*/
@IsNotEmpty()
@IsDefined()
@IsString()
input:string;
//...........................................................................................................
/**
* address of the output log file
*/
@IsNotEmpty()
@IsDefined()
@IsString()
output:string;
//...........................................................................................................
/**
* list of log levels to be filtered by the parser
*/
@IsOptional()
@IsEnum(LogLevel, { each: true })
logLevel:LogLevel[]
//...........................................................................................................
/**
* Constructs the DTO based on given properties
* @param properties list of passed properties to the DTO
*/
constructor(properties: any = {}) {
Object.keys(properties).forEach((key: string) => {
if (allowedProperties.includes(key)) this[key as keyof this] = properties[key];
});
}
}
* addres of the log file input
*/
@IsNotEmpty()
@IsDefined()
@IsString()
input: string;
//...........................................................................................................
/**
* address of the output log file
*/
@IsNotEmpty()
@IsDefined()
@IsString()
output: string;
//...........................................................................................................
/**
* list of log levels to be filtered by the parser
*/
@IsOptional()
@IsEnum(LogLevel, { each: true })
logLevel: LogLevel[];
//...........................................................................................................
/**
* Constructs the DTO based on given properties
* @param properties list of passed properties to the DTO
*/
constructor(properties: any = {}) {
Object.keys(properties).forEach((key: string) => {
if (allowedProperties.includes(key))
this[key as keyof this] = properties[key];
});
}
}
2 changes: 1 addition & 1 deletion src/parser/enum/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './log-level.enum'
export * from './log-level.enum';
Loading

0 comments on commit b5b730c

Please sign in to comment.