clean up scheduled commands

also add comments to each command
This commit is contained in:
hackerESQ
2024-09-05 21:21:18 -05:00
parent 6f63ac7067
commit 8511cc833d
12 changed files with 51 additions and 35 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ class CaptureDailyChange extends Command
return $holding->market_data->market_value * $holding->quantity; return $holding->market_data->market_value * $holding->quantity;
}); });
$portfolio->daily_changes()->create([ $portfolio->daily_change()->create([
'date' => now(), 'date' => now(),
'total_market_value' => $total_market_value, 'total_market_value' => $total_market_value,
'total_cost_basis' => $total_cost_basis, 'total_cost_basis' => $total_cost_basis,
+1 -2
View File
@@ -39,8 +39,7 @@ class RefreshDividendData extends Command
*/ */
public function handle() public function handle()
{ {
// $holdings = Holding::where('quantity', '>', 0)->distinct()->get(['symbol']); $holdings = Holding::where('quantity', '>', 0)->distinct()->get(['symbol']);
$holdings = Holding::distinct()->get(['symbol']);
foreach ($holdings as $holding) { foreach ($holdings as $holding) {
$this->line('Refreshing ' . $holding->symbol); $this->line('Refreshing ' . $holding->symbol);
+5 -6
View File
@@ -41,16 +41,15 @@ class RefreshMarketData extends Command
public function handle() public function handle()
{ {
// get all symbols from market data // get all symbols from market data
$symbols = Holding::where('quantity', '>', 0) $holdings = Holding::where('quantity', '>', 0)
->select(['symbol']) ->select(['symbol'])
->distinct() ->distinct()
->get() ->get();
->pluck('symbol');
foreach ($symbols as $symbol) { foreach ($holdings as $holding) {
$this->line('Refreshing ' . $symbol); $this->line('Refreshing ' . $holding->symbol);
MarketData::getMarketData($symbol); MarketData::getMarketData($holding->symbol);
} }
} }
} }
+1 -1
View File
@@ -40,7 +40,7 @@ class RefreshSplitData extends Command
*/ */
public function handle() public function handle()
{ {
$holdings = Holding::distinct()->get(['symbol']); $holdings = Holding::where('quantity', '>', 0)->distinct()->get(['symbol']);
foreach ($holdings as $holding) { foreach ($holdings as $holding) {
$this->line('Refreshing ' . $holding->symbol); $this->line('Refreshing ' . $holding->symbol);
+2 -2
View File
@@ -12,14 +12,14 @@ class SyncHoldingData extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'holding-data:refresh'; protected $signature = 'holding-data:sync';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Refresh holdings'; protected $description = 'Syncs holdings with transactions and dividends';
/** /**
* Create a new command instance. * Create a new command instance.
+1 -1
View File
@@ -25,7 +25,7 @@ class PortfolioController extends Controller
$portfolio->load(['transactions', 'holdings']); $portfolio->load(['transactions', 'holdings']);
// get portfolio metrics // get portfolio metrics
$metrics = cache()->tags(['metrics', 'portfolio', auth()->user()->id, $portfolio->id])->remember( $metrics = cache()->tags(['metrics', 'portfolio', $portfolio->id])->remember(
'portfolio-metrics-' . $portfolio->id, 'portfolio-metrics-' . $portfolio->id,
60, 60,
function () use ($portfolio) { function () use ($portfolio) {
-1
View File
@@ -22,7 +22,6 @@ class Dividend extends Model
protected $casts = [ protected $casts = [
'date' => 'datetime', 'date' => 'datetime',
'first_date' => 'datetime',
'last_date' => 'datetime', 'last_date' => 'datetime',
]; ];
-2
View File
@@ -25,12 +25,10 @@ class Holding extends Model
'realized_gain_dollars', 'realized_gain_dollars',
'dividends_earned', 'dividends_earned',
'splits_synced_at', 'splits_synced_at',
'dividends_synced_at'
]; ];
protected $casts = [ protected $casts = [
'splits_synced_at' => 'datetime', 'splits_synced_at' => 'datetime',
'dividends_synced_at' => 'datetime',
]; ];
protected $attributes = [ protected $attributes = [
+9 -9
View File
@@ -24,7 +24,6 @@ class Split extends Model
protected $casts = [ protected $casts = [
'date' => 'datetime', 'date' => 'datetime',
'first_date' => 'datetime',
'last_date' => 'datetime', 'last_date' => 'datetime',
]; ];
@@ -48,7 +47,6 @@ class Split extends Model
// dates for split data // dates for split data
$splits_meta = self::where(['symbol' => $symbol]) $splits_meta = self::where(['symbol' => $symbol])
->selectRaw('COUNT(symbol) as total_splits') ->selectRaw('COUNT(symbol) as total_splits')
->selectRaw('MIN(date) as first_date')
->selectRaw('MAX(date) as last_date') ->selectRaw('MAX(date) as last_date')
->get() ->get()
->first(); ->first();
@@ -76,8 +74,6 @@ class Split extends Model
// sync to transactions // sync to transactions
self::syncToTransactions($symbol); self::syncToTransactions($symbol);
return $split_data;
} }
/** /**
@@ -92,7 +88,7 @@ class Split extends Model
$splits = self::where([ $splits = self::where([
'splits.symbol' => $symbol, 'splits.symbol' => $symbol,
]) ])
->whereDate('transactions.date', '>', DB::raw('IFNULL(market_data.splits_synced_to_holdings_at, "0000-00-00")')) ->whereDate('transactions.date', '>', DB::raw('IFNULL(holdings.splits_synced_at, "0000-00-00")'))
->select([ ->select([
'splits.date', 'splits.date',
'splits.symbol', 'splits.symbol',
@@ -100,7 +96,7 @@ class Split extends Model
'transactions.portfolio_id' 'transactions.portfolio_id'
]) ])
->join('transactions', 'transactions.symbol', 'splits.symbol') ->join('transactions', 'transactions.symbol', 'splits.symbol')
->join('market_data', 'transactions.symbol', 'market_data.symbol') ->join('holdings', 'transactions.symbol', 'holdings.symbol')
->orderBy('splits.date', 'ASC') ->orderBy('splits.date', 'ASC')
->get(); ->get();
@@ -126,11 +122,15 @@ class Split extends Model
'created_at' => now(), 'created_at' => now(),
'updated_at' => now() 'updated_at' => now()
]); ]);
Holding::where([
'symbol' => $split->symbol,
'portfolio_id' => $split->portfolio_id
])->update([
'splits_synced_at' => now()
]);
} }
} }
// // update market data with latest date
// MarketData::setSplitsHoldingSynced($symbol);
} }
+3 -2
View File
@@ -17,6 +17,7 @@ class Transaction extends Model
protected $fillable = [ protected $fillable = [
'symbol', 'symbol',
'date', 'date',
'portfolio_id',
'transaction_type', 'transaction_type',
'quantity', 'quantity',
'cost_basis', 'cost_basis',
@@ -48,14 +49,14 @@ class Transaction extends Model
$transaction->refreshMarketData(); $transaction->refreshMarketData();
cache()->tags(['metrics', auth()->user()->id])->flush(); cache()->tags(['metrics', $transaction->portfolio_id])->flush();
}); });
static::deleted(function ($transaction) { static::deleted(function ($transaction) {
$transaction->syncToHolding(); $transaction->syncToHolding();
cache()->tags(['metrics', auth()->user()->id])->flush(); cache()->tags(['metrics', $transaction->portfolio_id])->flush();
}); });
} }
@@ -25,7 +25,6 @@ class CreateHoldingsTable extends Migration
$table->float('realized_gain_dollars', 12, 4)->nullable(); $table->float('realized_gain_dollars', 12, 4)->nullable();
$table->float('dividends_earned', 12, 4)->nullable(); $table->float('dividends_earned', 12, 4)->nullable();
$table->timestamp('splits_synced_at')->nullable(); $table->timestamp('splits_synced_at')->nullable();
$table->timestamp('dividends_synced_at')->nullable();
$table->timestamps(); $table->timestamps();
}); });
} }
+28 -7
View File
@@ -1,14 +1,35 @@
<?php <?php
use Illuminate\Support\Facades\Schedule; use Illuminate\Support\Facades\Schedule;
use App\Console\Commands\{ use App\Console\Commands\{RefreshMarketData, CaptureDailyChange, RefreshDividendData, RefreshSplitData, SyncHoldingData};
RefreshMarketData,
CaptureDailyChange,
RefreshDividendData,
RefreshSplitData
};
Schedule::command(RefreshMarketData::class)->weekdays()->everyMinute(); // configurable in 'config.market_data' /**
*
* This scheduled job refreshes market data from your selected data provider
* Update the cadence with the MARKET_DATA_REFRESH key in your env file
*/
Schedule::command(RefreshMarketData::class)->weekdays()->everyMinute();
/**
*
* This scheduled job records daily changes to your portfolios every weekday
*/
Schedule::command(CaptureDailyChange::class)->weekdays(); Schedule::command(CaptureDailyChange::class)->weekdays();
/**
*
* Refreshes dividend data for your holdings (and syncs new dividends to holdings)
*/
Schedule::command(RefreshDividendData::class)->weekly(); Schedule::command(RefreshDividendData::class)->weekly();
/**
*
* Refreshes split data for your holdings (and creates new transactions for new splits)
*/
Schedule::command(RefreshSplitData::class)->monthly(); Schedule::command(RefreshSplitData::class)->monthly();
/**
*
* Periodically reconciles your holdings with transactions and dividends
*/
Schedule::command(SyncHoldingData::class)->yearly();