Skip to content

Commit

Permalink
Merge pull request #330 from Steinbeck-Lab/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
CS76 authored Jan 15, 2025
2 parents 7e58a31 + 78e604c commit e4f8733
Show file tree
Hide file tree
Showing 50 changed files with 3,346 additions and 1,499 deletions.
18 changes: 18 additions & 0 deletions SocialiteProviders/src/NFDIAAI/NFDIAAIExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SocialiteProviders\NFDIAAI;

use SocialiteProviders\Manager\SocialiteWasCalled;

class NFDIAAIExtendSocialite
{
/**
* Register the provider.
*
* @return void
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('nfdi-aai', Provider::class);
}
}
66 changes: 66 additions & 0 deletions SocialiteProviders/src/NFDIAAI/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace SocialiteProviders\NFDIAAI;

use GuzzleHttp\RequestOptions;
use InvalidArgumentException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
const IDENTIFIER = 'NFDI-AAI';

/**
* {@inheritdoc}
*/
protected $scopes = ['basic'];

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://regapp.nfdi-aai.de/oidc/realms/nfdi/protocol/openid-connect/auth', $state);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://regapp.nfdi-aai.de/oidc/realms/nfdi/protocol/openid-connect/token';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://regapp.nfdi-aai.de/oidc/realms/nfdi/protocol/openid-connect/userinfo', [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
],
]);

return json_decode((string) $response->getBody(), true);
}

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
if (! isset($user['id']) || empty($user['id'])) {
throw new InvalidArgumentException('The user data is invalid: a unique "id" is required.');
}

return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['username'],
'name' => $user['name'],
'email' => $user['email'],
'avatar' => $user['avatar'],
]);
}
}
21 changes: 21 additions & 0 deletions SocialiteProviders/src/NFDIAAI/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "socialiteproviders/nfdi-aai",
"description": "NFDIAAI OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"authors": [
{
"name": "VCNainala",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"ext-json": "*",
"socialiteproviders/manager": "^4.4"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\NFDIAAI\\": ""
}
}
}
18 changes: 9 additions & 9 deletions app/Console/Commands/DashWidgetsRefresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,49 @@ public function handle()
// Cache::flush();

