Exemple #1
0
        private static BsonDocument buildMatchCondition(IReportSpecification specification)
        {
            IMongoQuery orClause       = createSearchClauseForAnyFilter(specification);
            IMongoQuery typeNameClause = createSearchClauseForAnyType(specification);
            // Query.EQ("TypeName", specification.TrackerTypeName);
            IMongoQuery dateClause = Query.And(Query.GTE("TimeSlot", specification.FromDateUtc),
                                               Query.LTE("TimeSlot", specification.ToDateUtc));


            var conditions = new BsonDocument(dateClause.ToBsonDocument());

            conditions.AddRange(typeNameClause.ToBsonDocument());
            if (orClause != null)
            {
                conditions.AddRange(orClause.ToBsonDocument());
            }
            var match = new BsonDocument
            {
                {
                    "$match", conditions
                }
            };

            return(match);
        }
Exemple #2
0
        /// <summary>
        /// Build entity sentiment result in bson document format
        /// </summary>
        /// <param name="articleId">ObjectId of the article</param>
        /// <param name="entities">List of entities</param>
        /// <returns>Entity sentiment result in BsonDocument</returns>
        public static BsonDocument BuildSentimentBsonDocument(BsonDocument article, Sentiment overallSentiment, List <Entity> entities, List <ClassificationCategory> categories)
        {
            // Convert overall sentiment to BsonDocument
            var bsonOverallSentiment = overallSentiment.ToBsonDocument();

            // Convert list of entities -> JSON -> BsonDocument
            var jsonEntities = $"{{ Entities: {JsonConvert.SerializeObject(entities)} }}";
            var bsonEntities = BsonDocument.Parse(jsonEntities);

            // Convert list of categories -> JSON -> BsonDocument
            var jsonCategories = $"{{ Categories: {JsonConvert.SerializeObject(categories)} }}";
            var bsonCategories = BsonDocument.Parse(jsonCategories);

            //Create Sentiment Bson Doc
            BsonDocument sentimentDoc = new BsonDocument
            {
                { "News", article },
                { "OverallSentiment", bsonOverallSentiment },
            };

            sentimentDoc.AddRange(bsonEntities);
            sentimentDoc.AddRange(bsonCategories);
            sentimentDoc.Add("AnalyzedAt", DateTime.Now);

            return(sentimentDoc);
        }
        BsonDocument ParseFields(string key, object obj)
        {
            var rtn = new BsonDocument(true);

            if (key.ToLower() == "$fields")
            {
                if (obj is FrameDLRObject)
                {
                    var dobj = (FrameDLRObject)obj;
                    foreach (var k in dobj.Keys)
                    {
                        rtn.AddRange(ParseFields(k, dobj.GetValue(k)));
                    }
                }
            }
            else
            {
                if (obj is FrameDLRObject)
                {
                    var dobj = (FrameDLRObject)obj;
                    foreach (var k in dobj.Keys)
                    {
                        if (k.ToLower() == "$slice")
                        {
                            if (dobj.GetValue(k) is int)
                            {
                                rtn.AddRange(Builders <BsonDocument> .Projection.Slice(key, IntStd.ParseStd(dobj.GetValue(k)).Value).ToBsonDocument());
                            }
                        }
                    }
                }
            }
            return(rtn);
        }
Exemple #4
0
        /// <summary>
        /// 处理文档(key转小写)
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="projectFields"></param>
        /// <returns></returns>
        static BsonDocument HandleDoc(BsonDocument doc, string projectFields)
        {
            var fieldsArr        = projectFields.Split(",").ToList().ConvertAll(x => x.Split('.')[0]);
            var subProjectFields = string.Join(',', projectFields.Split(",").ToList().ConvertAll(x => x.Contains(".") ? x.Substring(x.IndexOf(".") + 1) : null));
            var names            = doc.Names.ToList();

            BsonDocument newDoc = new BsonDocument();

            if (doc.Contains("_id"))
            {
                newDoc.Add(new BsonElement("id", HandleID(doc["_id"])));
            }

            foreach (var name in names)
            {
                if (fieldsArr.Contains(name))
                {
                    if (doc[name].IsBsonArray)
                    {
                        newDoc.AddRange(new BsonDocument(name.ToLower(), HandleDocs(doc[name].AsBsonArray, subProjectFields)));
                    }
                    else if (doc[name].IsBsonDocument)
                    {
                        newDoc.AddRange(new BsonDocument(name.ToLower(), HandleDoc(doc[name].AsBsonDocument, subProjectFields)));
                    }
                    else
                    {
                        newDoc.Add(new BsonElement(name.ToLower(), doc[name]));
                    }
                }
            }

            return(newDoc);
        }
Exemple #5
0
        public string GetOrderByCustomer(int customerId, int orderId)
        {
            var bd = new BsonDocument();

            bd.AddRange(new BsonDocument("a", customerId));
            bd.AddRange(new BsonDocument("b", orderId));
            return(bd.ToJson());
        }
