Ejemplo n.º 1
0
        /// <summary>
        /// Convert a user-supplied object to a JObject that can be sent to the Keen.IO API.
        ///
        /// This writes any global properies to the object and records the time.
        /// </summary>
        /// <param name="eventInfo"></param>
        /// <param name="addOns">Optional collection of Data Enhancement Add-ons</param>
        /// <returns></returns>
        private JObject PrepareUserObject(object eventInfo, IEnumerable <AddOn> addOns)
        {
            var jEvent = JObject.FromObject(eventInfo);

            // Add global properties to the event
            foreach (var p in _globalProperties)
            {
                // If the property value is an IDynamicPropertyValue,
                // exec the Value() to generate the property value.
                var dynProp = p.Value as IDynamicPropertyValue;
                if (dynProp == null)
                {
                    KeenUtil.ValidatePropertyName(p.Key);
                    jEvent.Add(p.Key, JToken.FromObject(p.Value));
                }
                else
                {
                    var val = dynProp.Value();
                    if (null == val)
                    {
                        throw new KeenException(string.Format("Dynamic property \"{0}\" returned a null value", p.Key));
                    }
                    jEvent.Add(p.Key, JToken.FromObject(val));
                }
            }

            // Ensure this event has a 'keen' object of the correct type
            if (null == jEvent.Property("keen"))
            {
                jEvent.Add("keen", new JObject());
            }
            else if (jEvent.Property("keen").Value.GetType() != typeof(JObject))
            {
                throw new KeenException(string.Format("Value of property \"keen\" must be an object, is {0}", jEvent.Property("keen").GetType()));
            }

            var keen = ((JObject)jEvent.Property("keen").Value);

            if (addOns.Any())
            {
                keen.Add("addons", JArray.FromObject(addOns));
            }

            // Set the keen.timestamp if it has not already been set
            if (null == keen.Property("timestamp"))
            {
                keen.Add("timestamp", DateTime.Now);
            }

            return(jEvent);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add a static global property. This property will be added to
        /// every event.
        /// </summary>
        /// <param name="property">Property name</param>
        /// <param name="value">Property value. This may be a simple value, array, or object,
        /// or an object that supports IDynamicPropertyValue returning one of those.</param>
        public void AddGlobalProperty(string property, object value)
        {
            // Verify that the property name is allowable, and that the value is populated.
            KeenUtil.ValidatePropertyName(property);
            if (value == null)
            {
                throw new KeenException("Global properties must have a non-null value.");
            }
            var dynProp = value as IDynamicPropertyValue;

            if (dynProp != null)
            {
                // Execute the property once before it is needed to check the value
                ExecDynamicPropertyValue(property, dynProp);
            }

            _globalProperties.Add(property, value);
        }