Example #1
0
 // GET: DocType
 public ActionResult Index()
 {
     return(View(DocTypeRepository.Index()));
 }
Example #2
0
        public ActionResult Index()
        {
            var model = new HomeViewModel();

            // DateTime Range
            var queryRange = new Nest.DateRangeQuery();

            queryRange.Field                = new Nest.Field();
            queryRange.Field.Name           = "datum";
            queryRange.GreaterThanOrEqualTo = DateTime.Now.AddDays(-DateTime.Now.Day);
            queryRange.LessThanOrEqualTo    = DateTime.Now.AddDays(0);
            var rangeContainer = new Nest.QueryContainer(queryRange);

            // Name TagCloud
            var nameTagCloudModel = new TagCloudViewModel();

            nameTagCloudModel.Title = "Meist verwendete Namen";
            nameTagCloudModel.Tags  = new Dictionary <string, float>();
            var listOfQueriesTagCloud = new List <Nest.QueryContainer>();

            listOfQueriesTagCloud.Add(rangeContainer);

            var res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(listOfQueriesTagCloud.ToArray())
                                                                                      )
                                                                                )
                                                                         .Aggregations(a =>
                                                                                       a.Terms("tagcloud", ta => ta.Field("name"))
                                                                                       )
                                                                         );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                nameTagCloudModel.Tags.Add(nestTag.Key, nestTag.DocCount.Value);
            }
            model.NameTagCloud = nameTagCloudModel;

            // DocType TagCloud
            var docTagCloudModel = new TagCloudViewModel();

            docTagCloudModel.Title = "Meist verwendete DocTypes";
            docTagCloudModel.Tags  = new Dictionary <string, float>();

            foreach (var docType in DocTypeRepository.Index())
            {
                var listOfQueriesDocType = new List <Nest.QueryContainer>();
                listOfQueriesDocType.Add(rangeContainer);

                var queryDocType = new Nest.MatchQuery();
                queryDocType.Field      = new Nest.Field();
                queryDocType.Field.Name = "type";
                queryDocType.Query      = docType.Id.ToString();
                var docTypesContainer = new Nest.QueryContainer(queryDocType);
                listOfQueriesDocType.Add(docTypesContainer);

                res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(listOfQueriesDocType.ToArray())
                                                                                      )
                                                                                )
                                                                         );
                docTagCloudModel.Tags.Add(docType.Name, res.Total);
            }
            model.DocTypeTagCloud = docTagCloudModel;

            // Ort TagCloud
            var ortTagCloudModel = new TagCloudViewModel();

            ortTagCloudModel.Title = "Meist verwendete Orte";
            ortTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("ort"))
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                ortTagCloudModel.Tags.Add(nestTag.Key, nestTag.DocCount.Value);
            }
            model.OrtTagCloud = ortTagCloudModel;

            // Kategorie TagCloud
            var kategorieTagCloudModel = new TagCloudViewModel();

            kategorieTagCloudModel.Title = "Meist verwendete Kategorien";
            kategorieTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("kategorie")
                                                                                           .Aggregations(aa =>
                                                                                                         aa.Sum("summe", ts => ts.Field("preis"))
                                                                                                         ))
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var    nestTag = tag as Nest.KeyedBucket;
                double sum     = 0.0;
                var    sumAggs = (nestTag.Aggregations["summe"] as Nest.ValueAggregate);
                sum = sumAggs.Value.Value;

                kategorieTagCloudModel.Tags.Add(nestTag.Key, (int)sum);
            }
            model.KategorieTagCloud = kategorieTagCloudModel;

            // Handel TagCloud
            var handelTagCloudModel = new TagCloudViewModel();

            handelTagCloudModel.Title = "Meist verwendete Handel";
            handelTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("handel")
                                                                                           .Aggregations(aa =>
                                                                                                         aa.Sum("summe", ts => ts.Field("preis"))
                                                                                                         )
                                                                                           )
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var    nestTag = tag as Nest.KeyedBucket;
                double sum     = 0.0;
                var    sumAggs = (nestTag.Aggregations["summe"] as Nest.ValueAggregate);
                sum = sumAggs.Value.Value;

                handelTagCloudModel.Tags.Add(nestTag.Key, (int)sum);
            }
            model.HandelTagCloud = handelTagCloudModel;

            // Preis Summe

            var listOfQueriesAusgaben = new List <Nest.QueryContainer>();

            listOfQueriesAusgaben.Add(rangeContainer);

            var queryAusgabe = new Nest.MatchQuery();

            queryAusgabe.Field      = new Nest.Field();
            queryAusgabe.Field.Name = "type";
            queryAusgabe.Query      = "e0c4f438-4509-449b-a491-3fa3093b4129";
            var docTypeContainer = new Nest.QueryContainer(queryAusgabe);

            listOfQueriesAusgaben.Add(docTypeContainer);


            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Size(1000)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesAusgaben.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Sum("summe", aa => aa
                                                                                         .Field("preis"))
                                                                                   )
                                                                     );
            model.SummeAusgaben = res.Aggs.Sum("summe").Value.Value;

            // Einnahmen Summe
            var listOfQueriesEinnahmen = new List <Nest.QueryContainer>();

            listOfQueriesEinnahmen.Add(rangeContainer);
            var queryEinnahme = new Nest.MatchQuery();

            queryEinnahme.Field      = new Nest.Field();
            queryEinnahme.Field.Name = "type";
            queryEinnahme.Query      = "05ce209c-e837-4fc4-b2a4-6b54bf73be46";
            docTypeContainer         = new Nest.QueryContainer(queryEinnahme);
            listOfQueriesEinnahmen.Add(docTypeContainer);

            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Size(1000)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesEinnahmen.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Sum("summe", aa => aa
                                                                                         .Field("betrag"))
                                                                                   )
                                                                     );
            model.SummeEinnahmen = res.Aggs.Sum("summe").Value.Value;

            return(View(model));
        }
        // GET: Analysis
        public ActionResult Index(List <string> categories, List <string> facts, string query, string docType, string chartType, string aggFunc,
                                  string from, string to)
        {
            var model = new AnalysisViewModel();

            if (docType == "_all" || docType == null || docType == "")
            {
                model.Categories = MetaTagRepository.Index().Where(mt => mt.DataType == "string" || mt.DataType == "datetime" || mt.DataType == "text").ToDictionary <MetaTag, string>(mt => mt.Id.ToString());
                model.Facts      = MetaTagRepository.Index().Where(mt => mt.DataType == "number").ToDictionary <MetaTag, string>(mt => mt.Id.ToString());
            }
            else
            {
                model.Categories = MetaTagRepository.ReadMany(DocTypeRepository.Read(Guid.Parse(docType)).MetaTags)
                                   .Where(mt => mt.DataType == "string" || mt.DataType == "datetime" || mt.DataType == "text")
                                   .ToDictionary <MetaTag, string>(mt => mt.Id.ToString());

                model.Facts = MetaTagRepository.ReadMany(DocTypeRepository.Read(Guid.Parse(docType)).MetaTags)
                              .Where(mt => mt.DataType == "number")
                              .ToDictionary <MetaTag, string>(mt => mt.Id.ToString());
            }

            categories = (categories != null) ? categories  : new List <string>();
            model.SelectedCategories = categories;
            model.SelectedFacts      = (facts != null)? facts : new List <string>();

            if (chartType == null)
            {
                chartType = "bar";
            }

            if (aggFunc == null)
            {
                aggFunc = "sum";
            }

            model.ChartType = chartType;
            model.AggFunc   = aggFunc;

            model.Query = query;
            Guid docTypeGuid;

            if (Guid.TryParse(docType, out docTypeGuid))
            {
                if (query == null)
                {
                    query = "";
                }

                if (query.Length > 0)
                {
                    query += ", ";
                }

                query += "type:" + docType;
            }

            // from and to date
            model.From = from;
            model.To   = to;

            DateTime fromDate;
            DateTime toDate;
            int?     diffFomDays = null;
            int?     diffToDays  = null;

            if (DateTime.TryParse(from, out fromDate))
            {
                diffFomDays = (fromDate.Date - DateTime.Now.Date).Days;
            }

            if (DateTime.TryParse(to, out toDate))
            {
                diffToDays = (toDate.Date - DateTime.Now.Date).Days;
            }

            var selectedCategoryNames = new List <string>();
            var selectedCategoryTypes = new List <string>();

            // in order to save order of selected categories, use iteration
            foreach (var cat in categories)
            {
                var catName = model.Categories.Where(c => c.Value.Id.ToString() == cat).Select(c => c.Value.Name).FirstOrDefault();
                var catType = model.Categories.Where(c => c.Value.Id.ToString() == cat).Select(c => c.Value.DataType).FirstOrDefault();
                selectedCategoryNames.Add(catName);
                selectedCategoryTypes.Add(catType);
            }

            var selectedFactNames = (facts != null) ? model.Facts.Where(c => facts.Contains(c.Key.ToString())).Select(c => c.Value.Name).ToList <string>() : new List <string>();

            var valueList = new List <List <float> >();
            var labelList = new List <string>();
            var titleList = new List <string>();

            // Todo Build Datasets for chartjs markup
            if (selectedCategoryNames.Count > 1)
            {
                var aggs = DocRepository.Aggregate(selectedCategoryNames, selectedFactNames[0], query, aggFunc, diffFomDays, diffToDays);
                titleList = aggs.Keys.Select(k => k.Split('_')[1]).Distinct().ToList();

                var groupKeys = aggs.Keys.Select(k => k.Split('_')[0]).Distinct().ToList();
                labelList = groupKeys;

                foreach (var group in groupKeys)
                {
                    var valuePairs = aggs.Where(a => a.Key.ToLower().Contains(group.ToLower())).ToList();

                    var values = new List <float>();
                    foreach (var title in titleList)
                    {
                        var pair = valuePairs.Where(p => p.Key.ToLower().Contains(title.ToLower())).ToList();

                        if (pair.Count > 0)
                        {
                            values.Add(pair[0].Value);
                        }
                        else
                        {
                            values.Add(0.0f);
                        }
                    }

                    valueList.Add(values);
                }
            }
            else
            {
                foreach (var fact in selectedFactNames)
                {
                    var aggs   = DocRepository.Aggregate(selectedCategoryNames, fact, query, aggFunc, diffFomDays, diffToDays);
                    var values = (selectedCategoryNames.Count > 0 && selectedFactNames.Count > 0) ? aggs.Values.ToList <float>() : new List <float>();
                    valueList.Add(values);
                    titleList = aggs.Keys.Select(k => k.Replace("_", "")).ToList <string>().Distinct().ToList();

                    labelList.Add(fact);
                    labelList = (model.AggFunc != "count") ? labelList : new List <string>()
                    {
                        "Anzahl"
                    };

                    if (model.AggFunc == "count")
                    {
                        break;
                    }
                }
            }

            model.ChartModel = new ChartViewModel()
            {
                Height     = 400,
                Categories = titleList.Select(val =>
                {
                    DateTime dateTime;
                    if (DateTime.TryParse(val, out dateTime))
                    {
                        return(String.Format("{0:yyyy-MM-dd}", dateTime));
                    }
                    return(val);
                }
                                              ).ToList(),
                SeriesNames = labelList.Select(val =>
                {
                    DateTime dateTime;
                    if (DateTime.TryParse(val, out dateTime))
                    {
                        return(String.Format("{0:yyyy-MM-dd}", dateTime));
                    }
                    return(val);
                }
                                               ).ToList(),
                Values    = valueList,
                Type      = model.ChartType,
                Title     = "",
                HidePanel = true
            };

            model.DocType  = docType;
            model.DocTypes = DocTypeRepository.Index();

            return(View(model));
        }
