Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JCN-112-Log #1

Merged
merged 31 commits into from Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
122 changes: 122 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,122 @@
'use strict';

module.exports = {
extends: 'airbnb-base',

env: {
node: true,
es6: true,
mocha: true
},

globals: {
__rootpath: true,
coreRequire: true,
mainRequire: true,
JANIS_CORE: true,
JANIS_ENV: true,
JANIS_ENV_ALIAS: true
},

parserOptions: {
sourceType: 'script'
},

settings: {
'import/core-modules': ['aws-sdk', 'lodash', 'yamljs', 'openapi-schema-validator']
},

rules: {
'operator-linebreak': 0,
'no-continue': 0,
'no-plusplus': 0,
'prefer-spread': 0,
'prefer-rest-params': 0,
'class-methods-use-this': 0,
'consistent-return': 0,
'prefer-template': 0,
'import/no-unresolved': 0,
'import/no-extraneous-dependencies': ['error', { devDependencies: ['**/tests/**/*.js'] }],

'no-bitwise': 0,

curly: ['error', 'multi-or-nest'],

'no-underscore-dangle': ['warn', {
allowAfterThis: true,
allowAfterSuper: true,
allow: ['_call', '__rootpath', '_where']
}],

'no-tabs': 0,

'no-new': 0,

'func-names': 0,

'space-before-function-paren': ['error', {
'anonymous': 'never',
'named': 'never',
'asyncArrow': 'always'
}],

'arrow-parens': ['error', 'as-needed'],
'arrow-body-style': 0,

indent: ['error', 'tab', {
SwitchCase: 1
}],

'comma-dangle': ['error', 'never'],

'padded-blocks': 0,

'max-len': ['error', {
code: 150,
tabWidth: 1,
comments: 200
}],

'spaced-comment': ['error', 'always', {
exceptions: ['*']
}],

'newline-per-chained-call': ['error', {
ignoreChainWithDepth: 2
}],

'no-param-reassign': 0,

'no-prototype-builtins': 0,

'keyword-spacing': ['error', {
overrides: {
if: {
after: false
},
for: {
after: false
},
while: {
after: false
},
switch: {
after: false
},
catch: {
after: false
}
}
}],

'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'],
'function-paren-newline': 0,
'no-await-in-loop': 0,

'object-curly-newline': ['error', {
ObjectExpression: { minProperties: 5, multiline: true, consistent: true },
ObjectPattern: { minProperties: 5, multiline: true, consistent: true }
}],
'nonblock-statement-body-position': ['error', 'below', { overrides: { else: 'any' } }]
}
};
61 changes: 61 additions & 0 deletions .gitignore
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
5 changes: 5 additions & 0 deletions .huskyrc.json
@@ -0,0 +1,5 @@
{
"hooks": {
"pre-commit": "npm run lint && npm t"
}
}
20 changes: 20 additions & 0 deletions .nycrc
@@ -0,0 +1,20 @@
{
"exclude": [
"coverages/",
"tests/",
"mocks/",
".eslintrc.js",
".travis.yml"
],
"extension": [
".js"
],
"cache": true,
"all": true,
"default-excludes": true,
"check-coverage": true,
"statements": 95,
"branches": 95,
"functions": 95,
"lines": 95
}
15 changes: 15 additions & 0 deletions .travis.yml
@@ -0,0 +1,15 @@
language: node_js
node_js:
- "lts/*"
cache: npm
script:
- |
# Run test script
npm run test-ci
after_script:
- |
# Upload coverage to coveralls
if [[ -d .nyc_output ]]; then
npm install --save-dev coveralls@2
nyc report --reporter=text-lcov | coveralls
fi
13 changes: 13 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Log package
- Unit tests
- S3 Mock
- Docs
33 changes: 30 additions & 3 deletions README.md
@@ -1,7 +1,7 @@
# log
Nataniel4 marked this conversation as resolved.
Show resolved Hide resolved

