Example #1
0
        public async Task<string> Metric(QueryType queryType, string collection, string targetProperty, QueryTimeframe timeframe = null, IEnumerable<QueryFilter> filters = null, string timezone = "")
        {
            if (queryType == null)
                throw new ArgumentNullException("queryType");
            if (string.IsNullOrWhiteSpace(collection))
                throw new ArgumentNullException("collection");
            if (string.IsNullOrWhiteSpace(targetProperty) && (queryType!=QueryType.Count()))
                throw new ArgumentNullException("targetProperty");

            var parms = new Dictionary<string, string>();
            parms.Add(KeenConstants.QueryParmEventCollection, collection);
            parms.Add(KeenConstants.QueryParmTargetProperty, targetProperty);
            parms.Add(KeenConstants.QueryParmTimeframe, timeframe.ToSafeString());
            parms.Add(KeenConstants.QueryParmTimezone, timezone);
            parms.Add(KeenConstants.QueryParmFilters, filters == null ? "" : JArray.FromObject(filters).ToString());

            var reply = await KeenWebApiRequest(queryType.ToString(), parms);

            string result;
            if (queryType == QueryType.SelectUnique())
                // This is to support SelectUnique which is the only query type with a list-type result.
                result = string.Join(",", (reply.Value<JArray>("result").Values<string>()));
            else
                result = reply.Value<string>("result");
            return result;
        }
Example #2
0
 /// <summary>
 /// Returns items collected by time interval and group.
 /// </summary>
 /// <param name="queryType">Type of query to run.</param>
 /// <param name="collection">Name of event collection to query.</param>
 /// <param name="targetProperty">Name of property to analyse.</param>
 /// <param name="groupBy">Name of field by which to group results.</param>
 /// <param name="timeframe">Specifies window of time from which to select events for analysis. May be absolute or relative.</param>
 /// <param name="interval">The block size for partitioning the specified timeframe. Optional, may be null.</param>
 /// <param name="filters">Filters to narrow down the events used in analysis. Optional, may be null.</param>
 /// <param name="timezone">The timezone to use when specifying a relative timeframe. Optional, may be blank.</param>
 /// <returns></returns>
 public IEnumerable<QueryIntervalValue<IEnumerable<QueryGroupValue<string>>>> QueryIntervalGroup(
     QueryType queryType, string collection, string targetProperty, string groupBy, QueryTimeframe timeframe,
     QueryInterval interval, IEnumerable<QueryFilter> filters = null, string timezone = "")
 {
     try
     {
         return
             QueryIntervalGroupAsync(queryType, collection, targetProperty, groupBy, timeframe, interval, filters,
                 timezone).Result;
     }
     catch (AggregateException ex)
     {
         throw ex.TryUnwrap();
     }
 }
Example #3
0
 /// <summary>
 /// Returns items collected by time interval and group.
 /// </summary>
 /// <param name="queryType">Type of query to run.</param>
 /// <param name="collection">Name of event collection to query.</param>
 /// <param name="targetProperty">Name of property to analyse.</param>
 /// <param name="groupBy">Name of field by which to group results.</param>
 /// <param name="timeframe">Specifies window of time from which to select events for analysis. May be absolute or relative.</param>
 /// <param name="interval">The block size for partitioning the specified timeframe. Optional, may be null.</param>
 /// <param name="filters">Filters to narrow down the events used in analysis. Optional, may be null.</param>
 /// <param name="timezone">The timezone to use when specifying a relative timeframe. Optional, may be blank.</param>
 /// <returns></returns>
 public async Task<IEnumerable<QueryIntervalValue<IEnumerable<QueryGroupValue<string>>>>> QueryIntervalGroupAsync
     (QueryType queryType, string collection, string targetProperty, string groupBy, QueryTimeframe timeframe,
         QueryInterval interval, IEnumerable<QueryFilter> filters = null, string timezone = "")
 {
     return
         await
             Queries.Metric(queryType, collection, targetProperty, groupBy, timeframe, interval, filters,
                 timezone).ConfigureAwait(false);
 }
