Skip to content

Commit

Permalink
api change
Browse files Browse the repository at this point in the history
  • Loading branch information
SaminYaser-work committed Sep 15, 2023
1 parent 5e5dca6 commit e4d3b40
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 41 deletions.
18 changes: 12 additions & 6 deletions app/Filament/Resources/AnimalResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\HtmlString;

class AnimalResource extends Resource
{
Expand Down Expand Up @@ -50,12 +51,17 @@ public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('storage_id'),
Tables\Columns\TextColumn::make('farm_id'),
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('type'),
Tables\Columns\TextColumn::make('breed'),
Tables\Columns\TextColumn::make('gender'),
Tables\Columns\TextColumn::make('name')
->getStateUsing(function (Animal $record) {
return $record->name . ' <br/><span class="text-gray-700 text-xs">' . $record->type . ' (' . $record->breed . ')</span>';
})
->html()
->label('Animal Name'),
Tables\Columns\TextColumn::make('farm.name'),
Tables\Columns\TextColumn::make('storage.name'),
Tables\Columns\TextColumn::make('gender')->getStateUsing(function (Animal $record) {
return ucfirst($record->gender);
}),
Tables\Columns\TextColumn::make('color'),
])
->filters([
Expand Down
7 changes: 7 additions & 0 deletions app/Filament/Resources/AnimalResource/Pages/ListAnimals.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class ListAnimals extends ListRecords
{
protected static string $resource = AnimalResource::class;

protected function getHeaderWidgets(): array
{
return [
AnimalResource\Widgets\CowBreedPieChart::class
];
}

protected function getActions(): array
{
return [
Expand Down
67 changes: 67 additions & 0 deletions app/Filament/Resources/AnimalResource/Widgets/CowBreedPieChart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Filament\Resources\AnimalResource\Widgets;

use App\Models\Animal;
use App\Models\AnimalProduction;
use Illuminate\Contracts\View\View;
use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;

class CowBreedPieChart extends ApexChartWidget
{

protected int | string | array $columnSpan = 2;
protected static string $chartId = 'cowBreedPieChart';
protected static ?string $heading = 'Types of Cow by Breed';
protected static ?int $contentHeight = 300;
protected static ?string $pollingInterval = null;
protected static bool $deferLoading = true;

protected function getLoadingIndicator(): null|string|View
{
return view('loading');
}


protected function getOptions(): array
{

if(!$this->readyToLoad) {
return [];
}

$data = Animal::query()
->groupBy('breed')
->selectRaw('breed, COUNT(*) as quantity')
->get()
->toArray();


return [
'chart' => [
'type' => 'pie',
'height' => 300,
],
'series' => array_column($data, 'quantity'),
'labels' => array_column($data, 'breed'),
'legend' => [
'labels' => [
'colors' => '#9ca3af',
'fontWeight' => 600,
],
],
// 'fill' => [
// 'type' => 'gradient',
// 'gradient' => [
// 'shade' => 'dark',
// 'gradientToColors' => ['dodgerblue', 'blue'],
// 'shadeIntensity' => 1,
// 'type' => 'vertical',
// 'opacityFrom' => 1,
// 'opacityTo' => 1,
// 'stops' => [0, 90, 100]
// ],
// ],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Filament\Forms\Contracts\HasForms;
use Filament\Resources\Pages\Page;
use Illuminate\Support\HtmlString;
use Mockery\Exception;

class DiseaseDetection extends Page implements HasForms
{
Expand Down Expand Up @@ -45,18 +46,25 @@ public function submit(): void
}
foreach ($this->image as $key=>$image) {
$file_path = $image->getRealPath();
$response = \Http::attach(
'file', file_get_contents($file_path), $image->getFilename()
)->post(env('AI_API') . '/dd');
$res = $response->json();
foreach ($res as $r) {
if(!array_key_exists('confidence', $r)){
$this->hasError = true;
return;
}
if ($r['confidence'] > 0) {
$this->res[] = $r;
try {
$response = \Http::attach(
'file', file_get_contents($file_path), $image->getFilename()
)->post('https://agrosmartai.azurewebsites.net/dd');
$res = $response->json();
foreach ($res as $r) {
if(!array_key_exists('confidence', $r)){
\Log::error('Error in Disease Detection API: ' . $res);
$this->hasError = true;
return;
}
if ($r['confidence'] > 0) {
$this->res[] = $r;
}
}
} catch (Exception $e) {
\Log::error('Error in Disease Detection API: ' . $e->getMessage());
$this->hasError = true;
$this->res = [];
}
break;
}
Expand Down
20 changes: 13 additions & 7 deletions app/Filament/Resources/PondResource/Pages/ListPonds.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,19 @@ protected function getActions(): array

private function getFishPrediction(array $values): string
{
$response = \Http::post(env('AI_API') . '/fish', [
"metric" => $values
]);
$res = $response->json();
if (array_key_exists('fish', $res)) {
return $res['fish'] . ' (' . ($res['confidence'] * 100) . '%)';
try {
$response = \Http::post('https://agrosmartai.azurewebsites.net/fish', [
"metric" => $values
]);
$res = $response->json();
if (array_key_exists('fish', $res)) {
return $res['fish'] . ' (' . ($res['confidence'] * 100) . '%)';
}
\Log::error('Error in AI response: ' . $res);
return 'Error';
} catch (\Exception $e) {
\Log::error('Error in AI response: ' . $e->getMessage());
return 'Error';
}
return 'Error';
}
}
14 changes: 3 additions & 11 deletions app/Models/Animal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,14 @@ class Animal extends Model
{
use HasFactory;

protected $fillable = [
'name',
'type',
'breed',
'color',
'gender',
'storage_id',
'farm_id',
];
protected $guarded = [];

public function storages(): BelongsTo
public function storage(): BelongsTo
{
return $this->belongsTo(Storage::class);
}

public function farms(): BelongsTo
public function farm(): BelongsTo
{
return $this->belongsTo(Farm::class);
}
Expand Down
23 changes: 17 additions & 6 deletions startup.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#!/bin/sh

cp /home/site/wwwroot/nginx/default /etc/nginx/sites-enabled/ && service nginx restart
cd /home/site/wwwroot && cp .env.azure .env && php artisan key:generate && php artisan storage:link && php artisan config:cache && php artisan event:cache && php artisan route:cache && php artisan view:cache

# File upload fix
sed -i 's|abort_unless|//&|g' /home/site/wwwroot/vendor/livewire/livewire/src/Controllers/FileUploadHandler.php

sed -i '/tmpfile/a $tmpfname = tempnam(sys_get_temp_dir(), "");\n$tmpFile = fopen($tmpfname, "w");' /home/site/wwwroot/vendor/livewire/livewire/src/TemporaryUploadedFile.php
sed -i 's/$tmpFile/\/\/&/' /home/site/wwwroot/vendor/livewire/livewire/src/TemporaryUploadedFile.php

mkdir /home/site/wwwroot/storage/app/public/livewire-tmp

chmod -R 755 /home/site/wwwroot/storage

# Nginx config
cp /home/site/wwwroot/nginx/default /etc/nginx/sites-enabled/ && service nginx restart

# Laravel config
cd /home/site/wwwroot || echo "Unable to change directory" && exit 1
cp .env.azure .env
#php artisan key:generate
php artisan storage:link
php artisan config:cache
php artisan event:cache
php artisan route:cache
php artisan view:cache



0 comments on commit e4d3b40

Please sign in to comment.