Search in sources :

Example 1 with PlaybackHistory

use of net.robinfriedli.aiode.entities.PlaybackHistory in project aiode by robinfriedli.

the class ChartsCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    Guild guild = getContext().getGuild();
    QueryBuilderFactory queryBuilderFactory = getQueryBuilderFactory();
    LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
    Date dateAtStartOfMonth = Date.valueOf(startOfMonth.toLocalDate());
    SelectQueryBuilder<PlaybackHistory> trackChartsQuery = queryBuilderFactory.select(PlaybackHistory.class, (from, cb) -> from.get("fkSource"), (from, cb) -> from.get("trackId"), (from, cb) -> cb.count(from.get("pk")), (from, cb) -> from.get("fkSpotifyItemKind")).where((cb, root) -> cb.isNotNull(root.get("trackId"))).groupBySeveral((from, cb) -> Lists.newArrayList(from.get("trackId"), from.get("fkSource"), from.get("fkSpotifyItemKind"))).orderBy((from, cb) -> cb.desc(cb.count(from.get("pk"))));
    SelectQueryBuilder<PlaybackHistory> trackChartsQueryGuild = trackChartsQuery.fork().where((cb, root) -> cb.equal(root.get("guildId"), guild.getId()));
    SelectQueryBuilder<PlaybackHistory> trackChartsQueryMonthly = trackChartsQuery.fork().where((cb, root) -> cb.greaterThan(root.get("timestamp"), startOfMonth));
    SelectQueryBuilder<PlaybackHistory> trackChartsQueryMonthlyGuild = trackChartsQueryMonthly.fork().where((cb, root) -> cb.equal(root.get("guildId"), guild.getId()));
    Query<Object[]> globalQuery = trackChartsQuery.build(session).setMaxResults(5);
    Query<Object[]> guildQuery = trackChartsQueryGuild.build(session).setMaxResults(5);
    Query<Object[]> globalQueryMonthly = trackChartsQueryMonthly.build(session).setMaxResults(5);
    Query<Object[]> guildQueryMonthly = trackChartsQueryMonthlyGuild.build(session).setMaxResults(5);
    List<Object[]> globalResults = globalQuery.getResultList();
    List<Object[]> guildResults = guildQuery.getResultList();
    List<Object[]> globalMonthlyResults = globalQueryMonthly.getResultList();
    List<Object[]> guildMonthlyResults = guildQueryMonthly.getResultList();
    @SuppressWarnings("unchecked") Query<Object[]> globalArtistQuery = session.createSQLQuery("select artist_pk, count(*) as c " + "from playback_history_artist group by artist_pk order by c desc limit 3");
    @SuppressWarnings("unchecked") Query<Object[]> guildArtistQuery = session.createSQLQuery("select artist_pk, count(*) as c from " + "playback_history_artist as p where p.playback_history_pk in(select pk from playback_history where guild_id = ?) " + "group by artist_pk order by c desc limit 3");
    guildArtistQuery.setParameter(1, guild.getId());
    @SuppressWarnings("unchecked") Query<Object[]> globalArtistMonthlyQuery = session.createSQLQuery("select artist_pk, count(*) as c " + "from playback_history_artist as p " + "where p.playback_history_pk in(select pk from playback_history where timestamp > ?) " + "group by artist_pk order by c desc limit 3");
    globalArtistMonthlyQuery.setParameter(1, dateAtStartOfMonth);
    @SuppressWarnings("unchecked") Query<Object[]> guildArtistMonthlyQuery = session.createSQLQuery("select artist_pk, count(*) as c " + "from playback_history_artist where playback_history_pk in(select pk from playback_history " + "where timestamp > ? and guild_id = ?) " + "group by artist_pk order by c desc limit 3");
    guildArtistMonthlyQuery.setParameter(1, dateAtStartOfMonth);
    guildArtistMonthlyQuery.setParameter(2, guild.getId());
    List<Object[]> globalArtists = globalArtistQuery.getResultList();
    List<Object[]> guildArtists = guildArtistQuery.getResultList();
    List<Object[]> globalArtistsMonthly = globalArtistMonthlyQuery.getResultList();
    List<Object[]> guildArtistMonthly = guildArtistMonthlyQuery.getResultList();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.addField("Global", "Shows the charts across all guilds", false);
    addTrackCharts(globalResults, embedBuilder, "All time", session);
    addArtists(globalArtists, embedBuilder, "All time");
    embedBuilder.addBlankField(true);
    addTrackCharts(globalMonthlyResults, embedBuilder, "Monthly", session);
    addArtists(globalArtistsMonthly, embedBuilder, "Monthly");
    embedBuilder.addBlankField(false);
    embedBuilder.addField("Guild", "Shows the charts for this guild", false);
    addTrackCharts(guildResults, embedBuilder, "All time", session);
    addArtists(guildArtists, embedBuilder, "All time");
    embedBuilder.addBlankField(true);
    addTrackCharts(guildMonthlyResults, embedBuilder, "Monthly", session);
    addArtists(guildArtistMonthly, embedBuilder, "Monthly");
    sendMessage(embedBuilder);
}
Also used : LocalDateTime(java.time.LocalDateTime) Artist(net.robinfriedli.aiode.entities.Artist) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) PlaybackHistory(net.robinfriedli.aiode.entities.PlaybackHistory) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Playable(net.robinfriedli.aiode.audio.Playable) Session(org.hibernate.Session) HashMap(java.util.HashMap) PlayableTrackWrapper(net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper) PlaybackHistorySource(net.robinfriedli.aiode.entities.PlaybackHistorySource) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) Util(net.robinfriedli.aiode.util.Util) Lists(com.google.common.collect.Lists) Guild(net.dv8tion.jda.api.entities.Guild) Map(java.util.Map) Query(org.hibernate.query.Query) BigInteger(java.math.BigInteger) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) CommandManager(net.robinfriedli.aiode.command.CommandManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) SpotifyItemKind(net.robinfriedli.aiode.entities.SpotifyItemKind) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) Date(java.sql.Date) Aiode(net.robinfriedli.aiode.Aiode) SelectQueryBuilder(net.robinfriedli.aiode.persist.qb.builders.SelectQueryBuilder) List(java.util.List) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) CommandContext(net.robinfriedli.aiode.command.CommandContext) LocalDate(java.time.LocalDate) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) PlaybackHistory(net.robinfriedli.aiode.entities.PlaybackHistory) Guild(net.dv8tion.jda.api.entities.Guild) Date(java.sql.Date) LocalDate(java.time.LocalDate) Session(org.hibernate.Session)

