<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Relisten.Api;
using Relisten.Api.Models;
using Relisten.Api.Models.Api;
using Relisten.Data;

namespace Relisten.Controllers
{
    [Route("api/v2")]
    [Produces("application/json")]
    public class LiveController : RelistenBaseController
    {
        public ShowService _showService { get; set; }
        public SourceTrackService _sourceTrackService { get; set; }
		public RedisService _redisService { get; set; }

		readonly SourceService sourceService;

		public LiveController(
			RedisService redis,
			DbService db,
			ArtistService artistService,
			ShowService showService,
			SourceService sourceService,
			SourceTrackService sourceTrackService
		) : base(redis, db, artistService)
		{
			this.sourceService = sourceService;
			_redisService = redis;
            _showService = showService;
            _sourceTrackService = sourceTrackService;
        }


        [HttpPost("live/play")]
        [ProducesResponseType(typeof(bool), 200)]
        [ProducesResponseType(typeof(ResponseEnvelope<bool>), 404)]
		public async Task<IActionResult> PlayedTrack([FromQuery] int track_id)
        {
            var track = await _sourceTrackService.ForId(track_id);

            if(track == null)
            {
                return JsonNotFound(false);
            }

			await redis.db.SortedSetAddAsync("played", track_id, DateTimeOffset.UtcNow.ToUnixTimeSeconds());

            return JsonSuccess(true);
        }

		[HttpPost("live/recently-played")]
		[ProducesResponseType(typeof(IEnumerable<PlayedSourceTrack>), 200)]
		public async Task<IActionResult> RecentlyPlayed()
		{
			var trackIds = (await redis.db.SortedSetRangeByRankAsync("played", -26, -1)).Select(t =>
			{
				t.TryParse(out int id);
				return id;
			});

			var tracks = await _sourceTrackService.ForIds(trackIds.ToList());

			var sourceIds = tracks
				.Select(t => t.source_id)
				.GroupBy(t => t)
				.Select(g => g.First());

			var sources = (await sourceService.SlimSourceWithShowAndArtistForIds(sourceIds.ToList()))
				.GroupBy(s => s.id)
				.ToDictionary(g => g.Key, g => g.First());

			return JsonSuccess(tracks.Select(t => new PlayedSourceTrack
			{
				source = sources[t.source_id],
				track = t
			}));
		}
    }
}<commit_msg>Attempt to fix recently played<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Relisten.Api;
using Relisten.Api.Models;
using Relisten.Api.Models.Api;
using Relisten.Data;

namespace Relisten.Controllers
{
    [Route("api/v2")]
    [Produces("application/json")]
    public class LiveController : RelistenBaseController
    {
        public ShowService _showService { get; set; }
        public SourceTrackService _sourceTrackService { get; set; }
		public RedisService _redisService { get; set; }

		readonly SourceService sourceService;

		public LiveController(
			RedisService redis,
			DbService db,
			ArtistService artistService,
			ShowService showService,
			SourceService sourceService,
			SourceTrackService sourceTrackService
		) : base(redis, db, artistService)
		{
			this.sourceService = sourceService;
			_redisService = redis;
            _showService = showService;
            _sourceTrackService = sourceTrackService;
        }


        [HttpPost("live/play")]
        [ProducesResponseType(typeof(bool), 200)]
        [ProducesResponseType(typeof(ResponseEnvelope<bool>), 404)]
		public async Task<IActionResult> PlayedTrack([FromQuery] int track_id)
        {
            var track = await _sourceTrackService.ForId(track_id);

            if(track == null)
            {
                return JsonNotFound(false);
            }

			await redis.db.SortedSetAddAsync("played", track_id, DateTimeOffset.UtcNow.ToUnixTimeSeconds());

            return JsonSuccess(true);
        }

		[HttpPost("live/recently-played")]
		[ProducesResponseType(typeof(IEnumerable<PlayedSourceTrack>), 200)]
		public async Task<IActionResult> RecentlyPlayed()
		{
			var trackIds = (await redis.db.SortedSetRangeByRankAsync("played", -1, -26)).Select(t =>
			{
				t.TryParse(out int id);
				return id;
			});

			var tracks = await _sourceTrackService.ForIds(trackIds.ToList());

			var sourceIds = tracks
				.Select(t => t.source_id)
				.GroupBy(t => t)
				.Select(g => g.First());

			var sources = (await sourceService.SlimSourceWithShowAndArtistForIds(sourceIds.ToList()))
				.GroupBy(s => s.id)
				.ToDictionary(g => g.Key, g => g.First());

			return JsonSuccess(tracks.Select(t => new PlayedSourceTrack
			{
				source = sources[t.source_id],
				track = t
			}));
		}
    }
}

{'fraction_non_alphanumeric': 0.1090610906109061, 'fraction_numerical': 0.005330053300533005, 'mean_word_length': 3.0658333333333334, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6426536', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1469, 'n_words': 290}