Skip to content

Commit

Permalink
Add update lookup and telemetry, Add version and build to app config
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Oct 5, 2024
1 parent 1653918 commit 6a95980
Show file tree
Hide file tree
Showing 28 changed files with 946 additions and 674 deletions.
2 changes: 2 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
APP_NAME=solidtime
APP_VERSION=0.0.0
APP_BUILD=0
VITE_APP_NAME=solidtime
APP_ENV=production
APP_DEBUG=false
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/build-public.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,44 @@ jobs:
- name: "Check out code"
uses: actions/checkout@v4

- name: "Set variable sha_short"
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

- name: "Get Previous tag (normal push)"
id: previoustag
if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
uses: "WyriHaximus/github-action-get-previous-tag@v1"
with:
prefix: "v"

- name: "Get Previous tag (normal push)"
uses: actions-ecosystem/action-get-latest-tag@v1
if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
id: get-latest-tag
with:
semver_only: true

- name: "Get version (normal push)"
if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
run: echo "app_version=${{ steps.get-latest-tag.outputs.tag }}" >> "$GITHUB_OUTPUT"

- name: "Get version (release)"
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
run: echo "app_version=${{ github.ref }}" >> "$GITHUB_OUTPUT"

- name: "Copy .env template for production"
run: cp .env.production .env

- name: "Add version to .env"
run: sed -i 's/APP_VERSION=0.0.0/APP_VERSION=${{ steps.vars.outputs.app_version }}/g' .env

- name: "Add build to .env"
run: sed -i 's/APP_BUILD=0/APP_BUILD=${{ steps.vars.outputs.sha_short }}/g' .env

- name: "Output .env"
run: cat .env

- name: "Install dependencies"
uses: php-actions/composer@v6
if: steps.cache-vendor.outputs.cache-hit != 'true' # Skip if cache hit
Expand Down
45 changes: 45 additions & 0 deletions app/Console/Commands/SelfHost/SelfHostCheckForUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands\SelfHost;

use App\Service\ApiService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class SelfHostCheckForUpdateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'self-host:check-for-update';

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

/**
* Execute the console command.
*/
public function handle(): int
{
$apiService = app(ApiService::class);

$latestVersion = $apiService->checkForUpdate();
if ($latestVersion === null) {
$this->error('Failed to check for update, check the logs for more information.');

return self::FAILURE;
}

Cache::put('latest_version', $latestVersion);

return self::SUCCESS;
}
}
37 changes: 37 additions & 0 deletions app/Console/Commands/SelfHost/SelfHostTelemetryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands\SelfHost;

use App\Service\ApiService;
use Illuminate\Console\Command;

class SelfHostTelemetryCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'self-host:telemetry';

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

/**
* Execute the console command.
*/
public function handle(): int
{
$apiService = app(ApiService::class);

$apiService->telemetry();

return self::SUCCESS;
}
}
8 changes: 8 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ protected function schedule(Schedule $schedule): void
$schedule->command('time-entry:send-still-running-mails')
->when(fn (): bool => config('scheduling.tasks.time_entry_send_still_running_mails'))
->everyTenMinutes();

$schedule->command('self-hosting:check-for-update')
->when(fn (): bool => config('scheduling.tasks.self_hosting_check_for_update'))
->twiceDaily();

$schedule->command('self-hosting:telemetry')
->when(fn (): bool => config('scheduling.tasks.self_hosting_telemetry'))
->twiceDaily();
}

/**
Expand Down
34 changes: 34 additions & 0 deletions app/Filament/Widgets/ServerOverview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use Filament\Widgets\Widget;
use Illuminate\Support\Facades\Cache;

class ServerOverview extends Widget
{
protected static string $view = 'filament.widgets.server-overview';

/**
* @return array<string, mixed>
*/
protected function getViewData(): array
{
// TODO:Remove this:
Cache::put('latest_version', '5.0.0');

$currentVersion = Cache::get('latest_version', null);
if (version_compare($currentVersion, config('app.version')) <= 0) {
$currentVersion = null;
}

return [
'version' => config('app.version'),
'build' => config('app.build'),
'environment' => config('app.env'),
'currentVersion' => $currentVersion,
];
}
}
3 changes: 3 additions & 0 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Providers\Filament;

use App\Filament\Widgets\ActiveUserOverview;
use App\Filament\Widgets\ServerOverview;
use App\Filament\Widgets\TimeEntriesCreated;
use App\Filament\Widgets\TimeEntriesImported;
use App\Filament\Widgets\UserRegistrations;
Expand Down Expand Up @@ -44,11 +45,13 @@ public function panel(Panel $panel): Panel
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
ServerOverview::class,
ActiveUserOverview::class,
UserRegistrations::class,
TimeEntriesCreated::class,
TimeEntriesImported::class,
])
->viteTheme('resources/css/filament/admin/theme.css')
->plugins([
EnvironmentIndicatorPlugin::make()
->color(fn () => match (App::environment()) {
Expand Down
68 changes: 68 additions & 0 deletions app/Service/ApiService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace App\Service;

use App\Models\Audit;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectMember;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Http;
use Log;

class ApiService
{
private const string API_URL = 'https://app.solidtime.io/api';

public function checkForUpdate(): ?string
{
try {
$response = Http::asJson()
->timeout(3)
->connectTimeout(2)
->post(self::API_URL.'/check-for-update', [
'version' => config('app.version'),
'build' => config('app.build'),
'url' => config('app.url'),
]);

return $response->json()['version'] ?? null;
} catch (\Throwable $e) {
Log::error('Failed to check for update', [
'message' => $e->getMessage(),
]);
return null;
}
}

public function telemetry(): void
{
try {
$response = Http::asJson()
->timeout(3)
->connectTimeout(2)
->post(self::API_URL.'/telemetry', [
'version' => config('app.version'),
'build' => config('app.build'),
'url' => config('app.url'),
// telemetry data
'user_count' => User::count(),
'organization_count' => Organization::count(),
'audit_count' => Audit::count(),
'project_count' => Project::count(),
'project_member_count' => ProjectMember::count(),
'client_count' => Client::count(),
'task_count' => Task::count(),
'time_entry_count' => TimeEntry::count(),
]);
} catch (Exception $e) {
// do nothing
}
}
}
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "solidtime-io/solidtime",
"type": "project",
"description": "An open-source time-tracking app",
"version": "0.0.1",
"keywords": [],
"license": "AGPL-3.0-or-later",
"require": {
Expand All @@ -29,7 +28,7 @@
"spatie/temporary-directory": "^2.2",
"stechstudio/filament-impersonate": "^3.8",
"tightenco/ziggy": "^2.1.0",
"tpetry/laravel-postgresql-enhanced": "^1.0.0",
"tpetry/laravel-postgresql-enhanced": "^2.0.0",
"wikimedia/composer-merge-plugin": "^2.1.0"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 6a95980

Please sign in to comment.