Skip to content

Commit

Permalink
feat(webapp): refactor producerList view (#817)
Browse files Browse the repository at this point in the history
* fix(front-end): fix eden ratign action

* fix(front-end): fix eden ratign action

* fix(webapp): load eden ratings stats from view

* fix(webapp): style radar on rating page

* fix(webapp): remove comments
  • Loading branch information
AngeloCG97 authored Oct 8, 2021
1 parent dc8e83a commit b892b97
Show file tree
Hide file tree
Showing 31 changed files with 459 additions and 157 deletions.
3 changes: 1 addition & 2 deletions hapi/src/config/server.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

module.exports = {
host: process.env.HAPI_POSTGRES_HOST || 'postgres',
port: process.env.HAPI_POSTGRES_PORT || 5432,
database: process.env.HAPI_POSTGRES_DB || 'eosrate',
user: process.env.HAPI_POSTGRES_USER || 'eoscr',
password: process.env.HAPI_POSTGRES_PASSWORD || 'password'
}
}
19 changes: 13 additions & 6 deletions hapi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const init = async () => {
server.route({
method: 'GET',
path: '/',
handler: function() {
handler: function () {
return '<h2>EOS Rate HTTP API service</h2>'
}
})

server.route({
method: 'POST',
path: '/ratebp',
handler: async req => {
handler: async (req) => {
try {
const {
payload: { input }
Expand All @@ -33,7 +33,7 @@ const init = async () => {
if (!input) throw new Error('Invalid ratebp Input')

const {
ratingInput: { user, producer, transaction }
ratingInput: { user, producer, transaction, isEden }
} = input
const isValidAccountName = accountValidation([
{ name: user, type: 'user account' },
Expand All @@ -44,7 +44,12 @@ const init = async () => {
throw new Error(isValidAccountName.message)

const resultEden = await updateBpStats(producer)
const result = await updateUserRatings(user, producer, transaction)
const result = await updateUserRatings(
user,
producer,
transaction,
isEden
)

return { resultEden: resultEden, ...result }
} catch (error) {
Expand All @@ -57,10 +62,12 @@ const init = async () => {

await server.start()
console.log(`🚀 Server ready at ${server.info.uri}`)
server.table().forEach(route => console.log(`${route.method}\t${route.path}`))
server
.table()
.forEach((route) => console.log(`${route.method}\t${route.path}`))
}

process.on('unhandledRejection', err => {
process.on('unhandledRejection', (err) => {
console.log(err)
process.exit(1)
})
Expand Down
28 changes: 19 additions & 9 deletions hapi/src/libs/sync-ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { JsonRpc } = require('eosjs')
const fetch = require('node-fetch')
const { massiveDB } = require('../config')

const HAPI_EOS_API_ENDPOINT = process.env.HAPI_EOS_API_ENDPOINT || 'https://jungle.eosio.cr'
const HAPI_EOS_API_ENDPOINT =
process.env.HAPI_EOS_API_ENDPOINT || 'https://jungle.eosio.cr'
const HAPI_RATING_CONTRACT = process.env.HAPI_RATING_CONTRACT || 'rateproducer'

const getUserRatings = async () => {
Expand All @@ -14,14 +15,14 @@ const getUserRatings = async () => {
json: true,
code: HAPI_RATING_CONTRACT,
scope: HAPI_RATING_CONTRACT,
table: 'ratings',
table: 'rating',
limit: 1000,
reverse: false,
show_payer: false
})

return ratings
} catch (err) {
} catch (err) {
console.log(`Database connection error ${err}`)
return []
}
Expand All @@ -33,7 +34,6 @@ const updateUserRatings = async () => {

userRatings.rows.forEach(async (rating) => {
const ratingsCore = {
uniq_rating: rating.uniq_rating,
user: rating.user,
bp: rating.bp,
ratings: {
Expand All @@ -46,11 +46,21 @@ const updateUserRatings = async () => {
}

try {
const resultRatingsSave = await (await massiveDB).user_ratings.save(ratingsCore)
const dbResult = resultRatingsSave ? resultRatingsSave : await (await massiveDB).user_ratings.insert(ratingsCore)
console.log(`Save or insert of ${ratingsCore.uniq_rating} was ${dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL'}`)
} catch (err) { console.log(`Error: ${err}`) }
const resultRatingsSave = await (
await massiveDB
).user_ratings.save(ratingsCore)
const dbResult = resultRatingsSave
? resultRatingsSave
: await (await massiveDB).user_ratings.insert(ratingsCore)
console.log(
`Save or insert of ${ratingsCore.user}-${ratingsCore.bp} was ${
dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL'
}`
)
} catch (err) {
console.log(`Error: ${err}`)
}
})
};
}

updateUserRatings()
33 changes: 21 additions & 12 deletions hapi/src/libs/sync-user-rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const fetch = require('node-fetch')

const { massiveDB } = require('../config')

const HAPI_EOS_API_ENDPOINT = process.env.HAPI_EOS_API_ENDPOINT || 'https://jungle.eosio.cr'
const HAPI_EOS_API_ENDPOINT =
process.env.HAPI_EOS_API_ENDPOINT || 'https://jungle.eosio.cr'
const HAPI_RATING_CONTRACT = process.env.HAPI_RATING_CONTRACT || 'rateproducer'

// gets data from blockchain
const getUserRatings = async () => {
const getUserRatings = async (isEden) => {
const eos = new JsonRpc(HAPI_EOS_API_ENDPOINT, { fetch })

let ratings = await eos.get_table_rows({
json: true,
code: HAPI_RATING_CONTRACT,
scope: HAPI_RATING_CONTRACT,
table: 'ratings',
scope: isEden ? 'eden' : HAPI_RATING_CONTRACT,
table: 'rating',
limit: 1000,
reverse: false,
show_payer: false
Expand All @@ -24,11 +25,16 @@ const getUserRatings = async () => {
}

// updates the postgresdb
const updateUserRatings = async (userAccount, bpAccount, transaction) => {
const updateUserRatings = async (
userAccount,
bpAccount,
transaction,
isEden
) => {
console.log('==== Updating user ratings ====')

try {
const userRatings = await getUserRatings()
const userRatings = await getUserRatings(isEden)

if (!userAccount || !bpAccount)
throw new Error('User Account and Block Producer owner are required!')
Expand All @@ -47,29 +53,32 @@ const updateUserRatings = async (userAccount, bpAccount, transaction) => {
community: blockProducer.community || 0
}

const result = await (await massiveDB).user_ratings.save({
uniq_rating: blockProducer.uniq_rating,
const result = await (
await massiveDB
).user_ratings.save({
user: blockProducer.user,
bp: blockProducer.bp,
ratings: ratings,
tx_data: transaction
})

if (!result) {
const insertResult = await (await massiveDB).user_ratings.insert({
uniq_rating: blockProducer.uniq_rating,
const insertResult = await (
await massiveDB
).user_ratings.insert({
user: blockProducer.user,
bp: blockProducer.bp,
ratings,
tx_data: transaction
})

if (!insertResult)
throw new Error(`Could not save or insert ${blockProducer.uniq_rating}`)
throw new Error(
`Could not save or insert ${blockProducer.user}-${blockProducer.bp}`
)
}

return {
uniq_rating: blockProducer.uniq_rating,
user: blockProducer.user,
bp: blockProducer.bp,
ratings,
Expand Down
25 changes: 11 additions & 14 deletions hasura/metadata/actions.graphql
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
type Mutation {
rateProducer (
rateProducer(
ratingInput: RatingInput!
): RatingOutput
}




input RatingInput {
user : String!
producer : String!
transaction : jsonb!
user: String!
isEden: Boolean!
producer: String!
transaction: jsonb!
}

type RatingOutput {
message : String!
resultEden : jsonb
uniq_rating : String
user : String
bp : String
ratings : jsonb
message: String!
resultEden: jsonb
user: String
bp: String
ratings: jsonb
}

type deleteUserRateOutput {
message : String!
message: String!
}

Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
table:
name: producers_list
schema: public
select_permissions:
- permission:
allow_aggregations: true
columns:
- owner
- bpjson
- system
- candidate_name
- total_votes
- eden_average
- eden_ratings_cntr
- average
- transparency
- infrastructure
- trustiness
- community
- development
- ratings_cntr
- general_info
filter: {}
role: anonymous
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ select_permissions:
- bp
- ratings
- tx_data
- uniq_rating
- user
filter: {}
role: anonymous
Expand All @@ -18,7 +17,6 @@ update_permissions:
- bp
- ratings
- tx_data
- uniq_rating
- user
filter: {}
role: anonymous
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."user_ratings" add column "id" serial
-- not null unique;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."user_ratings" add column "id" serial
not null unique;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alter table "public"."user_ratings" drop constraint "user_ratings_pkey";
alter table "public"."user_ratings"
add constraint "user_ratings_pkey"
primary key ("uniq_rating");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BEGIN TRANSACTION;
ALTER TABLE "public"."user_ratings" DROP CONSTRAINT "user_ratings_pkey";

ALTER TABLE "public"."user_ratings"
ADD CONSTRAINT "user_ratings_pkey" PRIMARY KEY ("id");
COMMIT TRANSACTION;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table "public"."user_ratings" add constraint "user_ratings_uniq_rating_key" unique (uniq_rating);
alter table "public"."user_ratings" alter column "uniq_rating" drop not null;
alter table "public"."user_ratings" add column "uniq_rating" text;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "public"."user_ratings" drop column "uniq_rating" cascade;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- DROP VIEW "public"."producers_list";
--
-- CREATE OR REPLACE VIEW "public"."producers_list" AS
-- SELECT producers.owner,
-- producers.bpjson,
-- producers.system,
-- ((producers.bpjson -> 'org'::text) ->> 'candidate_name'::text) AS candidate_name,
-- (producers.system ->> 'total_votes'::text) AS total_votes,
-- ratings_stats.average,
-- ratings_stats.transparency,
-- ratings_stats.infrastructure,
-- ratings_stats.trustiness,
-- ratings_stats.community,
-- ratings_stats.development,
-- ratings_stats.ratings_cntr,
-- -- Eden Ratings Stats
-- eden_ratings_stats.average AS eden_average,
-- eden_ratings_stats.transparency AS eden_transparency,
-- eden_ratings_stats.infrastructure AS eden_infrastructure,
-- eden_ratings_stats.trustiness AS eden_trustiness,
-- eden_ratings_stats.community AS eden_community,
-- eden_ratings_stats.development AS eden_development,
-- eden_ratings_stats.ratings_cntr AS eden_ratings_cntr,
-- producers.general_info
-- FROM ((producers
-- FULL JOIN ratings_stats ON (((ratings_stats.bp)::text = producers.owner)))
-- FULL JOIN eden_ratings_stats ON (((eden_ratings_stats.bp)::text = producers.owner)));
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
DROP VIEW "public"."producers_list";

CREATE OR REPLACE VIEW "public"."producers_list" AS
SELECT producers.owner,
producers.bpjson,
producers.system,
((producers.bpjson -> 'org'::text) ->> 'candidate_name'::text) AS candidate_name,
(producers.system ->> 'total_votes'::text) AS total_votes,
ratings_stats.average,
ratings_stats.transparency,
ratings_stats.infrastructure,
ratings_stats.trustiness,
ratings_stats.community,
ratings_stats.development,
ratings_stats.ratings_cntr,
-- Eden Ratings Stats
eden_ratings_stats.average AS eden_average,
eden_ratings_stats.transparency AS eden_transparency,
eden_ratings_stats.infrastructure AS eden_infrastructure,
eden_ratings_stats.trustiness AS eden_trustiness,
eden_ratings_stats.community AS eden_community,
eden_ratings_stats.development AS eden_development,
eden_ratings_stats.ratings_cntr AS eden_ratings_cntr,
producers.general_info
FROM ((producers
FULL JOIN ratings_stats ON (((ratings_stats.bp)::text = producers.owner)))
FULL JOIN eden_ratings_stats ON (((eden_ratings_stats.bp)::text = producers.owner)));
Loading

0 comments on commit b892b97

Please sign in to comment.