[![Build Status](https://travis-ci.org/janis-commerce/log.svg?branch=master)](https://travis-ci.org/janis-commerce/log)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/log/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/log?branch=master)
[![Build Status](https://travis-ci.org/janis-commerce/log.svg?branch=JCN-112-log)](https://travis-ci.org/janis-commerce/log)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/log/badge.svg?branch=JCN-112-log)](https://coveralls.io/github/janis-commerce/log?branch=JCN-112-log)

A package for creating logs in S3

Expand All @@ -11,12 +11,39 @@ npm install @janiscommerce/log
```

## API
- `add(log, bucketName)`
Parameters: `log [Object]`, `bucketName [String]`
Puts the recieved log into the specified S3 bucket.

## Errors

The errors are informed with a `LogError`.
This object has a code that can be useful for a correct error handling.
The codes are the following:

| Code | Description |
|------|--------------------------------|
| 1 | Invalid log |
| 2 | Empty bucket |
| 3 | S3 Error |

## Usage
```js
const Log = require('@janiscommerce/log');

Log.add({
type: 1,
entity: 'api',
entity_id: 'product',
message: '[GET] Request from 0.0.0.0 of custom_data'
// ...
}, 'my-bucket');
```

## Examples
## Notes
In order to connect into the S3, this package uses ENV variables for getting the AWS access keys.

```sh
export AWS_ACCESS_KEY_ID='S3RVER'
export AWS_SECRET_ACCESS_KEY='S3RVER'
```
5 changes: 5 additions & 0 deletions index.js
@@ -0,0 +1,5 @@
'use strict';

const { Log } = require('./lib');

module.exports = Log;
9 changes: 9 additions & 0 deletions lib/index.js
@@ -0,0 +1,9 @@
'use strict';

const Log = require('./log');
const LogError = require('./log-error');

module.exports = {
Log,
LogError
};
23 changes: 23 additions & 0 deletions lib/log-error.js
@@ -0,0 +1,23 @@
'use strict';

class LogError extends Error {

static get codes() {

return {
INVALID_LOG: 1,
EMPTY_BUCKET: 2,
S3_ERROR: 3
};

}

constructor(err, code) {
super(err);
this.message = err.message || err;
this.code = code;
this.name = 'LogError';
}
}

module.exports = LogError;
61 changes: 61 additions & 0 deletions lib/log.js
@@ -0,0 +1,61 @@
'use strict';

const UUID = require('uuid/v4');

const AWS = require('aws-sdk');

const LogError = require('./log-error');

const S3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acá no hace falta pasarle las keys. De hecho, cuando se ejecute en lambda no hay keys.
Simplemente no lo pasás, y en el docker-compose.yml se monta el siguiente volumen - ~/.aws:/root/.aws

Ver el MS de router, que ya está implementado de esta manera.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PD: Documentar en el README.

secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});

class Log {

static add(log, bucket) {
Nataniel4 marked this conversation as resolved.
Show resolved Hide resolved

if(!bucket)
throw new LogError('Bucket name is required', LogError.codes.EMPTY_BUCKET);

if(!log || typeof log !== 'object' || Array.isArray(log))
throw new LogError('Invalid log', LogError.codes.INVALID_LOG);

const logDate = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perdón, pero qué necesidad de declarar un objeto con una prop y 3 getters, si se va a usar una sola vez, y los getters devuelven exactamente lo mismo cada vez que se los llama?

Es extremadamente más simple, fácil de entender y performante hacer simplemente

const date = log.date_created ? new Date(log.date_created * 1000) : new Date();
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, 0);
const day = date.getDate().toString()
		.padStart(2, 0);

date: log.date_created ? new Date(log.date_created * 1000) : new Date(),
Nataniel4 marked this conversation as resolved.
Show resolved Hide resolved
get year() {
return this.date.getFullYear();
},
get month() {
return (this.date.getMonth() + 1).toString().padStart(2, 0);
},
get day() {
return this.date.getDate().toString()
.padStart(2, 0);
}
};

if(!log.date_created)
log.date_created = Math.floor(logDate.date / 1000);

if(!log.id)
log.id = UUID();

try {

S3.putObject({
Bucket: bucket,
Key: `logs/${logDate.year}/${logDate.month}/${logDate.day}/${log.id}.json`,
Body: JSON.stringify(log),
ContentType: 'application/json'
});

} catch(err) {
throw new LogError(err.message, LogError.codes.S3_ERROR);
}

}

}

module.exports = Log;