Skip to content

Commit

Permalink
Start refactoring. Implement complicated sorting by rating for artists.
Browse files Browse the repository at this point in the history
  • Loading branch information
isaldin committed Jan 14, 2020
1 parent bc7c629 commit 08030b1
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 75 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
"^.+\\.(ts|tsx)$": "ts-jest"
},

rootDir: "./src",
rootDir: ".",
moduleNameMapper: {
"^@app/(.*)$": "<rootDir>/src/$1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import mongoose, { mongo } from "mongoose";
import DataLoader from "dataloader";
import { map, find, filter } from "ramda";

import { Artist, IArtist } from "@app/models/artist.model";
import { ArtistModel, IArtistModel } from "@app/models/.vaskir/artist.model";

const ArtistLoader = new DataLoader(
async (ids: readonly string[]): Promise<(IArtist | null)[]> => {
const artists = await Artist.find({ _id: { $in: ids } });
async (ids: readonly string[]): Promise<(IArtistModel | null)[]> => {
const artists = await ArtistModel.find({ _id: { $in: ids } });

const result = map(
id => find(artist => artist._id.equals(id), artists) || null,
Expand All @@ -24,8 +24,8 @@ interface IAllArtistsInput {
}
const allArtists = async (
input: IAllArtistsInput
): Promise<(IArtist | null)[]> => {
const artists: IArtist[] = await Artist.aggregate([
): Promise<(IArtistModel | null)[]> => {
const artists: IArtistModel[] = await ArtistModel.aggregate([
{
$match: {
$or: [
Expand Down Expand Up @@ -54,7 +54,10 @@ const allArtists = async (
{ $limit: input.limit }
]);

const artistsIds = map((artist: IArtist) => artist._id.toString(), artists);
const artistsIds = map(
(artist: IArtistModel) => artist._id.toString(),
artists
);
const rawData = await ArtistLoader.loadMany(artistsIds);

return filter(
Expand All @@ -64,7 +67,7 @@ const allArtists = async (
);
};

const artistById = async (id: string): Promise<IArtist | null> => {
const artistById = async (id: string): Promise<IArtistModel | null> => {
if (!mongoose.Types.ObjectId.isValid(id)) {
return null;
}
Expand All @@ -75,7 +78,7 @@ const artistById = async (id: string): Promise<IArtist | null> => {
};

const countOfArtists = async (searchCriteria: string | null): Promise<number> =>
await Artist.countDocuments({
await ArtistModel.countDocuments({
$or: [
{ name: { $regex: new RegExp(searchCriteria || "", "ig") } },
{ username: { $regex: new RegExp(searchCriteria || "", "ig") } }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import DataLoader from "dataloader";
import { map, find, filter } from "ramda";

import { ITrack, Track } from "@app/models/track.model";
import { ITrackModel, TrackModel } from "../../models/.vaskir/track.model";

const TracksLoader = new DataLoader(
async (ids: readonly string[]): Promise<(ITrack | null)[]> => {
const tracks = await Track.find({ _id: { $in: ids } });
async (ids: readonly string[]): Promise<(ITrackModel | null)[]> => {
const tracks = await TrackModel.find({ _id: { $in: ids } });

const result = map(
id => find(track => track._id.equals(id), tracks) || null,
Expand All @@ -19,22 +19,22 @@ const TracksLoader = new DataLoader(
interface IGetTracksInput {
authorId: string;
}
const getTracks = async (ids: string[]): Promise<ITrack[]> => {
const getTracks = async (ids: string[]): Promise<ITrackModel[]> => {
const rawResult = await TracksLoader.loadMany(ids);

return filter(
return []; /*filter(
// @ts-ignore // FIXME: to figure out
item => item && item._id !== undefined,
rawResult
);
);*/
};

interface IGetTrackInput {
authorId: string;
round: number;
}
const getTrack = async (input: IGetTrackInput) =>
await Track.findOne({
await TrackModel.findOne({
artist: input.authorId,
round: input.round
});
Expand Down
71 changes: 71 additions & 0 deletions src/controllers/artists.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { IArtistModel, ArtistModel } from "@app/models/artist.model";

type SortTypes = "sort";

// default sort by rating (see tests)
// sorting priority:
// rounds
// wins/rounds
// overall_judges
// overall_popular
const getAllArtists = async (
sort: SortTypes = "sort"
): Promise<IArtistModel[]> => {
const artists = await ArtistModel.aggregate([
{
$lookup: {
from: "tracks",
localField: "tracks",
foreignField: "_id",
as: "tracks"
}
},
{
$addFields: {
rounds: {
$size: "$tracks"
},
wins: {
$size: {
$filter: {
input: "$tracks",
as: "track",
cond: {
$or: [
{ $eq: ["$$track.status", 1] },
{ $eq: ["$$track.status", 3] }
]
}
}
}
},
overall_judges_rating: {
$sum: "$tracks.judges_rating"
},
overall_popular_rating: {
$sum: "$tracks.popular_rating"
}
}
},
{
$addFields: {
wins_of_rounds: {
$divide: ["$wins", "$rounds"]
}
}
},
{
$sort: {
rounds: -1,
wins_of_rounds: -1,
overall_judges_rating: -1,
overall_popular_rating: -1
}
}
]);
return artists;
};

export default {
getAllArtists
};
Loading

0 comments on commit 08030b1

Please sign in to comment.