Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions Ghostfolio/Ghostfolio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace App\SupportedApps\Ghostfolio;

use Exception;

class Ghostfolio extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;

public function __construct()
{
}

public function test()
{
try {
$this->auth();
$perf = $this->getPortfolioPerformance();

echo "Successfully communicated with the API";
} catch (Exception $err) {
echo "Error connecting to Ghostfolio: " . $err->getMessage();
}
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
}

public function auth(): void
{
$body = json_encode(["accessToken" => $this->config->password]);

$vars = [
"http_errors" => false,
"timeout" => 5,
"body" => $body,
"headers" => [
"Content-Type" => "application/json",
],
];

$result = parent::execute(
$this->url("api/v1/auth/anonymous"),
[],
$vars,
"POST"
);

if ($result === null) {
throw new Exception("Could not connect to Ghostfolio");
}

$responseBody = $result->getBody()->getContents();
$response = json_decode($responseBody, true);
if (null === $response || $result->getStatusCode() !== 201 || !isset($response['authToken'])) {
throw new Exception("Error logging in");
}

$this->config->jwt = $response['authToken'];
}


public function livestats()
{
$this->auth();
$status = 'inactive';

$data = $this->getPortfolioPerformance();
foreach ($this->config->availablestats as $num => $key) {
$stat = new \stdClass();
$stat->title = self::getAvailableStats()[$key];
$value = $data[$key] ?? null;
$stat->value = is_numeric($value)
? rtrim(
rtrim(number_format($value, 1, decimal_separator: '.', thousands_separator: ''), characters: '0'),
characters: '.'
)
: substr(string: $value, offset: 0, length: 1);
$details["visiblestats"][] = $stat;
}
return parent::getLiveStats($status, $details);
}



private function getPortfolioPerformance(): mixed
{
$attrs = [
"headers" => [
"Content-Type" => "application/json",
"Authorization" => "Bearer " . $this->config->jwt,
],
"query" => [
"range" => $this->config->selected_range,
],
];
$result = parent::execute(
$this->url("api/v2/portfolio/performance"),
$attrs,
);

if (null === $result || $result->getStatusCode() !== 200) {
throw new Exception("Error retrieving performance");
}
$data = json_decode($result->getBody()->getContents(), true);
return $data['performance'];
}

public static function getAvailableStats()
{
return [
"netPerformancePercentageWithCurrencyEffect" => "Net Perf (%)",
"netPerformanceWithCurrencyEffect" => "Net Perf",
"totalInvestment" => "Invested",
"currentValueInBaseCurrency" => "Value",
"currentNetWorth" => "Net Worth",
];
}
}
10 changes: 10 additions & 0 deletions Ghostfolio/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"appid": "636e86d7a1a80e47d1ec19e6782e3700227c7603",
"name": "Ghostfolio",
"website": "https://ghostfol.io",
"license": "GNU Affero General Public License v3.0 only",
"description": "test",
"enhanced": true,
"tile_background": "dark",
"icon": "ghostfolio.png"
}
31 changes: 31 additions & 0 deletions Ghostfolio/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>

<div class="items" style="flex-direction:column">
<div style="display:flex;flex-direction:row;">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.password') }} (secret token)</label>
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
</div>
</div>
<div style="display:flex;flex-direction:row;">
<div class="input">
<label>Time Range</label>
{!! Form::select(
'config[selected_range]',
['1d' => '1 Day', 'wtd' => 'Week To Date', 'ytd' => 'Year To Date', 'max' => 'MAX'],
isset($item) ? $item->getconfig()->selected_range : null,
['data-config' => 'selected_range', 'class' => 'form-control config-item'],
) !!}
</div>
<div class="input">
<label>Stats to show</label>
{!! Form::select('config[availablestats][]', App\SupportedApps\Ghostfolio\Ghostfolio::getAvailableStats(), isset($item) ? $item->getConfig()->availablestats ?? null : null, ['multiple' => 'multiple']) !!}
</div>
<dsclass="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
Binary file added Ghostfolio/ghostfolio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions Ghostfolio/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul class="livestats">
@foreach ($visiblestats as $stat)
<li>
<span class="title">{!! $stat->title !!}</span>
<strong>{!! $stat->value !!}</strong>
</li>
@endforeach
</ul>