コード例 #1
0
ファイル: Form1.cs プロジェクト: yonglehou/Bermuda
        private void GetData(ExternalServiceClient client)
        {
            InvokeOnFormThread(() =>
            {
                this.chart2.Series.Clear();
                var timeseries = this.chart2.Series.Add("timeseries");
                timeseries.XValueType = ChartValueType.Date;

                textBox2.Text = "Working...";

                label4.Text = "Working...";
            });

            string query = textBox1.Text;

            //object[] parameters = new object[] { textBox1.Text };

            //Expression<Func<Mention, object[], bool>> query = (x,p) => true;

            //if (!string.IsNullOrWhiteSpace(textBox1.Text))
            //{
            //    query = (x, p) => x.Name.Contains((string)p[0]) || x.Description.Contains((string)p[0]);
            //}

            //var query2 = EvoQLBuilder.GetLambda(textBox1.Text);

            //Expression<Func<IEnumerable<ThriftMention>, IEnumerable<ThriftDatapoint>>> mapreduce = mentions =>
            //    from mention in mentions
            //    from tag in mention.Tags
            //    group mention by new { mention.OccurredOn.Year, mention.OccurredOn.Month, mention.OccurredOn.Day, TagId = tag } into g
            //    select new ThriftDatapoint { Count = g.Count(), EntityId = g.Key.TagId, Value = g.Average(x => x.Sentiment), Timestamp = new DateTime(g.Key.Year, g.Key.Month, g.Key.Day).Ticks };

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce1 = collection =>
            //    from mention in collection
            //    from tag in mention.Tags
            //    from datasource in mention.Datasources
            //    group mention by new MentionGroup{  Timestamp = mention.OccurredOnTicks, Id = tag, Id2 = datasource } into g
            //    select new Datapoint { EntityId = g.Key.Id, EntityId2 = g.Key.Id2, Timestamp = g.Key.Timestamp };

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = collection =>
            //    collection.SelectMany(m => m.Tags, (m, t) => new MentionMetadata { Mention = m, Id = t }).SelectMany(x => x.Mention.Datasources, (md, ds) => new MentionMetadata2 { Child = md, Id = ds }).GroupBy(md => new MentionGroup { Timestamp = md.Child.Mention.OccurredOnTicks, Id = md.Child.Id, Id2 = md.Id }).Select(x => new Datapoint { Timestamp = x.Key.Timestamp, EntityId = x.Key.Id, EntityId2 = x.Key.Id2 });

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = x => x.GroupBy(y => y.OccurredOnDayTicks).Select(g => new Datapoint { Count = g.Count(), Timestamp = g.Key });
            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = x => x.GroupBy(y => new MentionGroup{ Timestamp = y.OccurredOnTicks - (y.OccurredOnTicks % 864000000000) }).Select(g => new Datapoint { Count = g.Count(), Value = g.Average(y => y.Sentiment), Timestamp = g.Key.Timestamp });

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = null; // ReduceExpressionGeneration.MakeExpression(null, SelectedSelect, SelectedGroupBy, SelectedGroupBy2);
            
            //Expression<Func<IEnumerable<Datapoint>, double>> merge = x => x.Sum(y => y.Value);
            //if (SelectedSelect == SelectTypes.Count) merge = x => x.Sum(y => (double)y.Count);

            var mapreduce = textBox9.Text;

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Mention>>> paging = c => c.OrderByDescending(x => x.OccurredOnTicks).Take(25);

            ExpressionSerializer serializer = new ExpressionSerializer();

            //InvokeOnFormThread(() =>
            //{

            //    //ExpressionSerializer serializer = new ExpressionSerializer();
            //    //textBox6.Text = serializer.Serialize(mapreduce).ToString();
            //});

            var domain = textBox3.Text;
            //var minDate = dateTimePicker1.Value;
            //var maxDate = dateTimePicker2.Value;

            string command = textBox7.Text;

            Stopwatch sw = new Stopwatch();
            sw.Start();
            var result = client.GetData(domain, "__ql__" + query, "__ql__" + mapreduce, "__default__", null, command);
            sw.Stop();

            result.Metadata.OperationTime = sw.Elapsed;

            InvokeOnFormThread(() =>
            {
                var area = chart2.ChartAreas.First();
                var oldseries = chart2.Series.ToArray();
                string separator = "\r\n################################################################################################################################\r\n";

                HardcodedBermudaDatapoint[] datapoints = new HardcodedBermudaDatapoint[0];
                string jsonError = null;

                if (result.Data != null)
                {
                    try
                    {
                        datapoints = new JavaScriptSerializer().Deserialize<HardcodedBermudaDatapoint[]>(result.Data);
                        label4.Text = "Retrieved " + datapoints.Count() + "\r\nin " + sw.Elapsed;
                    }
                    catch (Exception ex)
                    {
                        jsonError = ex.ToString() + separator;
                    }
                }
                else
                {
                    datapoints = new HardcodedBermudaDatapoint[0];
                }


                StringBuilder sb = new StringBuilder();

                if (result.Metadata != null) ConvertStatsToString(sb, result.Metadata);
                textBox2.Text = result.CacheKey + separator + jsonError + result.Error + separator + sb.ToString() + separator + result.Data;

                var groups = datapoints.GroupBy(x => new { x.Id, x.Text } );
                
                chart2.SuspendLayout();

                foreach (var s in chart2.Series.Skip(1).ToArray())
                {
                    chart2.Series.Remove(s);
                }

                int i = 0;
                foreach (var g in groups)
                {
                    string name = g.Key.Text ?? g.Key.Id.ToString();
                    var timeseries = i == 0 ? chart2.Series.FirstOrDefault() : chart2.Series.Add(name);
                    timeseries.Points.Clear();
                    timeseries.Name = name;

                    timeseries.XValueType = g.Any(p => new DateTime(p.Id2).Year > 2000) ? ChartValueType.Date : ChartValueType.Int64;

                    foreach (var d in g)
                    {
                        timeseries.Points.Add(timeseries.XValueType == ChartValueType.Date ? new DataPoint(new DateTime(d.Id2).ToOADate(), d.Value) : new DataPoint(d.Id2, d.Value));
                    }

                    i++;
                }
                
                chart2.ResumeLayout(true);
                
                
                //textBox2.Text = sb.ToString().Trim() + separator + string.Join("\r\n", datapoints.Datapoints.Select(x => x.Id.ToString().PadLeft(32) + x.Id2.ToString().PadLeft(32) + ((int)x.Value).ToString().PadLeft(16) + x.Count.ToString().PadLeft(16) ));
                
                //var sb2 = new StringBuilder();
                //ConvertStatsToString(sb2, mentions.Metadata);
                //textBox5.Text = sb2.ToString().Trim() + separator + string.Join(separator, mentions.Mentions.Select(x => DateTime.FromBinary( x.OccurredOnTicks ) + " :: " + x.Name + "\r\n" + x.Description));

                //label4.Text = "Received " + (datapoints.Datapoints.Count()) + " datapoints\r\nin " + sw.Elapsed;
            });

            //return datapoints.Datapoints;
        }