Example 2 with PlaybackHistory

use of net.robinfriedli.aiode.entities.PlaybackHistory in project aiode by robinfriedli.

the class AudioManager method createHistoryEntry.

void createHistoryEntry(Playable playable, Guild guild, VoiceChannel voiceChannel) {
    HistoryPool.execute(() -> {
        try {
            hibernateComponent.consumeSession(session -> {
                PlaybackHistory playbackHistory = new PlaybackHistory(LocalDateTime.now(), playable, guild, session);
                session.persist(playbackHistory);
                if (voiceChannel != null) {
                    Member selfMember = guild.getSelfMember();
                    for (Member member : voiceChannel.getMembers()) {
                        if (!member.equals(selfMember)) {
                            UserPlaybackHistory userPlaybackHistory = new UserPlaybackHistory(member.getUser(), playbackHistory);
                            session.persist(userPlaybackHistory);
                        }
                    }
                }
            });
        } catch (Exception e) {
            logger.error("Exception while creating playback history entry", e);
        }
    });
}
Also used : PlaybackHistory(net.robinfriedli.aiode.entities.PlaybackHistory) UserPlaybackHistory(net.robinfriedli.aiode.entities.UserPlaybackHistory) Member(net.dv8tion.jda.api.entities.Member) UserPlaybackHistory(net.robinfriedli.aiode.entities.UserPlaybackHistory) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException)

Aggregations

PlaybackHistory (net.robinfriedli.aiode.entities.PlaybackHistory)2 Lists (com.google.common.collect.Lists)1 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)1 BigInteger (java.math.BigInteger)1 Date (java.sql.Date)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Member (net.dv8tion.jda.api.entities.Member)1 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)1 Aiode (net.robinfriedli.aiode.Aiode)1 Playable (net.robinfriedli.aiode.audio.Playable)1 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)1 BlockingTrackLoadingExecutor (net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor)1 PlayableTrackWrapper (net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper)1 SpotifyService (net.robinfriedli.aiode.audio.spotify.SpotifyService)1