From 6137bcd8fa82000b29c780ad7a721711e6874ae3 Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Mon, 13 Feb 2023 23:38:00 -0600 Subject: [PATCH 01/13] fix(webapp): show right data in delegate view --- hapi/src/gql/eden-transaction.gql.js | 1 + ..._total_expense_by_delegate_and_election.js | 81 ++++++++++++++++++ hapi/src/gql/index.js | 3 +- .../edenexplorer-categorize.updater.js | 25 +++++- .../updaters/eosiotoken-transfer.updater.js | 19 ++++- hapi/src/utils/updater.util.js | 82 ++++++++++++++++++- .../default/tables/public_eden_election.yaml | 7 ++ ...ublic_eden_global_expense_by_election.yaml | 3 + ...otal_expense_by_delegate_and_election.yaml | 35 ++++++++ ...public_total_expense_by_election_view.yaml | 11 +++ .../databases/default/tables/tables.yaml | 3 + .../down.sql | 1 + .../up.sql | 2 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 4 + .../up.sql | 6 ++ .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 1 + .../up.sql | 1 + .../1676352327360_run_sql_migration/down.sql | 6 ++ .../1676352327360_run_sql_migration/up.sql | 4 + webapp/src/gql/eden-expense.gql.js | 16 ++-- webapp/src/gql/eden-transaction.gql.js | 12 +++ .../customHooks/useDelegateReportState.js | 33 ++++++-- .../customHooks/useExpenseReportState.js | 1 + webapp/src/utils/new-format-objects.js | 36 +++----- 30 files changed, 363 insertions(+), 44 deletions(-) create mode 100644 hapi/src/gql/eden_total_expense_by_delegate_and_election.js create mode 100644 hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml create mode 100644 hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml create mode 100644 hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml create mode 100644 hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql create mode 100644 hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql create mode 100644 hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql create mode 100644 hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql create mode 100644 hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql create mode 100644 hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql create mode 100644 hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql create mode 100644 hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql create mode 100644 hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql create mode 100644 hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql create mode 100644 hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql create mode 100644 hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql create mode 100644 hasura/migrations/default/1676352327360_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1676352327360_run_sql_migration/up.sql diff --git a/hapi/src/gql/eden-transaction.gql.js b/hapi/src/gql/eden-transaction.gql.js index f628e360..90a4f47c 100644 --- a/hapi/src/gql/eden-transaction.gql.js +++ b/hapi/src/gql/eden-transaction.gql.js @@ -28,6 +28,7 @@ const get = async (where, getMany = false) => { txid type updated_at + eos_exchange digest } } diff --git a/hapi/src/gql/eden_total_expense_by_delegate_and_election.js b/hapi/src/gql/eden_total_expense_by_delegate_and_election.js new file mode 100644 index 00000000..38775c7a --- /dev/null +++ b/hapi/src/gql/eden_total_expense_by_delegate_and_election.js @@ -0,0 +1,81 @@ +const { hasuraUtil } = require('../utils') + +const save = async payload => { + const mutation = ` + mutation ($payload: total_expense_by_delegate_and_election_insert_input!) { + insert_total_expense_by_delegate_and_election_one(object: $payload) { + id + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { payload }) + + return data.insert_total_expense_by_delegate_and_election_one +} + +const get = async (where, getMany = false) => { + const query = ` + query ($where: total_expense_by_delegate_and_election_bool_exp) { + total_expense_by_delegate_and_election(where: $where) { + id + eos_amount + usd_amount + id_election + category + } + } + ` + const { total_expense_by_delegate_and_election } = + await hasuraUtil.instance.request(query, { + where + }) + + return getMany + ? total_expense_by_delegate_and_election + : total_expense_by_delegate_and_election[0] +} + +const update = async ({ where, _set }) => { + const mutation = ` + mutation update($where: total_expense_by_delegate_and_election_bool_exp!, $_set: total_expense_by_delegate_and_election_set_input) { + update_total_expense_by_delegate_and_election(where: $where, _set: $_set) { + returning { + id + } + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { + where, + _set + }) + + return data.update_eden_transaction +} + +const getAggregate = async where => { + const query = ` + query ($where: total_expense_by_delegate_and_election_bool_exp) { + total_expense_by_delegate_and_election_aggregate(where: $where) { + aggregate { + sum { + eos_amount + } + } + } + } + ` + const { total_expense_by_delegate_and_election } = + await hasuraUtil.instance.request(query, { + where + }) + + return total_expense_by_delegate_and_election?.aggregate?.sum?.eos_amount || 0 +} + +module.exports = { + save, + get, + update, + getAggregate +} diff --git a/hapi/src/gql/index.js b/hapi/src/gql/index.js index 55bbe752..ff158598 100644 --- a/hapi/src/gql/index.js +++ b/hapi/src/gql/index.js @@ -3,5 +3,6 @@ module.exports = { edenTreasury: require('./eden-treasury.gql'), edenDelegatesGql: require('./eden-delegates.gql'), edenElectionGql: require('./eden-election.gql'), - edenHistoricElectionGql: require('./eden-historic-election.gql') + edenHistoricElectionGql: require('./eden-historic-election.gql'), + edenTotalExpenseByDelegateAndElection: require('./eden_total_expense_by_delegate_and_election') } diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index 158dc954..e7de0db9 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -1,4 +1,8 @@ -const { edenTransactionGql, edenElectionGql } = require('../../../gql') +const { + edenTransactionGql, + edenElectionGql, + edenTotalExpenseByDelegateAndElection +} = require('../../../gql') const { updaterUtil } = require('../../../utils') module.exports = { @@ -39,6 +43,25 @@ module.exports = { } await edenTransactionGql.update(updateQuery) + if (transactionToEdit.category === 'uncategorzed') { + await updaterUtil.saveTotalByDelegateAndElection( + transactionToEdit.txid, + account, + transactionToEdit.amount, + transactionToEdit.eos_exchange, + transactionToEdit.category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection + ) + } else { + await edenTotalExpenseByDelegateAndElection.update({ + where: { + tx_id: { _eq: transactionToEdit.txid } + }, + _set: { category } + }) + } } catch (error) { console.error(`categorize updater sync error: ${error.message}`) } diff --git a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js index e1da035e..a82ff11e 100644 --- a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js +++ b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js @@ -1,7 +1,11 @@ const moment = require('moment') const { sleepUtil, communityUtil, updaterUtil } = require('../../../utils') -const { edenTransactionGql, edenElectionGql } = require('../../../gql') +const { + edenTransactionGql, + edenElectionGql, + edenTotalExpenseByDelegateAndElection +} = require('../../../gql') const { transactionConstant } = require('../../../constants') let LASTEST_RATE_DATE_CONSULTED = null @@ -88,6 +92,19 @@ module.exports = { } await edenTransactionGql.save(transactionData) + + if (category === 'uncategorized') return + + await updaterUtil.saveTotalByDelegateAndElection( + action.transaction_id, + from, + amount, + LASTEST_RATE_DATA_CONSULTED, + category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection + ) } catch (error) { console.error( `transfer sync error ${action.action.name}: ${error.message}` diff --git a/hapi/src/utils/updater.util.js b/hapi/src/utils/updater.util.js index 00e2af6a..71c64986 100644 --- a/hapi/src/utils/updater.util.js +++ b/hapi/src/utils/updater.util.js @@ -64,8 +64,88 @@ const getElectionWithoutExpense = async ( } } +const saveTotalByDelegateAndElection = async ( + txId, + delegateAccount, + eosAmount, + eosRate, + category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection +) => { + try { + let tempAmount = eosAmount + const electionsQuery = { + eden_delegate: { account: { _eq: delegateAccount } } + } + const elections = await edenElectionGql.get(electionsQuery, true) + + for (const { id, election } of elections) { + const incomeQuery = { + eden_election: { + eden_delegate: { account: { _eq: delegateAccount } }, + election: { _eq: election } + }, + type: { _eq: 'income' } + } + + const totalByDelegateAndElectionQuery = { + id_election: { _eq: id } + } + + const income = (await edenTransactionGql.getAggregate(incomeQuery)) || 0 + + const totalByDelegateAndElection = + (await edenTotalExpenseByDelegateAndElection.getAggregate( + totalByDelegateAndElectionQuery + )) || 0 + + if (totalByDelegateAndElection + tempAmount > income) { + const totalToSave = income - totalByDelegateAndElection + + const totalByDelegateAndElectionData = { + eos_amount: totalToSave, + usd_amount: totalToSave * eosRate, + id_election: id, + category, + tx_id: txId, + election + } + + await edenTotalExpenseByDelegateAndElection.save( + totalByDelegateAndElectionData + ) + + tempAmount = tempAmount - totalToSave + } else { + const totalByDelegateAndElectionData = { + eos_amount: tempAmount, + usd_amount: tempAmount * eosRate, + id_election: id, + category, + tx_id: txId, + election + } + + await edenTotalExpenseByDelegateAndElection.save( + totalByDelegateAndElectionData + ) + + return + } + } + } catch (error) { + console.error( + 'An error occurred in saveTotalByDelegateAndElection: ', + error + ) + } +} + module.exports = { memoSplit, isEdenExpense, - getElectionWithoutExpense + getElectionWithoutExpense, + saveTotalByDelegateAndElection } diff --git a/hasura/metadata/databases/default/tables/public_eden_election.yaml b/hasura/metadata/databases/default/tables/public_eden_election.yaml index 06767ab7..ff759b4a 100644 --- a/hasura/metadata/databases/default/tables/public_eden_election.yaml +++ b/hasura/metadata/databases/default/tables/public_eden_election.yaml @@ -13,6 +13,13 @@ array_relationships: table: name: eden_transaction schema: public + - name: total_expense_by_delegate_and_elections + using: + foreign_key_constraint_on: + column: id_election + table: + name: total_expense_by_delegate_and_election + schema: public select_permissions: - role: guest permission: diff --git a/hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml b/hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml new file mode 100644 index 00000000..a7604393 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml @@ -0,0 +1,3 @@ +table: + name: eden_global_expense_by_election + schema: public diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml new file mode 100644 index 00000000..94ef11d7 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml @@ -0,0 +1,35 @@ +table: + name: total_expense_by_delegate_and_election + schema: public +object_relationships: + - name: eden_election + using: + foreign_key_constraint_on: id_election +insert_permissions: + - role: guest + permission: + check: {} + columns: + - category + - election + - eos_amount + - usd_amount + - create_at + - updated_at + - id + - id_election + - tx_id +select_permissions: + - role: guest + permission: + columns: + - category + - election + - eos_amount + - usd_amount + - create_at + - updated_at + - id + - id_election + - tx_id + filter: {} diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml new file mode 100644 index 00000000..83db3be5 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml @@ -0,0 +1,11 @@ +table: + name: total_expense_by_election_view + schema: public +select_permissions: + - role: guest + permission: + columns: + - total_eos_amount + - total_usd_amount + - id_election + filter: {} diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml index 47d775bb..61bd7083 100644 --- a/hasura/metadata/databases/default/tables/tables.yaml +++ b/hasura/metadata/databases/default/tables/tables.yaml @@ -1,5 +1,6 @@ - "!include public_eden_delegates.yaml" - "!include public_eden_election.yaml" +- "!include public_eden_global_expense_by_election.yaml" - "!include public_eden_historic_election.yaml" - "!include public_eden_transaction.yaml" - "!include public_eden_treasury.yaml" @@ -12,5 +13,7 @@ - "!include public_total_by_category_and_election.yaml" - "!include public_total_by_election.yaml" - "!include public_total_expense_by_all_election.yaml" +- "!include public_total_expense_by_delegate_and_election.yaml" +- "!include public_total_expense_by_election_view.yaml" - "!include public_total_income_by_all_elections.yaml" - "!include public_transaction_by_category_and_election.yaml" diff --git a/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql new file mode 100644 index 00000000..8bfd841a --- /dev/null +++ b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql @@ -0,0 +1 @@ +DROP TABLE "public"."total_expense_by_delegate_and_election"; diff --git a/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql new file mode 100644 index 00000000..bbfe3299 --- /dev/null +++ b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql @@ -0,0 +1,2 @@ +CREATE TABLE "public"."total_expense_by_delegate_and_election" ("id" uuid NOT NULL DEFAULT gen_random_uuid(), "eos_amount" numeric NOT NULL DEFAULT 0, "usd_amount" numeric NOT NULL DEFAULT 0, "id_election" uuid NOT NULL, "category" varchar NOT NULL, "create_at" timestamptz NOT NULL DEFAULT now(), "updated_at" timestamptz NOT NULL DEFAULT now(), PRIMARY KEY ("id","id_election") , FOREIGN KEY ("id_election") REFERENCES "public"."eden_election"("id") ON UPDATE restrict ON DELETE restrict, UNIQUE ("id"), UNIQUE ("id_election")); +CREATE EXTENSION IF NOT EXISTS pgcrypto; diff --git a/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql new file mode 100644 index 00000000..ce4cbda1 --- /dev/null +++ b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql @@ -0,0 +1 @@ +alter table "public"."total_expense_by_delegate_and_election" add constraint "total_expense_by_delegate_and_election_id_election_key" unique ("id_election"); diff --git a/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql new file mode 100644 index 00000000..fa1d79e4 --- /dev/null +++ b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql @@ -0,0 +1 @@ +alter table "public"."total_expense_by_delegate_and_election" drop constraint "total_expense_by_delegate_and_election_id_election_key"; diff --git a/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql new file mode 100644 index 00000000..c40af06d --- /dev/null +++ b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql @@ -0,0 +1,4 @@ +alter table "public"."total_expense_by_delegate_and_election" drop constraint "total_expense_by_delegate_and_election_pkey"; +alter table "public"."total_expense_by_delegate_and_election" + add constraint "total_expense_by_delegate_and_election_pkey" + primary key ("id", "id_election"); diff --git a/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql new file mode 100644 index 00000000..e58b6043 --- /dev/null +++ b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql @@ -0,0 +1,6 @@ +BEGIN TRANSACTION; +ALTER TABLE "public"."total_expense_by_delegate_and_election" DROP CONSTRAINT "total_expense_by_delegate_and_election_pkey"; + +ALTER TABLE "public"."total_expense_by_delegate_and_election" + ADD CONSTRAINT "total_expense_by_delegate_and_election_pkey" PRIMARY KEY ("id"); +COMMIT TRANSACTION; diff --git a/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql new file mode 100644 index 00000000..db189778 --- /dev/null +++ b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."total_expense_by_delegate_and_election" add column "election" integer +-- not null default '0'; diff --git a/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql new file mode 100644 index 00000000..703f3596 --- /dev/null +++ b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql @@ -0,0 +1,2 @@ +alter table "public"."total_expense_by_delegate_and_election" add column "election" integer + not null default '0'; diff --git a/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql new file mode 100644 index 00000000..2779ba67 --- /dev/null +++ b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."total_expense_by_delegate_and_election" add column "tx_id" uuid +-- null; diff --git a/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql new file mode 100644 index 00000000..66fcbe59 --- /dev/null +++ b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql @@ -0,0 +1,2 @@ +alter table "public"."total_expense_by_delegate_and_election" add column "tx_id" uuid + null; diff --git a/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql new file mode 100644 index 00000000..8dfe99fc --- /dev/null +++ b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."total_expense_by_delegate_and_election" ALTER COLUMN "tx_id" TYPE uuid; diff --git a/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql new file mode 100644 index 00000000..c50def10 --- /dev/null +++ b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."total_expense_by_delegate_and_election" ALTER COLUMN "tx_id" TYPE text; diff --git a/hasura/migrations/default/1676352327360_run_sql_migration/down.sql b/hasura/migrations/default/1676352327360_run_sql_migration/down.sql new file mode 100644 index 00000000..09445d43 --- /dev/null +++ b/hasura/migrations/default/1676352327360_run_sql_migration/down.sql @@ -0,0 +1,6 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW total_expense_by_election_view AS +-- SELECT id_election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY id_election; diff --git a/hasura/migrations/default/1676352327360_run_sql_migration/up.sql b/hasura/migrations/default/1676352327360_run_sql_migration/up.sql new file mode 100644 index 00000000..0dc43c67 --- /dev/null +++ b/hasura/migrations/default/1676352327360_run_sql_migration/up.sql @@ -0,0 +1,4 @@ +CREATE OR REPLACE VIEW total_expense_by_election_view AS +SELECT id_election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +FROM total_expense_by_delegate_and_election +GROUP BY id_election; diff --git a/webapp/src/gql/eden-expense.gql.js b/webapp/src/gql/eden-expense.gql.js index baf3827a..39e9fc8d 100644 --- a/webapp/src/gql/eden-expense.gql.js +++ b/webapp/src/gql/eden-expense.gql.js @@ -92,17 +92,17 @@ export const GET_EXPENSES_BY_ACCOUNT = gql` } } ` + export const GET_EXPENSE_BY_CATEGORY = gql` - query getExpenseByCategory($delegate: String, $election: Int) { - expenses_by_category_and_delegate( - where: { - election: { _eq: $election } - delegate_payer: { _eq: $delegate } - } + query getExpenseByCategory($id_election: uuid) { + total_expense_by_delegate_and_election( + where: { id_election: { _eq: $id_election } } ) { category - amount - usd_total + eos_amount + id + id_election + tx_id } } ` diff --git a/webapp/src/gql/eden-transaction.gql.js b/webapp/src/gql/eden-transaction.gql.js index 86526960..515c7d93 100644 --- a/webapp/src/gql/eden-transaction.gql.js +++ b/webapp/src/gql/eden-transaction.gql.js @@ -1,5 +1,17 @@ import gql from 'graphql-tag' +export const GET_TOTAL_EXPENSE_BY_ELECTION = gql` + query getTotalExpenseByDelegateView($id_election: uuid) { + total_expense_by_election_view( + where: { id_election: { _eq: $id_election } } + ) { + id_election + total_eos_amount + total_usd_amount + } + } +` + export const GET_TRANSACTIONS_BY_DELEGATE_AND_ELECTION = gql` query getTransactionsByDelegateAndElection( $election: Int diff --git a/webapp/src/hooks/customHooks/useDelegateReportState.js b/webapp/src/hooks/customHooks/useDelegateReportState.js index 99ab565d..1d3409ad 100644 --- a/webapp/src/hooks/customHooks/useDelegateReportState.js +++ b/webapp/src/hooks/customHooks/useDelegateReportState.js @@ -9,6 +9,7 @@ import { GET_MAX_DELEGATE_LEVEL, GET_HISTORIC_ELECTIONS, GET_EXPENSE_BY_CATEGORY, + GET_TOTAL_EXPENSE_BY_ELECTION, GET_TOTAL_DELEGATE_INCOME_BY_ELECTION, GET_TRANSACTIONS_BY_DELEGATE_AND_ELECTION } from '../../gql' @@ -37,6 +38,9 @@ const useDelegateReportState = () => { const loadDelegatesByElection = useImperativeQuery( GET_TOTAL_DELEGATE_INCOME_BY_ELECTION ) + const getTotalExpenseByDelegate = useImperativeQuery( + GET_TOTAL_EXPENSE_BY_ELECTION + ) useEffect(async () => { const { data: electionsData } = await loadElections() @@ -84,9 +88,21 @@ const useDelegateReportState = () => { useEffect(async () => { if (!delegateSelect) return - const responseCategory = await loadCategoryList({ - election: Number(electionRoundSelect), - delegate: delegateSelect + // TODO: HERE + // const responseCategory = await loadCategoryList({ + // election: Number(electionRoundSelect), + // delegate: delegateSelect + // }) + + const { data: electionId } = await loadElections({ + where: { + eden_delegate: { account: { _eq: delegateSelect } }, + election: { _eq: electionRoundSelect } + } + }) + + const { data: responseCategory } = await loadCategoryList({ + id_election: electionId?.eden_election[0].id }) const responseTransaction = await loadTransactions({ @@ -94,13 +110,18 @@ const useDelegateReportState = () => { delegate: delegateSelect }) + const { data: responseTotalExpenseByElection } = + await getTotalExpenseByDelegate({ + id_election: electionId?.eden_election[0].id + }) + const transactions = newDataFormatByTypeDelegate( responseTransaction.data.historic_incomes || [], - responseTransaction.data.historic_expenses || [] + responseTotalExpenseByElection.total_expense_by_election_view || [] ) + const categories = newDataFormatByCategoryDelegate( - responseCategory.data?.expenses_by_category_and_delegate || [], - transactions + responseCategory?.total_expense_by_delegate_and_election || [] ) setCategoryList(categories) diff --git a/webapp/src/hooks/customHooks/useExpenseReportState.js b/webapp/src/hooks/customHooks/useExpenseReportState.js index bab1d8e2..68cf4bc6 100644 --- a/webapp/src/hooks/customHooks/useExpenseReportState.js +++ b/webapp/src/hooks/customHooks/useExpenseReportState.js @@ -74,6 +74,7 @@ const useExpenseReportState = () => { setCategoryList([]) setDelegatesList([]) + // TODO: const delegatesExpenseByElections = await loadDelegatesExpenseByElections( { election: electionRoundSelect diff --git a/webapp/src/utils/new-format-objects.js b/webapp/src/utils/new-format-objects.js index e52a83bf..2165bb67 100644 --- a/webapp/src/utils/new-format-objects.js +++ b/webapp/src/utils/new-format-objects.js @@ -164,11 +164,11 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { const resultUncategorizedEOS = (incomeList[0]?.eos_claimed || 0) + (incomeList[0]?.eos_unclaimed || 0) - - (expenseList[0]?.eos_categorized || 0) + (expenseList[0]?.total_eos_amount || 0) const resultUncategorizedUSD = (incomeList[0]?.usd_claimed || 0) + (incomeList[0]?.usd_unclaimed || 0) - - (expenseList[0]?.usd_categorized || 0) + (expenseList[0]?.total_usd_amount || 0) transactions.push( generateDelegateData( @@ -185,8 +185,8 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { generateDelegateData( 'expense', 'Categorized', - expenseList[0]?.eos_categorized || 0, - expenseList[0]?.usd_categorized || 0, + expenseList[0]?.total_eos_amount || 0, + expenseList[0]?.total_usd_amount || 0, resultUncategorizedEOS >= 0 ? resultUncategorizedEOS : 0 || 0, resultUncategorizedUSD >= 0 ? resultUncategorizedUSD : 0 || 0 ) @@ -195,26 +195,10 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { return transactions } -export const newDataFormatByCategoryDelegate = (categoryList, transaction) => { - const newCategoryList = [] - - categoryList.forEach(data => { - if (data.category === 'uncategorized') return - - newCategoryList.push({ - category: data.category, - EOS: Number(data.amount), - USD: Number(data.usd_total), - color: generateColor() - }) - }) - - newCategoryList.push({ - category: 'uncategorized', - EOS: transaction[1].EOS_UN, - USD: transaction[1].USD_UN, +export const newDataFormatByCategoryDelegate = categoryList => + categoryList.map(data => ({ + category: data.category, + EOS: Number(data.eos_amount), + USD: Number(data.usd_amount), color: generateColor() - }) - - return newCategoryList -} + })) From 878be19fbf08c029ea1e7588ccc08e924b2e70b7 Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Tue, 14 Feb 2023 11:11:45 -0600 Subject: [PATCH 02/13] feat(hasura): adding migrations for global table --- hapi/src/gql/eden-transaction.gql.js | 3 +- hapi/src/gql/eden_global_amount.gql.js | 86 +++++++++++++++++++ hapi/src/gql/index.js | 3 +- hapi/src/services/dfuse/index.js | 2 +- .../edenexplorer-categorize.updater.js | 6 +- .../updaters/eosiotoken-transfer.updater.js | 6 +- hapi/src/utils/updater.util.js | 64 ++++++++++++-- ...ublic_eden_global_expense_by_election.yaml | 3 - .../default/tables/public_global_amount.yaml | 40 +++++++++ ...otal_expense_by_delegate_and_election.yaml | 14 +-- .../databases/default/tables/tables.yaml | 2 +- .../down.sql | 4 + .../up.sql | 2 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 1 + .../up.sql | 1 + .../down.sql | 3 + .../up.sql | 1 + .../down.sql | 2 + .../up.sql | 1 + .../down.sql | 5 ++ .../up.sql | 3 + .../down.sql | 1 + .../up.sql | 3 + 33 files changed, 241 insertions(+), 25 deletions(-) create mode 100644 hapi/src/gql/eden_global_amount.gql.js delete mode 100644 hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml create mode 100644 hasura/metadata/databases/default/tables/public_global_amount.yaml create mode 100644 hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql create mode 100644 hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql create mode 100644 hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql create mode 100644 hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql create mode 100644 hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql create mode 100644 hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql create mode 100644 hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql create mode 100644 hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql create mode 100644 hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql create mode 100644 hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql create mode 100644 hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql create mode 100644 hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql create mode 100644 hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql create mode 100644 hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql create mode 100644 hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql create mode 100644 hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql create mode 100644 hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql create mode 100644 hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql create mode 100644 hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql create mode 100644 hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql create mode 100644 hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql create mode 100644 hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql diff --git a/hapi/src/gql/eden-transaction.gql.js b/hapi/src/gql/eden-transaction.gql.js index 90a4f47c..c6a58e55 100644 --- a/hapi/src/gql/eden-transaction.gql.js +++ b/hapi/src/gql/eden-transaction.gql.js @@ -81,6 +81,7 @@ const getAggregate = async where => { aggregate { sum { amount + usd_total } } } @@ -91,7 +92,7 @@ const getAggregate = async where => { where }) - return edenTransactionAggregate?.aggregate?.sum?.amount || 0 + return edenTransactionAggregate?.aggregate?.sum || {} } module.exports = { diff --git a/hapi/src/gql/eden_global_amount.gql.js b/hapi/src/gql/eden_global_amount.gql.js new file mode 100644 index 00000000..0600548f --- /dev/null +++ b/hapi/src/gql/eden_global_amount.gql.js @@ -0,0 +1,86 @@ +const { hasuraUtil } = require('../utils') + +const save = async payload => { + const mutation = ` + mutation ($payload: global_amount_insert_input!) { + insert_global_amount_one(object: $payload) { + id + eos_income + usd_income + usd_expense + eos_expense + election + account + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { payload }) + + return data.insert_global_amount_one +} + +const get = async (where, getMany = false) => { + const query = ` + query ($where: global_amount_bool_exp) { + global_amount(where: $where) { + id + eos_income + usd_income + usd_expense + eos_expense + election + account + } + } + ` + const { global_amount } = await hasuraUtil.instance.request(query, { + where + }) + + return getMany ? global_amount : global_amount[0] +} + +const update = async ({ where, _set }) => { + const mutation = ` + mutation update($where: global_amount_bool_exp!, $_set: global_amount_set_input) { + update_global_amount(where: $where, _set: $_set) { + returning { + id + } + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { + where, + _set + }) + + return data.update_eden_transaction +} + +const getAggregate = async where => { + const query = ` + query ($where: global_amount_bool_exp) { + global_amount_aggregate(where: $where) { + aggregate { + sum { + eos_income + usd_income + } + } + } + } + ` + const { global_amount } = await hasuraUtil.instance.request(query, { + where + }) + + return global_amount?.aggregate?.sum || {} +} + +module.exports = { + save, + get, + update, + getAggregate +} diff --git a/hapi/src/gql/index.js b/hapi/src/gql/index.js index ff158598..041c2abc 100644 --- a/hapi/src/gql/index.js +++ b/hapi/src/gql/index.js @@ -4,5 +4,6 @@ module.exports = { edenDelegatesGql: require('./eden-delegates.gql'), edenElectionGql: require('./eden-election.gql'), edenHistoricElectionGql: require('./eden-historic-election.gql'), - edenTotalExpenseByDelegateAndElection: require('./eden_total_expense_by_delegate_and_election') + edenTotalExpenseByDelegateAndElection: require('./eden_total_expense_by_delegate_and_election'), + edenGlobalAmountGql: require('./eden_global_amount.gql') } diff --git a/hapi/src/services/dfuse/index.js b/hapi/src/services/dfuse/index.js index 4a5bf5d4..27ce883c 100644 --- a/hapi/src/services/dfuse/index.js +++ b/hapi/src/services/dfuse/index.js @@ -193,7 +193,7 @@ const getTreasuryData = async () => { } const sync = async () => { - await getTreasuryData() + //TODO: await getTreasuryData() await getDelegateData() return sync() diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index e7de0db9..87dbfe8b 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -1,7 +1,8 @@ const { edenTransactionGql, edenElectionGql, - edenTotalExpenseByDelegateAndElection + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql } = require('../../../gql') const { updaterUtil } = require('../../../utils') @@ -52,7 +53,8 @@ module.exports = { transactionToEdit.category, edenElectionGql, edenTransactionGql, - edenTotalExpenseByDelegateAndElection + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql ) } else { await edenTotalExpenseByDelegateAndElection.update({ diff --git a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js index a82ff11e..c5ada6e2 100644 --- a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js +++ b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js @@ -4,7 +4,8 @@ const { sleepUtil, communityUtil, updaterUtil } = require('../../../utils') const { edenTransactionGql, edenElectionGql, - edenTotalExpenseByDelegateAndElection + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql } = require('../../../gql') const { transactionConstant } = require('../../../constants') @@ -103,7 +104,8 @@ module.exports = { category, edenElectionGql, edenTransactionGql, - edenTotalExpenseByDelegateAndElection + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql ) } catch (error) { console.error( diff --git a/hapi/src/utils/updater.util.js b/hapi/src/utils/updater.util.js index 71c64986..0e2e7968 100644 --- a/hapi/src/utils/updater.util.js +++ b/hapi/src/utils/updater.util.js @@ -46,9 +46,11 @@ const getElectionWithoutExpense = async ( category: { _neq: 'uncategorized' } } - const income = (await edenTransactionGql.getAggregate(incomeQuery)) || 0 + const { amount: income } = + (await edenTransactionGql.getAggregate(incomeQuery)) || 0 - const expense = (await edenTransactionGql.getAggregate(expenseQuery)) || 0 + const { amount: expense } = + (await edenTransactionGql.getAggregate(expenseQuery)) || 0 if (expense + amount <= income) { return { election, idElection: id } @@ -72,7 +74,8 @@ const saveTotalByDelegateAndElection = async ( category, edenElectionGql, edenTransactionGql, - edenTotalExpenseByDelegateAndElection + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql ) => { try { let tempAmount = eosAmount @@ -94,15 +97,34 @@ const saveTotalByDelegateAndElection = async ( id_election: { _eq: id } } - const income = (await edenTransactionGql.getAggregate(incomeQuery)) || 0 + const globalAmountQuery = { + election: { _eq: election }, + account: { _eq: delegateAccount } + } + + const { amount, usd_total } = + (await edenTransactionGql.getAggregate(incomeQuery)) || 0 const totalByDelegateAndElection = (await edenTotalExpenseByDelegateAndElection.getAggregate( totalByDelegateAndElectionQuery )) || 0 - if (totalByDelegateAndElection + tempAmount > income) { - const totalToSave = income - totalByDelegateAndElection + let globalAmount = await edenGlobalAmountGql.get(globalAmountQuery) + + if (!globalAmount) { + globalAmount = await edenGlobalAmountGql.save({ + account: delegateAccount, + election, + eos_income: amount, + usd_income: usd_total, + eos_expense: 0, + usd_expense: 0 + }) + } + + if (totalByDelegateAndElection + tempAmount > amount) { + const totalToSave = amount - totalByDelegateAndElection const totalByDelegateAndElectionData = { eos_amount: totalToSave, @@ -110,9 +132,22 @@ const saveTotalByDelegateAndElection = async ( id_election: id, category, tx_id: txId, - election + election, + account: delegateAccount } + await edenGlobalAmountGql.update({ + where: globalAmountQuery, + _set: { + eos_expense: Number(globalAmount.eos_expense + totalToSave).toFixed( + 2 + ), + usd_expense: Number( + globalAmount.usd_expense + totalToSave * eosRate + ).toFixed(2) + } + }) + await edenTotalExpenseByDelegateAndElection.save( totalByDelegateAndElectionData ) @@ -125,9 +160,22 @@ const saveTotalByDelegateAndElection = async ( id_election: id, category, tx_id: txId, - election + election, + account: delegateAccount } + await edenGlobalAmountGql.update({ + where: globalAmountQuery, + _set: { + eos_expense: Number(globalAmount.eos_expense + tempAmount).toFixed( + 2 + ), + usd_expense: Number( + globalAmount.usd_expense + tempAmount * eosRate + ).toFixed(2) + } + }) + await edenTotalExpenseByDelegateAndElection.save( totalByDelegateAndElectionData ) diff --git a/hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml b/hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml deleted file mode 100644 index a7604393..00000000 --- a/hasura/metadata/databases/default/tables/public_eden_global_expense_by_election.yaml +++ /dev/null @@ -1,3 +0,0 @@ -table: - name: eden_global_expense_by_election - schema: public diff --git a/hasura/metadata/databases/default/tables/public_global_amount.yaml b/hasura/metadata/databases/default/tables/public_global_amount.yaml new file mode 100644 index 00000000..3b49afb7 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_global_amount.yaml @@ -0,0 +1,40 @@ +table: + name: global_amount + schema: public +insert_permissions: + - role: guest + permission: + check: {} + columns: + - account + - election + - eos_income + - usd_income + - eos_expense + - usd_expense + - id +select_permissions: + - role: guest + permission: + columns: + - election + - eos_expense + - eos_income + - usd_expense + - usd_income + - account + - id + filter: {} +update_permissions: + - role: guest + permission: + columns: + - election + - eos_expense + - eos_income + - usd_expense + - usd_income + - account + - id + filter: {} + check: null diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml index 94ef11d7..9a46fe24 100644 --- a/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml @@ -10,26 +10,28 @@ insert_permissions: permission: check: {} columns: + - account - category + - create_at - election - eos_amount - - usd_amount - - create_at - - updated_at - id - id_election - tx_id + - updated_at + - usd_amount select_permissions: - role: guest permission: columns: + - account - category + - create_at - election - eos_amount - - usd_amount - - create_at - - updated_at - id - id_election - tx_id + - updated_at + - usd_amount filter: {} diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml index 61bd7083..d8886f6e 100644 --- a/hasura/metadata/databases/default/tables/tables.yaml +++ b/hasura/metadata/databases/default/tables/tables.yaml @@ -1,11 +1,11 @@ - "!include public_eden_delegates.yaml" - "!include public_eden_election.yaml" -- "!include public_eden_global_expense_by_election.yaml" - "!include public_eden_historic_election.yaml" - "!include public_eden_transaction.yaml" - "!include public_eden_treasury.yaml" - "!include public_expenses_by_category_and_delegate.yaml" - "!include public_expenses_by_delegate.yaml" +- "!include public_global_amount.yaml" - "!include public_historic_expenses.yaml" - "!include public_historic_incomes.yaml" - "!include public_incomes_by_delegate.yaml" diff --git a/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql new file mode 100644 index 00000000..4c0f8967 --- /dev/null +++ b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."total_expense_by_delegate_and_election" add column "account" varchar +-- null; diff --git a/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql new file mode 100644 index 00000000..99922db2 --- /dev/null +++ b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql @@ -0,0 +1,2 @@ +alter table "public"."total_expense_by_delegate_and_election" add column "account" varchar + null; diff --git a/hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql b/hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql new file mode 100644 index 00000000..4516582b --- /dev/null +++ b/hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql @@ -0,0 +1 @@ +DROP TABLE "public"."global_amount"; diff --git a/hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql b/hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql new file mode 100644 index 00000000..ee12212a --- /dev/null +++ b/hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql @@ -0,0 +1 @@ +CREATE TABLE "public"."global_amount" ("account" text NOT NULL, "election" integer NOT NULL, "eos_income" integer NOT NULL, "usd_income" integer NOT NULL, "eos_expense" integer NOT NULL, "usd_expense" integer NOT NULL, "id" uuid NOT NULL, PRIMARY KEY ("id") ); diff --git a/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql new file mode 100644 index 00000000..375631cc --- /dev/null +++ b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "election" TYPE integer; diff --git a/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql new file mode 100644 index 00000000..7a6e1ab2 --- /dev/null +++ b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "election" TYPE numeric; diff --git a/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql new file mode 100644 index 00000000..2d340730 --- /dev/null +++ b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_income" TYPE integer; diff --git a/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql new file mode 100644 index 00000000..7e1c8259 --- /dev/null +++ b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_income" TYPE numeric; diff --git a/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql new file mode 100644 index 00000000..5dd24b86 --- /dev/null +++ b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_income" TYPE integer; diff --git a/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql new file mode 100644 index 00000000..1c14a620 --- /dev/null +++ b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_income" TYPE numeric; diff --git a/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql new file mode 100644 index 00000000..49b0aa7f --- /dev/null +++ b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_expense" TYPE integer; diff --git a/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql new file mode 100644 index 00000000..28219d97 --- /dev/null +++ b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_expense" TYPE numeric; diff --git a/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql new file mode 100644 index 00000000..17525e07 --- /dev/null +++ b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_expense" TYPE integer; diff --git a/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql new file mode 100644 index 00000000..ca4830c2 --- /dev/null +++ b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_expense" TYPE numeric; diff --git a/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql new file mode 100644 index 00000000..621f303e --- /dev/null +++ b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql @@ -0,0 +1,3 @@ +alter table "public"."global_amount" + add constraint "global_amount_pkey" + primary key ("id"); diff --git a/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql new file mode 100644 index 00000000..b39e56c6 --- /dev/null +++ b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql @@ -0,0 +1 @@ +alter table "public"."global_amount" drop constraint "global_amount_pkey"; diff --git a/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql new file mode 100644 index 00000000..c64338c4 --- /dev/null +++ b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql @@ -0,0 +1,2 @@ +alter table "public"."global_amount" alter column "id" drop not null; +alter table "public"."global_amount" add column "id" uuid; diff --git a/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql new file mode 100644 index 00000000..1abd4b4f --- /dev/null +++ b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql @@ -0,0 +1 @@ +alter table "public"."global_amount" drop column "id" cascade; diff --git a/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql new file mode 100644 index 00000000..3bb509c9 --- /dev/null +++ b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql @@ -0,0 +1,5 @@ +alter table "public"."global_amount" drop column "id" cascade +alter table "public"."global_amount" drop column "id"; +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE EXTENSION IF NOT EXISTS pgcrypto; diff --git a/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql new file mode 100644 index 00000000..03639e86 --- /dev/null +++ b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql @@ -0,0 +1,3 @@ +CREATE EXTENSION IF NOT EXISTS pgcrypto; +alter table "public"."global_amount" add column "id" uuid + not null default gen_random_uuid(); diff --git a/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql new file mode 100644 index 00000000..b39e56c6 --- /dev/null +++ b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql @@ -0,0 +1 @@ +alter table "public"."global_amount" drop constraint "global_amount_pkey"; diff --git a/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql new file mode 100644 index 00000000..621f303e --- /dev/null +++ b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql @@ -0,0 +1,3 @@ +alter table "public"."global_amount" + add constraint "global_amount_pkey" + primary key ("id"); From 0227bc2c59412999a60aa44e813cd91c20657d08 Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Wed, 15 Feb 2023 00:49:15 -0600 Subject: [PATCH 03/13] fix(webapp) delete unuse code --- .../edenexplorer-categorize.updater.js | 9 +--- .../updaters/eosiotoken-transfer.updater.js | 10 +--- hapi/src/utils/updater.util.js | 35 +------------- ...lic_expenses_by_category_and_election.yaml | 13 +++++ .../databases/default/tables/tables.yaml | 3 +- .../1676413782853_run_sql_migration/down.sql | 6 +++ .../1676413782853_run_sql_migration/up.sql | 4 ++ .../down.sql | 3 ++ .../up.sql | 1 + .../down.sql | 3 ++ .../up.sql | 1 + webapp/src/gql/eden-expense.gql.js | 30 +++++------- webapp/src/gql/eden-transaction.gql.js | 23 +++------ .../customHooks/useDelegateReportState.js | 11 ++--- .../customHooks/useExpenseReportState.js | 22 ++++----- webapp/src/utils/new-format-objects.js | 48 ++++++++++--------- 16 files changed, 95 insertions(+), 127 deletions(-) create mode 100644 hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml create mode 100644 hasura/migrations/default/1676413782853_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1676413782853_run_sql_migration/up.sql create mode 100644 hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql create mode 100644 hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql create mode 100644 hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql create mode 100644 hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index 87dbfe8b..b9833dc9 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -28,12 +28,7 @@ module.exports = { if (!transactionToEdit) return const { idElection: id_election } = - await updaterUtil.getElectionWithoutExpense( - account, - transactionToEdit.amount, - edenElectionGql, - edenTransactionGql - ) + await updaterUtil.getElectionWithoutExpense(account, edenElectionGql) const updateQuery = { where: { @@ -44,7 +39,7 @@ module.exports = { } await edenTransactionGql.update(updateQuery) - if (transactionToEdit.category === 'uncategorzed') { + if (transactionToEdit.category === 'uncategorized') { await updaterUtil.saveTotalByDelegateAndElection( transactionToEdit.txid, account, diff --git a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js index c5ada6e2..c621709a 100644 --- a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js +++ b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js @@ -52,13 +52,6 @@ module.exports = { } try { - const { idElection } = await updaterUtil.getElectionWithoutExpense( - from, - amount, - edenElectionGql, - edenTransactionGql - ) - const txDate = moment(action.timestamp).format('DD-MM-YYYY') if (LASTEST_RATE_DATE_CONSULTED !== txDate) { @@ -83,8 +76,7 @@ module.exports = { category, date: action.timestamp, description, - id_election: - category === 'uncategorized' ? action.election.id : idElection, + id_election: action.election.id, recipient: to, type: 'expense', eos_exchange: LASTEST_RATE_DATA_CONSULTED, diff --git a/hapi/src/utils/updater.util.js b/hapi/src/utils/updater.util.js index 0e2e7968..dd91be4c 100644 --- a/hapi/src/utils/updater.util.js +++ b/hapi/src/utils/updater.util.js @@ -17,46 +17,13 @@ const memoSplit = memoString => { return { category, description } } -const getElectionWithoutExpense = async ( - delegateAccount, - amount, - edenElectionGql, - edenTransactionGql -) => { +const getElectionWithoutExpense = async (delegateAccount, edenElectionGql) => { try { const electionsQuery = { eden_delegate: { account: { _eq: delegateAccount } } } const elections = await edenElectionGql.get(electionsQuery, true) - for (const { id, election } of elections) { - const incomeQuery = { - eden_election: { - eden_delegate: { account: { _eq: delegateAccount } }, - election: { _eq: election } - }, - type: { _eq: 'income' } - } - const expenseQuery = { - eden_election: { - eden_delegate: { account: { _eq: delegateAccount } }, - election: { _eq: election } - }, - type: { _eq: 'expense' }, - category: { _neq: 'uncategorized' } - } - - const { amount: income } = - (await edenTransactionGql.getAggregate(incomeQuery)) || 0 - - const { amount: expense } = - (await edenTransactionGql.getAggregate(expenseQuery)) || 0 - - if (expense + amount <= income) { - return { election, idElection: id } - } - } - return { election: elections.at(-1).election, idElection: elections.at(-1).id diff --git a/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml b/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml new file mode 100644 index 00000000..4562504c --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml @@ -0,0 +1,13 @@ +table: + name: expenses_by_category_and_election + schema: public +select_permissions: + - role: guest + permission: + columns: + - id_election + - category + - election + - total_eos_amount + - total_usd_amount + filter: {} diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml index d8886f6e..d34860d0 100644 --- a/hasura/metadata/databases/default/tables/tables.yaml +++ b/hasura/metadata/databases/default/tables/tables.yaml @@ -4,9 +4,9 @@ - "!include public_eden_transaction.yaml" - "!include public_eden_treasury.yaml" - "!include public_expenses_by_category_and_delegate.yaml" +- "!include public_expenses_by_category_and_election.yaml" - "!include public_expenses_by_delegate.yaml" - "!include public_global_amount.yaml" -- "!include public_historic_expenses.yaml" - "!include public_historic_incomes.yaml" - "!include public_incomes_by_delegate.yaml" - "!include public_total_by_category.yaml" @@ -14,6 +14,5 @@ - "!include public_total_by_election.yaml" - "!include public_total_expense_by_all_election.yaml" - "!include public_total_expense_by_delegate_and_election.yaml" -- "!include public_total_expense_by_election_view.yaml" - "!include public_total_income_by_all_elections.yaml" - "!include public_transaction_by_category_and_election.yaml" diff --git a/hasura/migrations/default/1676413782853_run_sql_migration/down.sql b/hasura/migrations/default/1676413782853_run_sql_migration/down.sql new file mode 100644 index 00000000..4b67c47b --- /dev/null +++ b/hasura/migrations/default/1676413782853_run_sql_migration/down.sql @@ -0,0 +1,6 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS +-- SELECT id_election,category,election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY id_election, category, election; diff --git a/hasura/migrations/default/1676413782853_run_sql_migration/up.sql b/hasura/migrations/default/1676413782853_run_sql_migration/up.sql new file mode 100644 index 00000000..3a9d23b1 --- /dev/null +++ b/hasura/migrations/default/1676413782853_run_sql_migration/up.sql @@ -0,0 +1,4 @@ +CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS +SELECT id_election,category,election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +FROM total_expense_by_delegate_and_election +GROUP BY id_election, category, election; diff --git a/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql new file mode 100644 index 00000000..930490cb --- /dev/null +++ b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP VIEW "public"."historic_expenses"; diff --git a/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql new file mode 100644 index 00000000..1303ce38 --- /dev/null +++ b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql @@ -0,0 +1 @@ +DROP VIEW "public"."historic_expenses"; diff --git a/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql new file mode 100644 index 00000000..a5c53dba --- /dev/null +++ b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP VIEW "public"."total_expense_by_election_view"; diff --git a/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql new file mode 100644 index 00000000..6629491b --- /dev/null +++ b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql @@ -0,0 +1 @@ +DROP VIEW "public"."total_expense_by_election_view"; diff --git a/webapp/src/gql/eden-expense.gql.js b/webapp/src/gql/eden-expense.gql.js index 39e9fc8d..09c9bf7d 100644 --- a/webapp/src/gql/eden-expense.gql.js +++ b/webapp/src/gql/eden-expense.gql.js @@ -29,30 +29,25 @@ export const GET_TOTAL_EXPENSE_BY_DELEGATE = gql` ` export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql` - query getDelegatesByElection($election: Int) { - historic_expenses(where: { election: { _eq: $election } }) { - delegate_payer - usd_categorized - eos_claimed - eos_unclaimed - eos_categorized + query getDelegatesByElection($election: numeric) { + global_amount(where: { election: { _eq: $election } }) { + account + election + eos_income + usd_income + eos_expense + usd_expense } } ` export const GET_TOTAL_EXPENSE_BY_CATEGORY_AND_ELECTION = gql` - query getTotalClaimedAndUnclaimedByElection($election: Int) { - total_by_category_and_election( - where: { - election: { _eq: $election } - category: { _neq: "uncategorized" } - type: { _eq: "expense" } - } - ) { - amount + query getExpensesByCategoryAndElection($election: Int) { + expenses_by_category_and_election(where: { election: { _eq: $election } }) { + total_usd_amount category + total_eos_amount election - usd_total } } ` @@ -100,6 +95,7 @@ export const GET_EXPENSE_BY_CATEGORY = gql` ) { category eos_amount + usd_amount id id_election tx_id diff --git a/webapp/src/gql/eden-transaction.gql.js b/webapp/src/gql/eden-transaction.gql.js index 515c7d93..2f222456 100644 --- a/webapp/src/gql/eden-transaction.gql.js +++ b/webapp/src/gql/eden-transaction.gql.js @@ -1,13 +1,13 @@ import gql from 'graphql-tag' export const GET_TOTAL_EXPENSE_BY_ELECTION = gql` - query getTotalExpenseByDelegateView($id_election: uuid) { - total_expense_by_election_view( - where: { id_election: { _eq: $id_election } } + query getTotalExpenseByDelegateView($election: numeric, $account: String) { + global_amount( + where: { election: { _eq: $election }, account: { _eq: $account } } ) { - id_election - total_eos_amount - total_usd_amount + election + eos_expense + usd_expense } } ` @@ -17,17 +17,6 @@ export const GET_TRANSACTIONS_BY_DELEGATE_AND_ELECTION = gql` $election: Int $delegate: String ) { - historic_expenses( - where: { - election: { _eq: $election } - delegate_payer: { _eq: $delegate } - } - ) { - eos_categorized - eos_claimed - usd_categorized - eos_unclaimed - } historic_incomes( where: { election: { _eq: $election }, recipient: { _eq: $delegate } } ) { diff --git a/webapp/src/hooks/customHooks/useDelegateReportState.js b/webapp/src/hooks/customHooks/useDelegateReportState.js index 1d3409ad..3cc483e9 100644 --- a/webapp/src/hooks/customHooks/useDelegateReportState.js +++ b/webapp/src/hooks/customHooks/useDelegateReportState.js @@ -88,12 +88,6 @@ const useDelegateReportState = () => { useEffect(async () => { if (!delegateSelect) return - // TODO: HERE - // const responseCategory = await loadCategoryList({ - // election: Number(electionRoundSelect), - // delegate: delegateSelect - // }) - const { data: electionId } = await loadElections({ where: { eden_delegate: { account: { _eq: delegateSelect } }, @@ -112,12 +106,13 @@ const useDelegateReportState = () => { const { data: responseTotalExpenseByElection } = await getTotalExpenseByDelegate({ - id_election: electionId?.eden_election[0].id + election: electionRoundSelect, + account: delegateSelect }) const transactions = newDataFormatByTypeDelegate( responseTransaction.data.historic_incomes || [], - responseTotalExpenseByElection.total_expense_by_election_view || [] + responseTotalExpenseByElection.global_amount || [] ) const categories = newDataFormatByCategoryDelegate( diff --git a/webapp/src/hooks/customHooks/useExpenseReportState.js b/webapp/src/hooks/customHooks/useExpenseReportState.js index 68cf4bc6..a9836169 100644 --- a/webapp/src/hooks/customHooks/useExpenseReportState.js +++ b/webapp/src/hooks/customHooks/useExpenseReportState.js @@ -12,6 +12,7 @@ import { newDataExpenseFormatByAllElections, newDataFormatByAllDelegatesExpense, newDataFormatByElectionAndDelegateExpense, + newDataFormatExpenseByCategoryAndElection, newDataFormatTotalByCategoryExpense } from '../../utils/new-format-objects' import { useImperativeQuery } from '../../utils' @@ -74,26 +75,25 @@ const useExpenseReportState = () => { setCategoryList([]) setDelegatesList([]) - // TODO: - const delegatesExpenseByElections = await loadDelegatesExpenseByElections( - { + const { data: delegatesExpenseByElections } = + await loadDelegatesExpenseByElections({ election: electionRoundSelect - } - ) + }) - const totalByCategoryAndElection = await loadTotalByCategoryAndElection({ - election: electionRoundSelect - }) + const { data: totalByCategoryAndElection } = + await loadTotalByCategoryAndElection({ + election: electionRoundSelect + }) setDelegatesList( newDataFormatByElectionAndDelegateExpense( - delegatesExpenseByElections?.data?.historic_expenses || [] + delegatesExpenseByElections?.global_amount || [] ) ) setCategoryList( - newDataFormatTotalByCategoryExpense( - totalByCategoryAndElection?.data?.total_by_category_and_election || [] + newDataFormatExpenseByCategoryAndElection( + totalByCategoryAndElection?.expenses_by_category_and_election || [] ) ) } diff --git a/webapp/src/utils/new-format-objects.js b/webapp/src/utils/new-format-objects.js index 2165bb67..de83e4e5 100644 --- a/webapp/src/utils/new-format-objects.js +++ b/webapp/src/utils/new-format-objects.js @@ -47,14 +47,14 @@ export const newDataFormatByTreasuryList = ( isValued: false }) } - const curDate = treasuryList.at(-1).date.split('T')[0] + const curDate = treasuryList.at(-1)?.date.split('T')[0] if (!newTreasuryList.find(element => element.date.split('T')[0] === curDate)) newTreasuryList.push({ - EOS_BALANCE: treasuryList.at(-1).balance, - USD_BALANCE: treasuryList.at(-1).usd_total, - EOS_VALUED: treasuryList.at(-1).balance, - USD_VALUED: treasuryList.at(-1).usd_total, - date: treasuryList.at(-1).date.split('T')[0], + EOS_BALANCE: treasuryList.at(-1)?.balance, + USD_BALANCE: treasuryList.at(-1)?.usd_total, + EOS_VALUED: treasuryList.at(-1)?.balance, + USD_VALUED: treasuryList.at(-1)?.usd_total, + date: treasuryList.at(-1)?.date.split('T')[0], isValued: false }) @@ -113,32 +113,36 @@ export const newDataFormatByAllDelegatesExpense = transactionsList => export const newDataFormatByElectionAndDelegateExpense = transactionsList => transactionsList.map(data => ({ - name: data.delegate_payer, - EOS_CATEGORIZED: Number(data.eos_categorized), - USD_CATEGORIZED: Number(data.usd_categorized), - EOS_UNCATEGORIZED: Number( - data.eos_claimed + data.eos_unclaimed - data.eos_categorized - ), - USD_UNCATEGORIZED: Number(data.usd_uncategorized), + name: data.account, + EOS_CATEGORIZED: Number(data.eos_expense), + USD_CATEGORIZED: Number(data.usd_expense), + EOS_UNCATEGORIZED: Number(data.eos_income - data.eos_expense), + USD_UNCATEGORIZED: Number(data.usd_income - data.usd_expense), EOS_CATEGORIZED_PERCENT: Number( - (data.eos_categorized / (data.eos_claimed + data.eos_unclaimed || 1)) * - 100 + (data.eos_expense / (data.eos_income || 1)) * 100 ), EOS_UNCATEGORIZED_PERCENT: Number( - ((data.eos_claimed + data.eos_unclaimed - data.eos_categorized) / - (data.eos_claimed + data.eos_unclaimed) || 1) * 100 + ((data.eos_income - data.eos_expense) / data.eos_income || 1) * 100 ), color: generateColor() })) export const newDataFormatTotalByCategoryExpense = totalByCategory => totalByCategory.map(data => ({ - name: data.category ? data.category : `Election ${data.election + 1}`, + name: data.category, EOS_CATEGORIZED: Number(data.amount), USD_CATEGORIZED: Number(data.usd_total), color: generateColor() })) +export const newDataFormatExpenseByCategoryAndElection = totalByCategory => + totalByCategory.map(data => ({ + name: data.category, + EOS_CATEGORIZED: Number(data.total_eos_amount), + USD_CATEGORIZED: Number(data.total_usd_amount), + color: generateColor() + })) + export const generateDelegateData = ( type, category, @@ -164,11 +168,11 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { const resultUncategorizedEOS = (incomeList[0]?.eos_claimed || 0) + (incomeList[0]?.eos_unclaimed || 0) - - (expenseList[0]?.total_eos_amount || 0) + (expenseList[0]?.eos_expense || 0) const resultUncategorizedUSD = (incomeList[0]?.usd_claimed || 0) + (incomeList[0]?.usd_unclaimed || 0) - - (expenseList[0]?.total_usd_amount || 0) + (expenseList[0]?.usd_expense || 0) transactions.push( generateDelegateData( @@ -185,8 +189,8 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { generateDelegateData( 'expense', 'Categorized', - expenseList[0]?.total_eos_amount || 0, - expenseList[0]?.total_usd_amount || 0, + expenseList[0]?.eos_expense || 0, + expenseList[0]?.usd_expense || 0, resultUncategorizedEOS >= 0 ? resultUncategorizedEOS : 0 || 0, resultUncategorizedUSD >= 0 ? resultUncategorizedUSD : 0 || 0 ) From ab3dc0223793d037d32dd9ab6dac889f76a6ba4b Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Thu, 16 Feb 2023 10:49:55 -0600 Subject: [PATCH 04/13] fix(webapp): save incomes first --- hapi/src/gql/eden-delegates.gql.js | 6 +-- hapi/src/gql/eden-transaction.gql.js | 27 ++++++++++++- hapi/src/services/dfuse/index.js | 30 ++++++++++++-- .../edenexplorer-categorize.updater.js | 2 +- .../services/eden-unclaimed-funds.service.js | 9 ++++- hapi/src/services/worker.service.js | 1 + hapi/src/utils/updater.util.js | 39 ++++++++----------- .../default/tables/public_eden_delegates.yaml | 2 + .../down.sql | 4 ++ .../up.sql | 2 + webapp/src/gql/eden-expense.gql.js | 14 +++---- webapp/src/gql/eden-transaction.gql.js | 2 +- .../customHooks/useDelegateReportState.js | 4 +- webapp/src/utils/new-format-objects.js | 4 +- 14 files changed, 102 insertions(+), 44 deletions(-) create mode 100644 hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql create mode 100644 hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql diff --git a/hapi/src/gql/eden-delegates.gql.js b/hapi/src/gql/eden-delegates.gql.js index 19b82470..c3f96d42 100644 --- a/hapi/src/gql/eden-delegates.gql.js +++ b/hapi/src/gql/eden-delegates.gql.js @@ -36,7 +36,7 @@ const get = async (where, getMany = false) => { return getMany ? edenDelegates : edenDelegates[0] } -const update = async (id, lastSyncedAt) => { +const update = async (id, data) => { const mutation = ` mutation ($id: uuid!, $payload: eden_delegates_set_input) { update_eden_delegates_by_pk(pk_columns: {id: $id}, _set: $payload) { @@ -48,9 +48,7 @@ const update = async (id, lastSyncedAt) => { await hasuraUtil.instance.request(mutation, { id, - payload: { - last_synced_at: lastSyncedAt - } + payload: data }) } diff --git a/hapi/src/gql/eden-transaction.gql.js b/hapi/src/gql/eden-transaction.gql.js index c6a58e55..692b2ec5 100644 --- a/hapi/src/gql/eden-transaction.gql.js +++ b/hapi/src/gql/eden-transaction.gql.js @@ -74,10 +74,32 @@ const update = async ({ where, _set }) => { return data.update_eden_transaction } +const getHistoricIncome = async (where, getMany = false) => { + const query = ` + query ($where: historic_incomes_bool_exp) { + historic_incomes(where: $where) { + recipient + eos_claimed + usd_claimed + eos_unclaimed + usd_unclaimed + election + delegate_level + } + }` + + const { historic_incomes: historicIncomes } = + await hasuraUtil.instance.request(query, { + where + }) + + return getMany ? historicIncomes : historicIncomes[0] +} + const getAggregate = async where => { const query = ` query ($where: eden_transaction_bool_exp) { - eden_transaction_aggregate(where: $where) { + eden_transaction_aggregate(where: $where, distinct_on: txid) { aggregate { sum { amount @@ -100,5 +122,6 @@ module.exports = { get, deleteTx, update, - getAggregate + getAggregate, + getHistoricIncome } diff --git a/hapi/src/services/dfuse/index.js b/hapi/src/services/dfuse/index.js index 27ce883c..f8270784 100644 --- a/hapi/src/services/dfuse/index.js +++ b/hapi/src/services/dfuse/index.js @@ -141,16 +141,40 @@ const getDelegateData = async () => { const delegate = delegatesList[index] let hasMore = true let actions = [] - let blockNumber = delegate.last_synced_at + let blockNumber = delegate.last_synced_income_at try { + console.log(delegate.account) while (hasMore) { ;({ hasMore, actions, blockNumber } = await getActions({ - query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account} OR account:eosio.token data.from:${delegate.account} receiver:eosio.token OR data.account:${delegate.account} receiver:edenexplorer`, + query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account}`, lowBlockNum: blockNumber })) await runUpdaters(actions) - await edenDelegatesGql.update(delegate.id, blockNumber) + + console.log(blockNumber) + + await edenDelegatesGql.update(delegate.id, { + last_synced_income_at: blockNumber + }) + await sleepUtil(10) + } + + hasMore = true + blockNumber = delegate.last_synced_at + + while (hasMore) { + await runUpdaters(actions) + ;({ hasMore, actions, blockNumber } = await getActions({ + query: `account:eosio.token data.from:${delegate.account} receiver:eosio.token OR data.account:${delegate.account} receiver:edenexplorer`, + lowBlockNum: blockNumber + })) + + await runUpdaters(actions) + + await edenDelegatesGql.update(delegate.id, { + last_synced_at: blockNumber + }) await sleepUtil(10) } } catch (error) { diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index b9833dc9..2ada6dff 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -45,7 +45,7 @@ module.exports = { account, transactionToEdit.amount, transactionToEdit.eos_exchange, - transactionToEdit.category, + category, edenElectionGql, edenTransactionGql, edenTotalExpenseByDelegateAndElection, diff --git a/hapi/src/services/eden-unclaimed-funds.service.js b/hapi/src/services/eden-unclaimed-funds.service.js index 33319e95..c67255c5 100644 --- a/hapi/src/services/eden-unclaimed-funds.service.js +++ b/hapi/src/services/eden-unclaimed-funds.service.js @@ -104,7 +104,14 @@ const updateEdenUncleimedFundsWorker = () => { action: updateUnclaimedFunds } } +const updateEdenUncleimedFundsWorker2 = () => { + return { + name: servicesConstant.MESSAGES.uncleimedFunds, + action: updateUnclaimedFunds + } +} module.exports = { - updateEdenUncleimedFundsWorker + updateEdenUncleimedFundsWorker, + updateEdenUncleimedFundsWorker2 } diff --git a/hapi/src/services/worker.service.js b/hapi/src/services/worker.service.js index 15bda0ac..13904524 100644 --- a/hapi/src/services/worker.service.js +++ b/hapi/src/services/worker.service.js @@ -47,6 +47,7 @@ const run = async ({ name, action, interval }) => { const init = async () => { await hasuraUtil.hasuraAssembled() await run(edenHistoricDelegateService.updateHistoricDelegatesWorker()) + await run(edenUnclaimedFundsService.updateEdenUncleimedFundsWorker2()) run(edenDelegatesService.updateEdenTableWorker()) run(edenUnclaimedFundsService.updateEdenUncleimedFundsWorker()) run(dfuseService.syncWorker()) diff --git a/hapi/src/utils/updater.util.js b/hapi/src/utils/updater.util.js index dd91be4c..23962f12 100644 --- a/hapi/src/utils/updater.util.js +++ b/hapi/src/utils/updater.util.js @@ -52,30 +52,19 @@ const saveTotalByDelegateAndElection = async ( const elections = await edenElectionGql.get(electionsQuery, true) for (const { id, election } of elections) { - const incomeQuery = { - eden_election: { - eden_delegate: { account: { _eq: delegateAccount } }, - election: { _eq: election } - }, - type: { _eq: 'income' } - } - - const totalByDelegateAndElectionQuery = { - id_election: { _eq: id } - } - const globalAmountQuery = { election: { _eq: election }, account: { _eq: delegateAccount } } - const { amount, usd_total } = - (await edenTransactionGql.getAggregate(incomeQuery)) || 0 + const { eos_claimed, eos_unclaimed, usd_claimed, usd_unclaimed } = + await edenTransactionGql.getHistoricIncome({ + election: { _eq: election }, + recipient: { _eq: delegateAccount } + }) - const totalByDelegateAndElection = - (await edenTotalExpenseByDelegateAndElection.getAggregate( - totalByDelegateAndElectionQuery - )) || 0 + const amount = eos_claimed + eos_unclaimed || 0 + const usd_total = usd_claimed + usd_unclaimed || 0 let globalAmount = await edenGlobalAmountGql.get(globalAmountQuery) @@ -83,15 +72,19 @@ const saveTotalByDelegateAndElection = async ( globalAmount = await edenGlobalAmountGql.save({ account: delegateAccount, election, - eos_income: amount, - usd_income: usd_total, + eos_income: Number(amount).toFixed(2), + usd_income: Number(usd_total).toFixed(2), eos_expense: 0, usd_expense: 0 }) } - if (totalByDelegateAndElection + tempAmount > amount) { - const totalToSave = amount - totalByDelegateAndElection + if (globalAmount.eos_expense >= globalAmount.eos_income) continue + + if (tempAmount < 0) console.log(tempAmount, 'tempAmount') + + if (globalAmount.eos_expense + tempAmount > amount) { + const totalToSave = globalAmount.eos_income - globalAmount.eos_expense const totalByDelegateAndElectionData = { eos_amount: totalToSave, @@ -120,6 +113,8 @@ const saveTotalByDelegateAndElection = async ( ) tempAmount = tempAmount - totalToSave + + if (totalToSave < 0) console.log(totalToSave, 'Total To Save') } else { const totalByDelegateAndElectionData = { eos_amount: tempAmount, diff --git a/hasura/metadata/databases/default/tables/public_eden_delegates.yaml b/hasura/metadata/databases/default/tables/public_eden_delegates.yaml index 6a8849c9..6205e654 100644 --- a/hasura/metadata/databases/default/tables/public_eden_delegates.yaml +++ b/hasura/metadata/databases/default/tables/public_eden_delegates.yaml @@ -17,6 +17,7 @@ select_permissions: - created_at - id - last_synced_at + - last_synced_income_at - updated_at filter: {} allow_aggregations: true @@ -25,5 +26,6 @@ update_permissions: permission: columns: - last_synced_at + - last_synced_income_at filter: {} check: null diff --git a/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql new file mode 100644 index 00000000..910d1733 --- /dev/null +++ b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."eden_delegates" add column "last_synced_income_at" int8 +-- null default '209274902'; diff --git a/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql new file mode 100644 index 00000000..50058362 --- /dev/null +++ b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql @@ -0,0 +1,2 @@ +alter table "public"."eden_delegates" add column "last_synced_income_at" int8 + null default '209274902'; diff --git a/webapp/src/gql/eden-expense.gql.js b/webapp/src/gql/eden-expense.gql.js index 09c9bf7d..33176aae 100644 --- a/webapp/src/gql/eden-expense.gql.js +++ b/webapp/src/gql/eden-expense.gql.js @@ -29,7 +29,7 @@ export const GET_TOTAL_EXPENSE_BY_DELEGATE = gql` ` export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql` - query getDelegatesByElection($election: numeric) { + query getDelegatesByElection($election: Int) { global_amount(where: { election: { _eq: $election } }) { account election @@ -41,6 +41,7 @@ export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql` } ` +// TODO: export const GET_TOTAL_EXPENSE_BY_CATEGORY_AND_ELECTION = gql` query getExpensesByCategoryAndElection($election: Int) { expenses_by_category_and_election(where: { election: { _eq: $election } }) { @@ -89,16 +90,15 @@ export const GET_EXPENSES_BY_ACCOUNT = gql` ` export const GET_EXPENSE_BY_CATEGORY = gql` - query getExpenseByCategory($id_election: uuid) { - total_expense_by_delegate_and_election( + query getExpensesByCategoryAndElection($id_election: uuid) { + expenses_by_category_and_election( where: { id_election: { _eq: $id_election } } ) { + total_usd_amount category - eos_amount - usd_amount - id + total_eos_amount + election id_election - tx_id } } ` diff --git a/webapp/src/gql/eden-transaction.gql.js b/webapp/src/gql/eden-transaction.gql.js index 2f222456..76997ac5 100644 --- a/webapp/src/gql/eden-transaction.gql.js +++ b/webapp/src/gql/eden-transaction.gql.js @@ -1,7 +1,7 @@ import gql from 'graphql-tag' export const GET_TOTAL_EXPENSE_BY_ELECTION = gql` - query getTotalExpenseByDelegateView($election: numeric, $account: String) { + query getTotalExpenseByDelegateView($election: Int, $account: String) { global_amount( where: { election: { _eq: $election }, account: { _eq: $account } } ) { diff --git a/webapp/src/hooks/customHooks/useDelegateReportState.js b/webapp/src/hooks/customHooks/useDelegateReportState.js index 3cc483e9..850087f5 100644 --- a/webapp/src/hooks/customHooks/useDelegateReportState.js +++ b/webapp/src/hooks/customHooks/useDelegateReportState.js @@ -99,6 +99,8 @@ const useDelegateReportState = () => { id_election: electionId?.eden_election[0].id }) + console.log(responseCategory) + const responseTransaction = await loadTransactions({ election: Number(electionRoundSelect), delegate: delegateSelect @@ -116,7 +118,7 @@ const useDelegateReportState = () => { ) const categories = newDataFormatByCategoryDelegate( - responseCategory?.total_expense_by_delegate_and_election || [] + responseCategory?.expenses_by_category_and_election || [] ) setCategoryList(categories) diff --git a/webapp/src/utils/new-format-objects.js b/webapp/src/utils/new-format-objects.js index de83e4e5..b75a4aa0 100644 --- a/webapp/src/utils/new-format-objects.js +++ b/webapp/src/utils/new-format-objects.js @@ -202,7 +202,7 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { export const newDataFormatByCategoryDelegate = categoryList => categoryList.map(data => ({ category: data.category, - EOS: Number(data.eos_amount), - USD: Number(data.usd_amount), + EOS: Number(data.total_eos_amount), + USD: Number(data.total_usd_amount), color: generateColor() })) From 6aeda5ce3e02e24e9e232a802f06610940d8b300 Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Fri, 17 Feb 2023 08:27:39 -0600 Subject: [PATCH 05/13] fix(webapp): expense higher than income --- hapi/src/services/dfuse/index.js | 3 --- hapi/src/utils/updater.util.js | 14 +++++++---- .../tables/public_election_summary.yaml | 3 +++ ...public_total_by_delegate_and_election.yaml | 15 +++++++++++ .../public_total_expense_by_all_election.yaml | 5 ++-- .../databases/default/tables/tables.yaml | 1 + .../1676601295310_run_sql_migration/down.sql | 3 +++ .../1676601295310_run_sql_migration/up.sql | 1 + .../1676601302025_run_sql_migration/down.sql | 20 +++++++++++++++ .../1676601302025_run_sql_migration/up.sql | 18 +++++++++++++ .../1676601336185_run_sql_migration/down.sql | 13 ++++++++++ .../1676601336185_run_sql_migration/up.sql | 11 ++++++++ webapp/src/gql/eden-expense.gql.js | 5 ++-- webapp/src/gql/eden-transaction.gql.js | 2 +- .../customHooks/useDelegateReportState.js | 2 -- .../customHooks/useExpenseReportState.js | 2 +- webapp/src/utils/new-format-objects.js | 25 ++++++++++++------- 17 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 hasura/metadata/databases/default/tables/public_election_summary.yaml create mode 100644 hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml create mode 100644 hasura/migrations/default/1676601295310_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1676601295310_run_sql_migration/up.sql create mode 100644 hasura/migrations/default/1676601302025_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1676601302025_run_sql_migration/up.sql create mode 100644 hasura/migrations/default/1676601336185_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1676601336185_run_sql_migration/up.sql diff --git a/hapi/src/services/dfuse/index.js b/hapi/src/services/dfuse/index.js index f8270784..6f5e76de 100644 --- a/hapi/src/services/dfuse/index.js +++ b/hapi/src/services/dfuse/index.js @@ -143,7 +143,6 @@ const getDelegateData = async () => { let actions = [] let blockNumber = delegate.last_synced_income_at try { - console.log(delegate.account) while (hasMore) { ;({ hasMore, actions, blockNumber } = await getActions({ query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account}`, @@ -152,8 +151,6 @@ const getDelegateData = async () => { await runUpdaters(actions) - console.log(blockNumber) - await edenDelegatesGql.update(delegate.id, { last_synced_income_at: blockNumber }) diff --git a/hapi/src/utils/updater.util.js b/hapi/src/utils/updater.util.js index 23962f12..f6ef837b 100644 --- a/hapi/src/utils/updater.util.js +++ b/hapi/src/utils/updater.util.js @@ -10,7 +10,7 @@ const memoSplit = memoString => { const category = transactionConstant.CATEGORIES.includes( memoSplit[0].toLowerCase().trim() ) - ? memoSplit[0] + ? memoSplit[0].toLowerCase().trim() : 'uncategorized' const description = category !== 'uncategorized' ? memoSplit[1] : memoString @@ -79,9 +79,15 @@ const saveTotalByDelegateAndElection = async ( }) } - if (globalAmount.eos_expense >= globalAmount.eos_income) continue + await edenGlobalAmountGql.update({ + where: globalAmountQuery, + _set: { + eos_income: Number(amount).toFixed(2), + usd_income: Number(usd_total).toFixed(2) + } + }) - if (tempAmount < 0) console.log(tempAmount, 'tempAmount') + if (globalAmount.eos_expense >= globalAmount.eos_income) continue if (globalAmount.eos_expense + tempAmount > amount) { const totalToSave = globalAmount.eos_income - globalAmount.eos_expense @@ -113,8 +119,6 @@ const saveTotalByDelegateAndElection = async ( ) tempAmount = tempAmount - totalToSave - - if (totalToSave < 0) console.log(totalToSave, 'Total To Save') } else { const totalByDelegateAndElectionData = { eos_amount: tempAmount, diff --git a/hasura/metadata/databases/default/tables/public_election_summary.yaml b/hasura/metadata/databases/default/tables/public_election_summary.yaml new file mode 100644 index 00000000..0a95adb1 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_election_summary.yaml @@ -0,0 +1,3 @@ +table: + name: election_summary + schema: public diff --git a/hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml b/hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml new file mode 100644 index 00000000..e2024df8 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml @@ -0,0 +1,15 @@ +table: + name: total_by_delegate_and_election + schema: public +select_permissions: + - role: guest + permission: + columns: + - account + - election + - eos_expense + - eos_income + - percent_claimed + - usd_expense + - usd_income + filter: {} diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml index b5d9ba04..2fef4467 100644 --- a/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml @@ -7,9 +7,8 @@ select_permissions: columns: - election - eos_categorized - - eos_uncategorized - - percent_categorized - - percent_uncategorized - usd_categorized + - eos_uncategorized - usd_uncategorized + - percent_categorized filter: {} diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml index d34860d0..89c3b003 100644 --- a/hasura/metadata/databases/default/tables/tables.yaml +++ b/hasura/metadata/databases/default/tables/tables.yaml @@ -11,6 +11,7 @@ - "!include public_incomes_by_delegate.yaml" - "!include public_total_by_category.yaml" - "!include public_total_by_category_and_election.yaml" +- "!include public_total_by_delegate_and_election.yaml" - "!include public_total_by_election.yaml" - "!include public_total_expense_by_all_election.yaml" - "!include public_total_expense_by_delegate_and_election.yaml" diff --git a/hasura/migrations/default/1676601295310_run_sql_migration/down.sql b/hasura/migrations/default/1676601295310_run_sql_migration/down.sql new file mode 100644 index 00000000..20f63ecc --- /dev/null +++ b/hasura/migrations/default/1676601295310_run_sql_migration/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- Drop VIEW total_expense_by_all_election; diff --git a/hasura/migrations/default/1676601295310_run_sql_migration/up.sql b/hasura/migrations/default/1676601295310_run_sql_migration/up.sql new file mode 100644 index 00000000..9742283e --- /dev/null +++ b/hasura/migrations/default/1676601295310_run_sql_migration/up.sql @@ -0,0 +1 @@ +Drop VIEW total_expense_by_all_election; diff --git a/hasura/migrations/default/1676601302025_run_sql_migration/down.sql b/hasura/migrations/default/1676601302025_run_sql_migration/down.sql new file mode 100644 index 00000000..50e71caa --- /dev/null +++ b/hasura/migrations/default/1676601302025_run_sql_migration/down.sql @@ -0,0 +1,20 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW total_expense_by_all_election AS +-- WITH election_sums AS ( +-- SELECT election, +-- COALESCE(SUM(eos_claimed + eos_unclaimed), 0) AS eos_sum, +-- COALESCE(SUM(usd_claimed + usd_unclaimed), 0) AS usd_sum +-- FROM historic_incomes +-- GROUP BY election +-- ) +-- SELECT ga.election, +-- COALESCE(SUM(ga.eos_expense), 0) AS eos_categorized, +-- COALESCE(SUM(ga.usd_expense), 0) AS usd_categorized, +-- COALESCE(election_sums.eos_sum - SUM(ga.eos_expense), 0) AS eos_uncategorized, +-- COALESCE(election_sums.usd_sum - SUM(ga.usd_expense), 0) AS usd_uncategorized, +-- COALESCE(SUM(ga.eos_expense) / NULLIF(election_sums.eos_sum, 0) * 100, 0) AS percent_categorized +-- FROM global_amount ga +-- LEFT JOIN election_sums ON ga.election = election_sums.election +-- GROUP BY ga.election, election_sums.eos_sum, election_sums.usd_sum +-- ORDER BY ga.election; diff --git a/hasura/migrations/default/1676601302025_run_sql_migration/up.sql b/hasura/migrations/default/1676601302025_run_sql_migration/up.sql new file mode 100644 index 00000000..1c3b7022 --- /dev/null +++ b/hasura/migrations/default/1676601302025_run_sql_migration/up.sql @@ -0,0 +1,18 @@ +CREATE OR REPLACE VIEW total_expense_by_all_election AS +WITH election_sums AS ( + SELECT election, + COALESCE(SUM(eos_claimed + eos_unclaimed), 0) AS eos_sum, + COALESCE(SUM(usd_claimed + usd_unclaimed), 0) AS usd_sum + FROM historic_incomes + GROUP BY election +) +SELECT ga.election, + COALESCE(SUM(ga.eos_expense), 0) AS eos_categorized, + COALESCE(SUM(ga.usd_expense), 0) AS usd_categorized, + COALESCE(election_sums.eos_sum - SUM(ga.eos_expense), 0) AS eos_uncategorized, + COALESCE(election_sums.usd_sum - SUM(ga.usd_expense), 0) AS usd_uncategorized, + COALESCE(SUM(ga.eos_expense) / NULLIF(election_sums.eos_sum, 0) * 100, 0) AS percent_categorized +FROM global_amount ga +LEFT JOIN election_sums ON ga.election = election_sums.election +GROUP BY ga.election, election_sums.eos_sum, election_sums.usd_sum +ORDER BY ga.election; diff --git a/hasura/migrations/default/1676601336185_run_sql_migration/down.sql b/hasura/migrations/default/1676601336185_run_sql_migration/down.sql new file mode 100644 index 00000000..686ed6df --- /dev/null +++ b/hasura/migrations/default/1676601336185_run_sql_migration/down.sql @@ -0,0 +1,13 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."total_by_delegate_and_election" AS +-- SELECT hi.election, +-- hi.recipient AS account, +-- sum((hi.eos_claimed + hi.eos_unclaimed)) AS eos_income, +-- sum((hi.usd_unclaimed + hi.usd_claimed)) AS usd_income, +-- COALESCE(sum(ga.eos_expense), (0)::numeric) AS eos_expense, +-- COALESCE(sum(ga.usd_expense), (0)::numeric) AS usd_expense, +-- COALESCE(((sum(ga.eos_expense) / sum((hi.eos_claimed + hi.eos_unclaimed))) * (100)::numeric), (0)::numeric) AS percent_claimed +-- FROM (historic_incomes hi +-- LEFT JOIN global_amount ga ON (((hi.election = ga.election) AND ((hi.recipient)::text = ga.account)))) +-- GROUP BY hi.election, hi.recipient; diff --git a/hasura/migrations/default/1676601336185_run_sql_migration/up.sql b/hasura/migrations/default/1676601336185_run_sql_migration/up.sql new file mode 100644 index 00000000..5ef279d4 --- /dev/null +++ b/hasura/migrations/default/1676601336185_run_sql_migration/up.sql @@ -0,0 +1,11 @@ +CREATE OR REPLACE VIEW "public"."total_by_delegate_and_election" AS + SELECT hi.election, + hi.recipient AS account, + sum((hi.eos_claimed + hi.eos_unclaimed)) AS eos_income, + sum((hi.usd_unclaimed + hi.usd_claimed)) AS usd_income, + COALESCE(sum(ga.eos_expense), (0)::numeric) AS eos_expense, + COALESCE(sum(ga.usd_expense), (0)::numeric) AS usd_expense, + COALESCE(((sum(ga.eos_expense) / sum((hi.eos_claimed + hi.eos_unclaimed))) * (100)::numeric), (0)::numeric) AS percent_claimed + FROM (historic_incomes hi + LEFT JOIN global_amount ga ON (((hi.election = ga.election) AND ((hi.recipient)::text = ga.account)))) + GROUP BY hi.election, hi.recipient; diff --git a/webapp/src/gql/eden-expense.gql.js b/webapp/src/gql/eden-expense.gql.js index 33176aae..184b098a 100644 --- a/webapp/src/gql/eden-expense.gql.js +++ b/webapp/src/gql/eden-expense.gql.js @@ -7,7 +7,6 @@ export const GET_TOTAL_EXPENSE_BY_ALL_ELECTIONS = gql` eos_categorized eos_uncategorized percent_categorized - percent_uncategorized usd_categorized usd_uncategorized } @@ -30,18 +29,18 @@ export const GET_TOTAL_EXPENSE_BY_DELEGATE = gql` export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql` query getDelegatesByElection($election: Int) { - global_amount(where: { election: { _eq: $election } }) { + total_by_delegate_and_election(where: { election: { _eq: $election } }) { account election eos_income usd_income eos_expense usd_expense + percent_claimed } } ` -// TODO: export const GET_TOTAL_EXPENSE_BY_CATEGORY_AND_ELECTION = gql` query getExpensesByCategoryAndElection($election: Int) { expenses_by_category_and_election(where: { election: { _eq: $election } }) { diff --git a/webapp/src/gql/eden-transaction.gql.js b/webapp/src/gql/eden-transaction.gql.js index 76997ac5..2f222456 100644 --- a/webapp/src/gql/eden-transaction.gql.js +++ b/webapp/src/gql/eden-transaction.gql.js @@ -1,7 +1,7 @@ import gql from 'graphql-tag' export const GET_TOTAL_EXPENSE_BY_ELECTION = gql` - query getTotalExpenseByDelegateView($election: Int, $account: String) { + query getTotalExpenseByDelegateView($election: numeric, $account: String) { global_amount( where: { election: { _eq: $election }, account: { _eq: $account } } ) { diff --git a/webapp/src/hooks/customHooks/useDelegateReportState.js b/webapp/src/hooks/customHooks/useDelegateReportState.js index 850087f5..7478d789 100644 --- a/webapp/src/hooks/customHooks/useDelegateReportState.js +++ b/webapp/src/hooks/customHooks/useDelegateReportState.js @@ -99,8 +99,6 @@ const useDelegateReportState = () => { id_election: electionId?.eden_election[0].id }) - console.log(responseCategory) - const responseTransaction = await loadTransactions({ election: Number(electionRoundSelect), delegate: delegateSelect diff --git a/webapp/src/hooks/customHooks/useExpenseReportState.js b/webapp/src/hooks/customHooks/useExpenseReportState.js index a9836169..66a261f4 100644 --- a/webapp/src/hooks/customHooks/useExpenseReportState.js +++ b/webapp/src/hooks/customHooks/useExpenseReportState.js @@ -87,7 +87,7 @@ const useExpenseReportState = () => { setDelegatesList( newDataFormatByElectionAndDelegateExpense( - delegatesExpenseByElections?.global_amount || [] + delegatesExpenseByElections?.total_by_delegate_and_election || [] ) ) diff --git a/webapp/src/utils/new-format-objects.js b/webapp/src/utils/new-format-objects.js index b75a4aa0..cb645e78 100644 --- a/webapp/src/utils/new-format-objects.js +++ b/webapp/src/utils/new-format-objects.js @@ -83,7 +83,7 @@ export const newDataExpenseFormatByAllElections = ({ EOS_TOTAL: Number(data.eos_categorized + data.eos_uncategorized), USD_TOTAL: Number(data.usd_categorized + data.usd_uncategorized), EOS_CATEGORIZED_PERCENT: data.percent_categorized, - EOS_UNCATEGORIZED_PERCENT: data.percent_uncategorized + EOS_UNCATEGORIZED_PERCENT: 100 - data.percent_categorized })) export const newDataIncomeFormatByAllElections = ({ @@ -114,16 +114,23 @@ export const newDataFormatByAllDelegatesExpense = transactionsList => export const newDataFormatByElectionAndDelegateExpense = transactionsList => transactionsList.map(data => ({ name: data.account, - EOS_CATEGORIZED: Number(data.eos_expense), - USD_CATEGORIZED: Number(data.usd_expense), + EOS_CATEGORIZED: Number(data.eos_income), + USD_CATEGORIZED: Number(data.usd_income), EOS_UNCATEGORIZED: Number(data.eos_income - data.eos_expense), USD_UNCATEGORIZED: Number(data.usd_income - data.usd_expense), - EOS_CATEGORIZED_PERCENT: Number( - (data.eos_expense / (data.eos_income || 1)) * 100 - ), - EOS_UNCATEGORIZED_PERCENT: Number( - ((data.eos_income - data.eos_expense) / data.eos_income || 1) * 100 - ), + EOS_CATEGORIZED_PERCENT: + data.percent_claimed > 100 + ? 100 + : data.percent_claimed < 0 + ? 0 + : data.percent_claimed, + EOS_UNCATEGORIZED_PERCENT: + 100 - data.percent_claimed > 100 + ? 100 + : 100 - data.percent_claimed < 0 + ? 0 + : 100 - data.percent_claimed, + color: generateColor() })) From d3fee4490b73ae263c5d4dbb6be8b1b01af43323 Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Tue, 28 Feb 2023 12:23:43 -0600 Subject: [PATCH 06/13] chore(env): update elections --- .env.example | 2 +- hapi/src/services/dfuse/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 92f25840..2817b98e 100644 --- a/.env.example +++ b/.env.example @@ -22,7 +22,7 @@ HAPI_NETWORK_WALLET_URL=http://wallet:8888 HAPI_HASURA_URL=http://hasura:8080/v1/graphql HAPI_HASURA_ADMIN_SECRET=myadminsecretkey HAPI_EDEN_ELECTION_INTERVAL=7889400 -HAPI_EDEN_HISTORIC_ELECTION= [{"election": 0, "date_election": "2022-09-18"}, {"election": 1, "date_election": "2022-09-22"}, {"election": 2, "date_election": "2022-09-23T13:00:00.000"}, {"election": 3, "date_election": "2022-09-23T20:00:00.000"}, {"election": 4, "date_election": "2022-09-27"}, {"election": 5, "date_election": "2022-09-30"}] +HAPI_EDEN_HISTORIC_ELECTION= [{"election": 0, "date_election": "2022-09-18"}, {"election": 1, "date_election": "2022-09-22"}, {"election": 2, "date_election": "2022-09-23T13:00:00.000"}, {"election": 3, "date_election": "2022-09-23T20:00:00.000"}, {"election": 4, "date_election": "2022-09-27"}, {"election": 5, "date_election": "2022-09-30"}, {"election": 6, "date_election": "2022-12-26T13:05:00.000"}, {"election": 7, "date_election": "2022-12-26T19:05:00.000"}, {"election": 8, "date_election": "2022-12-28T19:05:00.000"}, {"election": 9, "date_election": "2023-01-03T19:00:00.000"}, {"election": 10, "date_election": "2023-02-13T17:30:00.000"}, {"election": 11, "date_election": "2023-02-15T17:30:00.000"}, {"election": 12, "date_election": "2023-02-17T20:30:00.000"}, {"election": 13, "date_election": "2023-02-23T17:30:00.000"}] HAPI_EDEN_CONTRACT=genesis.eden HAPI_RATE_HISTORY=https://api.coingecko.com/api/v3 HAPI_DFUSE_API_KEY= diff --git a/hapi/src/services/dfuse/index.js b/hapi/src/services/dfuse/index.js index 6f5e76de..a7526068 100644 --- a/hapi/src/services/dfuse/index.js +++ b/hapi/src/services/dfuse/index.js @@ -214,7 +214,7 @@ const getTreasuryData = async () => { } const sync = async () => { - //TODO: await getTreasuryData() + await getTreasuryData() await getDelegateData() return sync() From 3eb9f71ebf1809cf36280bbb6ef532338e52a416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Tue, 28 Feb 2023 14:49:58 -0500 Subject: [PATCH 07/13] Renew db postgres dev --- .github/workflows/push-dev-environment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/push-dev-environment.yaml b/.github/workflows/push-dev-environment.yaml index efb02f59..694002b5 100644 --- a/.github/workflows/push-dev-environment.yaml +++ b/.github/workflows/push-dev-environment.yaml @@ -84,7 +84,7 @@ jobs: HAPI_DFUSE_API: jungle4.dfuse.eosnation.io HAPI_DFUSE_FIRST_BLOCK: 35955488 HAPI_DFUSE_FIRST_TREASURY_BLOCK: 34779185 - # hasura + # hasura dev HASURA_GRAPHQL_ENABLE_CONSOLE: true HASURA_GRAPHQL_DATABASE_URL: ${{ secrets.HASURA_GRAPHQL_DATABASE_URL }} HASURA_GRAPHQL_ADMIN_SECRET: ${{ secrets.HASURA_GRAPHQL_ADMIN_SECRET }} From 3db16f09baaaad1c035ece8f919568694acfdfc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Tue, 28 Feb 2023 15:41:30 -0500 Subject: [PATCH 08/13] Renew Bd dev --- .github/workflows/push-dev-environment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/push-dev-environment.yaml b/.github/workflows/push-dev-environment.yaml index 694002b5..efb02f59 100644 --- a/.github/workflows/push-dev-environment.yaml +++ b/.github/workflows/push-dev-environment.yaml @@ -84,7 +84,7 @@ jobs: HAPI_DFUSE_API: jungle4.dfuse.eosnation.io HAPI_DFUSE_FIRST_BLOCK: 35955488 HAPI_DFUSE_FIRST_TREASURY_BLOCK: 34779185 - # hasura dev + # hasura HASURA_GRAPHQL_ENABLE_CONSOLE: true HASURA_GRAPHQL_DATABASE_URL: ${{ secrets.HASURA_GRAPHQL_DATABASE_URL }} HASURA_GRAPHQL_ADMIN_SECRET: ${{ secrets.HASURA_GRAPHQL_ADMIN_SECRET }} From a9058098687b17664dbf214f69ea872ade57df0f Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Wed, 1 Mar 2023 10:42:41 -0600 Subject: [PATCH 09/13] fix(hapi): subtract claimed from unclaimed --- .../services/dfuse/updaters/eosiotoken-transfer.updater.js | 1 + .../dfuse/updaters/genesiseden-fundtransfer.updater.js | 2 +- hapi/src/utils/community.util.js | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js index c621709a..add6b921 100644 --- a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js +++ b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js @@ -37,6 +37,7 @@ module.exports = { }, _set: { digest: action.action.receipt.digest } }) + return } diff --git a/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js b/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js index f931da98..4298bc3a 100644 --- a/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js +++ b/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js @@ -69,7 +69,7 @@ module.exports = { eden_delegate: { account: { _eq: from } } }, description: { _eq: `distribution funds rank ${rank}` }, - type: { _eq: 'unclaimed' } + category: { _eq: 'unclaimed' } }) if (!existUnclaimedTx) return diff --git a/hapi/src/utils/community.util.js b/hapi/src/utils/community.util.js index a18733ad..d3344114 100644 --- a/hapi/src/utils/community.util.js +++ b/hapi/src/utils/community.util.js @@ -11,7 +11,7 @@ const nextElectionDate = async () => { const response = await loadTableData({ next_key: null }, 'elect.curr') const nextElection = response.rows[0] const now = moment(new Date()) - const end = moment(nextElection[1].start_time).add(1, 'day') + const end = moment(nextElection[1].start_time) const duration = moment.duration(end.diff(now)).asSeconds() return duration > 0 ? duration : servicesConstant.INTERVALS.oneHour @@ -29,7 +29,7 @@ const nextDistributionDate = async () => { const nextDistribution = response.rows[0] const now = moment(new Date()) - const end = moment(nextDistribution[1].distribution_time).add(1, 'day') + const end = moment(nextDistribution[1].distribution_time) const duration = moment.duration(end.diff(now)).asSeconds() return duration > 0 ? duration : servicesConstant.INTERVALS.oneHour From 77d0e6112f08129e15eeb91b74462aeacc9c522d Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Thu, 2 Mar 2023 10:42:15 -0600 Subject: [PATCH 10/13] fix(webapp): expenses by category --- .../updaters/edenexplorer-categorize.updater.js | 1 + .../public_expenses_by_category_and_election.yaml | 1 - ...expenses_by_category_election_and_delegate.yaml | 13 +++++++++++++ .../metadata/databases/default/tables/tables.yaml | 1 + .../1677714380669_run_sql_migration/down.sql | 3 +++ .../default/1677714380669_run_sql_migration/up.sql | 1 + .../1677714387991_run_sql_migration/down.sql | 10 ++++++++++ .../default/1677714387991_run_sql_migration/up.sql | 8 ++++++++ .../1677733616588_run_sql_migration/down.sql | 14 ++++++++++++++ .../default/1677733616588_run_sql_migration/up.sql | 12 ++++++++++++ webapp/src/gql/eden-expense.gql.js | 2 +- .../hooks/customHooks/useDelegateReportState.js | 2 +- webapp/src/utils/new-format-objects.js | 4 ++-- 13 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml create mode 100644 hasura/migrations/default/1677714380669_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1677714380669_run_sql_migration/up.sql create mode 100644 hasura/migrations/default/1677714387991_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1677714387991_run_sql_migration/up.sql create mode 100644 hasura/migrations/default/1677733616588_run_sql_migration/down.sql create mode 100644 hasura/migrations/default/1677733616588_run_sql_migration/up.sql diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index 2ada6dff..a3ceeedb 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -17,6 +17,7 @@ module.exports = { const { category, description } = updaterUtil.memoSplit(memo) const transactionToEditQuery = { + type: { _eq: 'expense' }, txid: { _eq: tx_id }, ...(digest && { digest: { _eq: digest } }) } diff --git a/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml b/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml index 4562504c..fa0be86a 100644 --- a/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml +++ b/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml @@ -5,7 +5,6 @@ select_permissions: - role: guest permission: columns: - - id_election - category - election - total_eos_amount diff --git a/hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml b/hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml new file mode 100644 index 00000000..ed81e7f6 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml @@ -0,0 +1,13 @@ +table: + name: expenses_by_category_election_and_delegate + schema: public +select_permissions: + - role: guest + permission: + columns: + - category + - election + - total_eos_amount + - total_usd_amount + - id_election + filter: {} diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml index 89c3b003..785ffc86 100644 --- a/hasura/metadata/databases/default/tables/tables.yaml +++ b/hasura/metadata/databases/default/tables/tables.yaml @@ -5,6 +5,7 @@ - "!include public_eden_treasury.yaml" - "!include public_expenses_by_category_and_delegate.yaml" - "!include public_expenses_by_category_and_election.yaml" +- "!include public_expenses_by_category_election_and_delegate.yaml" - "!include public_expenses_by_delegate.yaml" - "!include public_global_amount.yaml" - "!include public_historic_incomes.yaml" diff --git a/hasura/migrations/default/1677714380669_run_sql_migration/down.sql b/hasura/migrations/default/1677714380669_run_sql_migration/down.sql new file mode 100644 index 00000000..d4cdc5b2 --- /dev/null +++ b/hasura/migrations/default/1677714380669_run_sql_migration/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP VIEW "public"."expenses_by_category_and_election"; diff --git a/hasura/migrations/default/1677714380669_run_sql_migration/up.sql b/hasura/migrations/default/1677714380669_run_sql_migration/up.sql new file mode 100644 index 00000000..5029bec0 --- /dev/null +++ b/hasura/migrations/default/1677714380669_run_sql_migration/up.sql @@ -0,0 +1 @@ +DROP VIEW "public"."expenses_by_category_and_election"; diff --git a/hasura/migrations/default/1677714387991_run_sql_migration/down.sql b/hasura/migrations/default/1677714387991_run_sql_migration/down.sql new file mode 100644 index 00000000..df8893f7 --- /dev/null +++ b/hasura/migrations/default/1677714387991_run_sql_migration/down.sql @@ -0,0 +1,10 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS +-- SELECT +-- total_expense_by_delegate_and_election.category, +-- total_expense_by_delegate_and_election.election, +-- sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, +-- sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY total_expense_by_delegate_and_election.category, total_expense_by_delegate_and_election.election; diff --git a/hasura/migrations/default/1677714387991_run_sql_migration/up.sql b/hasura/migrations/default/1677714387991_run_sql_migration/up.sql new file mode 100644 index 00000000..8b5cf0cf --- /dev/null +++ b/hasura/migrations/default/1677714387991_run_sql_migration/up.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS + SELECT + total_expense_by_delegate_and_election.category, + total_expense_by_delegate_and_election.election, + sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, + sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount + FROM total_expense_by_delegate_and_election + GROUP BY total_expense_by_delegate_and_election.category, total_expense_by_delegate_and_election.election; diff --git a/hasura/migrations/default/1677733616588_run_sql_migration/down.sql b/hasura/migrations/default/1677733616588_run_sql_migration/down.sql new file mode 100644 index 00000000..cf9435b6 --- /dev/null +++ b/hasura/migrations/default/1677733616588_run_sql_migration/down.sql @@ -0,0 +1,14 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."expenses_by_category_election_and_delegate" AS +-- +-- SELECT total_expense_by_delegate_and_election.id_election, +-- total_expense_by_delegate_and_election.category, +-- total_expense_by_delegate_and_election.election, +-- sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, +-- sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY +-- total_expense_by_delegate_and_election.category, +-- total_expense_by_delegate_and_election.election, +-- total_expense_by_delegate_and_election.id_election; diff --git a/hasura/migrations/default/1677733616588_run_sql_migration/up.sql b/hasura/migrations/default/1677733616588_run_sql_migration/up.sql new file mode 100644 index 00000000..b3a1ff84 --- /dev/null +++ b/hasura/migrations/default/1677733616588_run_sql_migration/up.sql @@ -0,0 +1,12 @@ +CREATE OR REPLACE VIEW "public"."expenses_by_category_election_and_delegate" AS + + SELECT total_expense_by_delegate_and_election.id_election, + total_expense_by_delegate_and_election.category, + total_expense_by_delegate_and_election.election, + sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, + sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount + FROM total_expense_by_delegate_and_election + GROUP BY + total_expense_by_delegate_and_election.category, + total_expense_by_delegate_and_election.election, + total_expense_by_delegate_and_election.id_election; diff --git a/webapp/src/gql/eden-expense.gql.js b/webapp/src/gql/eden-expense.gql.js index 184b098a..517958ea 100644 --- a/webapp/src/gql/eden-expense.gql.js +++ b/webapp/src/gql/eden-expense.gql.js @@ -90,7 +90,7 @@ export const GET_EXPENSES_BY_ACCOUNT = gql` export const GET_EXPENSE_BY_CATEGORY = gql` query getExpensesByCategoryAndElection($id_election: uuid) { - expenses_by_category_and_election( + expenses_by_category_election_and_delegate( where: { id_election: { _eq: $id_election } } ) { total_usd_amount diff --git a/webapp/src/hooks/customHooks/useDelegateReportState.js b/webapp/src/hooks/customHooks/useDelegateReportState.js index 7478d789..84db92df 100644 --- a/webapp/src/hooks/customHooks/useDelegateReportState.js +++ b/webapp/src/hooks/customHooks/useDelegateReportState.js @@ -116,7 +116,7 @@ const useDelegateReportState = () => { ) const categories = newDataFormatByCategoryDelegate( - responseCategory?.expenses_by_category_and_election || [] + responseCategory?.expenses_by_category_election_and_delegate || [] ) setCategoryList(categories) diff --git a/webapp/src/utils/new-format-objects.js b/webapp/src/utils/new-format-objects.js index cb645e78..7719f88f 100644 --- a/webapp/src/utils/new-format-objects.js +++ b/webapp/src/utils/new-format-objects.js @@ -114,8 +114,8 @@ export const newDataFormatByAllDelegatesExpense = transactionsList => export const newDataFormatByElectionAndDelegateExpense = transactionsList => transactionsList.map(data => ({ name: data.account, - EOS_CATEGORIZED: Number(data.eos_income), - USD_CATEGORIZED: Number(data.usd_income), + EOS_CATEGORIZED: Number(data.eos_expense), + USD_CATEGORIZED: Number(data.usd_expense), EOS_UNCATEGORIZED: Number(data.eos_income - data.eos_expense), USD_UNCATEGORIZED: Number(data.usd_income - data.usd_expense), EOS_CATEGORIZED_PERCENT: From 77e44d121ff977faaa82c0e5570d921b5da13ae1 Mon Sep 17 00:00:00 2001 From: warnerHurtado Date: Fri, 3 Mar 2023 09:43:36 -0600 Subject: [PATCH 11/13] fix(hapi): validate categorized transaction --- .../updaters/edenexplorer-categorize.updater.js | 11 ++++++++--- .../dfuse/updaters/eosiotoken-transfer.updater.js | 14 +++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index a3ceeedb..f106c119 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -40,9 +40,14 @@ module.exports = { } await edenTransactionGql.update(updateQuery) - if (transactionToEdit.category === 'uncategorized') { + + const existExpense = await edenTotalExpenseByDelegateAndElection.get({ + tx_id: { _eq: transactionToEdit.digest } + }) + + if (transactionToEdit.category === 'uncategorized' || !existExpense) { await updaterUtil.saveTotalByDelegateAndElection( - transactionToEdit.txid, + transactionToEdit.digest, account, transactionToEdit.amount, transactionToEdit.eos_exchange, @@ -55,7 +60,7 @@ module.exports = { } else { await edenTotalExpenseByDelegateAndElection.update({ where: { - tx_id: { _eq: transactionToEdit.txid } + tx_id: { _eq: transactionToEdit.digest } }, _set: { category } }) diff --git a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js index add6b921..7fbf1885 100644 --- a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js +++ b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js @@ -38,6 +38,18 @@ module.exports = { _set: { digest: action.action.receipt.digest } }) + await updaterUtil.saveTotalByDelegateAndElection( + action.action.receipt.digest, + from, + amount, + registeredTxWithoutDigest.eos_exchange, + registeredTxWithoutDigest.category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql + ) + return } @@ -90,7 +102,7 @@ module.exports = { if (category === 'uncategorized') return await updaterUtil.saveTotalByDelegateAndElection( - action.transaction_id, + action.action.receipt.digest, from, amount, LASTEST_RATE_DATA_CONSULTED, From 378aa8fef3a8f4ee28644d38235e6750bfd22ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Wed, 8 Mar 2023 17:22:49 -0500 Subject: [PATCH 12/13] fix(postgres): upgrade posgres version --- kubernetes/postgres-statefulset.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubernetes/postgres-statefulset.yaml b/kubernetes/postgres-statefulset.yaml index a5911a90..f9d3dda1 100644 --- a/kubernetes/postgres-statefulset.yaml +++ b/kubernetes/postgres-statefulset.yaml @@ -21,7 +21,7 @@ spec: imagePullSecrets: - name: regcred containers: - - image: postgres:13.6-alpine + - image: postgres:13.10-alpine imagePullPolicy: 'Always' name: postgres envFrom: From d2f78231f27899ee4d900a91d2a3a8ab86db1c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Wed, 8 Mar 2023 17:28:11 -0500 Subject: [PATCH 13/13] fix(postgres): upgrade posgres version docker compose --- docker-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index fcccc6b3..e4bc0add 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,7 +9,7 @@ services: - ./tmp/wallet/:/opt/application/data/ postgres: container_name: 'eden-spending-postgres' - image: postgres:12.12-bullseye + image: postgres:12.14-bullseye ports: - '5432:5432' volumes: