Skip to content

Commit

Permalink
Merge pull request #14 from ledgerleapllc/staging
Browse files Browse the repository at this point in the history
Staging to master
  • Loading branch information
ledgerleapllc authored Apr 28, 2022
2 parents 99e939d + bf89d11 commit 1954bfe
Show file tree
Hide file tree
Showing 190 changed files with 13,914 additions and 1,494 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

HELLOSIGN_API_KEY=
HELLOSIGN_API_KEY_HOOK=
HELLOSIGN_CLIENT_ID=

SHUFTI_CLIENT_ID=
SHUFTI_CLIENT_SECRET=
SITE_URL=
COINMARKETCAP_KEY=

SEENA_API_KEY=
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Contributing to Casper Association Member Portal

### Please follow these guidelines if you want to contribute to this project

1. Fork the project.
2. Create a new branch from master (e.g. features/my-new-feature or issue/123-my-bugfix)
3. Try to follow the same coding style with regards to spacing and line width, etc. Following PSR-4 will more than suffice.
4. Create a pull request. Please create PR to development branch so we can do proper testing and staging before merging with master branch.

### Report bugs using Github issues

You can report a bug by clicking on the issues tab and 'New Issue'. Describe the issue the best you can. This makes it easy to fork and create a branch named after the issue purposed for fixing it.

Thank you for your interest in the project!

For security related issues, please email [email protected]
81 changes: 75 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,81 @@
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400"></a></p>

<p align="center">
<a href="https://travis-ci.org/laravel/framework"><img src="https://travis-ci.org/laravel/framework.svg" alt="Build Status"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
<img src="https://caspermember.com/images/logo.png" width="400">
</p>

# Casper Association Member Portal

The Casper Association's member portal.

This is the backend repo of the portal. To see the frontend repo, visit https://github.com/ledgerleapllc/CasperAssociationPortal

## Prerequisites

- Apache/2.4.29+ (Ubuntu)
- PHP 7.4+
- MySql Ver 14.14 Distrib 5.7.37
- Laravel Framework 8.47.0

## Setup

We generally would use the latest version of Ubuntu for testing installs. Example hosting server: AWS ec2 t2 medium with at least 10Gb SSD.

```bash
sudo apt -y install apache2
sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod ssl
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.4
sudo apt-get install -y php7.4-{bcmath,bz2,intl,gd,mbstring,mysql,zip,common,curl,xml}
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
```

Setup the repo according to our VHOST path. Note, the actual VHOST path in this case should be set to **/var/www/CasperAssociationPortalBackend/public**

```bash
cd /var/www/
git clone https://github.com/ledgerleapllc/CasperAssociationPortalBackend
cd CasperAssociationPortalBackend
```

Install packages and setup environment

```bash
composer install
composer update
cp .env.example .env
```

After adjusting .env with your variables, run Artisan to finish setup

```bash
php artisan key:generate
php artisan migrate
php artisan passport:install
php artisan config:clear
php artisan route:clear
php artisan cache:clear
(crontab -l 2>>/dev/null; echo "* * * * * cd /var/www/CasperAssociationPortalBackend && php artisan schedule:run >> /dev/null 2>&1") | crontab -
```

You may also have to authorize Laravel to write to the storage directory

```bash
sudo chown -R www-data:www-data storage/
```

Last, you need to setup roles and admins to start using the portal and see it work. Visit the URL of the backend with the path **/install**. This will install these things for you. You will find your admin credentials generated in the Laravel log file. You may want to disable this endpoint after the initial install to prevent this install endpoint from being used again if you are planning on deploying to a production environment in the future. This is easily done by switching ENV variable **INSTALL_PATH_ENABLED** to 0, or false. You may need to run the following command if Laravel caching is on.

```bash
php artisan config:clear
```

<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400"></a></p>

## About Laravel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
Expand Down
62 changes: 62 additions & 0 deletions app/Console/Commands/CheckBallot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Console\Commands;

use App\Models\Ballot;
use Carbon\Carbon;
use Illuminate\Console\Command;

class CheckBallot extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ballot:check';

/**
* The console command description.
*
* @var string
*/
protected $description = 'ballot check';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$settings = getSettings();
$quorumRate = $settings['quorum_rate_ballot'] ?? 50;
$now = Carbon::now('UTC');
$ballots = Ballot::with(['vote'])->where('status', 'active')->where('time_end', '<=', $now)->get();
foreach ($ballots as $ballot) {
$vote = $ballot->vote;
if ($vote->result_count == 0) {
$ballot->status = 'fail';
$ballot->save();
} else {
$quorum = $vote->for_value / $vote->result_count * 100;
if($quorum >= $quorumRate) {
$ballot->status = 'pass';
} else {
$ballot->status = 'fail';
}
$ballot->save();
}
}
}
}
198 changes: 198 additions & 0 deletions app/Console/Commands/CheckNodeStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace App\Console\Commands;

use App\Models\MonitoringCriteria;
use App\Models\User;

use Carbon\Carbon;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class CheckNodeStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'node-status:check';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check node status';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uptime = MonitoringCriteria::where('type', 'uptime')->first();
$uptimeProbationStart = $uptime->probation_start;
if ($uptime->given_to_correct_unit == 'Weeks') {
$uptimeTime = (float) $uptime->given_to_correct_value * 7 * 24;
} else if ($uptime->given_to_correct_unit == 'Days') {
$uptimeTime = (float) $uptime->given_to_correct_value * 24;
} else {
$uptimeTime = (float) $uptime->given_to_correct_value;
}

$blockHeight = MonitoringCriteria::where('type', 'block-height')->first();
$blockHeightProbationStart = (float) $blockHeight->probation_start;
if ($blockHeight->given_to_correct_unit == 'Weeks') {
$blockHeightTime = (float) $blockHeight->given_to_correct_value * 7 * 24;
} else if ($blockHeight->given_to_correct_unit == 'Days') {
$blockHeightTime = (float) $blockHeight->given_to_correct_value * 24;
} else {
$blockHeightTime = (float) $blockHeight->given_to_correct_value;
}

$updateResponsiveness = MonitoringCriteria::where('type', 'update-responsiveness')->first();
$updateResponsivenessProbationStart = (float) $updateResponsiveness->probation_start;
if ($updateResponsiveness->given_to_correct_unit == 'Weeks') {
$updateResponsivenessTime = (float)$updateResponsiveness->given_to_correct_value * 7 * 24;
} else if ($updateResponsiveness->given_to_correct_unit == 'Days') {
$updateResponsivenessTime = (float) $updateResponsiveness->given_to_correct_value * 24;
} else {
$updateResponsivenessTime = (float) $updateResponsiveness->given_to_correct_value;
}

$now = Carbon::now('UTC');
$users = User::where('role', 'member')
->where('banned', 0)
->with(['metric', 'nodeInfo', 'profile'])
->get();

foreach ($users as $user) {
$user->node_status = 'Online';
$user->save();

$nodeInfo = $user->nodeInfo ? $user->nodeInfo : $user->metric;

if (!$nodeInfo || !$user->node_verified_at || !$user->letter_verified_at || !$user->signature_request_id) {
$user->node_status = null;
$user->save();
} else if ($user->is_fail_node == 1) {
$user->node_status = 'Offline';
$user->save();
} else if ($nodeInfo) {
$nodeInfo->uptime = $nodeInfo->uptime ? $nodeInfo->uptime : 0;
$nodeInfo->block_height_average = $nodeInfo->block_height_average ? $nodeInfo->block_height_average : 0;
$nodeInfo->update_responsiveness = $nodeInfo->update_responsiveness ? $nodeInfo->update_responsiveness : 100;
if (
$nodeInfo->uptime >= $uptimeProbationStart &&
$nodeInfo->block_height_average >= $blockHeightProbationStart &&
$nodeInfo->update_responsiveness >= $updateResponsivenessProbationStart
) {
$user->node_status = 'Online';
$user->save();

$nodeInfo->uptime_time_start = null;
$nodeInfo->uptime_time_end = null;

$nodeInfo->block_height_average_time_start = null;
$nodeInfo->block_height_average_time_end = null;

$nodeInfo->update_responsiveness_time_start = null;
$nodeInfo->update_responsiveness_time_end = null;

$nodeInfo->save();
}
}

if ($user->profile && $user->profile->status == 'approved' && $nodeInfo) {
$user->profile->extra_status = null;
$user->profile->save();

if ($nodeInfo->uptime < $uptimeProbationStart) {
$user->profile->extra_status = 'On Probation';
$nodeInfo->uptime_time_start = now();
$nodeInfo->uptime_time_end = Carbon::now('UTC')->addHours($uptimeTime);
}

if ($nodeInfo->block_height_average < $blockHeightProbationStart) {
$user->profile->extra_status = 'On Probation';
$nodeInfo->block_height_average_time_start = now();
$nodeInfo->block_height_average_time_end = Carbon::now('UTC')->addHours($blockHeightTime);
}

if ($nodeInfo->update_responsiveness < $updateResponsivenessProbationStart) {
$user->profile->extra_status = 'On Probation';
$nodeInfo->update_responsiveness_time_start = now();
$nodeInfo->update_responsiveness_time_end = Carbon::now('UTC')->addHours($updateResponsivenessTime);
}

$user->profile->save();
$nodeInfo->save();

if ($user->profile->extra_status == 'On Probation') {
if ($nodeInfo->uptime_time_end <= $now && $nodeInfo->uptime < $uptimeProbationStart) {
$user->profile->extra_status = 'Suspended';
}
if ($nodeInfo->block_height_average_time_end <= $now && $nodeInfo->block_height_average < $blockHeightProbationStart) {
$user->profile->extra_status = 'Suspended';
}
if ($nodeInfo->update_responsiveness_time_end <= $now && $nodeInfo->update_responsiveness < $updateResponsivenessProbationStart) {
$user->profile->extra_status = 'Suspended';
}
$user->profile->save();
}
}

/*
if ($user->node_status != 'Probation' && $user->node_status != 'Pulled') {
if ($nodeInfo->uptime < $uptimeProbationStart) {
$user->node_status = 'Probation';
$nodeInfo->uptime_time_start = now();
$nodeInfo->uptime_time_end = Carbon::now('UTC')->addHours($uptimeTime);
}
if ($nodeInfo->block_height_average < $blockHeightProbationStart) {
$user->node_status = 'Probation';
$nodeInfo->block_height_average_time_start = now();
$nodeInfo->block_height_average_time_end = Carbon::now('UTC')->addHours($blockHeightTime);
}
if ($nodeInfo->update_responsiveness < $updateResponsivenessProbationStart) {
$user->node_status = 'Probation';
$nodeInfo->update_responsiveness_time_start = now();
$nodeInfo->update_responsiveness_time_end = Carbon::now('UTC')->addHours($updateResponsivenessTime);
}
$user->save();
$nodeInfo->save();
continue;
}
if ($user->node_status == 'Probation') {
if ($nodeInfo->uptime_time_end <= $now && $nodeInfo->uptime < $uptimeProbationStart) {
$user->node_status = 'Pulled';
}
if ($nodeInfo->block_height_average_time_end <= $now && $nodeInfo->block_height_average < $blockHeightProbationStart) {
$user->node_status = 'Pulled';
}
if ($nodeInfo->update_responsiveness_time_end <= $now && $nodeInfo->update_responsiveness < $updateResponsivenessProbationStart) {
$user->node_status = 'Pulled';
}
$user->save();
}
*/
}
}
}
Loading

0 comments on commit 1954bfe

Please sign in to comment.