Example #4
0
 /// <summary>
 /// Return a single value.
 /// </summary>
 /// <param name="queryType">Type of query to run.</param>
 /// <param name="collection">Name of event collection to query.</param>
 /// <param name="targetProperty">Name of property to analyse.</param>
 /// <param name="timeframe">Specifies window of time from which to select events for analysis. May be absolute or relative.</param>
 /// <param name="filters">Filter to narrow down the events used in analysis. Optional, may be null.</param>
 /// <param name="timezone">The timezone to use when specifying a relative timeframe. Optional, may be blank.</param>
 /// <returns></returns>
 public string Query(QueryType queryType, string collection, string targetProperty,
     QueryTimeframe timeframe = null, IEnumerable<QueryFilter> filters = null, string timezone = "")
 {
     try
     {
         return QueryAsync(queryType, collection, targetProperty, timeframe, filters, timezone).Result;
     }
     catch (AggregateException ex)
     {
         throw ex.TryUnwrap();
     }
 }
Example #5
0
 /// <summary>
 /// Run a query returning a single value.
 /// </summary>
 /// <param name="queryType">Type of query to run.</param>
 /// <param name="collection">Name of event collection to query.</param>
 /// <param name="targetProperty">Name of property to analyse.</param>
 /// <param name="timeframe">Specifies window of time from which to select events for analysis. May be absolute or relative.</param>
 /// <param name="filters">Filter to narrow down the events used in analysis. Optional, may be null.</param>
 /// <param name="timezone">The timezone to use when specifying a relative timeframe. Optional, may be blank.</param>
 /// <returns></returns>
 public async Task<string> QueryAsync(QueryType queryType, string collection, string targetProperty,
     QueryTimeframe timeframe = null, IEnumerable<QueryFilter> filters = null, string timezone = "")
 {
     return
         await
             Queries.Metric(queryType, collection, targetProperty, timeframe, filters, timezone)
                 .ConfigureAwait(false);
 }
Example #6
0
        public async Task<IEnumerable<QueryIntervalValue<IEnumerable<QueryGroupValue<string>>>>> Metric(QueryType queryType, string collection, string targetProperty, string groupby, QueryTimeframe timeframe, QueryInterval interval, IEnumerable<QueryFilter> filters = null, string timezone = "")
        {
            if (queryType == null)
                throw new ArgumentNullException("queryType");
            if (string.IsNullOrWhiteSpace(collection))
                throw new ArgumentNullException("collection");
            if (string.IsNullOrWhiteSpace(targetProperty) && (queryType != QueryType.Count()))
                throw new ArgumentNullException("targetProperty");
            if (null == timeframe)
                throw new ArgumentException("timeframe", "Timeframe must be specified for a series query.");
            if (null == interval)
                throw new ArgumentNullException("interval", "interval must be specified for a series query");
            if (string.IsNullOrWhiteSpace(groupby))
                throw new ArgumentNullException("groupby", "groupby field name must be specified for a goupby query");

            var parms = new Dictionary<string, string>();
            parms.Add(KeenConstants.QueryParmEventCollection, collection);
            parms.Add(KeenConstants.QueryParmTargetProperty, targetProperty);
            parms.Add(KeenConstants.QueryParmGroupBy, groupby);
            parms.Add(KeenConstants.QueryParmTimeframe, timeframe.ToSafeString());
            parms.Add(KeenConstants.QueryParmInterval, interval.ToSafeString());
            parms.Add(KeenConstants.QueryParmTimezone, timezone);
            parms.Add(KeenConstants.QueryParmFilters, filters == null ? "" : JArray.FromObject(filters).ToString());

            var reply = await KeenWebApiRequest(queryType.ToString(), parms);

            IEnumerable<QueryIntervalValue<IEnumerable<QueryGroupValue<string>>>> result;
            if (queryType == QueryType.SelectUnique())
            {
                // This is to support SelectUnique which is the only query type with a list-type result.
                result = from i in reply.Value<JArray>("result")
                         let v = (from r in i.Value<JArray>("value")
                                  let c = string.Join(",", r.Value<JArray>("result").Values<string>())
                                  let g = r.Value<string>(groupby)
                                  select new QueryGroupValue<string>(c, g))
                         let t = i.Value<JObject>("timeframe")
                         select new QueryIntervalValue<IEnumerable<QueryGroupValue<string>>>(v, t.Value<DateTime>("start"), t.Value<DateTime>("end"));
            }
            else
            {
                result = from i in reply.Value<JArray>("result")
                         let v = (from r in i.Value<JArray>("value")
                                  let c = r.Value<string>("result")
                                  let g = r.Value<string>(groupby)
                                  select new QueryGroupValue<string>(c, g))
                         let t = i.Value<JObject>("timeframe")
                         select new QueryIntervalValue<IEnumerable<QueryGroupValue<string>>>(v, t.Value<DateTime>("start"), t.Value<DateTime>("end"));
            }
            return result;
        }