Example #4
0
        // GET: Doc
        public ActionResult Index(string query, string docType, string from, string to)
        {
            var model = new DocIndexViewModel();

            model.Query   = (query != null) ? query : "";
            model.DocType = docType;
            model.From    = from;
            model.To      = to;

            bool docTypeIsSet = (docType != null && docType.Length > 0);

            var queryWithFilters = model.Query;

            queryWithFilters += (docTypeIsSet && queryWithFilters.Length > 0) ? ", " : "";
            queryWithFilters += (docTypeIsSet) ? " type:" + docType : "";

            DateTime fromDate;
            DateTime toDate;
            int?     diffFomDays = null;
            int?     diffToDays  = null;

            if (DateTime.TryParse(from, out fromDate))
            {
                diffFomDays = (fromDate - DateTime.Now.Date).Days;
            }

            if (DateTime.TryParse(to, out toDate))
            {
                diffToDays = (toDate - DateTime.Now.Date).Days;
            }

            var docs = new List <dynamic>();

            if (queryWithFilters.Length > 0 || diffFomDays.HasValue || diffToDays.HasValue)
            {
                docs = DocRepository.Search(queryWithFilters, diffFomDays, diffToDays, 50);
            }
            else
            {
                docs = DocRepository.Index();
            }

            var list = new List <DocViewModel>();

            foreach (dynamic doc in docs)
            {
                var     docModel = new DocViewModel();
                DocType type     = DocTypeRepository.Read(Guid.Parse(doc.type));
                docModel.Id   = Guid.Parse(doc.id);
                docModel.Type = type.Name;
                try
                {
                    docModel.Name = doc.name;
                } catch (Exception e)
                {
                    docModel.Name = "";
                }

                docModel.MetaTags = MetaTagRepository.ReadMany(type.MetaTags);
                list.Add(docModel);
            }
            model.Docs = list;

            model.DocTypes = DocTypeRepository.Index();

            return(View(model));
        }