Exemple #6
0
        public BsonDocument toBson()
        {
            BsonDocument output = new BsonDocument();

            output.AddRange(new BsonDocument("width", width.ToString()));
            output.AddRange(new BsonDocument("uri150", uri150.ToString()));
            output.AddRange(new BsonDocument("uri", uri.ToString()));
            output.AddRange(new BsonDocument("ImageType", imageType.ToString()));
            output.AddRange(new BsonDocument("height", height.ToString()));
            return(output);
        }
 public static List <BitacoraMovSISTEMA> consultaFiltrada(String tipoMovimiento, String cveAutor, DateTime?fechaInicio, DateTime?fechaFin)
 {
     if (esActiva)
     {
         try
         {
             SortDefinition <BitacoraMovSISTEMA> sort = new BsonDocument("fechaMovimiento", -1);
             System.Threading.Tasks.Task <List <BitacoraMovSISTEMA> > query = null;
             Dictionary <String, object> filtro1       = new Dictionary <string, object>();
             Dictionary <String, object> filtro2       = new Dictionary <string, object>();
             List <BitacoraMovSISTEMA>   listaFiltrada = new List <BitacoraMovSISTEMA>();
             var filter = new BsonDocument();
             if (!tipoMovimiento.Equals("Todos") && !tipoMovimiento.Equals(""))
             {
                 filtro1.Add("cveTipoMovimiento", tipoMovimiento);
                 filter.AddRange(filtro1);
             }
             if (!cveAutor.Equals("Todos") && !cveAutor.Equals(""))
             {
                 filtro2.Add("cveAutor", cveAutor);
                 filter.AddRange(filtro2);
             }
             query = Movimientos.Find(filter).Sort(sort).ToListAsync();
             var lstMovimientos = query.Result;
             if (fechaInicio != null && fechaFin != null)
             {
                 listaFiltrada = lstMovimientos.Where(x => x.fechaMovimiento >= fechaInicio.Value && x.fechaMovimiento <= fechaFin.Value.AddDays(1)).ToList();
                 return(listaFiltrada.OrderByDescending(x => x.fechaMovimiento).ToList());
             }
             if (tipoMovimiento.Equals("Todos") && cveAutor.Equals("Todos"))
             {
                 ConfiguracionServiceProvider configuracionService = new ConfiguracionServiceProvider();
                 return(lstMovimientos.Take(int.Parse(configuracionService.getValorDeVariable("maxInicialBitacoraMovs"))).OrderByDescending(x => x.fechaMovimiento).ToList());
             }
             else
             {
                 return(lstMovimientos.OrderByDescending(x => x.fechaMovimiento).ToList());
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             return(new List <BitacoraMovSISTEMA>());
         }
     }
     else
     {
         return(new List <BitacoraMovSISTEMA>());
     }
 }
        /// <summary>
        /// 处理文档(key转小写)
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="projectFields"></param>
        /// <returns></returns>
        private BsonDocument HandleDoc(BsonDocument doc, string projectFields)
        {
            projectFields = projectFields ?? "";
            var fieldsArr = projectFields.Split(",").ToList().ConvertAll(x => x.Split('.')[0].Trim());
            var names     = doc.Names.ToList();

            if (fieldsArr.Count(x => string.IsNullOrWhiteSpace(x)) == fieldsArr.Count)
            {
                fieldsArr = names;
                fieldsArr.Remove("_id");
            }

            BsonDocument newDoc = new BsonDocument();

            if (doc.Contains("_id"))
            {
                newDoc.Add(new BsonElement("ID", HandleID(doc["_id"])));
            }

            foreach (var name in names)
            {
                if (fieldsArr.Contains(name))
                {
                    var subProjectFields = string.Join(',', projectFields.Split(",").Where(s => s.StartsWith(name, StringComparison.CurrentCultureIgnoreCase)).ToList().ConvertAll(x => x.Contains(".") ? x.Substring(x.IndexOf(".") + 1).Trim() : null));
                    if (doc[name].IsBsonArray)
                    {
                        newDoc.AddRange(new BsonDocument(name, HandleArray(doc[name].AsBsonArray, subProjectFields)));
                    }
                    else if (doc[name].IsBsonDocument)
                    {
                        newDoc.AddRange(new BsonDocument(name, HandleDoc(doc[name].AsBsonDocument, subProjectFields)));
                    }
                    else
                    {
                        if (doc[name].IsBsonDateTime || doc[name].IsValidDateTime)
                        {
                            newDoc.Add(new BsonElement(name, doc[name].ToString()));
                        }
                        else
                        {
                            newDoc.Add(new BsonElement(name, doc[name]));
                        }
                    }
                }
            }

            return(newDoc);
        }
 /// <summary>
 ///     获取Match
 /// </summary>
 /// <returns></returns>
 public BsonDocument GetMatchDocument()
 {
     var Matchlist = new BsonDocument();
     foreach (ctlMatchItem item in Controls)
     {
         BsonDocument match = item.getMatchItem();
         if (match != null)
         {
             string MatchName = match.GetElement(0).Name;
             if (Matchlist.Contains(MatchName))
             {
                 BsonDocument AddMatch = match.GetElement(0).Value.AsBsonDocument;
                 Matchlist.GetElement(MatchName).Value.AsBsonDocument.AddRange(AddMatch);
             }
             else
             {
                 Matchlist.AddRange(match);
             }
         }
     }
     if (Matchlist.ElementCount > 0)
     {
         return new BsonDocument("$match", Matchlist);
     }
     return null;
 }
Exemple #10
0
        /// <summary>
        ///     获取Match
        /// </summary>
        /// <returns></returns>
        public BsonDocument GetMatchDocument()
        {
            var matchlist = new BsonDocument();

            foreach (Control item in Controls)
            {
                if (item.GetType().FullName == typeof(Button).FullName)
                {
                    continue;
                }
                var match = ((CtlMatchItem)item).GetMatchItem();
                if (match != null)
                {
                    var matchName = match.GetElement(0).Name;
                    if (matchlist.Contains(matchName))
                    {
                        var addMatch = match.GetElement(0).Value.AsBsonDocument;
                        matchlist.GetElement(matchName).Value.AsBsonDocument.AddRange(addMatch);
                    }
                    else
                    {
                        matchlist.AddRange(match);
                    }
                }
            }
            if (matchlist.ElementCount > 0)
            {
                return(new BsonDocument("$match", matchlist));
            }
            return(null);
        }
        /// <summary>
        ///     获取Match
        /// </summary>
        /// <returns></returns>
        public BsonDocument GetMatchDocument()
        {
            var Matchlist = new BsonDocument();

            foreach (ctlMatchItem item in Controls)
            {
                BsonDocument match = item.getMatchItem();
                if (match != null)
                {
                    string MatchName = match.GetElement(0).Name;
                    if (Matchlist.Contains(MatchName))
                    {
                        BsonDocument AddMatch = match.GetElement(0).Value.AsBsonDocument;
                        Matchlist.GetElement(MatchName).Value.AsBsonDocument.AddRange(AddMatch);
                    }
                    else
                    {
                        Matchlist.AddRange(match);
                    }
                }
            }
            if (Matchlist.ElementCount > 0)
            {
                return(new BsonDocument("$match", Matchlist));
            }
            return(null);
        }
Exemple #12
0
        /// <summary>
        ///     获取Match
        /// </summary>
        /// <returns></returns>
        public BsonDocument GetMatchDocument()
        {
            var matchlist = new BsonDocument();

            foreach (CtlMatchItem item in Controls)
            {
                var match = item.GetMatchItem();
                if (match != null)
                {
                    var matchName = match.GetElement(0).Name;
                    if (matchlist.Contains(matchName))
                    {
                        var addMatch = match.GetElement(0).Value.AsBsonDocument;
                        matchlist.GetElement(matchName).Value.AsBsonDocument.AddRange(addMatch);
                    }
                    else
                    {
                        matchlist.AddRange(match);
                    }
                }
            }
            if (matchlist.ElementCount > 0)
            {
                return(new BsonDocument("$match", matchlist));
            }
            return(null);
        }
        public async Task <IActionResult> Status()
        {
            if (!Authorize())
            {
                return(Unauthorized());
            }

            var bson = new BsonDocument();
            var lastProfileSwitch = await collectionRepo.GetLastProfileSwitch();

            bson.Add("status", "ok");
            bson.Add("serverTime", DateTimeHelper.ToEpoch(DateTime.Now));

            var lastProfileDoc = await collectionRepo.GetLastProfilesDocument();

            if (lastProfileDoc != null)
            {
                var profilesList = new List <BsonDocument> {
                    lastProfileDoc
                };
                var profileElement = new BsonDocument {
                    { CollectionEnum.profile.ToString(), new BsonArray(profilesList) }
                };
                bson.AddRange(profileElement);
            }

            if (lastProfileSwitch != null && lastProfileSwitch.Contains("profile"))
            {
                bson.Add("activeProfile", lastProfileSwitch.GetValue("profile").AsString);
            }

            return(Content(bson.ToJson(jsonWriterSettings), JSON_CONTENT_TYPE));
        }
Exemple #14
0
        public string GetJsonGeneral()
        {
            try
            {
                var client      = GetClient();
                var db          = client.GetDatabase(_dataBaseName);
                var collections = db.ListCollections();

                var jsonDocument = new BsonDocument();
                var jsonElements = new List <BsonElement>();

                foreach (var item in db.ListCollectionsAsync().Result.ToListAsync <BsonDocument>().Result)
                {
                    var collection             = db.GetCollection <BsonDocument>(item["name"].ToString());
                    var collectionListElements = collection.Find(x => true).ToList();
                    var collectionArray        = new BsonArray();
                    foreach (var element in collectionListElements)
                    {
                        collectionArray.Add(element);
                    }
                    jsonElements.Add(new BsonElement(item["name"].ToString(), collectionArray));
                }
                return(jsonDocument.AddRange(jsonElements).ToJson());
            }
            catch (Exception e)
            {
                return(e.ToString());
            }
        }
Exemple #15
0
 /// <summary>
 ///     获取Match
 /// </summary>
 /// <returns></returns>
 public BsonDocument GetMatchDocument()
 {
     var matchlist = new BsonDocument();
     foreach (Control item in Controls)
     {
         if (item.GetType().FullName == typeof(Button).FullName) continue;
         var match = ((CtlMatchItem)item).GetMatchItem();
         if (match != null)
         {
             var matchName = match.GetElement(0).Name;
             if (matchlist.Contains(matchName))
             {
                 var addMatch = match.GetElement(0).Value.AsBsonDocument;
                 matchlist.GetElement(matchName).Value.AsBsonDocument.AddRange(addMatch);
             }
             else
             {
                 matchlist.AddRange(match);
             }
         }
     }
     if (matchlist.ElementCount > 0)
     {
         return new BsonDocument("$match", matchlist);
     }
     return null;
 }
Exemple #16
0
 /// <summary>
 ///     获取Match
 /// </summary>
 /// <returns></returns>
 public BsonDocument GetMatchDocument()
 {
     var matchlist = new BsonDocument();
     foreach (CtlMatchItem item in Controls)
     {
         var match = item.GetMatchItem();
         if (match != null)
         {
             var matchName = match.GetElement(0).Name;
             if (matchlist.Contains(matchName))
             {
                 var addMatch = match.GetElement(0).Value.AsBsonDocument;
                 matchlist.GetElement(matchName).Value.AsBsonDocument.AddRange(addMatch);
             }
             else
             {
                 matchlist.AddRange(match);
             }
         }
     }
     if (matchlist.ElementCount > 0)
     {
         return new BsonDocument("$match", matchlist);
     }
     return null;
 }
Exemple #17
0
        public BsonDocument CreateBsonDocument()
        {
            BsonDocument bsonDocument = new BsonDocument();

            bsonDocument.AddRange(GetDBKeyVal());
            return(bsonDocument);
        }
Exemple #18
0
 /// <summary>
 /// Generates a $group pipeline command based upon the specified group-by and grouping-aggregation specifications
 /// </summary>
 /// <param name="GroupBy">The group-by specification for grouping distinction</param>
 /// <param name="Aggregations">An enumerable of grouping-aggregation expressions</param>
 public static BsonDocument Group(BsonElement GroupBy, IEnumerable<BsonElement> Aggregations)
 {
     var value = new BsonDocument(GroupBy);
     if (Aggregations != null && Aggregations.Any()) {
         value.AddRange(Aggregations);
     }
     return new BsonDocument() { { "$group", value } };
 }
Exemple #19
0
        private void Right()
        {
            int use_time = (int)(DateTime.Now - start).TotalMilliseconds;

            if (use_time < 1000)
            {
                right = 1;
            }
            else if (use_time < 10000)
            {
                right = 2;
            }
            else
            {
                right = 3;
            }
            bson.AddRange(new BsonDocument("right", right));
            bson.AddRange(new BsonDocument("power", power));
        }
        private BsonDocument GetFilter(Contact filter)
        {
            var mfilter = new BsonDocument();

            if (!String.IsNullOrEmpty(filter.Name))
            {
                mfilter.AddRange(new Dictionary <string, object>()
                {
                    [nameof(filter.Name)] = filter.Name
                });
            }
            if (!String.IsNullOrEmpty(filter.FavouriteProgrammingLanguage))
            {
                mfilter.AddRange(new Dictionary <string, object>()
                {
                    [nameof(filter.FavouriteProgrammingLanguage)] = filter.FavouriteProgrammingLanguage
                });
            }
            return(mfilter);
        }
        private BsonDocument GetFilter(Message filter)
        {
            var mfilter = new BsonDocument();

            if (!String.IsNullOrEmpty(filter.From))
            {
                mfilter.AddRange(new Dictionary <string, object>()
                {
                    [nameof(filter.From)] = filter.From
                });
            }
            if (!String.IsNullOrEmpty(filter.To))
            {
                mfilter.AddRange(new Dictionary <string, object>()
                {
                    [nameof(filter.To)] = filter.To
                });
            }
            return(mfilter);
        }
Exemple #22
0
        public BsonDocument CreateFilterQuery(IList<QueryInfo> filterParams)
        {
            BsonDocument filterQuery = new BsonDocument();

            foreach (var item in filterParams)
            {
                BsonDocument newFilter = new BsonDocument(item.Key, new BsonDocument(item.QueryOperator, item.Value));
                filterQuery.AddRange(newFilter);
            }
            return filterQuery;
        }
Exemple #23
0
        public BsonDocument ToDocument()
        {
            var doc = new BsonDocument();

            foreach (var item in this)
            {
                doc.AddRange(item.ToDocument());
            }

            return(doc);
        }
        public void DeleteMany(IList <object> filterFields = null)
        {
            var filterDoc = new BsonDocument();

            filterDoc.AllowDuplicateNames = true;
            foreach (var filterField in filterFields)
            {
                var jsonDoc = Newtonsoft.Json.JsonConvert.SerializeObject(filterField);
                var bsonDoc = BsonSerializer.Deserialize <BsonDocument>(jsonDoc);
                filterDoc.AddRange(bsonDoc);
            }
            collection.DeleteMany(filterDoc);
        }
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="collectionName"></param>
        /// <param name="dictIndex"></param>
        public void CreateCollectionIndex(string collectionName, Dictionary <string, int> dictIndex)
        {
            CreateIndexOptions indexOp = new CreateIndexOptions()
            {
                Background = true
            };
            BsonDocument indexDoc = new BsonDocument();

            indexDoc.AddRange(dictIndex);
            IMongoCollection <BsonDocument> collection = this._database.GetCollection <BsonDocument>(collectionName);

            collection.Indexes.CreateOne(indexDoc, indexOp);
        }
Exemple #26
0
        private BsonDocument ListOfRegexes(IEnumerable <string> values)
        {
            var bsonElements = new List <BsonElement>();

            foreach (var val in values)
            {
                bsonElements.Add(new BsonElement("$regex", new BsonRegularExpression(Regex.Escape(val), "i")));
            }
            var regexDoc = new BsonDocument(true);

            regexDoc.AddRange(bsonElements);
            return(regexDoc);
        }
        public async Task CreateDocument(string databaseName, string collectionName, string userId, string userName, string title, string description, string coverImage, decimal price)
        {
            var collection = GetCollection(databaseName, collectionName);

            string documentJson = "{\"author\":\" " + userName + "\", \"authorId\":\"" + userId + "\", \"title\":\" " + title + "\", \"description\":\"" + description + "\", \"coverImage\":\" " + coverImage + "\", \"price\":" + price + "}";

            BsonDocument newBook = BsonDocument.Parse(documentJson);

            BsonDocument document = new BsonDocument();

            document.AddRange(newBook);
            await collection.InsertOneAsync(document);
        }
Exemple #28
0
        public async Task AddFileAsync(Guid id, string fileName, Stream source, string contentType = "application/octet-stream", string bucketName = null, IDictionary <string, object> metadata = null)
        {
            var bucket       = GetBucket(bucketName);
            var bsonMetadata = new BsonDocument {
                { nameof(IBlobInfo.ContentType), contentType }
            };

            if (metadata != null)
            {
                bsonMetadata.AddRange(metadata);
            }

            await bucket.UploadFromStreamAsync(id, fileName, source, new GridFSUploadOptions { Metadata = bsonMetadata });
        }
 public BsonDocument GetFlatDocument(BsonDocument document)
 {
     BsonDocument flatDocument = new BsonDocument();
     flatDocument.AllowDuplicateNames = true;
     flatDocument.AddRange(GetElements(document));
     //foreach (BsonElement element in new EnumerateElements { Options = options }.GetElements(document))
     //{
     //    if (!flatDocument.Contains(element.Name))
     //        flatDocument.Add(element);
     //    else
     //        Trace.WriteLine("warning duplicate \"{0}\" : value1 {1} value2 {2}", element.Name, flatDocument[element.Name], element.Value);
     //}
     return flatDocument;
 }
 protected override void UpdateDocument(BsonDocument document, Func<BsonDocument, UpdateCompiler> update)
 {
     var copy = document.DeepClone();
     try
     {
         update(document);
     }
     catch
     {
         document.Clear();
         document.AddRange(copy.AsBsonDocument);
         throw;
     }
 }
Exemple #31
0
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="image"></param>
        /// <param name="faceToken"></param>
        /// <param name="userName"></param>
        /// <param name="info"></param>
        public void SetFace(Image image, string faceToken, string userName)
        {
            DBHelper dBHelper = new DBHelper();
            //图片转码
            string base64 = ImageToBase64(image);

            Dictionary <string, object> data = new Dictionary <string, object> {
                { "Image", base64 }, { "FaceToken", faceToken }, { "UserName", userName }
            };
            BsonDocument bsonElements = new BsonDocument();

            bsonElements.AddRange(data);
            dBHelper.SetData(bsonElements);
        }
Exemple #32
0
        // private methods
        private void AssertWhere(Expression <Func <TestObject, bool> > expression, string expectedStages)
        {
            var actualStages = Execute(CreateWhereQuery(expression));
            var actual       = new BsonDocument();

            foreach (var actualStage in actualStages)
            {
                actual.AddRange(actualStage);
            }

            var expected = BsonDocument.Parse(expectedStages);

            actual.Should().Be(expected);
        }
Exemple #33
0
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            var username        = (string)context["_userName"];
            var isAuthenticated = (bool)context["IsAuthenticated"];

            if (string.IsNullOrWhiteSpace(username) || collection.Count < 1)
            {
                return;
            }

            var values = new Dictionary <string, object>();

            foreach (SettingsPropertyValue settingsPropertyValue in collection)
            {
                if (!settingsPropertyValue.IsDirty)
                {
                    continue;
                }

                if (!isAuthenticated && !(bool)settingsPropertyValue.Property.Attributes["AllowAnonymous"])
                {
                    continue;
                }

                values.Add(settingsPropertyValue.Name, settingsPropertyValue.PropertyValue);
            }

            IMongoQuery query        = Query.And(Query.EQ("ApplicationName", ApplicationName), Query.EQ("Username", username));
            var         bsonDocument = _mongoCollection.FindOneAs <BsonDocument>(query);

            if (bsonDocument == null)
            {
                bsonDocument = new BsonDocument
                {
                    { "ApplicationName", ApplicationName },
                    { "Username", username }
                };
            }

            var mergeDocument = new BsonDocument
            {
                { "LastActivityDate", DateTime.Now },
                { "LastUpdatedDate", DateTime.Now }
            };

            mergeDocument.AddRange(values as IDictionary <string, object>);
            bsonDocument.Merge(mergeDocument);

            _mongoCollection.Save(bsonDocument);
        }