Example #7
0
        public async Task <IEnumerable <QueryIntervalValue <IEnumerable <QueryGroupValue <string> > > > > Metric(QueryType queryType, string collection, string targetProperty, string groupby, IQueryTimeframe timeframe, QueryInterval interval, IEnumerable <QueryFilter> filters = null, string timezone = "")
        {
            if (queryType == null)
            {
                throw new ArgumentNullException("queryType");
            }
            if (string.IsNullOrWhiteSpace(collection))
            {
                throw new ArgumentNullException("collection");
            }
            if (string.IsNullOrWhiteSpace(targetProperty) && (queryType != QueryType.Count()))
            {
                throw new ArgumentNullException("targetProperty");
            }
            if (null == timeframe)
            {
                throw new ArgumentException("timeframe", "Timeframe must be specified for a series query.");
            }
            if (null == interval)
            {
                throw new ArgumentNullException("interval", "interval must be specified for a series query");
            }
            if (string.IsNullOrWhiteSpace(groupby))
            {
                throw new ArgumentNullException("groupby", "groupby field name must be specified for a goupby query");
            }

            var parms = new Dictionary <string, string>();

            parms.Add(KeenConstants.QueryParmEventCollection, collection);
            parms.Add(KeenConstants.QueryParmTargetProperty, targetProperty);
            parms.Add(KeenConstants.QueryParmGroupBy, groupby);
            parms.Add(KeenConstants.QueryParmTimeframe, timeframe.ToSafeString());
            parms.Add(KeenConstants.QueryParmInterval, interval.ToSafeString());
            parms.Add(KeenConstants.QueryParmTimezone, timezone);
            parms.Add(KeenConstants.QueryParmFilters, filters == null ? "" : JArray.FromObject(filters).ToString());

            var reply = await KeenWebApiRequest(queryType.ToString(), parms).ConfigureAwait(false);

            IEnumerable <QueryIntervalValue <IEnumerable <QueryGroupValue <string> > > > result;

            if (queryType == QueryType.SelectUnique())
            {
                // This is to support SelectUnique which is the only query type with a list-type result.
                result                 = from i in reply.Value <JArray>("result")
                                 let v = (from r in i.Value <JArray>("value")
                                          let c = string.Join(",", r.Value <JArray>("result").Values <string>())
                                                  let g = r.Value <string>(groupby)
                                                          select new QueryGroupValue <string>(c, g))
                                         let t = i.Value <JObject>("timeframe")
                                                 select new QueryIntervalValue <IEnumerable <QueryGroupValue <string> > >(v, t.Value <DateTime>("start"), t.Value <DateTime>("end"));
            }
            else
            {
                result                 = from i in reply.Value <JArray>("result")
                                 let v = (from r in i.Value <JArray>("value")
                                          let c = r.Value <string>("result")
                                                  let g = r.Value <string>(groupby)
                                                          select new QueryGroupValue <string>(c, g))
                                         let t = i.Value <JObject>("timeframe")
                                                 select new QueryIntervalValue <IEnumerable <QueryGroupValue <string> > >(v, t.Value <DateTime>("start"), t.Value <DateTime>("end"));
            }
            return(result);
        }