Skip to content

Commit

Permalink
Merge pull request #866 from Giveth/staging
Browse files Browse the repository at this point in the history
Release 19 Feb 2023
  • Loading branch information
mohammadranjbarz authored Feb 20, 2023
2 parents cb2aa99 + 2eaa6bc commit f31aa9f
Show file tree
Hide file tree
Showing 239 changed files with 28,890 additions and 38,874 deletions.
25 changes: 24 additions & 1 deletion .github/workflows/CI-CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ jobs:
run: npm ci
- name: Run tslint
run: npm run tslint
- name: Run build
run: npm run build
- name: Run migrations
run: npm run typeorm:cli:test -- migration:run
run: npm run db:migrate:run:test
- name: Run tests
run: npm run test
env:
Expand Down Expand Up @@ -105,3 +107,24 @@ jobs:
username: ${{ secrets.PRODUCTION_USERNAME }}
privateKey: ${{ secrets.PRODUCTION_PRIVATE_KEY}}


deploy-k8s:
if: github.ref == 'refs/heads/main'|| github.ref == 'refs/heads/staging'
needs: publish
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@master
- name: Deploy to Staging Cluster
if: github.ref == 'refs/heads/staging'
uses: steebchen/[email protected]
with: # defaults to latest kubectl binary version
config: ${{ secrets.KUBE_CONFIG }}
command: rollout restart deployments/impact-graph -n backend
# - name: Verify Staging Deployment
# if: github.ref == 'refs/heads/staging'
# uses: steebchen/[email protected]
# with:
# config: ${{ secrets.KUBE_CONFIG }}
# version: v1.24.2 # specify kubectl binary version explicitly
# command: rollout status deployments/impact-graph -n backend
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ src/scripts/*.json
*.DS_Store

.adminbro
.adminjs
13 changes: 13 additions & 0 deletions .mocharc.all-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extension": ["ts"],
"spec": [
"./test/pre-test-scripts.ts",
"./src/**/*.test.ts",
"./src/**/**/*.test.ts"
],
"timeout": 90000,
"exit": true,
"require": ["ts-node/register"],
"retries": 2
}