Exemple #34
0
        /// <summary>
        /// Add an automated $group stage to an existing aggregate
        /// </summary>
        /// <param name="target">The target field</param>
        /// <param name="parentType">The type of the parent (containing properties)</param>
        /// <param name="aggregate">The aggregate to be modified</param>
        private void AddGroupHook(string target, Type parentType, ref IAggregateFluent <BsonDocument> aggregate)
        {
            var doc = new BsonDocument("_id", "$_id");

            //Get parent properties, not id or name of target collection
            var props = parentType.GetProperties().Where(x => x.Name != "Id" && x.Name != target)
                        .Select(x => (x.Name, new BsonDocument("$first", "$" + x.Name)))
                        .ToDictionary(x => x.Item1, y => y.Item2);

            doc.AddRange(props);
            doc.Add(target, new BsonDocument("$push", "$" + target));

            aggregate = aggregate.Group(doc);
        }
Exemple #35
0
        protected override void UpdateDocument(BsonDocument document, Func <BsonDocument, UpdateCompiler> update)
        {
            var copy = document.DeepClone();

            try
            {
                update(document);
            }
            catch
            {
                document.Clear();
                document.AddRange(copy.AsBsonDocument);
                throw;
            }
        }
Exemple #36
0
        public BsonDocument GetFlatDocument(BsonDocument document)
        {
            BsonDocument flatDocument = new BsonDocument();

            flatDocument.AllowDuplicateNames = true;
            flatDocument.AddRange(GetElements(document));
            //foreach (BsonElement element in new EnumerateElements { Options = options }.GetElements(document))
            //{
            //    if (!flatDocument.Contains(element.Name))
            //        flatDocument.Add(element);
            //    else
            //        Trace.WriteLine("warning duplicate \"{0}\" : value1 {1} value2 {2}", element.Name, flatDocument[element.Name], element.Value);
            //}
            return(flatDocument);
        }
        private Task SaveReading(Context context)
        {
            var database   = mongoClient.GetDatabase(this.database);
            var collection = database.GetCollection <Reading>("Readings");
            var valuesBson = new BsonDocument();

            valuesBson.AddRange(context.Payload);
            var reading = new Reading()
            {
                ClientId  = context.ClientId,
                Values    = context.Payload,
                CreatedAt = DateTime.UtcNow
            };

            return(collection.InsertOneAsync(reading));
        }
Exemple #38
0
        private void EntryToDocument(IndexValue indexValue, int level, List<BsonDocument> result)
        {
            //Add the real values (not contained) to a document and add that to the result.
            List<IndexValue> notNestedValues = indexValue.Values.Where(exp => (exp is IndexValue) && ((IndexValue)exp).Name != "contained").Select(exp => (IndexValue)exp).ToList();
            var doc = new BsonDocument(new BsonElement(InternalField.LEVEL, level));
            doc.AddRange(notNestedValues.Select(iv => IndexValueToElement(iv)));
            result.Add(doc);

            //Then do that recursively for all contained indexed resources.
            List<IndexValue> containedValues = indexValue.Values.Where(exp => (exp is IndexValue) && ((IndexValue)exp).Name == "contained").Select(exp => (IndexValue)exp).ToList();
            foreach (var contained in containedValues)
            {
                EntryToDocument(contained, level + 1, result);
            }

        }
        public static BsonDocument ToMatchDocument(this MatchDefinition source)
        {
            var result = new BsonDocument();
            var matchFilterElements = new List<BsonElement>();
            source.Filters.Where(x=>x.AvailableFilterValues.Any(y=>y.Active)).ToList().ForEach(f =>
            {
                var colDoc = new BsonDocument();
                var selectedValues = new BsonArray();
                var selectedFilterValues = f.AvailableFilterValues.Where(x=>x.Active).Select(x => x.Value).Select(x => new BsonString(x)).ToList();
                selectedValues.AddRange(selectedFilterValues);
                //var itemE
                var itemElm = new BsonElement("$in", selectedValues);
                colDoc.Add(itemElm);
                var colElm = new BsonElement(f.Column.ColumnName, colDoc);
                matchFilterElements.Add(colElm);
            });
            var elementsDoc = new BsonDocument();
            elementsDoc.AddRange(matchFilterElements);

            var matchElement = new BsonElement("$match", elementsDoc);
            result.Add(matchElement);
            return result;
        }
Exemple #40
0
        public NoSqlPipeline GeoNear(double[] location, string distanceField, params BsonElement[] opts)
        {
            if (Pipeline.Count > 0)
            {
                throw new InvalidOperationException("You can only use $geoNear as the first stage of a pipeline.");
            }

            if (location.Length != 2)
            {
                throw new InvalidOperationException("location[] must contain two double values.");
            }

            var doc = new BsonDocument
            {
                { "near", new BsonArray(location) },
                { "distanceField", distanceField }
            };

            doc.AddRange(opts);

            Pipeline.Add(new BsonDocument { { "$geoNear", doc } });
            Fields.Add("dist");
            return this;
        }
        protected override void UpdateDocument(BsonDocument document, Func<BsonDocument, UpdateCompiler> update)
        {
            var oldId = document[MyValue.Id];
            var copy = document.DeepClone();
            try
            {
                update(document);

                BsonValue newId;
                if (!document.TryGetValue(MyValue.Id, out newId) || !oldId.Equals(newId))
                    throw new InvalidOperationException("Modification of _id is not allowed.");
            }
            catch
            {
                document.Clear();
                document.AddRange(copy.AsBsonDocument);
                throw;
            }
        }
Exemple #42
0
        public BsonDocument getNestedData(Object tcd, string[] excludeDataFields)
        {
            BsonDocument nestedData = new BsonDocument { };
            Dictionary<string, object> dictData = new Dictionary<string, object>();

            var accessor2 = TypeAccessor.Create(tcd.GetType());
            foreach (var prop in accessor2.GetMembers())
            {
                if (!excludeDataFields.Contains(prop.Name))
                {
                    dictData.Add(prop.Name, accessor2[tcd, prop.Name]);
                }
            }
            nestedData.AddRange(dictData);
            return nestedData;
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var obj = Request["reqdata"];
            BsonDocument t = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(obj);
            BsonValue objOrg = t.GetElement("originalGridData").Value;
            BsonValue objDummy = t.GetElement("dummyGridData").Value;
            MyDB db = new MyDB();
            string merchatsess = Session["dedcolname"].ToString();
            string merchdummycoll = Session["dummycol"].ToString();
            var coll = db.GetBColl(merchatsess);
            var dummycoll = db.GetBColl(merchdummycoll);

            if (coll.Count() > 0)
            {
                coll.Drop();
            }
            List<object> orgList = new List<object>();
            BsonDocument bexp = new BsonDocument();
            var coll77 = db.GetBColl(merchatsess);
            for (int i = 0; i <= objOrg.AsBsonArray.Count - 1; i++)
            {

                bexp = new BsonDocument();
                var bids = objOrg[i].AsBsonDocument;
                bexp.AddRange(bids);
                orgList.Add(bexp);
                bexp = null;
            }
            coll77.InsertBatch(orgList);
            BsonDocument bdums = new BsonDocument();
            BsonDocument bdums1 = new BsonDocument();

            var dummycount = dummycoll.Count();
            if (dummycoll.Exists() && dummycount > 0)
            {
                var maxsnosno = dummycoll.FindAll().SetSortOrder(SortBy.Descending("sno")).SetLimit(1).FirstOrDefault();
                var bsonsno = maxsnosno.GetElement("sno").Value;
                var longsno = Convert.ToInt64(bsonsno);

                for (int i = 0; i <= objDummy.AsBsonArray.Count - 1; i++)
                {
                    longsno++;
                    bdums = new BsonDocument();
                    Dictionary<string, long> dict = new Dictionary<string, long>();
                    dict.Add("sno", longsno);

                    var biddums = objDummy[i].AsBsonDocument;
                    bdums.AddRange(biddums);
                    bdums.AddRange(dict);

                    dummycoll.Insert(bdums);
                    bdums = null;
                }
            }
            else
            {
                var longsno1 = 0;
                for (int i = 0; i <= objDummy.AsBsonArray.Count - 1; i++)
                {
                    longsno1++;
                    bdums1 = new BsonDocument();
                    Dictionary<string, long> mydict = new Dictionary<string, long>();
                    mydict.Add("sno", longsno1);

                    var biddums1 = objDummy[i].AsBsonDocument;
                    bdums1.AddRange(biddums1);
                    bdums1.AddRange(mydict);

                    dummycoll.Insert(bdums1);
                    bdums1 = null;
                }
            }

            string f_dedcoll = Session["dedcolname"].ToString();
            var f_coll = db.GetBColl(f_dedcoll);
            IEnumerable<BsonValue> typesregex = f_coll.Distinct("type");
            var outjson = "";
            var outjsonconcat = "";
            var varjson = "";
            BsonElement btype = null;
            BsonValue btypevalue = null;
            BsonElement bmeasures = null;
            string dums = null;
            var jsonoffer = "";
            foreach (string str in typesregex)
            {
                dums = null;
                var query = new QueryDocument("type", str);
                foreach (BsonDocument docs in f_coll.Find(query))
                {
                    btype = docs.GetElement("type");
                    btypevalue = btype.Value;
                    bmeasures = docs.GetElement("measures");
                    string bmes = bmeasures.ToString();
                    var s = bmes.Replace(";", "\",\"");
                    var squareconcat = "[" + "\"" + s.Substring(9) + "\"" + "]";
            var addtobson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(squareconcat);
                    docs.Remove("_id");
                    docs.Remove("measures");
                    docs.Add("measures", addtobson);
                    varjson = docs.ToJson();
                    jsonoffer = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(varjson.ToString()).ToString();
                    dums += jsonoffer.ToString() + ",";
                }
                outjsonconcat = "{\"category\":" + "\"" + btypevalue + "\"" + ",\"items\":[" + dums.Remove(dums.Length - 1, 1) + "]}" + ",";
                outjson += outjsonconcat;
            }
            var stringremove = "[" + outjson.Remove(outjson.Length - 1, 1) + "]";
            string fstore = Session["storename"].ToString();
            string path = Server.MapPath("~/inventory/") + fstore + ".json";

            if (!File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                StreamWriter str11 = new StreamWriter(fs);
                str11.WriteLine(stringremove.ToArray());
                str11.Flush();
                str11.Close();
                fs.Dispose();
                fs.Close();
            }
            else if (File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.Open);
                StreamWriter str12 = new StreamWriter(fs);
                str12.WriteLine(stringremove.ToArray());
                str12.Flush();
                str12.Close();
                fs.Dispose();
                fs.Close();
            }

            Response.Clear();
            Response.CacheControl = "no-cache";
            Response.ContentType = "application/json";
            Response.Write("777");
            Response.End();
        }
        catch (ThreadAbortException e4)
        {

        }
        catch (IOException e3)
        {

        }
        catch (Exception e2)
        {
            Response.Clear();
            Response.CacheControl = "no-cache";
            Response.ContentType = "application/json";
            Response.Write("exception");
            Response.End();
        }
        finally
        {

        }
    }
Exemple #44
0
        public void BuildQuery()
        {
            var source = GetQueryBuilder();
            var jobFamily = source.AvailableFilters.Single(x => x.Column.ColumnName == "Job_Family");
            var jobTrack = source.AvailableFilters.Single(x => x.Column.ColumnName == "Job_Track");

            var matchBuilder = new FilterDefinitionBuilder<BsonDocument>();
            jobFamily.AvailableFilterValues.Add(new FilterValue {Key = "Executive", Value = "Executive", Active=true});

            var jobFamilyFilter = matchBuilder.All(jobFamily.Column.ColumnName,
                jobFamily.AvailableFilterValues.Where(x=>x.Active).Select(x=>x.Value).ToList());
            //take care of grouping key
            var groupDoc = new BsonDocument();
            var slicers = new List<BsonElement>();
            slicers.Add(new BsonElement("s1", new BsonString("$Year")));
            slicers.Add(new BsonElement("s2", new BsonString("$orgType")));
            var idDoc = new BsonDocument(slicers);
            var idElm = new BsonElement("_id", idDoc);

            var idDocument = new BsonDocument();
            idDocument.Add(idElm);

            //take care of grouping val
            var factDoc = new BsonDocument();
            var factEl = new BsonElement("$avg", new BsonString("$Base_Pay"));
            factDoc.Add(factEl);

            var factElementDoc = new BsonDocument();
            factElementDoc.Add(new BsonElement("f1", factDoc));
            var factDocResult = new BsonDocument();
            factDocResult.AddRange(factElementDoc);

            groupDoc.AddRange(idDocument);
            groupDoc.AddRange(factDocResult);

            var groupElement = new BsonElement("$group", groupDoc);

            var matchDoc = new BsonDocument();
            var filter1Element = new BsonElement("Job_Family", new BsonString("Executive"));
            var filter1Doc = new BsonDocument();
            filter1Doc.Add(filter1Element);
            var filterFinalElement = new BsonElement("$match", filter1Doc);

            var finalDoc = new BsonDocument();
            finalDoc.Add(filterFinalElement);
            finalDoc.Add(groupElement);

            var dd = finalDoc.ToString();
            Console.Out.Write(dd);
            var xx = "Y";

            // jobTrack.
        }
    public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
    {
      var username = (string)context["UserName"];
      var isAuthenticated = (bool)context["IsAuthenticated"];

      if (string.IsNullOrWhiteSpace(username) || collection.Count < 1)
      {
        return;
      }

      var values = new Dictionary<string, object>();

      foreach (SettingsPropertyValue settingsPropertyValue in collection)
      {
        if (!settingsPropertyValue.IsDirty)
        {
          continue;
        }

        if (!isAuthenticated && !(bool)settingsPropertyValue.Property.Attributes["AllowAnonymous"])
        {
          continue;
        }

        values.Add(settingsPropertyValue.Name, settingsPropertyValue.PropertyValue);
      }

      var query = Query.And(Query.EQ("ApplicationName", ApplicationName), Query.EQ("Username", username));
      var bsonDocument = mongoCollection.FindOneAs<BsonDocument>(query) ?? new BsonDocument
            {
              { "ApplicationName", ApplicationName },
              { "Username", username }
            };

      var mergeDocument = new BsonDocument
            {
                { "LastActivityDate", DateTime.Now },
                { "LastUpdatedDate", DateTime.Now }
            };

      mergeDocument.AddRange(values as IDictionary<string, object>);
      bsonDocument.Merge(mergeDocument);

      mongoCollection.Save(bsonDocument);
    }
 static IEnumerable<BsonElement> ToGroup(this GroupDefinition source)
 {
     var result = new BsonDocument();
     var groupDoc = new BsonDocument();
     //
     groupDoc.AddRange(source.ToId());
     groupDoc.AddRange(source.ToFact());
     var groupElement = new BsonElement("$group", groupDoc);
     result.Add(groupElement);
     return result;
 }
 /// <summary>
 /// InsertOneAsync in the Collection
 /// </summary>
 /// <param name="departmentName"></param>
 /// <param name="headOfDepartmentId"></param>
 async private static void InsertOneAsync(string collectionName, List<BsonElement> jsonElements)
 {
     IMongoDatabase myDB = Connection();
     IMongoCollection<BsonDocument> collections = myDB.GetCollection<BsonDocument>(collectionName);
     BsonDocument document = new BsonDocument();
     List<BsonElement> elements = jsonElements;
     document.AddRange(elements);
     await collections.InsertOneAsync(document);
 }
        public static BsonDocument ToProjectionDocument(this GroupDefinition source)
        {
            var keyItems = new List<BsonElement>();
            var valueItems = new List<BsonElement>();

            var ignoreId = new BsonElement("_id", new BsonInt32(0));

            for (var i = 0; i < source.Dimensions.Count; i++)
            {
                var el = new BsonElement(String.Format("s{0}", i), new BsonString(String.Format("$_id.s{0}", i)));
                keyItems.Add(el);
            }
            for (var i = 0; i < source.Measures.Count; i++)
            {
                var el = new BsonElement(String.Format("f{0}", i), new BsonString(String.Format("$f{0}", i)));
                valueItems.Add(el);
            }

            var keyValuesDoc = new BsonDocument();
            keyValuesDoc.AddRange(keyItems);
            var keyValuesElement = new BsonElement("key", keyValuesDoc);

            var valueValuesDoc = new BsonDocument();
            valueValuesDoc.AddRange(valueItems);

            var valueValuesElement = new BsonElement("value", valueValuesDoc);

            var ignoreIdDoc = new BsonDocument();
            ignoreIdDoc.Add();

            var projectDoc = new BsonDocument();
            projectDoc.Add(new BsonElement("_id", new BsonInt32(0)));
            projectDoc.Add(keyValuesElement);
            projectDoc.Add(valueValuesElement);
            var projectElement = new BsonElement("$project", projectDoc);
            var result = new BsonDocument {projectElement};
            return result;
        }
Exemple #49
0
        private void ButtonConsolidateOutputFiles_Click(object sender, RoutedEventArgs e)
        {
            var writeEachConceptFeature = CheckBoxExtractTextFeaturesEachTweet.IsChecked.Value;
            var concepteaturesOutputTableName = TextBoxConceptFeatures.Text;
            var conceptFeaturesOutputTable = database.GetCollection<BsonDocument>(concepteaturesOutputTableName);
            var imagesFolder = TextBoxImagesFilesFolder.Text;
            var filesInDir = Directory.GetFiles(imagesFolder, "*.txt");

            var normaliseFeatures = CheckBoxNormaliseFeatures.IsChecked.Value;

            Task.Run(() =>
            {
                var filesProcessed = 0;

                foreach (var file in filesInDir)
                {
                    try
                    {
                        var imageId = Path.GetFileNameWithoutExtension(file);

                        if (imageId == tweetsPathFileName) continue;

                        var userRecordValues = File.ReadAllText(file)
                            .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                        var concepts = userRecordValues.Select(userRecordValue => float.Parse(userRecordValue, CultureInfo.InvariantCulture)).ToList();

                        if (featureSetSize != concepts.Count)
                        {
                            Write("Inconsistent features set size: " + concepts.Count + " " + imageId);
                            continue;
                        }

                        var twitterId = mediaTable.FindOne(Query.EQ("_id", imageId))["twitter_id"].AsString;

                        if (!usersScores.Contains(twitterId))
                        {
                            usersScores.Add(twitterId, new Dictionary<string, List<float>>());
                        }

                        usersScores[twitterId].Add(imageId, concepts);
                    }
                    catch (Exception exc)
                    {
                        Write(exc.ToString());
                    }

                    filesProcessed++;

                    if (filesProcessed % 1000 == 0)
                    {
                        Write("File processed: " + filesProcessed);
                    }
                }

                var min = double.MaxValue;
                var max = double.MinValue;

                foreach (var userScore in usersScores)
                {
                    foreach (var scoreList in userScore.Value)
                    {
                        foreach (var score in scoreList.Value)
                        {
                            if (score < min) min = score;
                            if (score > max) max = score;
                        }
                    }
                }

                double range;
                double topUp;

                if (Math.Sign(max) == Math.Sign(min))
                {
                    range = Math.Abs(max) - Math.Abs(min);
                    topUp = - Math.Abs(min);
                }
                else
                {
                    range = Math.Abs(max) + Math.Abs(min);
                    topUp = Math.Abs(min);
                }

                Write("Max: " + max);
                Write("Min: " + min);
                Write("Range: " + range);
                Write("Topup: " + topUp);

                foreach (var userScore in usersScores)
                {
                    if (writeEachConceptFeature)
                    {
                        foreach (var scoreList in userScore.Value)
                        {
                            var conceptFeaturesDoc = new BsonDocument
                            {
                                {"_id", scoreList.Key},
                                {"concepts", new BsonArray(scoreList.Value)}
                            };

                            conceptFeaturesOutputTable.Insert(conceptFeaturesDoc);
                        }
                    }
                    else
                    {
                        if (conceptFeaturesOutputTable.FindOne(Query.EQ("_id", userScore.Key)) != null) continue;

                        var probabilities = new Dictionary<int, double>();
                        var numberOfPicturesTaken = userScore.Value.Count;

                        var conceptFeaturesDoc = new BsonDocument
                        {
                            {"_id", userScore.Key},
                            {"numberOfImages", numberOfPicturesTaken}
                        };

                        foreach (var scoreList in userScore.Value)
                        {
                            var counter = 0;

                            foreach (var score in scoreList.Value)
                            {
                                counter++;
                                var probability = (score + topUp)/range;

                                if (!probabilities.ContainsKey(counter))
                                {
                                    probabilities.Add(counter, 0);
                                }

                                if (binaryFeatures)
                                {
                                    probabilities[counter] += probability > treshhold ? 1 : 0;
                                }
                                else
                                {
                                    probabilities[counter] += probability;
                                }
                            }
                        }

                        if (!normaliseFeatures)
                        {
                            numberOfPicturesTaken = 1;
                        }

                        var record =
                            probabilities.ToDictionary(probability => probability.Key.ToString(CultureInfo.InvariantCulture),
                                probability => probability.Value/numberOfPicturesTaken);

                        conceptFeaturesDoc.AddRange(record);
                        conceptFeaturesOutputTable.Insert(conceptFeaturesDoc);
                    }
                }
            });
        }
        static IEnumerable<BsonElement> ToFact(this GroupDefinition source)
        {
            var result = new BsonDocument();
            for (var i = 0; i < source.Measures.Count; i++)
            {
                var fact = source.Measures[i];
                var op = "";
                switch (fact.Operation)
                {
                    case AggregateOperations.Average:
                        op = "$avg";
                        break;
                    case AggregateOperations.Sum:
                        op = "$sum";
                        break;
                }
                var el = new BsonElement(op, new BsonString(String.Format("${0}", fact.Column.ColumnName)));
                var factDoc = new BsonDocument();
                factDoc.Add(el);
                var factElementDoc = new BsonDocument();
                factElementDoc.Add(new BsonElement(String.Format("f{0}", i), factDoc));
                result.AddRange(factElementDoc);

            }
            return result;
        }
Exemple #51
0
        public void BuildProjectTest()
        {
            var groupDefinition = GetGroupDefinition();
            var keyItems = new List<BsonElement>();
            var valueItems = new List<BsonElement>();

            var ignoreId = new BsonElement("_id", new BsonInt32(0));

            for (var i = 0; i < groupDefinition.Dimensions.Count; i++)
            {
                var el = new BsonElement(String.Format("s{0}", i), new BsonString(String.Format("$_id.s{0}", i)));
                keyItems.Add(el);
            }
            for (var i = 0; i < groupDefinition.Measures.Count; i++)
            {
                var el = new BsonElement(String.Format("f{0}", i), new BsonString(String.Format("$f{0}", i)));
                valueItems.Add(el);
            }

            var keyValuesDoc = new BsonDocument();
            keyValuesDoc.AddRange(keyItems);
            var keyValuesElement = new BsonElement("key", keyValuesDoc);

            var valueValuesDoc = new BsonDocument();
            valueValuesDoc.AddRange(valueItems);

            var valueValuesElement = new BsonElement("value", valueValuesDoc);

            var ignoreIdDoc = new BsonDocument();
            ignoreIdDoc.Add();

            var projectDoc = new BsonDocument();
            projectDoc.Add(new BsonElement("_id", new BsonInt32(0)));
            projectDoc.Add(keyValuesElement);
            projectDoc.Add(valueValuesElement);
            var projectElement = new BsonElement("$project", projectDoc);
            var finalDoc = new BsonDocument();
            finalDoc.Add(projectElement);
            //var projectValuesDoc  = new BsonDocument();

            //projectValuesDoc.AddRange(keyItems);

            //var projectItemsElement = new BsonElement("$project", projectValuesDoc);
            //var finalDoc = new BsonDocument();
            //finalDoc.Add(projectItemsElement);
            Console.Out.Write(finalDoc.ToString());
        }
Exemple #52
0
        private static BsonDocument BuildPropertiesBsonDocument(Properties props, Properties globProps)
        {
            var result = new BsonDocument();

            if (globProps != null)
                result.AddRange(globProps.PropsDictionary);

            if (props != null)
                result.AddRange(props.PropsDictionary);

            return result;
        }
Exemple #53
0
        public override int processTestData(DateTime start, DateTime end)
        {
            int cnt = 0;
            // Import TOSA data from Scotland database (mySQL
            try
            {
                MongoViewHelper mvh = new MongoViewHelper("testdata");
                String connstr = "SERVER=liv-svr-mysql3;DATABASE=xosa;UID=newark;PASSWORD=GFS54ad:)4dfH;Connection Timeout=7000";
                MySqlConnection connection = new MySqlConnection(connstr);
                connection.Open();
                string query = "SELECT * FROM osa_test,osa_sub_test,stripe,osa_sub_test_osa_stripe " +
                    "WHERE test_date >= '" + start.ToString("yyyy-MM-dd HH:mm:ss") + "' AND test_date < '" + end.ToString("yyyy-MM-dd HH:mm:ss") + "' " +
                    "AND osa_test.id = osa_sub_test.test_id " +
                    "AND stripe.id = osa_sub_test_osa_stripe.osa_stripe_id " +
                    "AND osa_sub_test.id = osa_sub_test_osa_stripe.osa_sub_test_stripes_id";
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.CommandTimeout = 7200;
                //Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();
                DataTable schemaTable = dataReader.GetSchemaTable();
                string[] lists = { "liv_current_ma", "liv_power_mw", "liv_voltagev", "liv_mpd_ua",
                    "mpd_current_ua_stripe0", "mpd_current_ua_stripe1", "mpd_current_ua_stripe2", "mpd_current_ua_stripe3",
                    "mpd_ratio_db_stripe0", "mpd_ratio_db_stripe1", "mpd_ratio_db_stripe2", "mpd_ratio_db_stripe3"};
                char[] sep = { ' ' };
                while (dataReader.Read())
                {
                    cnt++;
                    BsonDocument bson = new BsonDocument();
                    foreach (DataRow row in schemaTable.Rows)
                    {
                        String col = row["ColumnName"].ToString();
                        if (!excludeDataFields.Contains(col))
                        {
                            Dictionary<string, object> dictData = new Dictionary<string, object>();

                            if (dataReader[col].GetType().ToString() == "System.TimeSpan")
                            {
                                int secs = 0;
                                System.TimeSpan ts = (System.TimeSpan)dataReader[col];
                                secs += ts.Seconds;
                                secs += ts.Minutes * 60;
                                secs += ts.Hours * 3600;
                                secs += ts.Days * 86400;
                                dictData.Add(col, secs);
                            }
                            else
                            {
                                var s = dataReader[col].GetType().ToString();
                                if (s != "System.DBNull")
                                {
                                    if (!lists.Contains(col))
                                    {
                                        dictData.Add(col, dataReader[col]);
                                    }
                                    else
                                    {
                                        String sl = dataReader[col].ToString();
                                        string[] sa = sl.Split(sep, StringSplitOptions.RemoveEmptyEntries);
                                        dictData.Add(col, Array.ConvertAll(sa, i => float.Parse(i)));
                                    }
                                }
                            }
                            if (!bson.Contains(col))
                            {
                                bson.AddRange(dictData);
                            }
                        }
                    }

                    BsonDocument rootDoc = new BsonDocument {
                         { "_id", "TOSA-" + bson["osa_stripe_id"]},
                         { "mid",  "TOSAMID-" + bson["osa_sub_test_stripes_id"] },
                         { "timestamp", bson["test_date"]},
                         { "type", "tosa" },
                         { "subtype", "dc" },
                         { "result", bson["pass"] == 1 ? "P" : "F" },
                         { "measstatus", bson["pass"] == 1 ? "P" : "F"},
                         { "status", bson["pass"] == 1 ? "P" : "F" }
                    };

                    rootDoc.Add("meta", new BsonDocument {
                         { "StartDateTime",  bson["test_date"]},
                         { "EndDateTime",  bson["test_date"]},
                         { "Channel",  bson["stripe_number"]}
                    });

                    if (bson["tosa_serial_number"] != null)
                    {
                        string[] tsn = bson["tosa_serial_number"].ToString().Split('_');
                        bson["tsn"] = tsn[0];
                        bson.Add("laser_pn", new BsonArray());
                        for (int i = 1; i < tsn.Length; i++)
                        {
                            ((BsonArray)bson["laser_pn"]).Add(tsn[i]);
                        }
                        bson.Remove("tosa_serial_number");

                        string tosaType = bson["tsn"].ToString().Substring(0, 3);
                        string serNum = bson["tsn"].ToString().Substring(3);
                        rootDoc.Add("device", new BsonDocument {
                             { "SerialNumber", serNum},
                             { "TosaType", tosaType}
                        });

                        bson.Remove("osa_stripe_id");
                        bson.Remove("osa_sub_test_stripes_id");
                        bson.Remove("test_date");
                        bson.Remove("stripe_number");
                        bson.Remove("pass");
                        rootDoc.Add("data", bson);

                        mvh.Collection.Save(rootDoc);
                    }
                }

                //close Data Reader and connection
                dataReader.Close();
                connection.Close();
            }
            catch (Exception exc)
            {
                Program.log("TOSA import ERROR: " + exc.Message + "\n" + exc.StackTrace);
            }
            return cnt;
        }
