public static SummaryProcessStat ProcessCompetitionPack(LoggerWrapper logger, BrokerData brokerData, GatherBehaviorMode algoMode, BrokerCompetitionSettings brokerSettings, Action<SummaryProcessStat, BrokerType, SportType, CompetitionItemRawTransport, MatchParsed> actionForMatchedItem) { var stat = new SummaryProcessStat(); var competitorProvider = ProjectProvider.Instance.CompetitorProvider; var competitionProvider = ProjectProvider.Instance.CompetitionProvider; foreach (var competitionParsed in brokerData.Competitions) { var competition = competitionProvider.GetCompetitionSpecify(stat[ProcessStatType.CompetitionFromRaw], stat[ProcessStatType.CompetitionSpecifyFromRaw], brokerData.Broker, brokerData.Language, competitionParsed.Type, competitionParsed.Name, competitionParsed, algoMode); foreach (var matchParsed in competitionParsed.Matches) { var competitorStat = stat[ProcessStatType.CompetitorFromRaw]; var competitors = new[] { competitorProvider .GetCompetitor(competitorStat, brokerData.Broker, brokerData.Language, competitionParsed.Type, competition.Object.GenderType, matchParsed.CompetitorName1, algoMode), competitorProvider .GetCompetitor(competitorStat, brokerData.Broker, brokerData.Language, competitionParsed.Type, competition.Object.GenderType, matchParsed.CompetitorName2, algoMode) }; if (competitors.Any(c => c.RawObject.ID == default(int))) { continue; } if (competitors.Any(c => c.Object.ID == default(int)) && competitors.Any(c => c.Object.ID != default(int))) { var linker = new SystemStateProvder(); linker.ApplyLinker(competitors.First(c => c.Object.ID != default(int)).RawObject.ID, BrokerEntityType.Competitor); var unlikedCompetitor = competitors.First(c => c.Object.ID == default(int)); unlikedCompetitor.Object.ID = RawCompetitor.DataSource.GetByKey(unlikedCompetitor.RawObject.ID).CompetitoruniqueID; } var creationCiMode = (matchParsed.Odds.SafeAny() || matchParsed.Result != null) ? algoMode : algoMode.FlagDrop(GatherBehaviorMode.CreateOriginalIfMatchedAll); var competitionItemRawTransport = competitionProvider.GetCompetitionItem(stat[ProcessStatType.CompetitionItemFromRaw], brokerData.Broker, competitors, competition, matchParsed.DateUtc, creationCiMode, brokerSettings); if (competitionItemRawTransport != null && competitionItemRawTransport.CompetitionItemID != default(int)) { if (competitionItemRawTransport.CompetitionItemID < default(int)) { competitionItemRawTransport.CompetitionItemID = -competitionItemRawTransport.CompetitionItemID; logger.Info("Inverse data for ID = {0} {1} {2}", competitionItemRawTransport.CompetitionItemID, brokerData.Broker, brokerData.Language); ReverseAllDataInMatch(matchParsed); } actionForMatchedItem(stat, brokerData.Broker, competitionParsed.Type, competitionItemRawTransport, matchParsed); } } } logger.Info("SaveResults: {0} {1} {2} {3}: Competitions: {4}/{5} CompetitionItems: {6}/{7} Competitors {8}/{9} {10}", brokerData.Competitions.FirstOrDefault(c => c.Matches.Any())?.Matches.FirstOrDefault()?.DateUtc.Date.ToString("yyyy MMMM dd"), brokerData.Broker, brokerData.Language, algoMode, stat[ProcessStatType.CompetitionSpecifyFromRaw].FinallySuccessCount, stat[ProcessStatType.CompetitionSpecifyFromRaw].TotalCount, stat[ProcessStatType.CompetitionItemFromRaw].FinallySuccessCount, stat[ProcessStatType.CompetitionItemFromRaw].TotalCount, stat[ProcessStatType.CompetitorFromRaw].FinallySuccessCount, stat[ProcessStatType.CompetitorFromRaw].TotalCount, brokerData.Competitions.GroupBy(c => c.Type).Select(g => string.Format("{0}={1}", g.Key, g.Count())).StrJoin(", ")); return stat; }
public RawTemplateObj<CompetitorParsedTransport> GetCompetitor(ProcessStat competitorStat, BrokerType brokerType, LanguageType languageType, SportType sportType, GenderType genderType, string[] names, GatherBehaviorMode algoMode) { return InvokeSafeSingleCall(() => { names = names .Where(name => !string.IsNullOrWhiteSpace(name)) .Select(name => { var indexOf = name.IndexOf("(", StringComparison.InvariantCultureIgnoreCase); var cleanedName = (indexOf > 0 ? name.Substring(0, indexOf) : name).Trim(_trimChars); return cleanedName.RemoveDiacritics(); }) .Where(name => !string.IsNullOrWhiteSpace(name)) .ToArray(); if (!names.Any()) { throw new Exception("nameFull.IsNullOrWhiteSpace() && nameShort.IsNullOrWhiteSpace()"); } var competitors = new BrokerEntityBuilder<List<RawCompetitor>>(competitorStat) .SetupValidateObject(competitorsRaw => competitorsRaw.SafeAny() && competitorsRaw.All(c => c.CompetitoruniqueID != default(int))) .SetupGetRaw(() => RawCompetitorHelper.GetRawCompetitor[new RawCompetitorHelper.RawCompetitorCacheKey(brokerType, languageType, sportType, genderType, names)]) .SetupCreateOriginal(algoMode, list => { var uniqueID = new CompetitorUnique { IsUsed = true }; uniqueID.Save(); var competitor = new Competitor { CompetitoruniqueID = uniqueID.ID, SportType = sportType, Datecreatedutc = DateTime.UtcNow, Languagetype = languageType, Name = names[0], Gendertype = genderType }; competitor.Save(); list.Each(el => el.Linkstatus = LinkEntityStatus.Original | LinkEntityStatus.Linked); return list; }) .SetupFinally(list => { var firstElement = list.First(); if (algoMode.HasFlag(GatherBehaviorMode.CreateNewLanguageName) && firstElement.CompetitoruniqueID != default(int) && !Competitor.DataSource .WhereEquals(Competitor.Fields.CompetitoruniqueID, firstElement.CompetitoruniqueID) .WhereEquals(Competitor.Fields.Languagetype, (short)languageType) .IsExists()) { new Competitor { CompetitoruniqueID = firstElement.CompetitoruniqueID, SportType = sportType, Datecreatedutc = DateTime.UtcNow, Languagetype = languageType, Name = names[0], Gendertype = genderType }.Save(); } list.Each(el => el.Save()); return list; }) .MakeObject(); return competitors.Any() ? competitors.Select(c => new RawTemplateObj<CompetitorParsedTransport> { RawObject = { ID = c.ID }, Object = { LanguageType = languageType, SportType = sportType, GenderType = genderType, ID = c.CompetitoruniqueID } }).First() : new RawTemplateObj<CompetitorParsedTransport>(); }, new RawTemplateObj<CompetitorParsedTransport>()); }
public RawTemplateObj<CompetitionSpecifyTransport> GetCompetitionSpecify(ProcessStat competitionStat, ProcessStat competitionSpecifyStat, BrokerType brokerType, LanguageType language, SportType sportType, string[] nameOrigin, CompetitionParsed competitionToSave, GatherBehaviorMode algoMode) { return InvokeSafeSingleCall(() => { var genderDetected = GenderDetectorHelper.Instance[nameOrigin]; if (genderDetected == GenderType.Unknown) { _logger.Error("{0}: {1}", nameOrigin, genderDetected); } nameOrigin = CleanCompetitionName(nameOrigin); var rawCompetitionSpecify = new BrokerEntityBuilder<RawCompetitionSpecify>(competitionSpecifyStat) .SetupValidateObject(specify => true/*NOTE!! && specify.CompetitionSpecifyUniqueID != default(int)*/) .SetupGetRaw(() => RawCompetitionHelper.GetRawCompetitionSpecify[new RawCompetitionHelper.RawCompetitorSpecifyKey(brokerType, language, sportType, genderDetected, nameOrigin)]) .SetupCreateRaw(() => RawCompetitionHelper.CreateCompetitionSpecify(competitionStat, brokerType, language, sportType, genderDetected, nameOrigin, competitionToSave, algoMode)) .SetupCreateOriginal(algoMode, specify => { if (specify.CompetitionSpecifyUniqueID == default(int)) { var competitionSpecifyUnique = new CompetitionSpecifyUnique { CompetitionuniqueID = specify.CompetitionuniqueID }; competitionSpecifyUnique.Save(); specify.CompetitionSpecifyUniqueID = competitionSpecifyUnique.ID; specify.Save(); } var competitionSpecify = new CompetitionSpecify { Datecreatedutc = DateTime.UtcNow, Languagetype = language, SportType = sportType, Name = CompetitionHelper.ListStringToName(nameOrigin), Gendertype = genderDetected, CompetitionuniqueID = specify.CompetitionuniqueID }; specify.CompetitionSpecifyUniqueID = competitionSpecify.CompetitionSpecifyUniqueID; if (competitionSpecify.CompetitionuniqueID != specify.CompetitionuniqueID) { _logger.Error("{0} != {1}. {2}. SKIP", competitionSpecify.CompetitionuniqueID, specify.CompetitionuniqueID, CompetitionHelper.ListStringToName(nameOrigin)); return null; } specify.Linkstatus = LinkEntityStatus.Original | LinkEntityStatus.Linked; return specify; }) .SetupFinally(specify => { if (specify.CompetitionSpecifyUniqueID != default(int)) { if (algoMode.HasFlag(GatherBehaviorMode.CreateNewLanguageName)) { if (specify.CompetitionSpecifyUniqueID != default(int) && specify.CompetitionuniqueID != default(int) && !CompetitionSpecify.DataSource .WhereEquals(CompetitionSpecify.Fields.CompetitionSpecifyUniqueID, specify.CompetitionSpecifyUniqueID) .FilterByLanguage(language) .IsExists()) { new CompetitionSpecify { Languagetype = language, Name = CompetitionHelper.ListStringToName(nameOrigin), CompetitionSpecifyUniqueID = specify.CompetitionSpecifyUniqueID, CompetitionuniqueID = specify.CompetitionuniqueID, Datecreatedutc = DateTime.UtcNow, Gendertype = genderDetected, SportType = sportType }.Save(); } } } specify.Save(); return specify; }) .MakeObject(); return RawCompetitionHelper.CreateCompetitionSpecifyRawObject(rawCompetitionSpecify.ID, rawCompetitionSpecify.RawCompetitionID, rawCompetitionSpecify, language, sportType, genderDetected); }, new RawTemplateObj<CompetitionSpecifyTransport>()); }
public CompetitionItemRawTransport GetCompetitionItem(ProcessStat competitorStat, BrokerType brokerType, RawTemplateObj<CompetitorParsedTransport>[] competitors, RawTemplateObj<CompetitionSpecifyTransport> competitionSpecifyTransport, DateTime eventDateUtc, GatherBehaviorMode algoMode, BrokerCompetitionSettings brokerSettings) { return InvokeSafeSingleCall(() => { var utcNow = DateTime.UtcNow; if (eventDateUtc > utcNow.AddDays(14) || competitors.Any(c => c.RawObject.ID == default(int))) { return null; } var competitionItemRaw = new BrokerEntityBuilder<RawCompetitionItem>(competitorStat) .SetupValidateObject(item => item.CompetitionitemID != default(int)) .SetupGetRaw(() => RawCompetitionItemHelper.GetCompetitionItem(brokerType, competitors, competitionSpecifyTransport, eventDateUtc, utcNow)) .SetupCreateRaw(() => RawCompetitionItemHelper.CreateCompetitionItem(brokerType, competitors, competitionSpecifyTransport, eventDateUtc, utcNow)) .SetupTryMatchRaw(GatherBehaviorMode.TryDetectAll, item => { /*brokerSettings*/ var ciDs = CompetitionItem.DataSource .Where(new DaoFilterOr( new DaoFilterAnd( new DaoFilterEq(CompetitionItem.Fields.Competitoruniqueid1, competitors[0].Object.ID), new DaoFilterEq(CompetitionItem.Fields.Competitoruniqueid2, competitors[1].Object.ID) ), new DaoFilterAnd( new DaoFilterEq(CompetitionItem.Fields.Competitoruniqueid2, competitors[0].Object.ID), new DaoFilterEq(CompetitionItem.Fields.Competitoruniqueid1, competitors[1].Object.ID) ) )) .WhereEquals(CompetitionItem.Fields.Sporttype, (short)competitionSpecifyTransport.Object.SportType) .Where(new DaoFilterOr( new DaoFilterNull(CompetitionItem.Fields.CompetitionSpecifyUniqueID, true), new DaoFilterEq(CompetitionItem.Fields.CompetitionSpecifyUniqueID, competitionSpecifyTransport.Object.ID))) .Where(eventDateUtc > DateTime.MinValue ? new DaoFilterAnd( new DaoFilter(CompetitionItem.Fields.Dateeventutc, Oper.GreaterOrEq, eventDateUtc.AddDays(-0.7)), new DaoFilter(CompetitionItem.Fields.Dateeventutc, Oper.LessOrEq, eventDateUtc.AddDays(0.7)) ) : new DaoFilterAnd( new DaoFilter(CompetitionItem.Fields.Dateeventutc, Oper.GreaterOrEq, utcNow.AddDays(-0.7)), new DaoFilter(CompetitionItem.Fields.Dateeventutc, Oper.LessOrEq, utcNow.AddDays(0.7)) ) ) .Sort(CompetitionItem.Fields.ID, SortDirection.Desc); if (!brokerSettings.HasFlag(BrokerCompetitionSettings.NoCompareCompetitionUnique)) { ciDs = ciDs .WhereEquals(CompetitionItem.Fields.CompetitionuniqueID, competitionSpecifyTransport.Object.CompetitionUniqueID); } var ci = ciDs.First(CompetitionItem.Fields.ID); if (ci != null) { item.CompetitionitemID = ci.ID; item.Linkstatus = LinkEntityStatus.LinkByStatistics | LinkEntityStatus.Linked; } return item; }) .SetupCreateOriginal(algoMode, item => { CreateCompetitionItem(competitors, competitionSpecifyTransport, eventDateUtc, utcNow, item); return item; }) .SetupFinally(item => { if (algoMode.HasFlag(GatherBehaviorMode.CreateOriginalIfMatchedAll) && item.CompetitionitemID == default(int) && competitionSpecifyTransport.Object.CompetitionUniqueID != default(int) && //NOTE competitionSpecifyTransport.Object.CompetitionSpecifyUniqueID != default(int) && competitors[0].Object.ID != default(int) && competitors[1].Object.ID != default(int)) { CreateCompetitionItem(competitors, competitionSpecifyTransport, eventDateUtc, utcNow, item); } item.Save(); return item; }) .MakeObject(); if (competitionItemRaw.CompetitionitemID == default(int)) { return null; } var competitionItem = CompetitionItem.DataSource.GetByKey(competitionItemRaw.CompetitionitemID, CompetitionItem.Fields.ID, CompetitionItem.Fields.Dateeventutc, CompetitionItem.Fields.Competitoruniqueid1, CompetitionItem.Fields.Competitoruniqueid2, CompetitionItem.Fields.CompetitionSpecifyUniqueID); if (competitionSpecifyTransport.Object.ID != default(int) && competitionItem.CompetitionSpecifyUniqueID == default(int)) { competitionItem.CompetitionSpecifyUniqueID = competitionSpecifyTransport.Object.ID; competitionItem.Save(); } if (eventDateUtc > DateTime.MinValue && ((algoMode & (GatherBehaviorMode.CreateOriginal | GatherBehaviorMode.CreateOriginalIfMatchedAll)) != 0)) { if (Math.Abs((competitionItem.Dateeventutc - eventDateUtc).TotalMinutes) > 5) { competitionItem.Dateeventutc = eventDateUtc; competitionItem.Save(); } } return new CompetitionItemRawTransport { CompetitionItemID = competitionItem.Competitoruniqueid1 == competitors[0].Object.ID && competitionItem.Competitoruniqueid2 == competitors[1].Object.ID ? competitionItem.ID : -competitionItem.ID, RawCompetitionItemID = competitionItemRaw.ID }; }, null); }
public static RawCompetitionSpecify CreateCompetitionSpecify(ProcessStat competitionStat, BrokerType brokerType, LanguageType language, SportType sportType, GenderType genderDetected, string[] nameOrigin, CompetitionParsed competitionToSave, GatherBehaviorMode algoMode) { var nameOriginShort = CompetitionHelper.GetShortCompetitionName(nameOrigin, sportType); var competitionRaw = new BrokerEntityBuilder<RawCompetition>(competitionStat) .SetupValidateObject(rawCompetition => rawCompetition.CompetitionuniqueID != default(int)) .SetupGetRaw(() => { return RawCompetition.DataSource.FilterByBroker(brokerType).FilterByLanguage(language).FilterBySportType(sportType) .FilterByNameCompetition(nameOriginShort) .FilterByGender(genderDetected, RawCompetition.Fields.CompetitionuniqueID, RawCompetition.Fields.Linkstatus).FirstOrDefault(); }) .SetupCreateRaw(() => BrokerEntityIfaceCreator.CreateEntity<RawCompetition>(brokerType, language, sportType, genderDetected, LinkEntityStatus.Unlinked, nameOriginShort)) .SetupCreateOriginal(algoMode, rawCompetition => { var competition = Competition.DataSource.FilterByLanguage(language).FilterBySportType(sportType) .FilterByNameCompetition(nameOriginShort) .FilterByGender(genderDetected, Competition.Fields.CompetitionuniqueID) .FirstOrDefault(); if (competition == null) { var competitionUnique = new CompetitionUnique { IsUsed = true }; competitionUnique.Save(); competition = new Competition { Datecreatedutc = DateTime.UtcNow, Languagetype = language, SportType = sportType, Name = CompetitionHelper.ListStringToName(nameOriginShort), Gendertype = genderDetected, CompetitionuniqueID = competitionUnique.ID }; competition.Save(); } rawCompetition.CompetitionuniqueID = competition.CompetitionuniqueID; rawCompetition.Linkstatus = LinkEntityStatus.Original | LinkEntityStatus.Linked; return rawCompetition; }) .SetupFinally(rawCompetition => { if (algoMode.HasFlag(GatherBehaviorMode.CreateNewLanguageName)) { if (rawCompetition.CompetitionuniqueID != default(int) && !Competition.DataSource .WhereEquals(Competition.Fields.CompetitionuniqueID, rawCompetition.CompetitionuniqueID) .FilterByLanguage(language) .IsExists()) { new Competition { CompetitionuniqueID = rawCompetition.CompetitionuniqueID, Datecreatedutc = DateTime.UtcNow, Gendertype = genderDetected, SportType = sportType, Name = CompetitionHelper.ListStringToName(nameOriginShort), Languagetype = language }.Save(); } } rawCompetition.Save(); return rawCompetition; }) .MakeObject(); return BrokerEntityIfaceCreator.CreateEntity<RawCompetitionSpecify>(brokerType, language, sportType, genderDetected, LinkEntityStatus.Unlinked, nameOrigin, specify => { specify.RawCompetitionID = competitionRaw.ID; if (specify.CompetitionuniqueID == default(int) && competitionRaw.CompetitionuniqueID != default(int)) { specify.CompetitionuniqueID = competitionRaw.CompetitionuniqueID; } }); }