Skip to content

Commit

Permalink
feat: add requests stats (#12)
Browse files Browse the repository at this point in the history
* feat: add requests stats

* fix: requests test

* v0.2.3

* chore: update version
  • Loading branch information
omarciovsena committed Dec 26, 2018
1 parent 3fdad7a commit 303aca9
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 31,157 deletions.
36 changes: 24 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
## [0.2.2](https://github.com/marciovsena/bibleapi/compare/0.2.0...0.2.2) (2018-11-30)
## [0.2.3](https://github.com/marciovsena/bibleapi/compare/v0.2.2...v0.2.3) (2018-12-26)


### Bug Fixes

- remove unused nodemon ([4b1ca0e](https://github.com/marciovsena/bibleapi/commit/4b1ca0e))
* copy and menu links on homepage ([#11](https://github.com/marciovsena/bibleapi/issues/11)) ([3fdad7a](https://github.com/marciovsena/bibleapi/commit/3fdad7a))
* requests test ([ebd597e](https://github.com/marciovsena/bibleapi/commit/ebd597e))


### Features

- posting new homepage ([acb2332](https://github.com/marciovsena/bibleapi/commit/acb2332))
* add requests stats ([68697e6](https://github.com/marciovsena/bibleapi/commit/68697e6))



## [0.2.2](https://github.com/marciovsena/bibleapi/compare/0.2.0...v0.2.2) (2018-11-30)

# 0.2.1 (2018-11-30)

### Bug Fixes

- **docs:** update version ([098832e](https://github.com/marciovsena/bibleapi/commit/098832e))
- change the sample user ([256be0d](https://github.com/marciovsena/bibleapi/commit/256be0d))
- circle ci ([#1](https://github.com/marciovsena/bibleapi/issues/1)) ([b58afe5](https://github.com/marciovsena/bibleapi/commit/b58afe5))
- move dependencies ([1783b1f](https://github.com/marciovsena/bibleapi/commit/1783b1f))
- set node version ([3dfb7e0](https://github.com/marciovsena/bibleapi/commit/3dfb7e0))
- set props on package.json ([c6f37ca](https://github.com/marciovsena/bibleapi/commit/c6f37ca))
* **docs:** update version ([098832e](https://github.com/marciovsena/bibleapi/commit/098832e))
* change the sample user ([256be0d](https://github.com/marciovsena/bibleapi/commit/256be0d))
* circle ci ([#1](https://github.com/marciovsena/bibleapi/issues/1)) ([b58afe5](https://github.com/marciovsena/bibleapi/commit/b58afe5))
* move dependencies ([1783b1f](https://github.com/marciovsena/bibleapi/commit/1783b1f))
* remove unused nodemon ([4b1ca0e](https://github.com/marciovsena/bibleapi/commit/4b1ca0e))
* set node version ([3dfb7e0](https://github.com/marciovsena/bibleapi/commit/3dfb7e0))
* set props on package.json ([c6f37ca](https://github.com/marciovsena/bibleapi/commit/c6f37ca))


### Features

- add patreon button ([#7](https://github.com/marciovsena/bibleapi/issues/7)) ([13f301e](https://github.com/marciovsena/bibleapi/commit/13f301e))
* add patreon button ([#7](https://github.com/marciovsena/bibleapi/issues/7)) ([13f301e](https://github.com/marciovsena/bibleapi/commit/13f301e))
* posting new homepage ([acb2332](https://github.com/marciovsena/bibleapi/commit/acb2332))



# 0.2.0 (2018-11-25)

- Init project


2 changes: 1 addition & 1 deletion apidoc/api_data.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apidoc/api_data.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apidoc/api_project.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apidoc/api_project.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "name": "bibleAPI", "version": "0.2.0", "description": "A RESTful API for Bible", "title": "bibleAPI", "url": "https://bibleapi.co/", "header": { "title": "bibleAPI", "index": "Home", "indexHref": "https://bibleapi.co" }, "template": { "forceLanguage": "en" }, "sampleUrl": false, "defaultVersion": "0.0.0", "apidoc": "0.3.0", "generator": { "name": "apidoc", "time": "2018-11-25T04:02:19.553Z", "url": "http://apidocjs.com", "version": "0.17.6" }}
{ "name": "bibleAPI", "version": "0.2.0", "description": "A RESTful API for Bible", "title": "bibleAPI", "url": "https://bibleapi.co/", "header": { "title": "bibleAPI", "index": "Home", "indexHref": "https://bibleapi.co" }, "template": { "forceLanguage": "en" }, "sampleUrl": false, "defaultVersion": "0.0.0", "apidoc": "0.3.0", "generator": { "name": "apidoc", "time": "2018-12-08T23:25:23.298Z", "url": "http://apidocjs.com", "version": "0.17.6" }}
Expand Down
113 changes: 113 additions & 0 deletions app/Controllers/Http/RequestController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict'

const Database = use('Database')
const Request = use('App/Models/Request')
const moment = require('moment')
const { newRequest } = use('App/utils')

const getPeriod = period => {
switch (period) {
case 'month':
return moment().subtract(30, 'days')
case 'week':
return moment().subtract(7, 'days')
default:
return moment().subtract(1, 'days')
}
}

class RequestController {
/**
* @api {get} api/requests/:period? Get Requests
* @apiVersion 0.2.0
* @apiName show
* @apiGroup Requests
*
*
* @apiParam {String} period [month, week, day]
*
* @apiSuccess {String} url Requisition url
* @apiSuccess {String} data Requisition date
*
* @apiExample Example usage:
* curl -i https://bibleapi.co/api/requests/month
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* [{
* "url": "/api/books/gn",
* "date": "2018-12-08T22:16:52.000Z"
* }]
*
* @apiError TooManyRequests When performing more than 3 requests at the same endpoint in an interval less than 5 minutes
*
* @apiErrorExample {text} Error-Response:
* HTTP/1.1 429 Too Many Requests
*
*
*/

async show({ request, params }) {
newRequest(request.url())
const { period } = params
const date = getPeriod(period)
const requests = date
? await Request.query().where('created_at', '>', date)
: await Request.query()
return await requests.map(request => ({
url: request.url,
date: request.created_at
}))
}

/**
* @api {get} api/requests/count/:period? Get Count Requests
* @apiVersion 0.2.0
* @apiName count
* @apiGroup Requests
*
*
* @apiParam {String} period [month, week, day]
*
* @apiSuccess {String} url Requisition url
* @apiSuccess {String} count Number of requisitions in the period
*
* @apiExample Example usage:
* curl -i https://bibleapi.co/api/requests/count/month
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* [{
* "url": "/api/books/gn",
* "count": "3",
* }]
*
* @apiError TooManyRequests When performing more than 3 requests at the same endpoint in an interval less than 5 minutes
*
* @apiErrorExample {text} Error-Response:
* HTTP/1.1 429 Too Many Requests
*
*
*/

async count({ request, params }) {
newRequest(request.url())
const { period } = params
const date = getPeriod(period)
const requests = date
? await Database.count()
.select('url')
.from('requests')
.where('created_at', '>', date)
.groupBy('url')
.orderBy('count', 'desc')
: await Database.count()
.select('url')
.from('requests')
.groupBy('url')
.orderBy('count', 'desc')
return requests
}
}

module.exports = RequestController
17 changes: 11 additions & 6 deletions app/Controllers/Http/SearchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Book = use('App/Models/Book')
const Verse = use('App/Models/Verse')
const { notFound, badRequest } = use('App/utils')
const { notFound, badRequest, newRequest } = use('App/utils')

class SearchController {
/**
Expand Down Expand Up @@ -62,7 +62,8 @@ class SearchController {
*
*/

async getVerse({ params, response }) {
async getVerse({ request, params, response }) {
newRequest(request.url())
const { chapter, number, book, version } = params
const bookData = await Book.query().where('abbrev', book)
if (bookData.length > 0) {
Expand Down Expand Up @@ -178,7 +179,8 @@ class SearchController {
*
*/

async getChapter({ params, response }) {
async getChapter({ request, params, response }) {
newRequest(request.url())
const { chapter, book, version } = params
const bookData = await Book.query().where('abbrev', book)
if (bookData.length > 0) {
Expand Down Expand Up @@ -277,7 +279,8 @@ class SearchController {
* HTTP/1.1 429 Too Many Requests
*/

async getBooks() {
async getBooks({ request }) {
newRequest(request.url())
const books = await Book.query()
return books.map(book => ({
abbrev: book.abbrev,
Expand Down Expand Up @@ -335,7 +338,8 @@ class SearchController {
*
*/

async getBook({ params, response }) {
async getBook({ request, params, response }) {
newRequest(request.url())
const book = await Book.findBy('abbrev', params.book)
return book
? {
Expand Down Expand Up @@ -417,7 +421,8 @@ class SearchController {
* }
*/

async search({ response, request }) {
async search({ request, response }) {
newRequest(request.url())
const { version, search } = request.all()
if (!version || !search) {
return badRequest(
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class Request extends Model {
}

module.exports = Request
14 changes: 13 additions & 1 deletion app/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Request = use('App/Models/Request')

const notFound = response => {
response.status(404)
return {
Expand All @@ -18,7 +20,17 @@ const badRequest = (response, message) => {
}
}

const newRequest = async url => {
const req = { url }
try {
await Request.create(req)
} catch (err) {
console.log(err)
}
}

module.exports = {
badRequest,
notFound
notFound,
newRequest
}
28 changes: 28 additions & 0 deletions database/data/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const requests = [
{
url: '/api/books/gn'
},
{
url: '/api/books'
},
{
url: '/api/requests/count/week'
},
{
url: '/api/requests/count/week'
},
{
url: '/api/requests/count/month'
},
{
url: '/api/requests/'
},
{
url: '/api/requests/week'
},
{
url: '/api/requests/count/month'
}
]

module.exports = requests

0 comments on commit 303aca9

Please sign in to comment.