Exemple #54
0
 public BsonDocument CreateBsonDocument()
 {
     BsonDocument bsonDocument = new BsonDocument();
     bsonDocument.AddRange(GetDBKeyVal());
     return bsonDocument;
 }
Exemple #55
0
        // Loops through domains and executing sync for each domain
        public void startSync()
        {
            // Import data from SQL server
            foreach (var domain in domains)
            {
                try
                {
                    Type type = Type.GetType("kaiam.MongoSync.Sync." + domain);
                    if (type != null)
                    {
                        SyncBase sync = (SyncBase)Activator.CreateInstance(type);
                        sync.toMongoTestData(-15);
                    }
                }
                catch (Exception exc)
                {
                    Program.log(domain + " ERROR: " + exc.Message + "\n" + exc.StackTrace);
                }
            }

            // Import measurement records without any down

            // Import beta module data from excel files
            try
            {
                Type typeBm = Type.GetType("kaiam.MongoSync.Sync.BetaModule");
                SyncBase syncBm = (SyncBase)Activator.CreateInstance(typeBm);
                syncBm.toMongoTestData(0);
            }
            catch (Exception exc)
            {
                Program.log("BetaModule ERROR: " + exc.Message + "\n" + exc.StackTrace);
            }

            // Import views from SQL server every night at 2 AM
            DateTime date1 = DateTime.Now;
            String hour = date1.ToString("%htt");
            if (hour != "2AM")
            {
                return;
            }
            try
            {
                String connstr = "data source=dbs1.kaiam.local;initial catalog=KAIAM.Data.Test.Production49;user id=KAIAM.TestUser;password=5525Iamkaiam!";
                SqlConnection remoteConnection = new SqlConnection(connstr);
                remoteConnection.Open();
                foreach (var dbview in dbviews)
                {
                    MongoViewHelper mvh = new MongoViewHelper(dbview);
                    mvh.Collection.RemoveAll();
                    SqlCommand myCommand = new SqlCommand("select * from " + dbview, remoteConnection);
                    SqlDataReader myReader = myCommand.ExecuteReader();
                    DataTable schemaTable = myReader.GetSchemaTable();
                    while (myReader.Read())
                    {
                        BsonDocument bson = new BsonDocument();
                        foreach (DataRow row in schemaTable.Rows)
                        {
                            Dictionary<string, object> dictData = new Dictionary<string, object>();
                            if (myReader[row["ColumnName"].ToString()].GetType().ToString() == "System.TimeSpan")
                            {
                                int secs = 0;
                                System.TimeSpan ts = (System.TimeSpan)myReader[row["ColumnName"].ToString()];
                                secs += ts.Seconds;
                                secs += ts.Minutes * 60;
                                secs += ts.Hours * 3600;
                                secs += ts.Days * 86400;
                                dictData.Add(row["ColumnName"].ToString(), secs);
                            }
                            else
                            {
                                dictData.Add(row["ColumnName"].ToString(), myReader[row["ColumnName"].ToString()]);
                            }
                            bson.AddRange(dictData);
                        }
                        mvh.Collection.Save(bson);
                    }
                    myReader.Close();
                }
                remoteConnection.Close();

            }
            catch (Exception exc) {
                Program.log("VIEW import ERROR: " + exc.Message + "\n" + exc.StackTrace);
            }
        }
        /// <summary>
        ///     OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            //Project
            var project = QueryFieldPicker.GetAggregation();
            var supressAggr = project[0];
            var projectAggr = project[1];
            if (supressAggr[0].AsBsonDocument.ElementCount > 0)
            {
                Aggregation.Add(supressAggr);
            }
            //TODO:需要优化,全项目的时候,不用输出
            if (projectAggr[0].AsBsonDocument.ElementCount > 0)
            {
                Aggregation.Add(projectAggr);
            }

            //match
            var match = ConditionPan.GetMatchDocument();
            if (match != null)
            {
                Aggregation.Add(match);
            }

            //Sort
            var sort = SortPanel.GetSortDocument();
            if (sort != null)
            {
                Aggregation.Add(sort);
            }

            //Group
            if (chkIdNull.Checked)
            {
                var id = new BsonDocument();
                id.Add(new BsonElement("_id", BsonNull.Value));
                id.AddRange(FieldsElement.Value.AsBsonDocument.Elements);
                var group = new BsonDocument("$group", id);
                Aggregation.Add(group);
            }
            else
            {
                if (!string.IsNullOrEmpty(GroupIdElement.Name))
                {
                    var id = new BsonDocument();
                    id.Add(new BsonElement("_id", GroupIdElement.Value));
                    id.AddRange(FieldsElement.Value.AsBsonDocument.Elements);
                    var group = new BsonDocument("$group", id);
                    Aggregation.Add(group);
                }
            }

            //Skip
            if (chkSkip.Checked && int.Parse(txtSkip.Text) > 0)
            {
                Aggregation.Add(new BsonDocument("$skip", int.Parse(txtSkip.Text)));
            }
            //Limit
            if (chkLimit.Checked && int.Parse(txtLimit.Text) > 0)
            {
                Aggregation.Add(new BsonDocument("$limit", int.Parse(txtLimit.Text)));
            }
            //IndexStats
            if (chkIndexStats.Checked)
            {
                Aggregation.Add(new BsonDocument("$indexStats", new BsonDocument()));
            }
            //sortByCount
            if (chkSortByCount.Checked)
            {
                Aggregation.Add(new BsonDocument("$sortByCount", cmbSortByCount.Text));
            }
            //Sample
            if (chkSample.Checked)
            {
                var size = new BsonDocument("size", (int.Parse(txtSample.Text)));
                Aggregation.Add(new BsonDocument("$sample", size));
            }
            //unwind
            if (chkUnwind.Checked)
            {
                if (!chkPreserveNullAndEmptyArrays.Checked && string.IsNullOrEmpty(txtincludeArrayIndex.Text))
                {
                    Aggregation.Add(new BsonDocument("$unwind", cmbUnwind.Text));
                }
                else
                {
                    var UnwindDoc = new BsonDocument();
                    var field = new BsonElement("path", cmbUnwind.Text);
                    UnwindDoc.Add(field);
                    if (chkPreserveNullAndEmptyArrays.Checked)
                    {
                        var preserveNullAndEmptyArrays = new BsonElement("preserveNullAndEmptyArrays", BsonBoolean.True);
                        UnwindDoc.Add(preserveNullAndEmptyArrays);
                    }
                    if (!string.IsNullOrEmpty(txtincludeArrayIndex.Text))
                    {
                        var includeArrayIndex = new BsonElement("includeArrayIndex", txtincludeArrayIndex.Text);
                        UnwindDoc.Add(includeArrayIndex);
                    }
                    Aggregation.Add(new BsonDocument("$unwind", UnwindDoc));
                }
            }
            Close();
        }
 public static BsonDocument ToGroupDocument(this GroupDefinition source)
 {
     var result = new BsonDocument();
     result.AddRange(source.ToGroup());
     return result;
 }
        private static BsonDocument buildMatchCondition(IReportSpecification specification)
        {
            IMongoQuery orClause = createSearchClauseForAllFilters(specification);
            IMongoQuery typeNameClause = createSearchClauseForAllTypes(specification);
            // Query.EQ("TypeName", specification.TrackerTypeName);
            IMongoQuery dateClause = Query.And(Query.GTE("TimeSlot", specification.FromDateUtc),
                Query.LTE("TimeSlot", specification.ToDateUtc));

            var conditions = new BsonDocument(dateClause.ToBsonDocument());
            conditions.AddRange(typeNameClause.ToBsonDocument());
            if (orClause != null)
                conditions.AddRange(orClause.ToBsonDocument());
            var match = new BsonDocument
            {
                {
                    "$match", conditions
                }
            };
            return match;
        }