Skip to content

Commit

Permalink
Merge pull request #121 from glowspace/develop
Browse files Browse the repository at this point in the history
Backup, count aggregation, hymnology field
  • Loading branch information
thes01 authored Nov 26, 2023
2 parents a412773 + 228a947 commit 3b07e1e
Show file tree
Hide file tree
Showing 18 changed files with 719 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy_staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
port: ${{ secrets.PORT }}
username: ${{ secrets.USERNAME }}
privateKey: ${{ secrets.PRIVATE_KEY}}
passphrase: ${{ secrets.PRIVATE_KEY_PASSPHRASE}}
- name: Discord notification
uses: Ilshidur/action-discord@master
env:
Expand Down
33 changes: 25 additions & 8 deletions app/Console/Commands/ComputeAggregateVisits.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DB;
use Carbon\Carbon;
use App\User;
use App\Visit;
use App\VisitAggregate;

class ComputeAggregateVisits extends Command
Expand Down Expand Up @@ -42,15 +43,16 @@ public function __construct()
public function handle()
{
$this->info('Counting the visits count data');
$result = $this->countSongLyricVisits(Carbon::now()->subWeeks(1));
$result = $this->countSongLyricVisits(Carbon::now()->subWeeks(1), Carbon::now()->subMonths(4));

$this->info('Updating the visits_aggregates table');
$result->map(function ($row) {
return [
'visitable_id' => $row->id,
'visitable_type' => 'App\SongLyric',
'count_week' => $row->count_from,
'count_total' => $row->count_total
'count_after_prune' => $row->count_after_prune,
'count_week' => $row->count_week,
'count_pruned' => $row->count_pruned + $row->count_pruned_old
];
})
->chunk(1000)
Expand All @@ -61,25 +63,40 @@ public function handle()
return 0;
}

