public void InitStats() { try { DateTime start = DateTime.Now; ClearAllData(); #region Get the data AnimeGroupRepository repGroups = new AnimeGroupRepository(); AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); AniDB_CategoryRepository repCats = new AniDB_CategoryRepository(); AniDB_Anime_CategoryRepository repAnimeCat = new AniDB_Anime_CategoryRepository(); AniDB_Anime_TitleRepository repTitles = new AniDB_Anime_TitleRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(); Dictionary<int, AnimeGroup> allGroupsDict = new Dictionary<int, AnimeGroup>(); foreach (AnimeGroup agrp in allGrps) allGroupsDict[agrp.AnimeGroupID] = agrp; TimeSpan ts = DateTime.Now - start; logger.Info("Get All GROUPS (Database) in {0} ms", ts.TotalMilliseconds); // anime start = DateTime.Now; List<AniDB_Anime> allAnime = repAnime.GetAll(); Dictionary<int, AniDB_Anime> allAnimeDict = new Dictionary<int, AniDB_Anime>(); foreach (AniDB_Anime anime in allAnime) allAnimeDict[anime.AnimeID] = anime; ts = DateTime.Now - start; logger.Info("Get All ANIME (Database) in {0} ms", ts.TotalMilliseconds); // categories start = DateTime.Now; List<AniDB_Category> allCatgeories = repCats.GetAll(); Dictionary<int, AniDB_Category> allCatgeoriesDict = new Dictionary<int, AniDB_Category>(); foreach (AniDB_Category cat in allCatgeories) allCatgeoriesDict[cat.CategoryID] = cat; List<AniDB_Anime_Category> allAnimeCatgeories = repAnimeCat.GetAll(); Dictionary<int, List<int>> allAnimeCatgeoriesDict = new Dictionary<int, List<int>>(); // animeid / list of category id's foreach (AniDB_Anime_Category aniCat in allAnimeCatgeories) { if (!allAnimeCatgeoriesDict.ContainsKey(aniCat.AnimeID)) allAnimeCatgeoriesDict[aniCat.AnimeID] = new List<int>(); allAnimeCatgeoriesDict[aniCat.AnimeID].Add(aniCat.CategoryID); } ts = DateTime.Now - start; logger.Info("Get All CATEGORIES (Database) in {0} ms", ts.TotalMilliseconds); // titles start = DateTime.Now; List<AniDB_Anime_Title> allTitles = repTitles.GetAll(); Dictionary<int, List<AniDB_Anime_Title>> allTitlesDict = new Dictionary<int, List<AniDB_Anime_Title>>(); // animeid / list of titles foreach (AniDB_Anime_Title aniTitle in allTitles) { if (!allTitlesDict.ContainsKey(aniTitle.AnimeID)) allTitlesDict[aniTitle.AnimeID] = new List<AniDB_Anime_Title>(); allTitlesDict[aniTitle.AnimeID].Add(aniTitle); } ts = DateTime.Now - start; logger.Info("Get All TITLES (Database) in {0} ms", ts.TotalMilliseconds); // user votes start = DateTime.Now; AniDB_VoteRepository repVotes = new AniDB_VoteRepository(); List<AniDB_Vote> allVotes = repVotes.GetAll(); ts = DateTime.Now - start; logger.Info("Get All VOTES (Database) in {0} ms", ts.TotalMilliseconds); // video quality start = DateTime.Now; AdhocRepository rep = new AdhocRepository(); Dictionary<int, string> allVidQuality = rep.GetAllVideoQualityByGroup(); ts = DateTime.Now - start; logger.Info("Get VIDEO QUALITY STATS (Database) in {0} ms", ts.TotalMilliseconds); // video quality episode stats start = DateTime.Now; Dictionary<int, AnimeVideoQualityStat> dictStats = rep.GetEpisodeVideoQualityStatsByAnime(); ts = DateTime.Now - start; logger.Info("Get VIDEO QUALITY EPISODE STATS (Database) in {0} ms", ts.TotalMilliseconds); // audio and subtitle language stats start = DateTime.Now; Dictionary<int, LanguageStat> dictAudioStats = rep.GetAudioLanguageStatsForAnime(); Dictionary<int, LanguageStat> dictSubtitleStats = rep.GetSubtitleLanguageStatsForAnime(); ts = DateTime.Now - start; logger.Info("Get LANGUAGE STATS (Database) in {0} ms", ts.TotalMilliseconds); start = DateTime.Now; List<AnimeSeries> allSeries = repSeries.GetAll(); ts = DateTime.Now - start; logger.Info("Get All Series (Database) in {0} ms", ts.TotalMilliseconds); // TvDB start = DateTime.Now; CrossRef_AniDB_TvDBV2Repository repCrossRef = new CrossRef_AniDB_TvDBV2Repository(); List<CrossRef_AniDB_TvDBV2> allCrossRefs = repCrossRef.GetAll(); List<int> animeWithTvDBCrossRef = new List<int>(); foreach (CrossRef_AniDB_TvDBV2 xref in allCrossRefs) { if (!animeWithTvDBCrossRef.Contains(xref.AnimeID)) animeWithTvDBCrossRef.Add(xref.AnimeID); } ts = DateTime.Now - start; logger.Info("Get All AniDB->TvDB Cross Refs (Database) in {0} ms", ts.TotalMilliseconds); // MovieDB start = DateTime.Now; CrossRef_AniDB_OtherRepository repOtherCrossRef = new CrossRef_AniDB_OtherRepository(); List<CrossRef_AniDB_Other> allOtherCrossRefs = repOtherCrossRef.GetAll(); List<int> animeWithMovieCrossRef = new List<int>(); foreach (CrossRef_AniDB_Other xref in allOtherCrossRefs) { if (!animeWithMovieCrossRef.Contains(xref.AnimeID) && xref.CrossRefType == (int)CrossRefType.MovieDB) animeWithMovieCrossRef.Add(xref.AnimeID); } ts = DateTime.Now - start; logger.Info("Get All AniDB->MovieDB Cross Refs (Database) in {0} ms", ts.TotalMilliseconds); // MAL start = DateTime.Now; CrossRef_AniDB_MALRepository repMALCrossRef = new CrossRef_AniDB_MALRepository(); List<CrossRef_AniDB_MAL> allMALCrossRefs = repMALCrossRef.GetAll(); List<int> animeWithMALCrossRef = new List<int>(); foreach (CrossRef_AniDB_MAL xref in allMALCrossRefs) { if (!animeWithMALCrossRef.Contains(xref.AnimeID)) animeWithMALCrossRef.Add(xref.AnimeID); } ts = DateTime.Now - start; logger.Info("Get All AniDB->MAL Cross Refs (Database) in {0} ms", ts.TotalMilliseconds); #endregion start = DateTime.Now; var session = JMMService.SessionFactory.OpenSession(); foreach (AnimeGroup ag in allGrps) { // get all the series for this group List<AnimeSeries> seriesForGroup = new List<AnimeSeries>(); GetAnimeSeriesRecursive(ag, ref seriesForGroup, allSeries, allGroupsDict); /* if (ag.AnimeGroupID == 915) { Console.Write(""); } */ DateTime? Stat_AirDate_Min = null; DateTime? Stat_AirDate_Max = null; DateTime? Stat_EndDate = new DateTime(1980, 1, 1); DateTime? Stat_SeriesCreatedDate = null; bool isComplete = false; bool hasFinishedAiring = false; bool isCurrentlyAiring = false; List<int> categoryIDList = new List<int>(); List<string> audioLanguageList = new List<string>(); List<string> subtitleLanguageList = new List<string>(); string Stat_AllTitles = ""; string Stat_AllCategories = ""; string Stat_AllVideoQualityEpisodes = ""; decimal totalVotesPerm = 0, totalVotesTemp = 0, totalVotes = 0; int countVotesPerm = 0, countVotesTemp = 0, countVotes = 0; bool hasTvDB = true; bool hasMAL = true; bool hasMovieDB = true; bool hasMovieDBOrTvDB = true; int seriesCount = 0; int epCount = 0; foreach (AnimeSeries series in seriesForGroup) { seriesCount++; if (allAnimeDict.ContainsKey(series.AniDB_ID)) { AniDB_Anime thisAnime = allAnimeDict[series.AniDB_ID]; epCount = epCount + thisAnime.EpisodeCountNormal; // All Video Quality Episodes // Try to determine if this anime has all the episodes available at a certain video quality // e.g. the series has all episodes in blu-ray if (dictStats.ContainsKey(series.AniDB_ID)) { if (series.AniDB_ID == 7656) { Debug.Print(""); } AnimeVideoQualityStat stat = dictStats[series.AniDB_ID]; foreach (KeyValuePair<string, int> kvp in stat.VideoQualityEpisodeCount) { if (kvp.Value >= thisAnime.EpisodeCountNormal) { if (Stat_AllVideoQualityEpisodes.Length > 0) Stat_AllVideoQualityEpisodes += ","; Stat_AllVideoQualityEpisodes += kvp.Key; } } } // Calculate Air Date DateTime? thisDate = thisAnime.AirDate; if (thisDate.HasValue) { if (Stat_AirDate_Min.HasValue) { if (thisDate.Value < Stat_AirDate_Min.Value) Stat_AirDate_Min = thisDate; } else Stat_AirDate_Min = thisDate; if (Stat_AirDate_Max.HasValue) { if (thisDate.Value > Stat_AirDate_Max.Value) Stat_AirDate_Max = thisDate; } else Stat_AirDate_Max = thisDate; } // calculate end date // if the end date is NULL it actually means it is ongoing, so this is the max possible value thisDate = thisAnime.EndDate; if (thisDate.HasValue && Stat_EndDate.HasValue) { if (thisDate.Value > Stat_EndDate.Value) Stat_EndDate = thisDate; } else Stat_EndDate = null; // Calculate Series Created Date thisDate = series.DateTimeCreated; if (thisDate.HasValue) { if (Stat_SeriesCreatedDate.HasValue) { if (thisDate.Value < Stat_SeriesCreatedDate.Value) Stat_SeriesCreatedDate = thisDate; } else Stat_SeriesCreatedDate = thisDate; } /* if (series.AniDB_ID == 2369) Debug.Write("Test"); */ // Note - only one series has to be finished airing to qualify if (thisAnime.EndDate.HasValue && thisAnime.EndDate.Value < DateTime.Now) hasFinishedAiring = true; // Note - only one series has to be currently airing to qualify if (!thisAnime.EndDate.HasValue || thisAnime.EndDate.Value > DateTime.Now) isCurrentlyAiring = true; // We evaluate IsComplete as true if // 1. series has finished airing // 2. user has all episodes locally // Note - only one series has to be complete for the group to be considered complete if (thisAnime.EndDate.HasValue) { if (thisAnime.EndDate.Value < DateTime.Now && series.MissingEpisodeCount == 0 && series.MissingEpisodeCountGroups == 0) { isComplete = true; } } // get categories if (allAnimeCatgeoriesDict.ContainsKey(series.AniDB_ID)) { foreach (int catID in allAnimeCatgeoriesDict[series.AniDB_ID]) { if (!categoryIDList.Contains(catID)) categoryIDList.Add(catID); } } // get audio languages if (dictAudioStats.ContainsKey(series.AniDB_ID)) { foreach (string lanName in dictAudioStats[series.AniDB_ID].LanguageNames) { if (!audioLanguageList.Contains(lanName)) audioLanguageList.Add(lanName); } } // get subtitle languages if (dictSubtitleStats.ContainsKey(series.AniDB_ID)) { foreach (string lanName in dictSubtitleStats[series.AniDB_ID].LanguageNames) { if (!subtitleLanguageList.Contains(lanName)) subtitleLanguageList.Add(lanName); } } // get titles if (allTitlesDict.ContainsKey(series.AniDB_ID)) { foreach (AniDB_Anime_Title title in allTitlesDict[series.AniDB_ID]) { if (Stat_AllTitles.Length > 0) Stat_AllTitles += ","; Stat_AllTitles += title.Title; } } // get votes foreach (AniDB_Vote vote in allVotes) { if (vote.EntityID == series.AniDB_ID && (vote.VoteType == (int)AniDBVoteType.Anime || vote.VoteType == (int)AniDBVoteType.AnimeTemp)) { countVotes++; totalVotes += (decimal)vote.VoteValue; if (vote.VoteType == (int)AniDBVoteType.Anime) { countVotesPerm++; totalVotesPerm += (decimal)vote.VoteValue; } if (vote.VoteType == (int)AniDBVoteType.AnimeTemp) { countVotesTemp++; totalVotesTemp += (decimal)vote.VoteValue; } break; } } } // for the group, if any of the series don't have a tvdb link // we will consider the group as not having a tvdb link if (!animeWithTvDBCrossRef.Contains(series.AniDB_ID)) hasTvDB = false; if (!animeWithMovieCrossRef.Contains(series.AniDB_ID)) hasMovieDB = false; if (!animeWithMALCrossRef.Contains(series.AniDB_ID)) hasMAL = false; if (!animeWithTvDBCrossRef.Contains(series.AniDB_ID) && !animeWithMovieCrossRef.Contains(series.AniDB_ID)) hasMovieDBOrTvDB = false; } if (allVidQuality.ContainsKey(ag.AnimeGroupID)) StatGroupVideoQuality[ag.AnimeGroupID] = allVidQuality[ag.AnimeGroupID]; else StatGroupVideoQuality[ag.AnimeGroupID] = ""; StatGroupVideoQualityEpisodes[ag.AnimeGroupID] = Stat_AllVideoQualityEpisodes; StatGroupIsComplete[ag.AnimeGroupID] = isComplete; StatGroupIsFinishedAiring[ag.AnimeGroupID] = hasFinishedAiring; StatGroupIsCurrentlyAiring[ag.AnimeGroupID] = isCurrentlyAiring; StatGroupSeriesCount[ag.AnimeGroupID] = seriesCount; StatGroupEpisodeCount[ag.AnimeGroupID] = epCount; StatGroupTitles[ag.AnimeGroupID] = Stat_AllTitles; StatGroupAirDate_Max[ag.AnimeGroupID] = Stat_AirDate_Max; StatGroupAirDate_Min[ag.AnimeGroupID] = Stat_AirDate_Min; StatGroupEndDate[ag.AnimeGroupID] = Stat_EndDate; StatGroupSeriesCreatedDate[ag.AnimeGroupID] = Stat_SeriesCreatedDate; StatGroupHasTvDB[ag.AnimeGroupID] = hasTvDB; StatGroupHasMAL[ag.AnimeGroupID] = hasMAL; StatGroupHasMovieDB[ag.AnimeGroupID] = hasMovieDB; StatGroupHasMovieDBOrTvDB[ag.AnimeGroupID] = hasMovieDBOrTvDB; decimal? Stat_UserVoteOverall = null; if (countVotes > 0) Stat_UserVoteOverall = totalVotes / (decimal)countVotes / (decimal)100; StatGroupUserVoteOverall[ag.AnimeGroupID] = Stat_UserVoteOverall; decimal? Stat_UserVotePermanent = null; if (countVotesPerm > 0) Stat_UserVotePermanent = totalVotesPerm / (decimal)countVotesPerm / (decimal)100; StatGroupUserVotePermanent[ag.AnimeGroupID] = Stat_UserVotePermanent; decimal? Stat_UserVoteTemporary = null; if (countVotesTemp > 0) Stat_UserVoteTemporary = totalVotesTemp / (decimal)countVotesTemp / (decimal)100; StatGroupUserVoteTemporary[ag.AnimeGroupID] = Stat_UserVoteTemporary; StatGroupAniDBRating[ag.AnimeGroupID] = ag.AniDBRating; Stat_AllCategories = ""; foreach (int catID in categoryIDList) { if (!allCatgeoriesDict.ContainsKey(catID)) continue; string catName = allCatgeoriesDict[catID].CategoryName; if (Stat_AllCategories.Length > 0) Stat_AllCategories += "|"; Stat_AllCategories += catName; } this.StatGroupCategories[ag.AnimeGroupID] = Stat_AllCategories; string Stat_AudioLanguages = ""; foreach (string audioLan in audioLanguageList) { if (Stat_AudioLanguages.Length > 0) Stat_AudioLanguages += ","; Stat_AudioLanguages += audioLan; } this.StatGroupAudioLanguages[ag.AnimeGroupID] = Stat_AudioLanguages; string Stat_SubtitleLanguages = ""; foreach (string subLan in subtitleLanguageList) { if (Stat_SubtitleLanguages.Length > 0) Stat_SubtitleLanguages += ","; Stat_SubtitleLanguages += subLan; } this.StatGroupSubtitleLanguages[ag.AnimeGroupID] = Stat_SubtitleLanguages; UpdateGroupFilterUsingGroup(ag.AnimeGroupID); UpdatePlexAnimeGroup(session, ag, allSeries); } ts = DateTime.Now - start; logger.Info("GetAllGroups (Contracts) in {0} ms", ts.TotalMilliseconds); //UpdateAllAnimeContracts(); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } }