Skip to content

Commit 2e34eca

Browse files
authored
feat(hapi): created eosrate stats api endpoint (#993)
* fix(enviroment): change env value * fix(hasura): add permissions tables * fix(comments): fix comments section * fix(hyperion): fix codefactor issues * fix(hyperion): fix codefactor issues * fix(hasura): delete migration restriction * fix(hyperion): fix updater flows * feat(hapi): created eosrate stats api endpoint * fix(hapi): fix codefactor error
1 parent 83675c4 commit 2e34eca

19 files changed

+568
-233
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ HAPI_POSTGRES_SCHEMA=public
1919
HAPI_SERVER_PORT=9090
2020
HAPI_SERVER_ADDRESS=0.0.0.0
2121
HAPI_EOS_API_ENDPOINT=https://jungle.edenia.cloud
22+
HAPI_VALID_USERS=[{"username": "username", "password": "password"}]
2223
HAPI_EOS_CHAIN_ID=aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906
2324
HAPI_PROXY_CONTRACT=proxyaccount
2425
HAPI_RATING_CONTRACT=rateproducer

.github/workflows/push-master-environment.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ jobs:
7676
# hapi
7777
HAPI_SERVER_ADDRESS: 0.0.0.0
7878
HAPI_SERVER_PORT: 9090
79+
HAPI_VALID_USERS: ${{secrets.HAPI_VALID_USERS}}
7980
HAPI_POSTGRES_USER: ${{ secrets.HAPI_POSTGRES_USER }}
8081
HAPI_POSTGRES_PASSWORD: ${{ secrets.HAPI_POSTGRES_PASSWORD }}
8182
HAPI_HASURA_URL: ${{ secrets.HAPI_HASURA_URL }}

.github/workflows/push-staging-environment.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ jobs:
7777
# hapi
7878
HAPI_SERVER_ADDRESS: 0.0.0.0
7979
HAPI_SERVER_PORT: 9090
80+
HAPI_VALID_USERS: ${{secrets.HAPI_VALID_USERS}}
8081
HAPI_POSTGRES_USER: ${{ secrets.HAPI_POSTGRES_USER }}
8182
HAPI_POSTGRES_PASSWORD: ${{ secrets.HAPI_POSTGRES_PASSWORD }}
8283
HAPI_HASURA_URL: ${{ secrets.HAPI_HASURA_URL }}

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ services:
3838
environment:
3939
HAPI_SERVER_ADDRESS: "${HAPI_SERVER_ADDRESS}"
4040
HAPI_SERVER_PORT: "${HAPI_SERVER_PORT}"
41+
HAPI_VALID_USERS: "${HAPI_VALID_USERS}"
4142
HAPI_POSTGRES_USER: "${HAPI_POSTGRES_USER}"
4243
HAPI_POSTGRES_PASSWORD: "${HAPI_POSTGRES_PASSWORD}"
4344
HAPI_POSTGRES_PORT: "${HAPI_POSTGRES_PORT}"

hapi/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
"author": "EOS Costa Rica",
1818
"license": "MIT",
1919
"dependencies": {
20+
"@hapi/basic": "^7.0.0",
2021
"@hapi/boom": "^10.0.0",
2122
"@hapi/hapi": "^20.1.5",
2223
"axios": "^0.26.1",
24+
"bcrypt": "^5.1.0",
2325
"eosjs": "^22.1.0",
2426
"eosjs-api": "^7.0.4",
2527
"graphql": "^16.3.0",

hapi/src/index.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
'use strict'
2-
const { HAPI_SERVER_PORT, HAPI_SERVER_ADDRESS } = process.env
2+
const { HAPI_SERVER_PORT, HAPI_SERVER_ADDRESS, HAPI_VALID_USERS } = process.env
33

44
const Hapi = require('@hapi/hapi')
5+
const Bcrypt = require('bcrypt')
56

67
const routes = require('./routes')
78
const { workerService } = require('./services')
89

10+
const validate = async (request, username, password) => {
11+
if (!HAPI_VALID_USERS) return { credentials: null, isValid: false }
12+
13+
const users = JSON.parse(HAPI_VALID_USERS || '[]')
14+
const user = users.find(user => user.username === username)
15+
16+
if (!user) {
17+
return { credentials: null, isValid: false }
18+
}
19+
20+
const saltRounds = 10
21+
const hashPassword = await Bcrypt.hash(user.password, saltRounds)
22+
const isValid = await Bcrypt.compare(password, hashPassword)
23+
const credentials = { id: users.indexOf(user), name: user.username }
24+
25+
return { isValid, credentials }
26+
}
27+
928
const init = async () => {
1029
const server = Hapi.server({
1130
port: HAPI_SERVER_PORT,
@@ -16,6 +35,10 @@ const init = async () => {
1635
debug: { request: ['handler'] }
1736
})
1837

38+
await server.register(require('@hapi/basic'))
39+
40+
server.auth.strategy('simple', 'basic', { validate })
41+
1942
server.route(routes)
2043

2144
await server.start()
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Boom = require('@hapi/boom')
2+
const Joi = require('joi')
3+
4+
const { ratesStatsService } = require('../services')
5+
6+
module.exports = {
7+
method: 'POST',
8+
path: '/get-rates-stats',
9+
handler: async ({ payload: { input } }) => {
10+
try {
11+
if (!input) throw new Error('Invalid get-rates-stats Input')
12+
13+
const whereCondition = input?.ratesStatsInput?.bps
14+
? { _in: input?.ratesStatsInput?.bps }
15+
: { _nin: [] }
16+
const bpsStats = await ratesStatsService.getRatesStats({
17+
where: { bp: whereCondition }
18+
})
19+
20+
return { bpsStats }
21+
} catch (error) {
22+
console.error('get-rates-stats', error)
23+
24+
return Boom.badRequest(error.message)
25+
}
26+
},
27+
options: {
28+
validate: {
29+
payload: Joi.object({
30+
input: Joi.object({
31+
ratesStatsInput: Joi.object({
32+
bps: Joi.array().items(Joi.string().required()).optional()
33+
}).required()
34+
}).required()
35+
}).options({ stripUnknown: true })
36+
},
37+
auth: 'simple'
38+
}
39+
}

hapi/src/routes/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const healthzRoute = require('./healthz.route')
22
const ratebpRoute = require('./ratebp.route')
3+
const getRatesStats = require('./get-rates-stats.route')
34

4-
module.exports = [healthzRoute, ratebpRoute]
5+
module.exports = [healthzRoute, ratebpRoute, getRatesStats]

hapi/src/routes/ratebp.route.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = {
5252
}).required()
5353
}).required()
5454
}).options({ stripUnknown: true })
55-
}
55+
},
56+
auth: false
5657
}
5758
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { hasuraUtil } = require('../utils')
2+
3+
const getRatesStats = async ({ where }) => {
4+
const query = `
5+
query ($where: total_ratings_stats_bool_exp!) {
6+
total_ratings_stats(where: $where) {
7+
average
8+
bp
9+
community
10+
development
11+
infrastructure
12+
ratings_cntr
13+
transparency
14+
trustiness
15+
}
16+
}
17+
`
18+
const data = await hasuraUtil.instance.request(query, { where })
19+
20+
return data?.total_ratings_stats || [{}]
21+
}
22+
23+
module.exports = {
24+
getRatesStats
25+
}

hapi/src/services/hyperion/updaters/rateproducer-logcomment.updater.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = {
3838
})
3939

4040
const [blockProducer] = userRatings.rows.filter(
41-
({ id }) => id == ratingId
41+
({ id }) => id === ratingId
4242
)
4343

4444
await save({

hapi/src/services/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const workerService = require('./worker.service')
2+
const ratesStatsService = require('./get-rates-stats.service')
23

34
module.exports = {
4-
workerService
5+
workerService,
6+
ratesStatsService
57
}

0 commit comments

Comments
 (0)