public Task <AvgMinMax> getAvgMinMax(ConcurrentBag <ExchangeRate> list) { ConcurrentDictionary <string, double> dictionary = new ConcurrentDictionary <string, double>(); double sumValues = 0; List <Task> TaskList = new List <Task>(); Parallel.ForEach(list, item => { if (item.rates.Count == 1 && !dictionary.ContainsKey(item.rates.Keys.First())) { double value = item.rates[item.rates.Keys.First()][item.rates[item.rates.Keys.First()].Keys.First()]; sumValues += value; dictionary.TryAdd(item.rates.Keys.First(), value); } }); Task <AvgMinMax> avgMinMax = Task.FromResult(new AvgMinMax()); if (dictionary.Keys.Count > 0) { avgMinMax.Result.avg = Math.Round(sumValues / list.Count, 12, MidpointRounding.AwayFromZero); avgMinMax.Result.dateMin = dictionary.Aggregate((l, r) => l.Value < r.Value ? l : r).Key; avgMinMax.Result.min = dictionary[avgMinMax.Result.dateMin]; avgMinMax.Result.dateMax = dictionary.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; avgMinMax.Result.max = dictionary[avgMinMax.Result.dateMax]; } return(avgMinMax); }
private string PerformGetAll(GetAllCommand command) { if (!_storage.IsEmpty) { return(_storage.Aggregate("", (current, keyValue) => current + ($"\nKey: {keyValue.Key}" + " " + $"Value: {keyValue.Value.Display()}"))); } // TODO: add error handling return("The storage is empty"); }
private string NewStra(string pInst1, string pInst2, DirectionType pDire, OffsetType pOffset, double pPrice, double pRate1, double pRate2, int pVol1, int pVol2, bool pIsMarket) { Stra stra = new Stra { StraID = _maxStraID++.ToString(CultureInfo.InvariantCulture), Instrument1 = pInst1, Instrument2 = pInst2, Direction = pDire, Offset = pOffset, Price = pPrice, Rate1 = pRate1, Rate2 = pRate2, Volume1 = pVol1, Volume2 = pVol2, Status = ArbStatus.NotTouch, IsMarket = pIsMarket, }; if (_dicStra.TryAdd(stra.StraID, stra)) { DataRow dr = _dt.NewRow(); foreach (FieldInfo fi in typeof(Stra).GetFields()) { dr[fi.Name] = fi.GetValue(stra); } _dt.Rows.Add(dr); string txt = _dicStra.Aggregate(string.Empty, (current1, vi) => vi.GetType().GetFields().Aggregate(current1, (current, fi) => current + (fi.GetValue(vi) + ",")).TrimEnd(',') + "\r\n"); File.WriteAllText(this.GetType().Name + ".txt", txt); } return(stra.StraID); }
public static string DisplayDocuments() { return(Documents.Aggregate(string.Empty, (current, kvp) => current + $"PartionId: {kvp.Value}{Environment.NewLine}{string.Join(Environment.NewLine, kvp.Value.Select(doc => JsonConvert.SerializeObject(doc)))}" )); }
/// <inheritdoc /> public void Load() { IEnumerable <TypeInfo> behaviors = ReflectionHelper.GetClassesAssignableFrom <IBehavior>(); foreach (var type in behaviors) { IEnumerable <BehaviorAttribute> behaviorAttributes = type.GetCustomAttributes <BehaviorAttribute>(); if (behaviorAttributes != null) { foreach (BehaviorAttribute attribute in behaviorAttributes) { if (!_behaviors.TryGetValue(attribute.Type, out IList <BehaviorEntryCache> behaviorsCache)) { behaviorsCache = new List <BehaviorEntryCache>(); _behaviors.TryAdd(attribute.Type, behaviorsCache); } if (attribute.IsDefault) { if (behaviorsCache.Any(x => x.IsDefault)) { throw new InvalidOperationException($"{attribute.Type} already has a default behavior."); } var entityBehavior = new BehaviorEntryCache(type, true); behaviorsCache.Add(entityBehavior); } else { if (behaviorsCache.Any(x => x.MoverId == attribute.MoverId)) { _logger.LogWarning($"Behavior for mover id {attribute.MoverId} and type {type} is already set."); continue; } var entityBehavior = new BehaviorEntryCache(type, false, attribute.MoverId); behaviorsCache.Add(entityBehavior); } } } } foreach (var behaviorsForType in _behaviors) { if (!behaviorsForType.Value.Any(x => x.IsDefault)) { throw new InvalidProgramException($"{behaviorsForType.Key}"); } } Count = _behaviors.Aggregate(0, (current, next) => current + next.Value.Count); _logger.LogInformation("-> {0} behaviors loaded.", Count); }
/// <summary> /// Get Top Emoji /// </summary> /// <returns></returns> public string GetTopItemByCategory(ConcurrentDictionary <string, int> conDict) { string result = " "; if (conDict.Count() > 0) { result = conDict.Aggregate((x, y) => x.Value > y.Value ? x : y).Key; } return(result); }
public virtual Task <string> GetAsync() { if (RemotePaths.Values.All(item => item < 0)) { throw new RemotePathExhaustedException(this); } return(Task.FromResult( RemotePaths.Aggregate((acc, item) => acc.Value < item.Value ? item : acc).Key)); }
public async Task <HttpResponseMessage> Count() { // For each partition client, keep track of partition information and the number of words ConcurrentDictionary <Int64RangePartitionInformation, long> totals = new ConcurrentDictionary <Int64RangePartitionInformation, long>(); IList <Task> tasks = new List <Task>(); foreach (Int64RangePartitionInformation partition in await this.GetServicePartitionKeysAsync()) { try { ServicePartitionClient <HttpCommunicationClient> partitionClient = new ServicePartitionClient <HttpCommunicationClient>(communicationFactory, serviceUri, new ServicePartitionKey(partition.LowKey)); await partitionClient.InvokeWithRetryAsync( async (client) => { HttpResponseMessage response = await client.HttpClient.GetAsync(new Uri(client.Url, "Count")); string content = await response.Content.ReadAsStringAsync(); totals[partition] = Int64.Parse(content.Trim()); }); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } } var total = totals.Aggregate <KeyValuePair <Int64RangePartitionInformation, long>, long>(0, (sum, next) => next.Value + sum); StringBuilder sb = new StringBuilder(); sb.Append("<h1> Total:"); sb.AppendFormat("{0} ({1})", total, ArabicRomanConverter.ToRoman(total)); sb.Append("</h1>"); sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>"); foreach (KeyValuePair <Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { sb.Append("<tr><td>"); sb.Append(partitionData.Key.Id); sb.Append("</td><td>"); sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey); sb.Append("</td><td>"); sb.Append(partitionData.Value); sb.Append("</td></tr>"); } sb.Append("</table>"); return(new HttpResponseMessage() { Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html") }); }
public async Task<HttpResponseMessage> Count() { // For each partition client, keep track of partition information and the number of words ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>(); IList<Task> tasks = new List<Task>(); foreach (Int64RangePartitionInformation partition in await this.GetServicePartitionKeysAsync()) { try { ServicePartitionClient<HttpCommunicationClient> partitionClient = new ServicePartitionClient<HttpCommunicationClient>(communicationFactory, serviceUri, new ServicePartitionKey(partition.LowKey)); await partitionClient.InvokeWithRetryAsync( async (client) => { HttpResponseMessage response = await client.HttpClient.GetAsync(new Uri(client.Url, "Count")); string content = await response.Content.ReadAsStringAsync(); totals[partition] = Int64.Parse(content.Trim()); }); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } } StringBuilder sb = new StringBuilder(); sb.Append("<h1> Total:"); sb.Append(totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total)); sb.Append("</h1>"); sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>"); foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { sb.Append("<tr><td>"); sb.Append(partitionData.Key.Id); sb.Append("</td><td>"); sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey); sb.Append("</td><td>"); sb.Append(partitionData.Value); sb.Append("</td></tr>"); } sb.Append("</table>"); return new HttpResponseMessage() { Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html") }; }
public string GetStatistics() { var stats = new StringBuilder(); int length = m_ProcessingGroups.Keys.Max(k => k.Length); m_ProcessingGroups.Aggregate(stats, (builder, pair) => builder.AppendFormat( "{0,-" + length + "}\tConcurrencyLevel:{1:-10}\tSent:{2}\tReceived:{3}\tProcessed:{4}" + Environment.NewLine, pair.Key, pair.Value.ConcurrencyLevel == 0 ? "[current thread]" : pair.Value.ConcurrencyLevel.ToString(), pair.Value.SentMessages, pair.Value.ReceivedMessages, pair.Value.ProcessedMessages)); return(stats.ToString()); }
public async Task <HitsCountResponse> GetHitsCount() { // Get the list of representative service partition clients. var partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words var totals = new ConcurrentDictionary <Int64RangePartitionInformation, Task <long> >(); IList <Task> tasks = new List <Task>(partitionClients.Count); foreach (var partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. Task <long> tt = partitionClient.beanCache.GetHitsCount(); tasks.Add(tt); totals[partitionClient.part as Int64RangePartitionInformation] = tt; } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.Message(ex.Message, "Count - run web request"); throw ex; } var response = new HitsCountResponse(); response.Total = totals.Aggregate(0, (total, next) => (int)next.Value.Result + total); foreach (var partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { var cachInfo = new CacheInfo(); cachInfo.Id = partitionData.Key.Id; cachInfo.LowKey = partitionData.Key.LowKey; cachInfo.HighKey = partitionData.Key.HighKey; cachInfo.Hits = partitionData.Value.Result; response.CacheInfo.Add(cachInfo); } return(response); }
private EmbedBuilder GetEmbed() { var i = 0; return phase == AcroPhase.Submitting ? new EmbedBuilder().WithOkColor() .WithTitle(GetText("acrophobia")) .WithDescription(GetText("acro_started", Format.Bold(string.Join(".", _startingLetters)))) .WithFooter(efb => efb.WithText(GetText("acro_started_footer", _time))) : new EmbedBuilder() .WithOkColor() .WithTitle(GetText("acrophobia") + " - " + GetText("submissions_closed")) .WithDescription(GetText("acro_nym_was", Format.Bold(string.Join(".", _startingLetters)) + "\n" + $@"-- {_submissions.Aggregate("",(agg, cur) => agg + $"`{++i}.` **{cur.Key.ToLowerInvariant().ToTitleCase()}**\n")} --")) .WithFooter(efb => efb.WithText(GetText("acro_vote"))); }
public KeyValuePair<IEnumerable<int>, double> Reproduction() { // Normalize, accumulate and order fitness by descending var fitnessSum = population.Sum(pair => pair.Value); var accumulator = 0.0; var roulette = (from pair in population orderby pair.Value descending select new { Chromosome = pair.Key, Fitness = pair.Value, AccumulatudFitness = (accumulator += pair.Value / fitnessSum) }).ToArray(); // Count of children pairs to be produced var childrenPairs = (int)(populationSize * (1 - elitismRate) / 2); // Take count of elite chromosomes population = new ConcurrentDictionary<IEnumerable<int>, double>(roulette.Take(populationSize - childrenPairs * 2).ToDictionary(pair => pair.Chromosome, pair => pair.Fitness)); // Produce children and fill population with them Parallel.For(0, childrenPairs, _ => { foreach (var child in from chromosome in Crossover(roulette.First(pair => pair.AccumulatudFitness >= random.NextDouble()).Chromosome, roulette.First(pair => pair.AccumulatudFitness >= random.NextDouble()).Chromosome) select Mutation(chromosome)) population[child] = Fitness(child); }); // Return fittest cluster return population.Aggregate((max, next) => next.Value > max.Value ? next : max); }
protected override void OnHandleDestroyed(EventArgs e) { _timer.Stop(); string txt = _dicStra.Aggregate(string.Empty, (current1, vi) => vi.GetType().GetFields().Aggregate(current1, (current, fi) => current + (fi.GetValue(vi) + ",")).TrimEnd(',') + "\r\n"); File.WriteAllText(this.GetType().Name + ".txt", txt); File.WriteAllText(_file, _config.Aggregate(txt, (current, v) => current + (v.Key + "," + v.Value + "\r\n"))); if (_q != null) { _q.OnRtnTick -= _q_OnRtnTick; //行情触发 } _t.OnRtnCancel -= _t_OnRtnCancel; _t.OnRtnError -= _t_OnRtnError; _t.OnRtnOrder -= _t_OnRtnOrder; _t.OnRtnTrade -= _t_OnRtnTrade; base.OnHandleDestroyed(e); }
private static void Run(int iterations, Bitmap newImage, string fileName, string fileDirectory, int numberOfPointsToPlot) { ReadonlyBitmap.Set(newImage); var nums = Enumerable.Range(0, iterations).ToArray(); var result = new ConcurrentDictionary <VoronoiOutput, double>(); Parallel.ForEach(nums, _ => { var voronoiOutput = Fortunes.Run(ReadonlyBitmap.Get().Width, ReadonlyBitmap.Get().Height, numberOfPointsToPlot); var averageDeltaE = voronoiOutput.CalculateAccuracy(); result.TryAdd(voronoiOutput, averageDeltaE); }); var bestVoronoi = result.Aggregate((l, r) => l.Value < r.Value ? l : r).Key; using (var writer = new Drawer(newImage)) { writer.DrawVoronoi(bestVoronoi); writer.SaveToNewImageFile(fileName, fileDirectory); } }
public KeyValuePair <IEnumerable <int>, double> Reproduction() { // Normalize, accumulate and order fitness by descending var fitnessSum = population.Sum(pair => pair.Value); var accumulator = 0.0; var roulette = (from pair in population orderby pair.Value descending select new { Chromosome = pair.Key, Fitness = pair.Value, AccumulatudFitness = (accumulator += pair.Value / fitnessSum) }).ToArray(); // Count of children pairs to be produced var childrenPairs = (int)(populationSize * (1 - elitismRate) / 2); // Take count of elite chromosomes population = new ConcurrentDictionary <IEnumerable <int>, double>(roulette.Take(populationSize - childrenPairs * 2).ToDictionary(pair => pair.Chromosome, pair => pair.Fitness)); // Produce children and fill population with them Parallel.For(0, childrenPairs, _ => { foreach (var child in from chromosome in Crossover(roulette.First(pair => pair.AccumulatudFitness >= random.NextDouble()).Chromosome, roulette.First(pair => pair.AccumulatudFitness >= random.NextDouble()).Chromosome) select Mutation(chromosome)) { population[child] = Fitness(child); } }); // Return fittest cluster return(population.Aggregate((max, next) => next.Value > max.Value ? next : max)); }
public async Task<HttpResponseMessage> Count() { // Get the list of representative service partition clients. IList<ServicePartitionClient<CommunicationClient>> partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>(); IList<Task> tasks = new List<Task>(partitionClients.Count); foreach (ServicePartitionClient<CommunicationClient> partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. tasks.Add( partitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, "Count"); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "GET"; request.Timeout = (int) client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int) client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim()); } return Task.FromResult(true); })); } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } StringBuilder sb = new StringBuilder(); sb.Append("<h1> Total:"); sb.Append(totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total)); sb.Append("</h1>"); sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>"); foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { sb.Append("<tr><td>"); sb.Append(partitionData.Key.Id); sb.Append("</td><td>"); sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey); sb.Append("</td><td>"); sb.Append(partitionData.Value); sb.Append("</td></tr>"); } sb.Append("</table>"); HttpResponseMessage message = new HttpResponseMessage(); message.Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html"); return message; }
public async Task<CountResponse> Count() { // Get the list of representative service partition clients. IList<ServicePartitionClient<CommunicationClient>> partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>(); IList<Task> tasks = new List<Task>(partitionClients.Count); foreach (ServicePartitionClient<CommunicationClient> partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. tasks.Add( partitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, "Count"); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "GET"; request.Timeout = (int)client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim()); } return Task.FromResult(true); })); } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } var retVal = new CountResponse(); retVal.Total = totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total); foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { var info = new Info(); info.Id = partitionData.Key.Id; info.LowKey = partitionData.Key.LowKey; info.HighKey = partitionData.Key.HighKey; info.Hits = partitionData.Value; retVal.Infos.Add(info); } return retVal; }
public string AllProxyStatus() { return(_allProxy.Aggregate("all proxy info >>>> \n", (current, entry) => current + (entry.Value + "\n"))); }
// get the closest cluster pair (i.e., min cluster pair distance). it is also important to reduce computational time public ClusterNodePair GetClosestClusterPair() { return(distanceMatrix.Aggregate((target, x) => x.Value > target.Value ? target : x).Key); }
private async Task Results() { // whitespace (e.g. can change to '·' for debugging) var ws = ' '; var totalDuration = results.Aggregate( TimeSpan.Zero, (total, result) => total + (result.Value.Duration ?? result.Value.InputResults.Aggregate(TimeSpan.Zero, (inputTotal, input) => inputTotal + input.Duration))); var rows = new List <SummaryRow> { new SummaryRow { TargetOrInput = $"{p.Default}Target{p.Reset}", Outcome = $"{p.Default}Outcome{p.Reset}", Duration = $"{p.Default}Duration{p.Reset}", Percentage = "" } }; foreach (var item in results.OrderBy(i => i.Value.Ordinal)) { var target = $"{p.Target}{item.Key}{p.Reset}"; var outcome = item.Value.Outcome == TargetOutcome.Failed ? $"{p.Failed}Failed!{p.Reset}" : item.Value.Outcome == TargetOutcome.NoInputs ? $"{p.Warning}No inputs!{p.Reset}" : $"{p.Succeeded}Succeeded{p.Reset}"; var duration = $"{p.Timing}{ToString(item.Value.Duration, true)}{p.Reset}"; var percentage = item.Value.Duration.HasValue && totalDuration > TimeSpan.Zero ? $"{p.Timing}{100 * item.Value.Duration.Value.TotalMilliseconds / totalDuration.TotalMilliseconds:N1}%{p.Reset}" : ""; rows.Add(new SummaryRow { TargetOrInput = target, Outcome = outcome, Duration = duration, Percentage = percentage }); var index = 0; foreach (var result in item.Value.InputResults) { var input = $"{ws}{ws}{p.Input}{result.Input}{p.Reset}"; var inputOutcome = result.Outcome == TargetInputOutcome.Failed ? $"{p.Failed}Failed!{p.Reset}" : $"{p.Succeeded}Succeeded{p.Reset}"; var inputDuration = $"{(index < item.Value.InputResults.Count - 1 ? p.TreeFork : p.TreeCorner)}{p.Timing}{ToString(result.Duration, true)}{p.Reset}"; var inputPercentage = totalDuration > TimeSpan.Zero ? $"{(index < item.Value.InputResults.Count - 1 ? p.TreeFork : p.TreeCorner)}{p.Timing}{100 * result.Duration.TotalMilliseconds / totalDuration.TotalMilliseconds:N1}%{p.Reset}" : ""; rows.Add(new SummaryRow { TargetOrInput = input, Outcome = inputOutcome, Duration = inputDuration, Percentage = inputPercentage }); ++index; } } // target or input column width var tarW = rows.Max(row => Palette.StripColours(row.TargetOrInput).Length); // outcome column width var outW = rows.Max(row => Palette.StripColours(row.Outcome).Length); // duration column width var durW = rows.Count > 1 ? rows.Skip(1).Max(row => Palette.StripColours(row.Duration).Length) : 0; // percentage column width var perW = rows.Max(row => Palette.StripColours(row.Percentage).Length); // timing column width (duration and percentage) var timW = Max(Palette.StripColours(rows[0].Duration).Length, durW + 2 + perW); // expand percentage column width to ensure time and percentage are as wide as duration perW = Max(timW - durW - 2, perW); // summary start separator await this.writer.WriteLineAsync($"{GetPrefix()}{p.Default}{"".Prp(tarW + 2 + outW + 2 + timW, p.Dash)}{p.Reset}").Tax(); // header await this.writer.WriteLineAsync($"{GetPrefix()}{rows[0].TargetOrInput.Prp(tarW, ws)}{ws}{ws}{rows[0].Outcome.Prp(outW, ws)}{ws}{ws}{rows[0].Duration.Prp(timW, ws)}").Tax(); // header separator await this.writer.WriteLineAsync($"{GetPrefix()}{p.Default}{"".Prp(tarW, p.Dash)}{p.Reset}{ws}{ws}{p.Default}{"".Prp(outW, p.Dash)}{p.Reset}{ws}{ws}{p.Default}{"".Prp(timW, p.Dash)}{p.Reset}").Tax(); // targets foreach (var row in rows.Skip(1)) { await this.writer.WriteLineAsync($"{GetPrefix()}{row.TargetOrInput.Prp(tarW, ws)}{p.Reset}{ws}{ws}{row.Outcome.Prp(outW, ws)}{p.Reset}{ws}{ws}{row.Duration.Prp(durW, ws)}{p.Reset}{ws}{ws}{row.Percentage.Prp(perW, ws)}{p.Reset}").Tax(); } // summary end separator await this.writer.WriteLineAsync($"{GetPrefix()}{p.Default}{"".Prp(tarW + 2 + outW + 2 + timW, p.Dash)}{p.Reset}").Tax(); }
public async Task <HttpResponseMessage> Count() { // Get the list of representative service partition clients. IList <ServicePartitionClient <CommunicationClient> > partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words ConcurrentDictionary <Int64RangePartitionInformation, long> totals = new ConcurrentDictionary <Int64RangePartitionInformation, long>(); IList <Task> tasks = new List <Task>(partitionClients.Count); foreach (ServicePartitionClient <CommunicationClient> partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. tasks.Add( partitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, "Count"); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "GET"; request.Timeout = (int)client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim()); } return(Task.FromResult(true)); })); } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } StringBuilder sb = new StringBuilder(); sb.Append("<h1> Total:"); sb.Append(totals.Aggregate <KeyValuePair <Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total)); sb.Append("</h1>"); sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>"); foreach (KeyValuePair <Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { sb.Append("<tr><td>"); sb.Append(partitionData.Key.Id); sb.Append("</td><td>"); sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey); sb.Append("</td><td>"); sb.Append(partitionData.Value); sb.Append("</td></tr>"); } sb.Append("</table>"); HttpResponseMessage message = new HttpResponseMessage(); message.Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html"); return(message); }
public async Task <CountResponse> Count() { // Get the list of representative service partition clients. IList <ServicePartitionClient <CommunicationClient> > partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words ConcurrentDictionary <Int64RangePartitionInformation, long> totals = new ConcurrentDictionary <Int64RangePartitionInformation, long>(); IList <Task> tasks = new List <Task>(partitionClients.Count); foreach (ServicePartitionClient <CommunicationClient> partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. tasks.Add( partitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, "Count"); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "GET"; request.Timeout = (int)client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim()); } return(Task.FromResult(true)); })); } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } var retVal = new CountResponse(); retVal.Total = totals.Aggregate <KeyValuePair <Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total); foreach (KeyValuePair <Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { var info = new Info(); info.Id = partitionData.Key.Id; info.LowKey = partitionData.Key.LowKey; info.HighKey = partitionData.Key.HighKey; info.Hits = partitionData.Value; retVal.Infos.Add(info); } return(retVal); }
static void Main(string[] args) { var input = System.IO.File.ReadAllText("input.txt") .Split("\r\n") .ToImmutableList(); var memV1 = new ConcurrentDictionary <ulong, ulong>(); var memV2 = new ConcurrentDictionary <ulong, ulong>(); var mask = ""; foreach (var instr in input) { var instrSplit = instr.Split("=").Select(s => s.Trim()).ToList(); if (instrSplit[0].StartsWith("mask")) { mask = instrSplit[1]; } if (instrSplit[0].StartsWith("mem")) { var addr = ulong.Parse(instrSplit[0].Replace("mem[", "").Replace("]", "")); var value = ulong.Parse(instrSplit[1]); var maskedValue = applyMask(value, mask); memV1.AddOrUpdate(addr, maskedValue, (_, _) => maskedValue); Parallel.ForEach(applyMaskV2(addr, mask, 0), (newAddr) => { memV2.AddOrUpdate(newAddr, value, (_, _) => value); }); } } Console.WriteLine($"{memV1.Aggregate((ulong)0, (ulong s, KeyValuePair<ulong, ulong> kv) => s += kv.Value)}"); Console.WriteLine($"{memV2.Aggregate((ulong)0, (ulong s, KeyValuePair<ulong, ulong> kv) => s += kv.Value)}"); ulong applyMask(ulong no, string mask) { var result = no; for (int i = 0; i < mask.Length; i++) { result = mask[mask.Length - 1 - i] switch { '1' => result |= ((ulong)1 << i), '0' => result &= ~((ulong)1 << i), _ => result }; } return(result); } IEnumerable <ulong> applyMaskV2(ulong no, string mask, int idx) { if (idx == mask.Length) { return(new List <ulong> { 0 }); } var result = new List <ulong>(); var resultInt = applyMaskV2(no, mask, idx + 1); foreach (var partialResultValue in resultInt) { var r = mask[mask.Length - 1 - idx] switch { '0' => new[] { partialResultValue | (no & ((ulong)1 << idx)) }, '1' => new[] { partialResultValue | ((ulong)1 << idx) }, 'X' => new[] { partialResultValue, partialResultValue | ((ulong)1 << idx) }, _ => new ulong[] { } }; result.AddRange(r); } return(result); } } } }
public async Task<HitsCountResponse> GetHitsCount() { // Get the list of representative service partition clients. var partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words var totals = new ConcurrentDictionary<Int64RangePartitionInformation, Task<long>>(); IList<Task> tasks = new List<Task>(partitionClients.Count); foreach (var partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. Task<long> tt = partitionClient.beanCache.GetHitsCount(); tasks.Add(tt); totals[partitionClient.part as Int64RangePartitionInformation] = tt; } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.Message(ex.Message, "Count - run web request"); throw ex; } var response = new HitsCountResponse(); response.Total = totals.Aggregate(0, (total, next) => (int)next.Value.Result + total); foreach (var partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { var cachInfo = new CacheInfo(); cachInfo.Id = partitionData.Key.Id; cachInfo.LowKey = partitionData.Key.LowKey; cachInfo.HighKey = partitionData.Key.HighKey; cachInfo.Hits = partitionData.Value.Result; response.CacheInfo.Add(cachInfo); } return response; }
/// <summary> /// Get the text body with html code. /// </summary> /// <param name="wordsCategory">Each used words and category.</param> /// <returns></returns> private string GetParsedBody(List <WordCategory> wordsCategory) { // this pattern is for all the characters we want to split the text into. // this is not exactly the same as the engine // but it should still split all the words. const string patternAll = @"[\s\p{P}\p{Z}\p{S}\p{C}\p{No}]"; // the regex we will be using over and over... var regex = new Regex(patternAll); // this is to split everything, without actually removing the tags. // that way we can rebuild the entire text. var pattern = $@"(?<={patternAll}+)"; var parts = Regex.Split(_rawText, pattern).ToList(); var guids = new Dictionary <string, string>(); var raws = new ConcurrentDictionary <string, string>(); // the categories var categories = _categories?.GetCategories() ?? new Dictionary <int, string>(); // the list of tasks. var tasks = new List <Task>(wordsCategory.Count); foreach (var wordCategory in wordsCategory) { tasks.Add( Task.Run(() => { // get akk the unique words we want to replace. var words = parts.FindAll(word => regex.Replace(word, " ").Trim() == wordCategory.Word) .Distinct(); foreach (var word in words) { // the category id probability and 'clean' word. var categoryId = wordCategory.Category; var probability = wordCategory.Probability; var partClean = wordCategory.Word; // the class we will be using. var cr = $"cr{categoryId}"; // wrapp the text with a class to give it color. var guid = Guid.NewGuid().ToString(); var percent = (probability * 100).ToString(@"0.#0\%"); guids.Add(guid, $"<span class='{cr}' title='{categories[categoryId]}, {percent}'>{partClean}</span>"); var guidAndPart = word.Replace(partClean, guid); raws.TryAdd(word, guidAndPart); } }) ); } // wait for all. Task.WaitAll(tasks.ToArray()); // now create the htm where we replace the raw text // with all our guid. var html = raws.Aggregate(_rawText, (guid, guidAndPart) => guid.Replace(guidAndPart.Key, guidAndPart.Value)); // make sure that the text is html encoded so we don't display 'wrong' html // some emails risk to display things wrong, so it is best to escape everything. html = $@"<div class='text'>{WebUtility.HtmlEncode(html)}</div>"; // now we can replace all the guids with the actual code. html = guids.Aggregate(html, (current, guid) => current.Replace(guid.Key, guid.Value)); return(GetParagraph("Classified", html)); }
public string Replace(string original) { var newValue = original; //Check for DateTime token var regex = Regex.Matches(newValue, $@"({{[L,U]?{{)(DateTime[:,-,\s]?)([^}}]*)(}}}})"); foreach (Match match in regex) { var pattern = match.Groups[3].Value; var formattedDate = DateTime.Now.ToString(pattern); var capitalCheck = Regex.Match(match.Groups[1].Value, $@"({{)([L,U])({{)"); if (capitalCheck.Success) { if (capitalCheck.Groups[2].Value == "L") { formattedDate = formattedDate.ToLower(); } if (capitalCheck.Groups[2].Value == "U") { formattedDate = formattedDate.ToUpper(); } } ReplacementDictionary[match.Groups[0].Value] = formattedDate; } // Check if field tokens are available, if not run replacement with current dictionary entries and return result. if (Fields == null || Fields.Count < 1) { return(ReplacementDictionary.Aggregate(newValue, (current, item) => current.Replace(item.Key, item.Value))); } //Check if value contains a field token foreach (var field in Fields) { regex = Regex.Matches(newValue, $@"({{[L,U]?{{)(\[\d+\])?({field.FieldName})(\[\d+\])?({{.}})?({{\d}})?(\[\d+\])?(}}}})"); foreach (Match match in regex) { // value contains token for this field. var upper = false; var lower = false; var capitalCheck = Regex.Match(match.Groups[1].Value, $@"({{)([L,U])({{)"); if (capitalCheck.Success) { if (capitalCheck.Groups[2].Value == "L") { lower = true; } if (capitalCheck.Groups[2].Value == "U") { upper = true; } } var fSubstringCheck = Regex.Match(match.Groups[2].Value, $@"(\[\d+\])"); var fSubstring = false; Int32 fSub = 0; if (fSubstringCheck.Success) { if (Int32.TryParse(Regex.Match(match.Groups[2].Value, "\\d+").Value, out fSub)) { fSubstring = true; } } var bSubstringCheck = Regex.Match(match.Groups[7].Value, $@"(\[\d+\])"); var bSubstring = false; Int32 bSub = 0; if (!fSubstring && bSubstringCheck.Success) { if (Int32.TryParse(Regex.Match(match.Groups[7].Value, "\\d+").Value, out bSub)) { bSubstring = true; } } var fieldValue = field.FieldValues[0].Value; var fieldTypeName = fieldValue.GetType().ToString(); var multiValueCheck = Regex.Match(fieldTypeName, @"(.*)(`)(\d+)(\[)(.*)(\])"); var splitRegex = Regex.Match(match.Groups[5].Value, $@"({{)(.)(}})"); if (splitRegex.Success) { var splitIndexRegex = Regex.Match(match.Groups[6].Value, $@"({{)(\d+)(}})"); var splitBy = splitRegex.Groups[2].Value; Int32 spIndex; if (Int32.TryParse(splitIndexRegex.Groups[2].Value, out spIndex)) { if (multiValueCheck.Success && match.Groups[4].Value != "") { // Multi Value Field Int32 vIndex; if (!Int32.TryParse(Regex.Match(match.Groups[4].Value, "\\d+").Value, out vIndex)) { continue; } dynamic val = fieldValue; var strValue = val[vIndex].ToString(); if (!strValue.Contains(splitBy)) { continue; } var sp = strValue.Split(new[] { splitBy }, StringSplitOptions.None); if (sp.Length <= spIndex) { spIndex = sp.Length - 1; } if (upper) { sp[spIndex] = sp[spIndex].ToUpper(); } if (lower) { sp[spIndex] = sp[spIndex].ToLower(); } if (fSubstring) { if (fSub > sp[spIndex].Length) { fSub = sp[spIndex].Length; } sp[spIndex] = sp[spIndex].Substring(0, fSub); } if (bSubstring) { if (bSub > sp[spIndex].Length) { bSub = sp[spIndex].Length; } sp[spIndex] = sp[spIndex].Substring(sp[spIndex].Length - bSub, bSub); } ReplacementDictionary[match.Groups[0].Value] = sp[spIndex]; } else { // Single Value Field if (fieldTypeName == "System.String") { var strValue = fieldValue.ToString(); if (!strValue.Contains(splitBy)) { continue; } var sp = strValue.Split(new[] { splitBy }, StringSplitOptions.None); if (sp.Length <= spIndex) { spIndex = sp.Length - 1; } if (upper) { sp[spIndex] = sp[spIndex].ToUpper(); } if (lower) { sp[spIndex] = sp[spIndex].ToLower(); } if (fSubstring) { sp[spIndex] = sp[spIndex].Substring(0, fSub); } if (bSubstring) { sp[spIndex] = sp[spIndex].Substring(sp[spIndex].Length - bSub, bSub); } ReplacementDictionary[match.Groups[0].Value] = sp[spIndex]; } } } } else { if (multiValueCheck.Success && match.Groups[4].Value != "") { // Multi Value Field Int32 vIndex; if (!Int32.TryParse(Regex.Match(match.Groups[4].Value, "\\d+").Value, out vIndex)) { continue; } dynamic val = fieldValue; var strValue = val[vIndex].ToString(); if (upper) { strValue = strValue.ToUpper(); } if (lower) { strValue = strValue.ToLower(); } if (fSubstring) { if (fSub > strValue.Length) { fSub = strValue.Length; } strValue = strValue.Substring(0, fSub); } if (bSubstring) { if (bSub > strValue.Length) { bSub = strValue.Length; } strValue = strValue.Substring(strValue.Length - bSub, bSub); } ReplacementDictionary[match.Groups[0].Value] = strValue; } else { // Single Value Field if (fieldTypeName == "System.String") { var strValue = fieldValue.ToString(); if (upper) { strValue = strValue.ToUpper(); } if (lower) { strValue = strValue.ToLower(); } if (fSubstring) { strValue = strValue.Substring(0, fSub); } if (bSubstring) { strValue = strValue.Substring(strValue.Length - bSub, bSub); } ReplacementDictionary[match.Groups[0].Value] = strValue; } } } } } return(ReplacementDictionary.Aggregate(newValue, (current, item) => current.Replace(item.Key, item.Value))); }
private static void ProcessRead() { List <string> allLines = new List <string>(); IEnumerable <string> fileEntries = Directory.EnumerateFiles(docPathIn); foreach (string fname in fileEntries) { string nameFile = Path.GetFileName(fname); try { allLines = new List <string>(); using (StreamReader sr = File.OpenText(fname)) { while (!sr.EndOfStream) { allLines.Add(sr.ReadLine()); } } //Fechando arquivo, pois não é mais necessário Parallel.ForEach(allLines, line => { try { dynamic parsedLine = Parser.ParseString(line); if (parsedLine != null) { Processor(parsedLine); } } catch (FormatException f) { Console.WriteLine(f); } catch (Exception e) { Console.WriteLine(e); } }); } catch (OutOfMemoryException) { Console.WriteLine("Insufficient memory"); } finally { if (allLines.Count > 0) { allLines.Clear(); allLines = null; using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPathOut, "output_" + nameFile))) { outputFile.WriteAsync(string.Format(@"Client count: {0}; Salesman count: {1}; Top sale: {2}; Worst salesman: {3}.", countClient, countSalesman, topSale, dictionarySalesmanSales.Aggregate((l, r) => l.Value < r.Value ? l : r).Key)); } File.Move(fname, Path.Combine(docPathProcessed, nameFile)); } } GC.Collect(); } }