Exemple #1
0
        public void ValidateTagField_ShouldNotValidate_DocumentWithInvalidTagType(object document, bool?tagIsArray)
        {
            // Arrange
            var globalStoreManagerMock = new Mock <IGlobalStoreManager>();
            var service = new DocumentService(globalStoreManagerMock.Object, null, null, null, null);
            var dataSet = GetSampleDataSet();

            // Act
            var result = service.ValidateTagField(JTokenHelper.GetToken(document), dataSet.DataSet.TagField, tagIsArray, null, false);

            // Assert
            Assert.Equal(true, result.IsFailure);
        }
        public Result ValidateUpdateDocument(string dataSetName, object document)
        {
            var dataSet = DataSet(dataSetName);

            return(ValidateUpdateDocument(
                       JTokenHelper.GetToken(document),
                       dataSet.DataSet.IdField,
                       dataSet.DataSet.TagField,
                       dataSet.TagIsArray,
                       dataSet.TagIsInteger,
                       dataSet.DataSet.InterpretedFields,
                       dataSet.AttachmentFields,
                       dataSet.DocumentFields));
        }
Exemple #3
0
        public void Read(Property property, PropertyItem item, Data data)
        {
            JObject task   = JObject.FromObject(data.Value);
            var     wtDate = JTokenHelper.GetPropertyValue <WtDate>(task, property.Key);

            item.TimeSpan = new TimeSpan();
            if (wtDate != null && wtDate.Date.HasValue)
            {
                item.Date = new DateTime(wtDate.Date.Value.Year, wtDate.Date.Value.Month, wtDate.Date.Value.Day);
                if (wtDate.WithTime)
                {
                    item.TimeSpan = wtDate.Date.Value - item.Date.Value;
                }
            }
        }
Exemple #4
0
        public override void Read(KanbanItem kanban, WtTaskProperty property, ValueElement task, TaskState state, ShowSetting setting, ApiMissionVnextKanbanContent data)
        {
            var kbp = new KanbanItemProperty
            {
                Name = "实际工时"
            };

            SetColor(kbp, setting.Color);

            JObject jObj     = JObject.FromObject(task);
            var     workload = JTokenHelper.GetPropertyValue <WorkloadValue>(jObj, property.Key);

            if (workload.Actual != 0)
            {
                kbp.Value = workload.Actual.ToString();
                kanban.Properties.Add(kbp);
            }
        }
Exemple #5
0
        public override void Read(KanbanItem kanban, WtTaskProperty property, ValueElement task, TaskState state, ShowSetting setting, ApiMissionVnextKanbanContent data)
        {
            var kbp = new KanbanItemProperty
            {
                Name = property.Name
            };

            SetColor(kbp, setting.Color);

            JObject jObj = JObject.FromObject(task);
            var     span = JTokenHelper.GetPropertyValue <DateSpan>(jObj, property.Key);

            if (span.Begin.Date.HasValue && span.End.Date.HasValue)
            {
                kbp.Value = WtDateTimeHelper.ToWtKanbanDate(span.Begin.Date.Value)
                            + " ~ "
                            + WtDateTimeHelper.ToWtKanbanDate(span.End.Date.Value);
                kanban.Properties.Add(kbp);
            }
        }
Exemple #6
0
        public override void Read(KanbanItem kanban, WtTaskProperty property, ValueElement task, TaskState state, ShowSetting setting, ApiMissionVnextKanbanContent data)
        {
            JObject jObj = JObject.FromObject(task);

            string[] tagIds = JTokenHelper.GetPropertyValue <string[]>(jObj, property.Key);
            if (tagIds.Length > 0)
            {
                foreach (var id in tagIds)
                {
                    var tag = data.Data.References.Lookups.Tags.Single(t => t.Id == id);
                    var kbp = new KanbanItemProperty
                    {
                        Value      = tag.Name,
                        Foreground = new SolidColorBrush(Colors.White),
                        Background = WtColorHelper.GetNewBrush(tag.Color)
                    };
                    kanban.Properties.Add(kbp);
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Returns with the value of the given path.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="path">E.g.: name.firstname</param>
        /// <returns>Framework's primitive types</returns>
        public static object GetValue(object document, string path)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException(nameof(path));
            }

            var token = JTokenHelper.GetPathToken(JToken.FromObject(document), path);

            if (token == null)
            {
                return(null);
            }

            var valueToken = token as JValue;

            if (valueToken != null)
            {
                return(valueToken.Value);
            }

            var arrayToken = token as JArray;

            if (arrayToken != null)
            {
                if (arrayToken.First?.Type == JTokenType.Integer)
                {
                    return(arrayToken.Select(item => (int)item).ToArray());
                }

                return(arrayToken.Select(item => (string)item).ToArray());
            }

            return(token);
        }
Exemple #8
0
 public override void Read(KanbanItem kanban, WtTaskProperty property, ValueElement task, TaskState state, ShowSetting setting, ApiMissionVnextKanbanContent data)
 {
     if (property.RawKey == "title")
     {
         kanban.Title = task.Title;
     }
     else
     {
         JObject jObj  = JObject.FromObject(task);
         string  value = JTokenHelper.GetPropertyValue <string>(jObj, property.Key);
         if (value != null)
         {
             var kbp = new KanbanItemProperty
             {
                 Name  = property.Name,
                 Value = value
             };
             SetColor(kbp, setting.Color);
             kanban.Properties.Add(kbp);
         }
     }
 }
Exemple #9
0
        public override void Read(KanbanItem kanban, WtTaskProperty property, ValueElement task, TaskState state, ShowSetting setting, ApiMissionVnextKanbanContent data)
        {
            var kbp = new KanbanItemProperty
            {
                Name = property.Name
            };

            SetColor(kbp, setting.Color);

            JObject jObj  = JObject.FromObject(task);
            string  value = JTokenHelper.GetPropertyValue <string>(jObj, property.Key);

            if (value != null)
            {
                if (property.Lookup == null)
                {
                    kbp.Value = value;
                }
                else
                {
                    JObject lookup = JObject.FromObject(data.Data.References.Lookups);
                    var     jItem  = lookup[property.Lookup].Single(item => item["_id"].Value <string>() == value);
                    if (jItem is JObject obj)
                    {
                        if (obj.ContainsKey("name"))
                        {
                            kbp.Value = jItem["name"].Value <string>();
                            kanban.Properties.Add(kbp);
                        }
                        else if (obj.ContainsKey("text"))
                        {
                            kbp.Value = jItem["text"].Value <string>();
                            kanban.Properties.Add(kbp);
                        }
                    }
                }
            }
        }
Exemple #10
0
        public void Read(Property property, PropertyItem item, Data data)
        {
            JObject task  = JObject.FromObject(data.Value);
            string  value = JTokenHelper.GetPropertyValue <string>(task, property.Key);

            if (value != null)
            {
                var lookup = JObject.FromObject(data.References.Lookups);
                if (property.Lookup != null)
                {
                    var jItem = lookup[property.Lookup].Single(l => l["_id"].Value <string>() == value);
                    var jObj  = jItem as JObject;
                    item.SelectedValue = new DropdownItem
                    {
                        Glyph = WtIconHelper.GetGlyph("wtf-level-" + jObj.Value <string>("icon")),
                        Text  = jObj.Value <string>("name"),
                        Value = jObj.Value <string>("_id"),
                        Color = WtColorHelper.GetNewBrush(jObj.Value <string>("color"))
                    };
                    item.PropertyId = JTokenHelper.GetPropertyValue <string>(task, property.PropertyKey + ".property_id");
                }
            }
        }
Exemple #11
0
        public override void Read(KanbanItem kanban, WtTaskProperty property, ValueElement task, TaskState state, ShowSetting setting, ApiMissionVnextKanbanContent data)
        {
            var kbp = new KanbanItemProperty
            {
                Name = property.Name
            };

            SetColor(kbp, setting.Color);

            JObject jObj = JObject.FromObject(task);
            WtDate  date = null;

            if (property.Key.Count(k => k == '.') > 0)
            {
                date = JTokenHelper.GetPropertyValue <WtDate>(jObj, property.Key);
            }
            else
            {
                date = new WtDate();
                string timestamp = jObj[property.Key].Value <string>();
                if (timestamp != null)
                {
                    date.Date = WtDateTimeHelper.GetDateTime(timestamp);
                }
            }

            if (date.Date.HasValue)
            {
                kbp.Value = date.Date.Value.ToWtKanbanDate();
                if (property.RawKey == "due" && date.Date.Value < DateTime.Now)
                {
                    kbp.Foreground = WtColorHelper.DangerColor;
                    kbp.Background = WtColorHelper.DangerColor1A;
                }
                kanban.Properties.Add(kbp);
            }
        }
Exemple #12
0
        public static void RemoveTagIds(object document, string fieldName, List <string> tagIdsToRemove)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var token = document as JToken;

            if (token == null)
            {
                return;
            }

            var field = JTokenHelper.GetPathToken(token, fieldName);

            var valueToken = field as JValue;

            if (valueToken != null)
            {
                valueToken.Value = string.Empty;
            }

            var arrayToken = field as JArray;

            if (arrayToken != null)
            {
                var items = arrayToken.Where(tag => tagIdsToRemove.Contains((string)tag))
                            .Select(tag => tag)
                            .ToList();

                foreach (var item in items.Where(i => i != null))
                {
                    arrayToken.Remove(item);
                }
            }
        }
        public IEnumerable <PrcRecommendationResult> RecommendById(string id, PrcSettingsElastic prcSettings, PrcRecommendationByIdRequest request)
        {
            var result             = new List <PrcRecommendationResult>();
            var globalStoreDataSet = GlobalStore.DataSets.Get(prcSettings.DataSetName);
            var dataSet            = globalStoreDataSet.DataSet;

            var documentQuery           = queryFactory.GetDocumentQuery(dataSet.Name);
            var fieldsForRecommendation = prcSettings.FieldsForRecommendation;

            var filterOrWeight = !string.IsNullOrEmpty(request.Query) || request?.Weights?.Any() == true;

            var tagId = string.Empty;

            if (string.IsNullOrEmpty(request.TagId))
            {
                var documentElastic = documentQuery.Get(request.DocumentId);
                if (documentElastic == null)
                {
                    return(result);
                }
                var tagToken = JTokenHelper.GetToken(documentElastic.DocumentObject).GetPathToken(dataSet.TagField);
                tagId = JTokenHelper.GetUnderlyingToken(tagToken)?.ToString();
                if (tagId == null)
                {
                    return(result);
                }
            }
            else
            {
                tagId = request.TagId;
            }

            var similarDocIdsWithScore = redisHandler.GetTopNDocuments(new PrcIndexRedisKey(id, tagId, request.DocumentId), filterOrWeight ? -1 : request.Count - 1);

            Dictionary <string, double> resultDictionary = similarDocIdsWithScore;

            var documentElastics = (filterOrWeight || request.NeedDocumentInResult) ?
                                   GetDocuments(dataSet.Name, request.Query, null, fieldsForRecommendation, similarDocIdsWithScore.Keys, request.NeedDocumentInResult) :
                                   null;

            // ha a Filter és a Weights is üres, a TOP Count doksi Id - t visszaadjuk score-jaikkal. (ha kell a document is, akkor elastic - tól elkérjük ezeket pluszban)
            if (filterOrWeight)
            {
                // ezekre a doksikra módosítjuk a prc score - t a Weights-el
                var docIdsWithScore = documentElastics.ToDictionary(k => k.Id, v => similarDocIdsWithScore[v.Id]);

                //súlyozás
                if (request?.Weights?.Any() == true)
                {
                    var weightsDic = request.Weights.ToDictionary(key => Guid.NewGuid().ToString(), value => value);

                    var docIds  = docIdsWithScore.Keys.ToList();
                    var queries = weightsDic.ToDictionary(key => key.Key, value => documentQuery.PrefixQueryFields(value.Value.Query, globalStoreDataSet.DocumentFields));
                    var ids     = documentQuery.GetExistsForQueries(queries, docIds).ToDictionary(k => k.Key, v => v.Value.ToDictionary(ke => ke, va => va));

                    var allWeightsCount = request.Weights.Count;
                    foreach (var docId in docIds)
                    {
                        var weightsSum = weightsDic.Where(w => ids[w.Key].ContainsKey(docId)).Sum(w => w.Value.Value);
                        var pow        = 1 + (weightsSum / allWeightsCount);
                        var score      = Math.Pow(docIdsWithScore[docId] + 1, pow) - 1;
                        docIdsWithScore[docId] = score;
                    }
                }

                resultDictionary = docIdsWithScore;
            }

            var recommendation = resultDictionary
                                 .OrderByDescending(o => o.Value)
                                 .Take(request.Count)
                                 .Select(s => new PrcRecommendationResult()
            {
                DocumentId = s.Key,
                Score      = s.Value,
                Document   = request.NeedDocumentInResult ? documentElastics.SingleOrDefault(d => d.Id == s.Key).DocumentObject : null
            });

            return(recommendation);
        }
        public Result ValidateSampleDocument(DataSet dataSet)
        {
            var token = JTokenHelper.GetToken(dataSet.SampleDocument);

            return(ValidateSampleDocument(token, dataSet.IdField, dataSet.TagField, dataSet.InterpretedFields));
        }
Exemple #15
0
        public void Read(Property property, PropertyItem item, Data data)
        {
            JObject task = JObject.FromObject(data.Value);

            item.Value = JTokenHelper.GetPropertyValue <string>(task, property.Key);
        }
        public Result ValidateSchema(DataSet dataSet)
        {
            var token = JTokenHelper.GetToken(dataSet.Schema);

            return(ValidateSchema(token, dataSet.IdField, dataSet.TagField, dataSet.InterpretedFields));
        }
 /// <summary>
 /// Returns with all schema path and type
 /// </summary>
 /// <param name="obj"></param>
 /// <returns>Key: path; Tuple.Item1: Type; Tuple.Item2: Array item Type if Item1 is array</returns>
 public static IDictionary <string, Tuple <string, string> > GetPaths(object obj)
 {
     return(GetPaths(JTokenHelper.GetToken(obj)));
 }
Exemple #18
0
 public static Dictionary <string, JToken> GetAllPathTokens(object obj)
 {
     return(GetAllPathTokens(JTokenHelper.GetToken(obj)));
 }
Exemple #19
0
 public static List <string> GetAllPaths(object obj)
 {
     return(GetAllPaths(JTokenHelper.GetToken(obj)));
 }
Exemple #20
0
        public static bool IsPrimitiveType(object document, string fieldName)
        {
            var token = JTokenHelper.GetPathToken(JToken.FromObject(document), fieldName);

            return(token != null && token is JValue);
        }
Exemple #21
0
        public static bool IsExist(object document, string fieldName)
        {
            var token = JTokenHelper.GetPathToken(JToken.FromObject(document), fieldName);

            return(token != null);
        }
        private void AddGlobalStoreInternal(string name, string indexName, DataSet dataSet)
        {
            var paths        = new List <string>();
            var attachments  = new List <string>();
            var tagIsArray   = false;
            var tagIsInteger = false;

            if (dataSet.SampleDocument != null)
            {
                var pathTokens = DocumentHelper.GetAllPathTokens(dataSet.SampleDocument);
                paths = pathTokens.Keys.ToList();

                if (!pathTokens.ContainsKey(dataSet.TagField))
                {
                    throw new InvalidOperationException($"DataSet Name: ´{name}´, Index: ´{indexName}´. SampleDocument does not contain TagField ´{dataSet.TagField}´");
                }

                var tagToken = pathTokens[dataSet.TagField];
                tagIsArray = tagToken.Type == JTokenType.Array;

                if (tagIsArray)
                {
                    tagIsInteger = JTokenHelper.GetUnderlyingToken(tagToken).Type == JTokenType.Integer;
                }
                else
                {
                    tagIsInteger = tagToken.Type == JTokenType.Integer;
                }
            }
            else if (dataSet.Schema != null)
            {
                var pathDictionary = SchemaHelper.GetPaths(dataSet.Schema);

                paths       = pathDictionary.Keys.ToList();
                attachments = pathDictionary
                              .Where(kv => kv.Value.Item1 == SchemaHelper.Types.Attachment)
                              .Select(kv => kv.Key)
                              .ToList();

                if (!pathDictionary.ContainsKey(dataSet.TagField))
                {
                    throw new InvalidOperationException($"DataSet Name: ´{name}´, Index: ´{indexName}´. Schema does not contain TagField ´{dataSet.TagField}´");
                }

                var tagTuple = pathDictionary[dataSet.TagField];
                tagIsArray = SchemaHelper.IsArray(tagTuple.Item1);
                if (tagIsArray)
                {
                    tagIsInteger = SchemaHelper.IsInteger(tagTuple.Item2);
                }
                else
                {
                    tagIsInteger = SchemaHelper.IsInteger(tagTuple.Item1);
                }
            }
            else
            {
                throw new InvalidOperationException("DataSet has no SampleDocument nor Schema property");
            }

            GlobalStore.DataSets.Add(name ?? indexName, new GlobalStoreDataSet(name, indexName, dataSet, paths, tagIsArray, tagIsInteger, attachments));
        }