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);
}
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);
}
});
}
Aggregations