Skip to content

Commit

Permalink
Added Mood-O-Meter for last 30 days
Browse files Browse the repository at this point in the history
closes #28
  • Loading branch information
MrKrisKrisu committed Nov 10, 2020
1 parent b510101 commit a259790
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 13 deletions.
28 changes: 24 additions & 4 deletions app/Http/Controllers/SpotifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ public function renderDailyHistory(Request $request, $date = null)
->where('timestamp_start', '<=', $date->toDateString() . ' 23:59:59');

$history = (clone $dayQuery)->with(['track', 'device'])
->select(['timestamp_start', 'track_id', 'device_id', DB::raw('MAX(created_at) AS played_until')])
->groupBy(['timestamp_start', 'track_id', 'device_id'])
->orderBy('timestamp_start')
->paginate(10);
->select(['timestamp_start', 'track_id', 'device_id', DB::raw('MAX(created_at) AS played_until')])
->groupBy(['timestamp_start', 'track_id', 'device_id'])
->orderBy('timestamp_start')
->paginate(10);

$tracksDistinct = (clone $dayQuery)->select('track_id')->groupBy('track_id')->get()->count();

Expand Down Expand Up @@ -487,4 +487,24 @@ public function renderArtist(int $id): Renderable
]);
}

public function renderMoodMeter(): Renderable
{
$daily = auth()->user()
->spotifyActivity()
->with(['track'])
->where('timestamp_start', '>=', Carbon::parse('-1 month')->startOfDay())
->orderBy('timestamp_start', 'DESC')
->get()
->groupBy(function ($playActivity) {
return $playActivity->timestamp_start->toDateString();
})
->map(function ($playActivities) {
return round($playActivities->avg('track.valence') * 100);
});

return view('spotify.mood-o-meter.main', [
'daily' => $daily
]);
}

}
3 changes: 2 additions & 1 deletion resources/lang/de/spotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
'heared_minutes_by_daytime' => 'Gehörte Minuten je Tageszeit',
'lost_tracks' => 'Verschollene Tracks',
'history' => 'Musikverlauf',
'heared_tracks' => 'Gehörte Lieder am :date'
'heared_tracks' => 'Gehörte Lieder am :date',
'mood_o_meter' => 'Mood-O-Meter'
],
'total' => 'gesamt',
'last_days' => 'letzte :days Tage',
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en/spotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
'heared_minutes_by_daytime' => 'Listened by daytime',
'lost_tracks' => 'Lost tracks',
'history' => 'History',
'heared_tracks' => 'Listened Tracks on :date'
'heared_tracks' => 'Listened Tracks on :date',
'mood_o_meter' => 'Mood-O-Meter'
],
'total' => 'total',
'last_days' => 'last :days days',
Expand Down
22 changes: 15 additions & 7 deletions resources/views/layout/includes/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@
Spotify
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('spotify') }}">{{__('spotify.statistic')}}</a>
<a class="dropdown-item"
href="{{ route('spotify.topTracks') }}">{{__('spotify.title.top_tracks')}}</a>
<a class="dropdown-item"
href="{{ route('spotify.history') }}">{{__('spotify.title.history')}}</a>
<a class="dropdown-item"
href="{{ route('spotify.lostTracks') }}">{{__('spotify.title.lost_tracks')}}</a>
<a class="dropdown-item" href="{{ route('spotify') }}">
{{__('spotify.statistic')}}
</a>
<a class="dropdown-item" href="{{ route('spotify.topTracks') }}">
{{__('spotify.title.top_tracks')}}
</a>
<a class="dropdown-item" href="{{ route('spotify.history') }}">
{{__('spotify.title.history')}}
</a>
<a class="dropdown-item" href="{{ route('spotify.lostTracks') }}">
{{__('spotify.title.lost_tracks')}}
</a>
<a class="dropdown-item" href="{{ route('spotify.mood-o-meter') }}">
{{__('spotify.title.mood_o_meter')}}
</a>
</div>
</li>
<li class="nav-item">
Expand Down
24 changes: 24 additions & 0 deletions resources/views/spotify/mood-o-meter/main.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@extends('layout.app')

@section('title') Mood-O-Meter @endsection

@section('content')
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<i class="far fa-sad-cry fa-3x text-danger float-left"></i>
<i class="far fa-smile fa-3x text-success float-right"></i>
<div class="clearfix"></div>
<hr/>
@for($date = \Carbon\Carbon::today(); $date->diffInDays() < 30; $date->subDay())
@include('spotify.mood-o-meter.mood-bar', [
'date' => $date,
'valence' => $daily[$date->toDateString()] ?? -1
])
@endfor
</div>
</div>
</div>
</div>
@endsection
13 changes: 13 additions & 0 deletions resources/views/spotify/mood-o-meter/mood-bar.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="progress" style="height: 20px; margin-bottom: 10px;">
@if($valence == -1 || $valence == null)
<div class="progress-bar bg-dark"
style="width: 100%" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100">
Keine Daten am {{$date->isoFormat('dddd, DD.MM.YYYY')}}
</div>
@else
<div class="progress-bar @if($valence > 60) bg-success @elseif($valence > 30) bg-info @else bg-danger @endif @if($date->isToday()) progress-bar-animated @endif"
style="width: {{$valence}}%" aria-valuenow="{{$valence}}" aria-valuemin="0" aria-valuemax="100">
{{$date->isoFormat('dddd, DD.MM.YYYY')}}
</div>
@endif
</div>
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

Route::get('/spotify/', 'SpotifyController@index')
->name('spotify');
Route::get('/spotify/mood-o-meter', [SpotifyController::class, 'renderMoodMeter'])
->name('spotify.mood-o-meter');
Route::get('/spotify/track/{id}', 'SpotifyController@trackDetails')
->name('spotify.track');
Route::get('/spotify/artist/{id}', [SpotifyController::class, 'renderArtist'])
Expand Down

0 comments on commit a259790

Please sign in to comment.