6 changes: 6 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": ["ts"],
"timeout": 30000,
"exit": true,
"require": ["ts-node/register"]
}
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ImpactQL is a GraphQL server, that enables rapid development of serverless impac
git clone [email protected]:topiahq/impact-graph.git
cd impact-graph
// we specief version in .nvmrc file, so when you just enter nvm use it will use exact version of required npm
// we specified version in .nvmrc file, so when you just enter nvm use it will use exact version of required npm
nvm use
// You should have installed chromium on your system, it can be installed by your or package maneger (apt,brew, ..)
Expand Down Expand Up @@ -111,10 +111,14 @@ If you want to see examples you can read test cases or see [Graphql queries](./t
### Admin panel
We use [Admin Bro](https://github.com/SoftwareBrothers/adminjs) for Admin dashboard
You should navigate to `/admin` for browsing admin panel.
in your local database you can hash a desired password with `BCRYPT_SALT` that is in your `config/development.env` with
in your local database you can hash a desired password with `BCRYPT_SALT` that is in your `config/development.env` with
[bcrypt](https://github.com/kelektiv/node.bcrypt.js) then you set that value in `encryptedPassword` of your user in DB and
change `role` of user to `admin` in db
Now you can login in admin dashboard with your user's `email` and the `password` you already set
Now you can login in admin dashboard with your user's `email` and the `password` you already set

We wrote a migration file to create an admin user in local DB to can test it easily,
so after executing migrations and run application you can navigate to http://localhost:4000/admin and login
with this data `username`: **[email protected]**, `password`: **admin**

**PS**:
A simple script for create encryptedPassword
Expand Down Expand Up @@ -156,39 +160,37 @@ After a site is live or if you want to keep your database you need to use databa

This can be done either by creating a new migration file with:

```
npm run typeorm:cli migration:create -- -n UpdateUserEmailUnique -d migration
```
#### Create new Migration file

Or by changing the entities and generating the migrations with:
```
npm run typeorm:cli migration:generate -- -n UpdateUserEmailUnique
```
npx typeorm-ts-node-esm migration:create ./migration/create_organization_tokens_token_table
Then you need to run the migrations like so:
```

#### Run migrations
```
npm run typeorm:cli -- migration:run
npm run db:migrate:run:local
```

#### Revert migrations
If you want to revert last migration :

```
npm run typeorm:cli -- migration:revert
npm run db:migrate:revert:local
```

### TEST
For running tests you need to register infura and etherscan api-key, and you should pass this environment variables

`PINATA_API_KEY=0000000000000 PINATA_SECRET_API_KEY=00000000000000000000000000000000000000000000000000000000 ETHERSCAN_API_KEY=0000000000000000000000000000000000 XDAI_NODE_HTTP_URL=https://xxxxxx.xdai.quiknode.pro INFURA_API_KEY=0000000000000000000000000000000000 ETHEREUM_NODE_ID=INFURA_API_KEY npm run test`
`PINATA_API_KEY=0000000000000 PINATA_SECRET_API_KEY=00000000000000000000000000000000000000000000000000000000 ETHERSCAN_API_KEY=0000000000000000000000000000000000 XDAI_NODE_HTTP_URL=https://xxxxxx.xdai.quiknode.pro INFURA_API_KEY=0000000000000000000000000000000000 ETHEREUM_NODE_ID=INFURA_API_KEY npm run test`

### PRE_COMMITS
Please before committing your changes run
`npm run prettify` to fix eslint and prettify warnings

You will need to add the above command to your build process so that all database migrations are run upon deployments.

### Statuses
### Statuses
You can generate table with this site
https://www.tablesgenerator.com/markdown_tables

Expand All @@ -203,7 +205,7 @@ https://www.tablesgenerator.com/markdown_tables
| 7 | cancelled | cancelled | Cancelled by Giveth Admin | admin |
| 8 | drafted | drafted | This project is created as a draft for a potential new project, but can be discarded | project owner |

**PS**
**PS**
* If a project is **cancelled** just admin can activate that
* If project is **deactive** both admins and project owner can activate it
* Both admins and project owner can deactivate an **active** project
Expand All @@ -222,14 +224,14 @@ in below image links

### Power Snapshot

Impact graph supports ranking projects based on power boosted by users.
Impact graph supports ranking projects based on power boosted by users.
Users who have GIVpower, can boost a project by allocating a portion (percentage) of their GIVpower to that project and after that impact-graph regularly takes snapshot of user GIVpower balance and boost percentages.
At the end of each givback round (14 days), average of allocated power will be the effective power balance of each project.
At the end of each givback round (14 days), average of allocated power will be the effective power balance of each project.

Snapshotting mechanism is implemented in by the hlp of database cron job and impact graph support of historic user balance on blockchain.
##### Database Snapshot
Snapshot taking on database is implemented by the help `pg_cron` extension on Postgres database.
On regular interval (defined by cron job expression), calls a db procedure called public."TAKE_POWER_BOOSTING_SNAPSHOT".
On regular interval (defined by cron job expression), calls a db procedure called public."TAKE_POWER_BOOSTING_SNAPSHOT".
This procedure creates a new record of power_snapshot and copies power boosting percentages content to another table and associates them to the new power_snapshot record.
###### Cron Job Creation
Cron job creation for test environment is already implemented in dbCronRepository.ts and a modified docker with enabled `pg_cron` extension.
Expand All @@ -251,15 +253,15 @@ The cronjob expression above `*/5 * * * *` is for getting snapshot every 5 minut
3. Find created job id by running
```sql
SELECT * FROM cron.job
ORDER BY jobid DESC
ORDER BY jobid DESC
```
![img.png](docs/img/pg_cron_jobs.png)
4. Alter the job (found its id above, supposed here 1) database to whatever is set for impact-graph, here `deveop`.
4. Alter the job (found its id above, supposed here 1) database to whatever is set for impact-graph, here `deveop`.
```sql
select cron.alter_job(job_id:=1,database:='develop');
```

For archiving old givpower snapshots data we must follow the same structure above, except in the 2nd step we must this command instead
For archiving old givpower snapshots data we must follow the same structure above, except in the 2nd step we must this command instead
```postgresql
CREATE EXTENSION IF NOT EXISTS PG_CRON;
SELECT CRON.schedule(
Expand Down
13 changes: 13 additions & 0 deletions config/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,18 @@ ENABLE_DB_POWER_BOOSTING_SNAPSHOT=
DB_POWER_BOOSTING_SNAPSHOT_CRONJOB_EXPRESSION=
ARCHIVE_POWER_BOOSTING_OLD_SNAPSHOT_DATA_CRONJOB_EXPRESSION=

PROJECT_FILTERS_THREADS_POOL_CONCURRENCY=1
PROJECT_FILTERS_THREADS_POOL_NAME=ProjectFiltersThreadPool
PROJECT_FILTERS_THREADS_POOL_SIZE=4
PROJECT_FILTERS_THREADS_POOL_DURATION=60000

# OPTIONAL - force logging to stdout when the value is true
LOG_STDOUT=false

CHAINVINE_ADAPTER=mock
#CHAINVINE_ADAPTER=chainvine
CHAINVINE_API_ENABLE_TEST_MODE=true


# We should not try to verify donaitons after some hours, because checking old donations would make lots of requests to web3 providers
DONATION_VERIFICAITON_EXPIRATION_HOURS=24
12 changes: 11 additions & 1 deletion config/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ FILL_POWER_SNAPSHOT_BALANCE_SERVICE_ACTIVE=false
FILL_POWER_SNAPSHOT_BALANCE_CRONJOB_EXPRESSION=0 * * * * *
# Set 10 to do it fast while running tests
NUMBER_OF_FILLING_POWER_SNAPSHOT_BALANCE_CONCURRENT_JOB=20
ENABLE_DB_POWER_BOOSTING_SNAPSHOT=true
ENABLE_DB_POWER_BOOSTING_SNAPSHOT=false
DB_POWER_BOOSTING_SNAPSHOT_CRONJOB_EXPRESSION =0 0 1 1 *

NOTIFICATION_CENTER_ADAPTER=mock
Expand All @@ -144,5 +144,15 @@ ONRAMPER_SECRET=secreto
ENABLE_GIV_POWER_TESTING=true
THIRD_PARTY_PROJECTS_ADMIN_USER_ID=4

PROJECT_FILTERS_THREADS_POOL_CONCURRENCY=1
PROJECT_FILTERS_THREADS_POOL_NAME=ProjectFiltersThreadPool
PROJECT_FILTERS_THREADS_POOL_SIZE=1
PROJECT_FILTERS_THREADS_POOL_DURATION=600

# OPTIONAL - force logging to stdout when the value is true
LOG_STDOUT=false

CHAINVINE_ADAPTER=mock
CHAINVINE_API_ENABLE_TEST_MODE=true
# We should not try to verify donaitons after some hours, because checking old donations would make lots of requests to web3 providers
DONATION_VERIFICAITON_EXPIRATION_HOURS=24
2 changes: 1 addition & 1 deletion migration-old-backup/1614082100757-SeedProjectStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SeedProjectStatus1614082100757 implements MigrationInterface {

// await projectStatusRepository.save(projectStatuses)

await queryRunner.query(`INSERT INTO public.project_status (symbol,"name",description) VALUES
await queryRunner.query(`INSERT INTO public.project_status (symbol,"name",description) VALUES
('rjt','rejected','This project has been rejected by Giveth or platform owner')
,('pen','pending','This project is created, but pending approval')
,('clr','clarificaiton','Clarification requested by Giveth or platform owner')
Expand Down
1 change: 1 addition & 0 deletions migration-old-backup/1615311053918-MakeSlugLowerCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export class MakeSlugLowerCase1615311053918 implements MigrationInterface {
);
}

// tslint:disable-next-line:no-empty
async down(queryRunner: QueryRunner): Promise<void> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export class AddVerifiedGivebackDefaults1620412189526
);
}

// tslint:disable-next-line:no-empty
async down(queryRunner: QueryRunner): Promise<void> {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

// tslint:disable-next-line:class-name
export class addSlugHistoryColumnToProjects1632203974464
implements MigrationInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

// tslint:disable-next-line:class-name
export class makeWalletAddressNotNull1634808878248
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
'ALTER TABLE "project" ALTER COLUMN "walletAddress" SET NOT NULL',
);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
// tslint:disable-next-line:no-empty
async down(queryRunner: QueryRunner): Promise<void> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
export class UpdateTotalProjectUpdates1637708818194
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "project" ADD COLUMN IF NOT EXISTS "totalProjectUpdates" Integer default 0`,
);
Expand All @@ -18,7 +18,7 @@ export class UpdateTotalProjectUpdates1637708818194
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "project" DROP COLUMN IF EXISTS "totalProjectUpdates"`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
export class SetupUpdatedAtOnProjects1637809572821
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "project" ADD COLUMN IF NOT EXISTS "updatedAt" TIMESTAMP`,
);
await queryRunner.query(`UPDATE project SET "updatedAt"="creationDate"`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE project DROP "updatedAt"`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
export class FixProjectUpdatesCountOnProjects1638381277963
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "project" ADD COLUMN IF NOT EXISTS "totalProjectUpdates" Integer default 0`,
);
Expand All @@ -18,7 +18,7 @@ export class FixProjectUpdatesCountOnProjects1638381277963
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "project" DROP COLUMN IF EXISTS "totalProjectUpdates"`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
export class FixAnonymousDonationBoolean1638386837934
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
'UPDATE donation SET anonymous = true WHERE "userId" IS NULL',
);
Expand All @@ -13,7 +13,7 @@ export class FixAnonymousDonationBoolean1638386837934
}

// Revert in case something happens, seems only the new design will use this
public async down(queryRunner: QueryRunner): Promise<void> {
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
'UPDATE "donation" SET anonymous = false WHERE "userId" IS NULL',
);
Expand Down
6 changes: 4 additions & 2 deletions migration-old-backup/1640240892974-fillDonationsUserId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
import { User } from '../src/entities/user';
import { Donation } from '../src/entities/donation';

// tslint:disable-next-line:class-name
export class fillDonationsUserId1640240892974 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
const donations = await queryRunner.query(
`
SELECT donation.id AS "donationId", "fromWalletAddress", u.id AS "userId" FROM donation
SELECT donation.id AS "donationId", "fromWalletAddress", u.id AS "userId" FROM donation
INNER JOIN public."user" AS u ON "walletAddress" = "fromWalletAddress"
WHERE anonymous=true AND "userId" IS null
WHERE anonymous=true AND "userId" IS null
ORDER BY donation.id ASC
`,
);
Expand All @@ -23,5 +24,6 @@ export class fillDonationsUserId1640240892974 implements MigrationInterface {
}
}

// tslint:disable-next-line:no-empty
async down(queryRunner: QueryRunner): Promise<void> {}
}
1 change: 1 addition & 0 deletions migration/1614079067364-AddProjectStatus.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:no-console */
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddProjectStatus1614079067364 implements MigrationInterface {
Expand Down
3 changes: 1 addition & 2 deletions migration/1614082100757-SeedProjectStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class SeedProjectStatus1614082100757 implements MigrationInterface {
if (projectStatuses.length > 0) {
return;
}
await queryRunner.query(`INSERT INTO public.project_status (symbol,"name",description) VALUES
await queryRunner.query(`INSERT INTO public.project_status (symbol,"name",description) VALUES
('rejected','rejected','This project has been rejected by Giveth or platform owner, We dont use it now')
,('pending','pending','This project is created, but pending approval, We dont use it now')
,('clarification','clarification','Clarification requested by Giveth or platform owner, We dont use it now')
Expand All @@ -20,7 +20,6 @@ export class SeedProjectStatus1614082100757 implements MigrationInterface {
,('drafted', 'drafted', 'This project is created as a draft for a potential new project, but can be discarded')
;`);
}
x;

async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
Expand Down
Loading

0 comments on commit f31aa9f

Please sign in to comment.