(typesense) Main dashboard

Lot of duplicate to have a simple improvment
This commit is contained in:
2023-09-21 01:57:26 +02:00
parent 1ff074aac3
commit b789c925c4
14 changed files with 1534 additions and 167 deletions

View File

@@ -1,11 +1,13 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ElsService } from '../els-service/els.service';
import { Song } from './../model/song';
import { Album } from './../model/album';
import { SongTableComponent } from '../song-table/song-table.component';
import { TsService } from '../ts-service/ts.service';
import { TsAlbumService } from '../ts-service/ts-album.service';
@Component({
selector: 'app-album',
templateUrl: './album.component.html',
@@ -25,7 +27,7 @@ export class AlbumComponent implements OnInit {
lockLoadData = false;
constructor(
private elsService: ElsService,
private tsAlbumService: TsAlbumService,
private route: ActivatedRoute
) { }
@@ -35,7 +37,7 @@ export class AlbumComponent implements OnInit {
this.loadSongs();
this.elsService.getAlbum(this.albumName).subscribe(data => this.album = data);
this.tsAlbumService.getAlbum(this.albumName).subscribe(data => this.album = data);
}
loadSongs(): void {
@@ -49,9 +51,9 @@ export class AlbumComponent implements OnInit {
}
this.lockLoadData = true;
this.elsService.getAlbumSongs(this.albumName, this.songs.length, this.sortFilter).subscribe(
this.tsAlbumService.getAlbumSongs(this.albumName, this.songs.length, this.sortFilter).subscribe(
data => {
this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE;
this.moreDataAvailable = data.length === TsService.DEFAULT_SIZE;
// Erase song array with result for first load, then add elements one by one
// instead use concat => concat will sort table at each load, very consuming! and not user friendly

View File

@@ -1,27 +1,28 @@
import { Component, OnInit } from '@angular/core';
import { ElsAlbumService } from '../els-service/els-album.service';
import { Component, OnInit } from "@angular/core";
import { Album } from '../model/album';
import { Album } from "../model/album";
import { Utils } from '../utils';
import { Utils } from "../utils";
import { TsAlbumService } from "../ts-service/ts-album.service";
enum query_edit_type {
exclude = 'must_not',
select = 'must'
exclude = "must_not",
select = "must",
}
@Component({
selector: 'app-albums',
templateUrl: './albums.component.html',
styleUrls: ['./albums.component.css']
selector: "app-albums",
templateUrl: "./albums.component.html",
styleUrls: ["./albums.component.css"],
})
export class AlbumsComponent implements OnInit {
numberToArray = Utils.numberToArray;
numberToArray = Utils.numberToArray; // For star representation
albums: Album[] = [];
filterQuery = Object.assign({}, ElsAlbumService.GET_ALBUMS_DEFAULT_QUERY);
queryEdited = false;
filterParams = TsAlbumService.GET_ALBUMS_DEFAULT_PARAMS();
queryEdited = false; // Show reset button if true
constructor(private elsService : ElsAlbumService) { }
constructor(private tsService: TsAlbumService) {}
ngOnInit(): void {
this.loadData();
@@ -30,42 +31,47 @@ export class AlbumsComponent implements OnInit {
private editQuery(field: string, value: Album, type: query_edit_type): void {
// TODO Move this method to a service
if (value[field] instanceof Array) {
value[field] = value[field][0]
value[field] = value[field][0];
}
// If firt edit, add needed fields in ELS Query
if (!this.filterQuery['query']) {
this.filterQuery['query']['bool'][type].push({ 'must': [] })
this.filterQuery['query']['bool'][type].push({ 'must_not': [] })
if (type == query_edit_type.exclude) {
// Filter can be cumulated
// TODO Specific treatment for array? https://typesense.org/docs/0.25.1/api/search.html#filter-parameters
this.filterParams = this.filterParams.append(
"filter_by",
field + ":!=`" + value[field] + "`"
);
}
this.filterQuery['query']['bool'][type].push({
'match_phrase': {
[field]: value[field]
if (type == query_edit_type.select) {
this.filterParams = this.filterParams
.delete("q")
.append("q", value[field])
.delete("query_by")
.append("query_by", field);
}
})
this.queryEdited = true;
}
exlude(field: string, value: Album): void {
this.editQuery(field, value, query_edit_type.exclude)
this.loadData()
this.editQuery(field, value, query_edit_type.exclude);
this.loadData();
}
select(field: string, value: Album): void {
this.editQuery(field, value, query_edit_type.select)
this.loadData()
this.editQuery(field, value, query_edit_type.select);
this.loadData();
}
resetQuery(): void {
this.filterQuery = Object.assign({}, ElsAlbumService.GET_ALBUMS_DEFAULT_QUERY);
this.filterParams = TsAlbumService.GET_ALBUMS_DEFAULT_PARAMS();
this.loadData();
}
loadData(): void {
// console.log(JSON.stringify(this.filterQuery))
this.elsService.getAlbums(this.filterQuery).subscribe(data => this.albums = data);
console.log(this.filterParams.toString());
this.tsService
.getAlbums(this.filterParams)
.subscribe((data) => (this.albums = data));
}
}

View File

@@ -11,8 +11,9 @@ import { GenreComponent } from './genre/genre.component';
import { SongTableComponent } from './song-table/song-table.component';
import { TopPlayedComponent } from './top-played/top-played.component';
import { ElsService } from './els-service/els.service';
import { ElsAlbumService } from './els-service/els-album.service';
import { TsService } from './ts-service/ts.service';
import { TsAlbumService } from './ts-service/ts-album.service';
import { TsArtistService } from './ts-service/ts-artist.service';
import { AppRoutingModule } from './app-routing.module';
@@ -25,7 +26,6 @@ import { AlbumsComponent } from './albums/albums.component';
import { ToSortComponent } from './to-sort/to-sort.component';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import { ElsArtistService } from './els-service/els-artist.service';
@NgModule({
imports: [
@@ -52,9 +52,9 @@ import { ElsArtistService } from './els-service/els-artist.service';
ToSortComponent
],
providers: [
ElsService,
ElsAlbumService,
ElsArtistService
TsService,
TsAlbumService,
TsArtistService
],
bootstrap: [ AppComponent ]
})

View File

@@ -4,8 +4,9 @@ import { ActivatedRoute, Params } from '@angular/router';
import { Song } from './../model/song';
import { Artist } from './../model/artist';
import { SongTableComponent } from '../song-table/song-table.component';
import { ElsArtistService } from '../els-service/els-artist.service';
import { ElsService } from '../els-service/els.service';
import { TsService } from '../ts-service/ts.service';
import { TsArtistService } from '../ts-service/ts-artist.service';
@Component({
selector: 'app-artist',
@@ -28,7 +29,7 @@ export class ArtistComponent implements OnInit {
toSortFilter: boolean = false; // Show only song to sort
constructor(
private elsService: ElsArtistService,
private elsService: TsArtistService,
private route: ActivatedRoute,
) { }
@@ -63,7 +64,7 @@ export class ArtistComponent implements OnInit {
this.lockLoadData = true;
this.elsService.getArtistSongs(this.artistName, this.songs.length, this.toSortFilter).subscribe(
data => {
this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE;
this.moreDataAvailable = data.length === TsService.DEFAULT_SIZE;
console.log(data.length)
console.log(this.moreDataAvailable)

View File

@@ -112,9 +112,9 @@
</thead>
<tbody>
<tr *ngFor="let album of lastAddedAlbums">
<td><a [routerLink]="['/album', album.key]">{{album.key}}</a></td>
<td>{{album.doc_count}}</td>
<td><a [routerLink]="['/artist', albumArtists[album.key]]">{{albumArtists[album.key]}}</a></td>
<td><a [routerLink]="['/album/', album.group_key[0]]">{{album.group_key[0]}}</a></td>
<td>{{album.found}}</td>
<td><a [routerLink]="['/artist', albumArtists[album.group_key[0]]]">{{albumArtists[album.group_key[0]]}}</a></td>
</tr>
</tbody>
</table>
@@ -156,8 +156,8 @@
</thead>
<tbody>
<tr *ngFor="let genre of topGenres">
<td><a [routerLink]="['/genre', genre.key]">{{genre.key}}</a></td>
<td>{{genre.doc_count}}</td>
<td><a [routerLink]="['/genre', genre.value]">{{genre.value}}</a></td>
<td>{{genre.count}}</td>
</tr>
</tbody>
</table>
@@ -173,8 +173,8 @@
</thead>
<tbody>
<tr *ngFor="let genre of bottomGenres">
<td><a [routerLink]="['/genre', genre.key]">{{genre.key}}</a></td>
<td>{{genre.doc_count}}</td>
<td><a [routerLink]="['/genre', genre.value]">{{genre.value}}</a></td>
<td>{{genre.count}}</td>
</tr>
</tbody>
</table>

View File

@@ -8,6 +8,8 @@ import { Suggested } from '../model/suggested';
import {Observable, of, OperatorFunction} from 'rxjs';
import {catchError, debounceTime, distinctUntilChanged, map, tap, switchMap} from 'rxjs/operators';
import { TsService } from '../ts-service/ts.service';
import { TsBucket } from '../model/tsBucket';
@Component({
selector: 'app-dashboard',
@@ -24,48 +26,42 @@ export class DashboardComponent implements OnInit {
albumArtistCount = 0;
topGenres: Bucket[] = [];
bottomGenres: Bucket[] = [];
topGenres: TsBucket[] = [];
bottomGenres: TsBucket[] = [];
mostPlayedSongs: Song[] = [];
lastAddedAlbums: Bucket[] = [];
lastAddedAlbums: TsBucket[] = [];
albumArtists = [];
searchTerm = ''
suggested : Suggested[] = []
constructor(private elsService: ElsService, private route: Router) { }
constructor(private tsService: TsService, private route: Router) { }
ngOnInit(): void {
this.elsService.getTime().then(result => {
this.tsService.getTime().then(result => {
this.totalTime = result;
});
this.elsService.getSize().then(result => this.totalSize = result);
this.tsService.getSize().then(result => this.totalSize = result);
this.elsService.getCountSong(ElsService.SONG_INDEX_NAME)
this.tsService.getCountSong(ElsService.SONG_INDEX_NAME)
.then(result => this.trackCountSong = result);
// TODO: Unused information
// this.elsService.getCountSong(ElsService.ARTIST_INDEX_NAME)
// .then(result => this.trackCountArtist = result);
// this.elsService.getCountSong(ElsService.ALBUM_INDEX_NAME)
// .then(result => this.trackCountAlbum = result);
this.elsService.getCountNeverListenSong()
this.tsService.getCountNeverListenSong()
.then(result => this.neverListenSong = result);
this.elsService.getMostPlayedTrack().subscribe(
this.tsService.getMostPlayedTrack().subscribe(
data => this.mostPlayedSongs = data
);
this.elsService.getGenres().subscribe(data => this.topGenres = data);
this.elsService.getGenres('asc').subscribe(data => this.bottomGenres = data);
// this.elsService.getGenreCount().subscribe(data => console.log(data));
this.tsService.getGenres().subscribe(data => this.topGenres = data);
this.tsService.getGenres('asc').subscribe(data => this.bottomGenres = data);
this.tsService.getGenreCount().subscribe(data => console.log(data));
const lastAddedAlbumsTemp: Bucket[] = [];
const BreakException = {};
this.elsService.getLastAddedAlbums(6).subscribe(buckets => {
const lastAddedAlbumsTemp: TsBucket[] = [];
this.tsService.getLastAddedAlbums(6).subscribe(buckets => {
buckets.forEach(bucket => {
// console.log(bucket);
@@ -74,8 +70,8 @@ export class DashboardComponent implements OnInit {
} else {
let found = false;
lastAddedAlbumsTemp.forEach(element => {
if (element.key === bucket.key) {
element.doc_count += bucket.doc_count;
if (element.group_key === bucket.group_key) {
element.found += bucket.found;
found = true;
}
});
@@ -84,24 +80,22 @@ export class DashboardComponent implements OnInit {
}
}
});
// console.log("alors");
// console.log(lastAddedAlbumsTemp);
this.lastAddedAlbums = lastAddedAlbumsTemp;
this.lastAddedAlbums.forEach(bucket => this.getArtistName(bucket));
});
}
private getArtistName(albumBucket: Bucket) {
private getArtistName(albumBucket: TsBucket) {
// For each bucket.key (album name), search artist.
// Use track count to compare
this.elsService.getArtistFromAlbumName(albumBucket.key).subscribe(albums => {
this.tsService.getArtistFromAlbumName(albumBucket.group_key[0]).subscribe(albums => {
// Identification of the good album
let goodAlbum;
if (albums.length > 1) {
// More than one result for an album name: search good by track count
albums.forEach(album => {
if (album['Track Count'] === albumBucket.doc_count) {
if (album['Track Count'] === albumBucket.found) {
goodAlbum = album;
}
});
@@ -132,7 +126,7 @@ export class DashboardComponent implements OnInit {
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this.elsService.getSuggest(term).pipe(
this.tsService.getSuggest(term).pipe(
tap(() => this.searchFailed = false),
catchError(() => {
this.searchFailed = true;

View File

@@ -1,7 +1,7 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ElsService } from '../els-service/els.service';
import { TsService } from '../ts-service/ts.service';
import { SongTableComponent } from '../song-table/song-table.component';
import { Song } from '../model/song';
@@ -17,7 +17,7 @@ export class GenreComponent implements OnInit {
songs: Array<Song> = [];
constructor(
private elsService: ElsService,
private tsService: TsService,
private route: ActivatedRoute
) { }
@@ -29,7 +29,7 @@ export class GenreComponent implements OnInit {
}
loadSongs(): any {
this.elsService.getGenreSongs(this.genreName, this.songs.length).subscribe(
this.tsService.getGenreSongs(this.genreName, this.songs.length).subscribe(
data => {
// this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE;

View File

@@ -0,0 +1,6 @@
export class TsBucket {
value: string;
count: number;
group_key: string[];
found: number;
}

View File

@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { ElsService } from '../els-service/els.service';
import { TsService } from '../ts-service/ts.service';
import { Album } from '../model/album';
import { Artist } from '../model/artist';
@@ -15,10 +15,10 @@ export class TopPlayedComponent implements OnInit {
mostPlayedArtistsNaive: Artist[] = [];
mostPlayedArtists: Artist[] = [];
constructor(private elsService: ElsService) { }
constructor(private tsService: TsService) { }
ngOnInit() {
this.elsService.getMostPlayedAlbumNaive()
this.tsService.getMostPlayedAlbumNaive()
.then(result => {
result.forEach(album => {
if (album.Artist.length <= 10) {
@@ -29,29 +29,29 @@ export class TopPlayedComponent implements OnInit {
this.mostPlayedAlbumsNaive.sort((a: any, b: any) => this.sortByAveragePlay(a, b)).splice(10);
});
this.elsService.getMostPlayedAlbum().subscribe(result => {
this.mostPlayedAlbums = result;
// this.elsService.getMostPlayedAlbum().subscribe(result => {
// this.mostPlayedAlbums = result;
this.mostPlayedAlbums.sort((a: any, b: any) => this.sortByAveragePlay(a, b)).splice(10);
// TODO Load more! (Use a )
});
// this.mostPlayedAlbums.sort((a: any, b: any) => this.sortByAveragePlay(a, b)).splice(10);
// // TODO Load more! (Use a )
// });
this.elsService.getMostPlayedArtistNaive()
this.tsService.getMostPlayedArtistNaive()
.then(result => {
this.mostPlayedArtistsNaive = result;
this.mostPlayedArtistsNaive.sort((a: any, b: any) => this.sortByAveragePlay(a, b)).splice(10);
});
this.elsService.getMostPlayedArtist().subscribe(result => {
result.forEach(artist => {
if (artist['Track Count'] > 10) {
this.mostPlayedArtists.push(artist);
}
});
// this.tsService.getMostPlayedArtist().subscribe(result => {
// result.forEach(artist => {
// if (artist['Track Count'] > 10) {
// this.mostPlayedArtists.push(artist);
// }
// });
this.mostPlayedArtists.sort((a: any, b: any) => this.sortByAveragePlay(a, b)).splice(10);
});
// this.mostPlayedArtists.sort((a: any, b: any) => this.sortByAveragePlay(a, b)).splice(10);
// });
}
sortByAveragePlay(a: any, b: any) {

View File

@@ -0,0 +1,101 @@
import { HttpClient, HttpParams } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { catchError, map } from "rxjs/operators";
import { TsService } from "./ts.service";
import { Album } from "../model/album";
import { Song } from "../model/song";
@Injectable()
export class TsAlbumService extends TsService {
constructor(protected http: HttpClient) {
super(http);
}
getAlbum(albumName: string): Observable<Album> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", albumName);
queryParams = queryParams.append("query_by", "Name");
console.log("coucou");
return this.http
.get(
this.tsUrl +
TsService.ALBUM_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToOneTypedResult<Album>(res, albumName)),
catchError((error) =>
this.handleError(error, "getAlbum(" + albumName + ")")
)
);
}
getAlbumSongs(
albumName: string,
from: number = 0,
toSortFilter = false
): Observable<Song[]> {
// TODO Move in els-album service
console.info(
"getAlbumSongs- Album name: " + albumName + " - from: " + from
);
let queryParams = new HttpParams();
queryParams = queryParams.append("q", albumName);
queryParams = queryParams.append("query_by", "Album");
queryParams = queryParams.append("per_page", TsService.DEFAULT_SIZE);
queryParams = queryParams.append("offset", from);
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToSongs(res)),
catchError((error) =>
this.handleError(
error,
"getAlbumSongs(" + albumName + "," + from + ")"
)
)
);
}
public static GET_ALBUMS_DEFAULT_PARAMS(): HttpParams {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append(
"sort_by",
"Play Count:desc,Avg Bit Rate:desc"
);
queryParams = queryParams.append("filter_by", "Min Bit Rate:<128");
queryParams = queryParams.append("per_page", "100");
return queryParams;
}
getAlbums(queryParams: HttpParams): Observable<Album[]> {
return this.http
.get<any>(
this.tsUrl +
TsService.ALBUM_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToAlbums(res)),
catchError((error) => this.handleError(error, "getAlbums"))
);
}
}

View File

@@ -0,0 +1,140 @@
import { HttpClient, HttpParams } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { catchError, map } from "rxjs/operators";
import { TsService } from "./ts.service";
import { Artist } from "../model/artist";
import { Song } from "../model/song";
@Injectable()
export class TsArtistService extends TsService {
constructor(protected http: HttpClient) {
super(http);
}
private getQuerySongsWithArtistName(
artistName: string,
sortFilter: boolean = false,
size: number = 0,
from: number = 0
) {
let query = {
query: {
bool: {
should: [
{ match_phrase: { "Album Artist": artistName } },
{ match_phrase: { Artist: artistName } },
],
must_not: [],
},
},
};
if (sortFilter) {
console.log("ElsArtistService- TO SORT filter enabled");
query = this.addSortFilterToQuery(query);
}
if (size) {
query["size"] = size;
}
if (from) {
query["from"] = from;
}
return query;
}
public getArtist(artistName: string): Observable<Artist> {
console.log("getArtist");
let queryParams = new HttpParams();
queryParams = queryParams.append("q", artistName);
queryParams = queryParams.append("query_by", "Name");
return this.http
.get<any>(
this.tsUrl +
TsService.ARTIST_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToOneTypedResult<Artist>(res, artistName)),
catchError((error) =>
this.handleError(error, "getArtist(" + artistName + ")")
)
);
}
public getArtistSongs(
artistName: string,
from: number = 0,
sortFilter = false
): Observable<Song[]> {
console.info(
"getArtistSongs- Artist name: " + artistName + " - from: " + from
);
let query = this.getQuerySongsWithArtistName(
artistName,
sortFilter,
TsService.DEFAULT_SIZE,
from
);
let queryParams = new HttpParams();
queryParams = queryParams.append("q", artistName);
queryParams = queryParams.append("query_by", "Artist,Album Artist");
queryParams = queryParams.append("per_page", TsService.DEFAULT_SIZE);
queryParams = queryParams.append("offset", from);
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToSongs(res)),
catchError((error) =>
this.handleError(
error,
"getArtistSongs(" + artistName + "," + from + ")"
)
)
);
}
public getCountArtistSong(
artistName: string,
sortFilter = false
): Observable<number> {
console.log("artistname: " + artistName);
const query = this.getQuerySongsWithArtistName(artistName, sortFilter);
let queryParams = new HttpParams();
queryParams = queryParams.append("q", artistName);
queryParams = queryParams.append("query_by", "Artist"); // QUESTION ArtistName?
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => res.found as number),
catchError((error) =>
this.handleError(error, "getCountArtistSong" + artistName + ")")
)
);
}
}

View File

@@ -0,0 +1,169 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { TsService } from './ts.service';
import { Album } from '../model/album';
@Injectable({
providedIn: 'root'
})
export class TsSortService extends TsService {
constructor(protected http: HttpClient) {
super(http);
}
getTime(): Promise<number> {
return this.http
.post<any>(this.tsUrl + TsService.SONG_INDEX_NAME + TsService.ACTION_SEARCH,
JSON.stringify({
query: {
bool: {
must_not: [
{
term: {
"Location.tree": "/F:/Musique"
}
}
]
}
},
aggs: {
sum_time: {
sum: { field: 'Total Time'}
}
},
'size': 0
}), {headers: this.headers})
.toPromise()
.then(res => res.aggregations.sum_time.value as number)
.catch(error => this.handleError(error, 'getTime()'));
}
getSize(): Promise<number> {
return this.http
.post<any>(this.tsUrl + TsService.SONG_INDEX_NAME + TsService.ACTION_SEARCH,
JSON.stringify({
query: {
bool: {
must_not: [
{
term: {
"Location.tree": "/F:/Musique"
}
}
]
}
},
aggs: {
sum_time: {
sum: { field: 'Size' }
}
},
'size': 0
}), {headers: this.headers})
.toPromise()
.then(res => res.aggregations.sum_time.value as number)
.catch(error => this.handleError(error, 'getSize()'));
}
getCountSong(): Promise<number> {
return this.http
.post<any>(this.tsUrl + TsService.SONG_INDEX_NAME + TsService.ACTION_COUNT,
JSON.stringify({
query: {
bool: {
must_not: [
{
term: {
"Location.tree": "/F:/Musique"
}
}
]
}
}
}), {headers: this.headers})
.toPromise()
.then(res => res.count as number)
.catch(error => this.handleError(error, 'getCountSong()'));
}
getCountNeverListenSong(): Promise<number> {
return this.http
.post<any>(this.tsUrl + TsService.SONG_INDEX_NAME + TsService.ACTION_COUNT,
JSON.stringify({
'query': {
'bool': {
'must_not': [
{
'exists': { 'field': 'Play Count'}
}, {
term: {
"Location.tree": "/F:/Musique"
} }
]
}
}
}), {headers: this.headers})
.toPromise()
.then(res => res.count as number)
.catch(error => this.handleError(error, 'getCountNeverListenSong()'));
}
getNbAlbums(): Promise<number> {
return this.http
.post<any>(this.tsUrl + TsService.ALBUM_INDEX_NAME + TsService.ACTION_SEARCH,
JSON.stringify({
'query': {
'bool': {
'must_not': [
{
term: {
"Location.tree": "/F:/Musique"
}
}
]
}
},
size: 0,
"aggs": {
"album_count": {
"cardinality": {
"field": "Album.raw"
}
}
}
}), {headers: this.headers})
.toPromise()
.then(res => res.aggregations.album_count.value as number )
.catch(error => this.handleError(error, 'getNbAlbums()'));
}
getAlbums(): Observable<Album[]> {
return this.http
.post(this.tsUrl + TsService.ALBUM_INDEX_NAME + TsService.ACTION_SEARCH,
JSON.stringify({
query: {
bool: {
must_not: [
{
term: {
"Location.tree": "/F:/Musique"
}
}
]
}
},
'size': 550,
"sort": [
{ "Play Count": "desc"}
]
}), {headers: this.headers})
.pipe(
map(res => this.responseToAlbums(res)),
catchError(error => this.handleError(error, 'getAlbums'))
);
}
}

View File

@@ -0,0 +1,458 @@
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { catchError, map } from "rxjs/operators";
import { Album } from "../model/album";
import { Artist } from "../model/artist";
import { Song } from "../model/song";
import { Suggested } from "../model/suggested";
import { TsBucket } from "../model/tsBucket";
@Injectable()
export class TsService {
public static readonly DEFAULT_SIZE: number = 20;
public static readonly SONG_INDEX_NAME = "/songs";
public static readonly ARTIST_INDEX_NAME = "/artists";
public static readonly ALBUM_INDEX_NAME = "/albums";
public static readonly SUGGEST_INDEX_NAME = "/suggest";
protected static readonly ACTION_SEARCH = "/search";
protected static readonly ACTION_COUNT = "/_count";
protected tsUrl = "http://localhost:8108/collections";
protected headers = new HttpHeaders({
"Content-Type": "application/json",
"X-TYPESENSE-API-KEY": "toto",
});
protected defaultLocation = "/F:/Musique"; // TODO Use conf
constructor(protected http: HttpClient) {}
getTime(): Promise<number> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append("limit", "0");
queryParams = queryParams.append(
"facet_by",
"Total Time(Range:[0,99999999999999999])"
);
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.toPromise()
.then((res) => res.facet_counts[0].stats.sum as number)
.catch((error) => this.handleError(error, "getTime()"));
}
getSize(): Promise<number> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append("limit", "0");
queryParams = queryParams.append(
"facet_by",
"Size(Range:[0,99999999999999999])"
);
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.toPromise()
.then((res) => res.facet_counts[0].stats.sum as number)
.catch((error) => this.handleError(error, "getSize()"));
}
getCountSong(index: string): Promise<number> {
return this.http
.get<any>(this.tsUrl + TsService.SONG_INDEX_NAME, {
headers: this.headers,
})
.toPromise()
.then((res) => res.num_documents as number)
.catch((error) => this.handleError(error, "getCountSong(" + index + ")"));
}
getCountNeverListenSong(): Promise<number> {
// TODO Impossible sans valeur par defaut dans Play Count
return new Promise((resolve) => {
resolve(0);
});
}
getMostPlayedTrack(): Observable<Song[]> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append("sort_by", "Play Count:desc");
queryParams = queryParams.append("limit", 5);
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToSongs(res)),
catchError((error) => this.handleError(error, "getMostPlayedTrack()"))
);
}
getMostPlayedArtist(): Observable<Artist[]> {
return this.http
.post(
this.tsUrl + TsService.ARTIST_INDEX_NAME + TsService.ACTION_SEARCH,
JSON.stringify({
sort: [
{
_script: {
type: "number",
script: {
inline: "doc['Play Count'].value / doc['Track Count'].value",
},
order: "desc",
},
},
],
size: 100,
}),
{ headers: this.headers }
)
.pipe(
map((res) => this.responseToArtists(res)),
catchError((error) => this.handleError(error, "getMostPlayedArtist()"))
);
}
/**
* A basic get of albums ordered by 'Play Count' field.
*/
getMostPlayedAlbumNaive(): Promise<Album[]> {
return this.http
.get(
this.tsUrl +
TsService.ALBUM_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH +
"?sort_by=Play%20Count%3Adesc,Track%20Count%3Adesc&limit=20&q=*",
{ headers: this.headers }
)
.toPromise()
.then((res) => this.responseToAlbums(res))
.catch((error) => this.handleError(error, "getMostPlayedAlbumNaive"));
// TODO Excluse 'Divers' + compilation
}
getMostPlayedArtistNaive(): Promise<Artist[]> {
return this.http
.get(
this.tsUrl +
TsService.ARTIST_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH +
"?sort_by=Play%20Count%3Adesc&limit=20&q=*",
{ headers: this.headers }
)
.toPromise()
.then((res) => this.responseToAlbums(res))
.catch((error) => this.handleError(error, "getMostPlayedArtistNaive"));
// TODO Excluse 'Divers' + compilation
}
getGenreSongs(genreName: string, from: number = 0): Observable<Song[]> {
console.info(
"getGenreSongs- Genre name: " + genreName + " - from: " + from
);
let queryParams = new HttpParams();
queryParams = queryParams.append("q", genreName);
queryParams = queryParams.append("query_by", "Genre");
queryParams = queryParams.append("per_page", TsService.DEFAULT_SIZE);
queryParams = queryParams.append("offset", from);
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseToSongs(res)),
catchError((error) =>
this.handleError(
error,
"getGenreSongs(" + genreName + "," + from + ")"
)
)
);
}
getGenres(ordering: string = "desc"): Observable<TsBucket[]> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append("facet_by", "Genre");
queryParams = queryParams.append("max_facet_values", "5");
queryParams = queryParams.append("limit", "0");
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseAggregationToBucket(res)),
catchError((error) =>
this.handleError(error, "getGenres(" + ordering + ")")
)
);
}
getGenreCount(ordering: string = "desc"): Observable<number> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append("facet_by", "Genre");
queryParams = queryParams.append("max_facet_values", "0");
queryParams = queryParams.append("limit", "0");
return this.http
.get<any>(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => res.facet_counts[0].stats.total_values as number),
catchError((error) =>
this.handleError(error, "getGenres(" + ordering + ")")
)
);
}
getLastAddedAlbums(month: number): Observable<TsBucket[]> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", "*");
queryParams = queryParams.append("sort_by", "Track ID:desc");
queryParams = queryParams.append("group_by", "Album");
// TODO Deal with mounth?
return this.http
.get(
this.tsUrl +
TsService.SONG_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => this.responseSubAggregationToBucket(res)),
catchError((error) =>
this.handleError(error, "getLastAddedAlbums(" + month + ")")
)
);
}
getArtistFromAlbumName(albumname: string): Observable<Album[]> {
let queryParams = new HttpParams();
queryParams = queryParams.append("q", albumname);
queryParams = queryParams.append("query_by", "Name");
return this.http
.get<any>(
this.tsUrl +
TsService.ALBUM_INDEX_NAME +
"/documents" +
TsService.ACTION_SEARCH,
{ headers: this.headers, params: queryParams }
)
.pipe(
map((res) => res.hits),
map((hits: Array<any>) => {
const result: Array<Album> = [];
hits.forEach((hit) => {
result.push(hit.document);
});
return result;
}),
catchError((error) =>
this.handleError(error, "getArtistFromAlbumName(" + albumname + ")")
)
);
}
getSuggest(text: string): Observable<Suggested[]> {
console.log("search sugget: " + text);
return this.http
.post<any>(
this.tsUrl + TsService.SUGGEST_INDEX_NAME + TsService.ACTION_SEARCH,
JSON.stringify({
_source: ["album", "artist"],
suggest: {
"album-suggest": {
prefix: text,
completion: {
field: "album_suggest",
},
},
"artist-suggest": {
prefix: text,
completion: {
field: "artist_suggest",
},
},
},
}),
{ headers: this.headers }
)
.pipe(
map((res) =>
this.responseSuggesterToSuggested(
res,
"album-suggest",
"artist-suggest"
)
),
catchError((error) =>
this.handleError(error, "getSuggest(" + text + ")")
)
);
}
/** Process a result to return just one result.
* Used to get an album or an artist.
* Take a name to put in console output if no result or more than one result.
*
* @param res Response to process
* @param name The searched name - for console output
*/
protected responseToOneTypedResult<T>(res: any, name: string): T {
const hits = res.hits;
if (hits.length < 1) {
console.info('No result found for name: "' + name);
return undefined;
}
if (hits.length > 1) {
// TODO Cumul results (for album)
console.error(
'More than one result for name: "' +
name +
'". Found (' +
hits.length +
"), return the first."
);
}
return hits[0].document;
}
/** Process a response to a array of songs.
*
* @param res Response to process
*/
protected responseToSongs(res: any): Song[] {
const result: Array<Song> = [];
res.hits.forEach((hit) => {
result.push(hit.document);
});
return result;
}
/** Process a response to a array of songs.
*
* @param res Response to process
*/
private responseToArtists(res: any): Artist[] {
const result: Array<Artist> = [];
res.hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
}
/** Process an aggregation response to an array of Bucket.
*
* @param res Response to process
* @param name Name of aggregation
*/
protected responseAggregationToBucket(res: any): TsBucket[] {
const result: Array<TsBucket> = [];
res.facet_counts[0].counts.forEach((bucket) => {
result.push(bucket);
});
return result;
}
private responseSubAggregationToBucket(res: any): TsBucket[] {
const result: Array<TsBucket> = [];
res.grouped_hits.forEach((tmp) => {
result.push(tmp);
});
return result;
}
protected responseSuggesterToSuggested(
res: any,
...suggestName: string[]
): Suggested[] {
const result: Array<Suggested> = [];
suggestName.forEach((sname) => {
res["suggest"][sname][0]["options"].forEach((option) => {
let suggest = new Suggested();
// TODO If more than one key, raise exception
suggest.type = String(Object.keys(option["_source"]));
suggest.name = option["_source"][suggest.type];
result.push(suggest);
});
});
return result;
}
protected handleError(error: any, origin: string): Promise<any> {
console.error("An error occurred!");
console.error("Origin function: ", origin);
console.error("An error occurred!", error); // for demo purposes only
console.error(error); // for demo purposes only
return Promise.reject(error.message || error);
}
protected addSortFilterToQuery(query) {
query.query.bool.must_not.push({
term: {
"Location.tree": this.defaultLocation,
},
});
return query;
}
/** Process a response to a array of songs.
*
* @param res Response to process
*/
protected responseToAlbums(res: any): Album[] {
const result: Array<Album> = [];
res.hits.forEach((hit) => {
result.push(hit.document);
});
return result;
}
}

View File

@@ -1,66 +1,556 @@
{
"settings": {
"analysis": {
"analyzer": {
"custom_path_tree": { "tokenizer": "custom_hierarchy" },
"custom_path_tree_reversed": { "tokenizer": "custom_hierarchy_reversed" }
"name": "songs",
"token_separators": [":", "/", "."],
"fields": [
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Album",
"optional": true,
"sort": false,
"type": "string"
},
"tokenizer": {
"custom_hierarchy": {
"type": "path_hierarchy",
"delimiter": "/",
"skip": 3
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Album Rating",
"optional": true,
"sort": true,
"type": "int64"
},
"custom_hierarchy_reversed": {
"type": "path_hierarchy",
"delimiter": "/",
"reverse": "true"
}
}
}
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Album Rating Computed",
"optional": true,
"sort": true,
"type": "bool"
},
"mappings" : {
"properties": {
"Artist": {
"type": "text",
"fields": {
"raw": {"type": "keyword"}
}
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Artist",
"optional": true,
"sort": false,
"type": "string"
},
"Album Artist": {
"type": "text",
"fields": {
"raw": {"type": "keyword"}
}
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Artwork Count",
"optional": true,
"sort": true,
"type": "int64"
},
"Album": {
"type": "text",
"fields": {
"raw": {"type": "keyword"}
}
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Bit Rate",
"optional": true,
"sort": true,
"type": "int64"
},
"Bit Rate": {
"type": "integer"
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Date Added",
"optional": true,
"sort": false,
"type": "string"
},
"Genre": {
"type": "keyword"
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Date Modified",
"optional": true,
"sort": false,
"type": "string"
},
"Kind": {
"type": "keyword"
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "File Folder Count",
"optional": true,
"sort": true,
"type": "int64"
},
"Location": {
"type": "text",
"fields": {
"tree": {
"type": "text",
"analyzer": "custom_path_tree"
{
"facet": true,
"index": true,
"infix": false,
"locale": "",
"name": "Genre",
"optional": true,
"sort": false,
"type": "string"
},
"tree_reversed": {
"type": "text",
"analyzer": "custom_path_tree_reversed"
}
}
}
}
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Kind",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Library Folder Count",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Location",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Name",
"optional": false,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Persistent ID",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Play Count",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Play Date",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Play Date UTC",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Rating",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Rating Computed",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Sample Rate",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": true,
"index": true,
"infix": false,
"locale": "",
"name": "Size",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Skip Count",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Skip Date",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": true,
"index": true,
"infix": false,
"locale": "",
"name": "Total Time",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Track ID",
"optional": false,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Track Number",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Track Type",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Year",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Composer",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Disabled",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Disc Count",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Disc Number",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Album Artist",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Sort Name",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Sort Album",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Comments",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Sort Artist",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Sort Composer",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Loved",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Volume Adjustment",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Compilation",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Part Of Gapless Album",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Track Count",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Sort Album Artist",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Album Loved",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "BPM",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Grouping",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Series",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Purchased",
"optional": true,
"sort": true,
"type": "bool"
},
{
"facet": false,
"index": true,
"infix": false,
"locale": "",
"name": "Release Date",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Movement Count",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Movement Name",
"optional": true,
"sort": false,
"type": "string"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Movement Number",
"optional": true,
"sort": true,
"type": "int64"
},
{
"facet": false,
"index": false,
"infix": false,
"locale": "",
"name": "Work",
"optional": true,
"sort": false,
"type": "string"
}
]
}