// Create the cache for all DashboardStats widgets
Cache::rememberForever('stats.collections', function () {
Cache::flexible('stats.collections', [172800, 259200], function () {
return DB::table('collections')->selectRaw('count(*)')->get()[0]->count;
});
$this->info('Cache for collections refreshed.');

Cache::rememberForever('stats.citations', function () {
Cache::flexible('stats.citations', [172800, 259200], function () {
return DB::table('citations')->selectRaw('count(*)')->get()[0]->count;
});
$this->info('Cache for citations refreshed.');

Cache::rememberForever('stats.organisms', function () {
Cache::flexible('stats.organisms', [172800, 259200], function () {
return DB::table('organisms')->selectRaw('count(*)')->get()[0]->count;
});
$this->info('Cache for organisms refreshed.');

Cache::rememberForever('stats.geo_locations', function () {
Cache::flexible('stats.geo_locations', [172800, 259200], function () {
return DB::table('geo_locations')->selectRaw('count(*)')->get()[0]->count;
});
$this->info('Cache for geo locations refreshed.');

Cache::rememberForever('stats.reports', function () {
Cache::flexible('stats.reports', [172800, 259200], function () {
return DB::table('reports')->selectRaw('count(*)')->get()[0]->count;
});
$this->info('Cache for reports refreshed.');

// Create the cache for all DashboardStatsMid widgets

Cache::rememberForever('stats.molecules.non_stereo', function () {
Cache::flexible('stats.molecules.non_stereo', [172800, 259200], function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('has_stereo=false and is_parent=false')->get()[0]->count;
});
$this->info('Cache for molecules non-stereo refreshed.');

Cache::rememberForever('stats.molecules.stereo', function () {
Cache::flexible('stats.molecules.stereo', [172800, 259200], function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('has_stereo=true')->get()[0]->count;
});
$this->info('Cache for molecules stereo refreshed.');

Cache::rememberForever('stats.molecules.parent', function () {
Cache::flexible('stats.molecules.parent', [172800, 259200], function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('has_stereo=false and is_parent=true')->get()[0]->count;
});
$this->info('Cache for molecules parent refreshed.');

Cache::rememberForever('stats.molecules', function () {
Cache::flexible('stats.molecules', [172800, 259200], function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('active=true and NOT (is_parent=true AND has_variants=true)')->get()[0]->count;
});
$this->info('Cache for molecules refreshed.');
Expand Down
79 changes: 79 additions & 0 deletions app/Console/Commands/GenerateHeatMapData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Console\Commands;

use App\Models\Collection;
use Illuminate\Console\Command;

class GenerateHeatMapData extends Command
{
protected $signature = 'coconut:generate-heat-map-data';

protected $description = 'This generates Heat Map data for collection overlaps.';

public function handle()
{
$heat_map_data = [];
$collections = Collection::all();

// Store molecule identifiers
foreach ($collections as $collection) {
$molecule_identifiers = $collection->molecules()->pluck('identifier')->toArray();
$molecule_identifiers = array_map(function ($item) {
return preg_replace('/^CNP/i', '', $item);
}, $molecule_identifiers);
$heat_map_data['ids'][$collection->id . '|' . $collection->title] = $molecule_identifiers;
}

// Calculate percentage overlaps -> ol_d = overlap data
$heat_map_data['ol_d'] = [];
$collection_keys = array_keys($heat_map_data['ids']);

foreach ($collection_keys as $collection1_key) {
$heat_map_data['ol_d'][$collection1_key] = [];
$set1 = array_unique($heat_map_data['ids'][$collection1_key]);
$set1_count = count($set1);

foreach ($collection_keys as $collection2_key) {
$set2 = array_unique($heat_map_data['ids'][$collection2_key]);
$set2_count = count($set2);

// Calculate intersection
$intersection = array_intersect($set1, $set2);
$intersection_count = count($intersection);

// Calculate percentage overlap
if ($set1_count > 0 && $set2_count > 0) {
// Using Jaccard similarity: intersection size / union size
$union_count = $set1_count + $set2_count - $intersection_count;
$overlap_percentage = ($intersection_count / $union_count) * 100;
} else {
$overlap_percentage = 0;
}

$heat_map_data['ol_d'][$collection1_key][$collection2_key] = round($overlap_percentage, 2);

// Add additional overlap statistics -> ol_s = overlap_stats
$heat_map_data['ol_s'][$collection1_key][$collection2_key] = [
// ol = overlap count
'ol' => $intersection_count,
'c1_count' => $set1_count,
'c2_count' => $set2_count,
'p' => round($overlap_percentage, 2),
];
}
}
unset($heat_map_data['ids']);

$json = json_encode($heat_map_data, JSON_UNESCAPED_SLASHES);

// Save the JSON to a file
$filePath = public_path('reports/heat_map_metadata.json');
if (! file_exists(dirname($filePath))) {
mkdir(dirname($filePath), 0777, true);
}
file_put_contents($filePath, $json);

$this->info('JSON metadata saved to public/reports/heat_map_metadata.json');
}
}
5 changes: 4 additions & 1 deletion app/Filament/Dashboard/Resources/CitationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;

class CitationResource extends Resource
Expand Down Expand Up @@ -76,6 +77,8 @@ public static function getPages(): array

public static function getNavigationBadge(): ?string
{
return Cache::get('stats.citations');
return Cache::flexible('stats.citations', [172800, 259200], function () {
return DB::table('citations')->selectRaw('count(*)')->get()[0]->count;
});
}
}
5 changes: 4 additions & 1 deletion app/Filament/Dashboard/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;

Expand Down Expand Up @@ -161,6 +162,8 @@ public static function getWidgets(): array

public static function getNavigationBadge(): ?string
{
return Cache::get('stats.collections');
return Cache::flexible('stats.collections', [172800, 259200], function () {
return DB::table('collections')->selectRaw('count(*)')->get()[0]->count;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
namespace App\Filament\Dashboard\Resources\CollectionResource\Pages;

use App\Filament\Dashboard\Resources\CollectionResource;
use App\Models\Collection;
use Filament\Resources\Pages\CreateRecord;

class CreateCollection extends CreateRecord
{
protected static string $resource = CollectionResource::class;

protected function mutateFormDataBeforeCreate(array $data): array
{
return Collection::mutateFormData($data);
}
}
5 changes: 4 additions & 1 deletion app/Filament/Dashboard/Resources/GeoLocationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;

class GeoLocationResource extends Resource
Expand Down Expand Up @@ -83,6 +84,8 @@ public static function getWidgets(): array

public static function getNavigationBadge(): ?string
{
return Cache::get('stats.geo_locations');
return Cache::flexible('stats.geo_locations', [172800, 259200], function () {
return DB::table('geo_locations')->selectRaw('count(*)')->get()[0]->count;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class GeoLocationStats extends BaseWidget
protected function getStats(): array
{
return [
Stat::make('Total Molecules', Cache::rememberForever('stats.geo_locations'.$this->record->id.'molecules.count', function () {
Stat::make('Total Molecules', Cache::flexible('stats.geo_locations'.$this->record->id.'molecules.count', [172800, 259200], function () {
return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('geo_location_id='.$this->record->id)->get()[0]->count;
})),
Stat::make('Total Organisms', Cache::rememberForever('stats.geo_locations'.$this->record->id.'organisms.count', function () {
Stat::make('Total Organisms', Cache::flexible('stats.geo_locations'.$this->record->id.'organisms.count', [172800, 259200], function () {
return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('geo_location_id='.$this->record->id)->Join('molecule_organism', 'geo_location_molecule.molecule_id', '=', 'molecule_organism.molecule_id')->get()[0]->count;
})),
];
Expand Down
6 changes: 4 additions & 2 deletions app/Filament/Dashboard/Resources/MoleculeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Filament\Dashboard\Resources\MoleculeResource\Widgets\MoleculeStats;
use App\Models\Molecule;
use Archilex\AdvancedTables\Filters\AdvancedFilter;
use DB;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
Expand All @@ -30,6 +29,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\HtmlString;
use pxlrbt\FilamentExcel\Actions\Tables\ExportBulkAction;
Expand Down Expand Up @@ -249,7 +249,9 @@ public static function getWidgets(): array

public static function getNavigationBadge(): ?string
{
return Cache::get('stats.molecules');
return Cache::flexible('stats.molecules', [172800, 259200], function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('active=true and NOT (is_parent=true AND has_variants=true)')->get()[0]->count;
});
}

public static function changeMoleculeStatus($record, $reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class MoleculeStats extends BaseWidget
protected function getStats(): array
{
return [
Stat::make('Total Organisms', Cache::remember('stats.molecules'.$this->record->id.'organisms.count', 172800, function () {
Stat::make('Total Organisms', Cache::flexible('stats.molecules'.$this->record->id.'organisms.count', [172800, 259200], function () {
return DB::table('molecule_organism')->selectRaw('count(*)')->whereRaw('molecule_id='.$this->record->id)->get()[0]->count;
})),
Stat::make('Total Geo Locations', Cache::remember('stats.molecules'.$this->record->id.'geo_locations.count', 172800, function () {
Stat::make('Total Geo Locations', Cache::flexible('stats.molecules'.$this->record->id.'geo_locations.count', [172800, 259200], function () {
return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('molecule_id='.$this->record->id)->get()[0]->count;
})),
];
Expand Down
5 changes: 4 additions & 1 deletion app/Filament/Dashboard/Resources/OrganismResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GuzzleHttp\Client;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Log;
use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;

Expand Down Expand Up @@ -130,7 +131,9 @@ public static function getWidgets(): array

public static function getNavigationBadge(): ?string
{
return Cache::get('stats.organisms');
return Cache::flexible('stats.organisms', [172800, 259200], function () {
return DB::table('organisms')->selectRaw('count(*)')->get()[0]->count;
});
}

protected static function getGNFMatches($name, $organism)
Expand Down
Loading

0 comments on commit e4f8733

Please sign in to comment.