private function countSongLyricVisits(Carbon $from)
private function countSongLyricVisits(Carbon $from, Carbon $prune_until)
{
$res = DB::select(DB::raw(
"SELECT
id,
(select count(*) from visits
where song_lyrics.id = visits.visitable_id and visits.visitable_type = \"App\\\SongLyric\")
as count_total,
where song_lyrics.id = visits.visitable_id and visits.visitable_type = \"App\\\SongLyric\"
and created_at > :prune_until_i)
as count_after_prune,
(select count(*) from visits
where song_lyrics.id = visits.visitable_id and visits.visitable_type = \"App\\\SongLyric\"
and created_at > :from)
as count_from
as count_week,
(select count(*) from visits
where song_lyrics.id = visits.visitable_id and visits.visitable_type = \"App\\\SongLyric\"
and created_at <= :prune_until)
as count_pruned,
-- we don't want to ovewrite the data in visit_aggregates.count_pruned but instead to add them up
-- that's why we retrieve them here to add them wit count_pruned
(select count_pruned from visit_aggregates
where song_lyrics.id = visit_aggregates.visitable_id and visit_aggregates.visitable_type = \"App\\\SongLyric\")
as count_pruned_old
from song_lyrics where song_lyrics.deleted_at is null
"
), [
// 'visit_type' => $visit_type,
'from' => $from->format('Y-m-d H:i:s')
'prune_until_i' => $prune_until->format('Y-m-d H:i:s'),
'from' => $from->format('Y-m-d H:i:s'),
'prune_until' => $prune_until->format('Y-m-d H:i:s'),
]);

Visit::where('created_at', '<', $prune_until->format('Y-m-d H:i:s'))->delete();

return collect($res);
}
}
29 changes: 22 additions & 7 deletions app/GraphQL/Mutations/UpdateSongLyric.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,33 @@ public function __invoke($rootValue, array $args, GraphQLContext $context, Resol
$song_lyric = SongLyric::find($input["id"]);
// $song_lyric_old = $song_lyric->replicate();

$this->sl_lily_service->handleLilypondOnUpdate($song_lyric, $input["lilypond"], $input["lilypond_key_major"], $input["lilypond_parts_sheet_music"]);
// if has key
if (isset($input["lilypond"])) {
$this->sl_lily_service->handleLilypondOnUpdate($song_lyric, $input["lilypond"], $input["lilypond_key_major"], $input["lilypond_parts_sheet_music"]);
}

$song_lyric->update($input);
// todo if has key
$this->sl_service->handleLyrics($song_lyric, $input["lyrics"]);
$this->sl_service->handleArrangementSourceUpdate($song_lyric, $input["arrangement_source"]);
$this->sl_service->handleSongGroup($song_lyric, $input["song"]);
if (isset($input["lyrics"])) {
$this->sl_service->handleLyrics($song_lyric, $input["lyrics"]);
}
if (isset($input["arrangement_source"])) {
$this->sl_service->handleArrangementSourceUpdate($song_lyric, $input["arrangement_source"]);
}
if (isset($input["song"])) {
$this->sl_service->handleSongGroup($song_lyric, $input["song"]);
}
$this->sl_service->handleHasChords($song_lyric);
$this->sl_service->handleAuthors($song_lyric, $input["authors"]);
$this->sl_service->handleSongbookRecords($song_lyric, $input["songbook_records"]);
if (isset($input["authors"])) {
$this->sl_service->handleAuthors($song_lyric, $input["authors"]);
}
if (isset($input["songbook_records"])) {
$this->sl_service->handleSongbookRecords($song_lyric, $input["songbook_records"]);
}
$this->sl_service->handleRevisionAssociacionsStats($song_lyric);
$this->sl_service->handleBibleReferences($song_lyric, $input['bible_refs_osis']);
if (isset($input["bible_refs_osis"])) {
$this->sl_service->handleBibleReferences($song_lyric, $input['bible_refs_osis']);
}

$song_lyric->save();

Expand Down
26 changes: 26 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;

class AppServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -51,6 +52,31 @@ public function boot()
// });

Validator::extend('recaptcha', '\App\Validators\ReCaptcha@validate');

try {
Storage::extend('google', function ($app, $config) {
logger($config['folder']);

$options = [];

if (!empty($config['teamDriveId'] ?? null)) {
$options['teamDriveId'] = $config['teamDriveId'];
}

$client = new \Google\Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);

$service = new \Google\Service\Drive($client);
$adapter = new \Masbug\Flysystem\GoogleDriveAdapter($service, $config['folder'] ?? '/', $options);
$driver = new \League\Flysystem\Filesystem($adapter);

return new \Illuminate\Filesystem\FilesystemAdapter($driver, $adapter);
});
} catch(\Exception $e) {
// your exception handling logic
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Services/SongLyricModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function createSongLyric(string $name): SongLyric

$song_lyric->visit_aggregate()->create([
'count_week' => 0,
'count_total' => 0
'count_after_prune' => 0,
'count_pruned' => 0
]);

return $song_lyric;
Expand Down
3 changes: 2 additions & 1 deletion app/SongLyric.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class SongLyric extends Model
'is_sealed',
'revision_n_tags',
'revision_n_authors',
'revision_n_songbook_records'
'revision_n_songbook_records',
'hymnology'
];

private static $lang_string_values = [
Expand Down
6 changes: 5 additions & 1 deletion app/VisitAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class VisitAggregate extends Model
{
protected $fillable = ['visitable_type', 'visitable_id', 'count_week', 'count_total'];
protected $fillable = ['visitable_type', 'visitable_id', 'count_week', 'count_after_prune', 'count_pruned'];

public $timestamps = false;

Expand All @@ -21,4 +21,8 @@ public function song_lyric(): MorphTo
{
return $this->morphTo(SongLyric::class, 'visitable');
}

public function getCountTotalAttribute(): int {
return $this->count_after_prune + $this->count_pruned;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"laravel/scout": "^9.4",
"laravel/tinker": "^2.0",
"laravel/ui": "^3.0",
"masbug/flysystem-google-drive-ext": "^2.2",
"mll-lab/graphql-php-scalars": "^5.4",
"mll-lab/laravel-graphql-playground": "^2.6",
"nuwave/lighthouse": "^5.55",
Expand Down
Loading

0 comments on commit 3b07e1e

Please sign in to comment.