Esempio n. 1
0
        /// <summary>
        /// Gets a schema for a given query
        /// </summary>
        /// <param name="schema"></param>
        /// <returns>A schema or null</returns>
        private async Task <Schema> GetSchemaProperties(Schema schema)
        {
            try
            {
                if (schema.Properties.Count > 0)
                {
                    return(schema);
                }

                //check if query is empty or invalid json
                if (string.IsNullOrWhiteSpace(schema.Query) || !IsValidJson(schema.Query))
                {
                    Logger.Error(null, "Invalid schema query");
                    return(null);
                }

                // add "_id", "_rev" as required field
                Logger.Info("getting couchdb response for schema discovery");
                var schemaQueryJson = JObject.Parse(schema.Query);
                var getSchemaUri    = $"{_server.Settings.DatabaseName}/_find";
                var response        = await _client.PostAsync(getSchemaUri,
                                                              new StringContent(Discover.GetValidSchemaQuery(schemaQueryJson), Encoding.UTF8,
                                                                                "application/json"));

                //Logger.Info($"discover schema response: {await response.Content.ReadAsStringAsync()}");
                response.EnsureSuccessStatusCode();

                var documents = JObject.Parse(await response.Content.ReadAsStringAsync())["docs"];

                // get each field and create a property for the field
                Logger.Info($"Getting property type for all {documents.ToList().Count} documents");
                if (documents.ToList().Count > 0)
                {
                    var discoveredPropertyTypes = Discover.GetPropertyTypes(documents, 100);
                    foreach (KeyValuePair <string, Dictionary <PropertyType, int> > entry in discoveredPropertyTypes)
                    {
                        var propertyTypeofMaxValue = entry.Value.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;
                        // create property
                        var property = new Property
                        {
                            Id                = entry.Key,
                            Name              = entry.Key,
                            Description       = "",
                            Type              = propertyTypeofMaxValue,
                            IsKey             = false,
                            IsCreateCounter   = false,
                            IsUpdateCounter   = false,
                            PublisherMetaJson = ""
                        };
                        schema.Properties.Add(property);
                    }
                }
                else
                {
                    schema = null;
                }

                return(schema);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                return(null);
            }
        }