public async Task Query_SimpleSelectUniqueInterval_Success()
        {
            var queryParameters = new QueryParameters()
            {
                Analysis       = QueryType.SelectUnique(),
                TargetProperty = "someProperty",
                Timeframe      = QueryRelativeTimeframe.ThisNHours(2),
                Interval       = QueryInterval.EveryNHours(1)
            };

            var expectedCounts = new List <string[]>()
            {
                new string[] { "10", "20" }, new string[] { "30", "40" }
            };
            var expectedTimeframes = new List <QueryAbsoluteTimeframe>()
            {
                new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-2), DateTime.Now.AddHours(-1)),
                new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-1), DateTime.Now)
            };
            var expectedResults = expectedCounts.Zip(
                expectedTimeframes,
                (count, time) => new { timeframe = time, value = count });

            var expectedResponse = new
            {
                result = expectedResults
            };

            var client = CreateQueryTestKeenClient(queryParameters, expectedResponse);

            var actualResults = await client.Queries.Metric(
                queryParameters.Analysis,
                queryParameters.EventCollection,
                queryParameters.TargetProperty,
                queryParameters.Timeframe,
                queryParameters.Interval);

            Assert.AreEqual(expectedResults.Count(), actualResults.Count());
            var expectedEnumerator = expectedResults.GetEnumerator();
            var actualEnumerator   = actualResults.GetEnumerator();

            while (expectedEnumerator.MoveNext() && actualEnumerator.MoveNext())
            {
                var expected = expectedEnumerator.Current;
                var actual   = actualEnumerator.Current;
                Assert.AreEqual(expected.timeframe.Start, actual.Start);
                Assert.AreEqual(expected.timeframe.End, actual.End);
                Assert.AreEqual(string.Join(",", expected.value), actual.Value);
            }
        }
Beispiel #2
0
        public async Task Query_SimpleSelectUniqueInterval_Success()
        {
            var queryParameters = new QueryParameters()
            {
                Analysis       = QueryType.SelectUnique(),
                TargetProperty = "someProperty",
                Timeframe      = QueryRelativeTimeframe.ThisNHours(2),
                Interval       = QueryInterval.EveryNHours(1)
            };

            var expectedCounts = new List <string[]>()
            {
                new string[] { "10", "20" }, new string[] { "30", "40" }
            };
            var expectedTimeframes = new List <QueryAbsoluteTimeframe>()
            {
                new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-2), DateTime.Now.AddHours(-1)),
                new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-1), DateTime.Now)
            };
            var expectedResults = expectedCounts.Zip(
                expectedTimeframes,
                (count, time) => new { timeframe = time, value = count });
            var expectedResponse = new
            {
                result = expectedResults
            };

            var client = CreateQueryTestKeenClient(queryParameters, expectedResponse);

            var actualResult = await client.Queries.Metric(
                queryParameters.Analysis,
                queryParameters.EventCollection,
                queryParameters.TargetProperty,
                queryParameters.Timeframe,
                queryParameters.Interval);

            var expectedSdkResult = expectedResults.Select((result) =>
            {
                return(new QueryIntervalValue <string>(
                           string.Join(',', result.value),
                           result.timeframe.Start,
                           result.timeframe.End));
            });

            Assert.That(actualResult, Is.EquivalentTo(expectedSdkResult));
        }
Beispiel #3
0
        public async Task Query_SimpleSelectUniqueGroupByInterval_Success()
        {
            var queryParameters = new QueryParameters()
            {
                Analysis       = QueryType.SelectUnique(),
                TargetProperty = "someProperty",
                Timeframe      = QueryRelativeTimeframe.ThisNHours(2),
                Interval       = QueryInterval.EveryNHours(1),
                GroupBy        = "someGroupProperty"
            };

            string resultsJson = @"[
                {
                    ""timeframe"": {
                        ""start"": ""2017-10-14T00:00:00.000Z"",
                        ""end"": ""2017-10-15T00:00:00.000Z""
                    },
                    ""value"": [
                        {
                            ""someGroupProperty"": ""group1"",
                            ""result"": [
                                ""10"",
                                ""20""
                            ]
                        },
                        {
                            ""someGroupProperty"": ""group2"",
                            ""result"": [
                                ""30"",
                                ""40""
                            ]
                        }
                    ]
                },
                {
                    ""timeframe"": {
                        ""start"": ""2017-10-15T00:00:00.000Z"",
                        ""end"": ""2017-10-16T00:00:00.000Z""
                    },
                    ""value"": [
                        {
                            ""someGroupProperty"": ""group1"",
                            ""result"": [
                                ""50"",
                                ""60""
                            ]
                        },
                        {
                            ""someGroupProperty"": ""group2"",
                            ""result"": [
                                ""70"",
                                ""80""
                            ]
                        }
                    ]
                }
            ]";

            var expectedResults = JArray.Parse(resultsJson);

            var expectedResponse = new
            {
                result = expectedResults
            };

            var client = CreateQueryTestKeenClient(queryParameters, expectedResponse);

            var actualResult = await client.Queries.Metric(
                queryParameters.Analysis,
                queryParameters.EventCollection,
                queryParameters.TargetProperty,
                queryParameters.GroupBy,
                queryParameters.Timeframe,
                queryParameters.Interval);

            var expectedSdkResult = expectedResults.Select((intervalToken) =>
            {
                return(new QueryIntervalValue <IEnumerable <QueryGroupValue <string> > >(
                           intervalToken["value"].Select((groupToken) =>
                {
                    return new QueryGroupValue <string>(
                        string.Join(',', groupToken["result"]),
                        groupToken[queryParameters.GroupBy].Value <string>());
                }),
                           intervalToken["timeframe"]["start"].Value <DateTime>(),
                           intervalToken["timeframe"]["end"].Value <DateTime>()));
            });

            // Use JArray objects as a way to normalize types here, since the
            // concrete types won't match for the QueryInternalValue IEnumerable implementation.
            Assert.That(JArray.FromObject(actualResult), Is.EquivalentTo(JArray.FromObject(expectedSdkResult)));
        }
Beispiel #4
0
        public async Task Query_SimpleCountGroupByInterval_Success()
        {
            var queryParameters = new QueryParameters()
            {
                TargetProperty = "someProperty",
                Timeframe      = QueryRelativeTimeframe.ThisNHours(2),
                Interval       = QueryInterval.EveryNHours(1),
                GroupBy        = "someGroupProperty"
            };

            var expectedResults = new[]
            {
                new
                {
                    timeframe = new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-2), DateTime.Now.AddHours(-1)),
                    value     = new List <Dictionary <string, string> >
                    {
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group1" }, { "result", "10" }
                        },
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group2" }, { "result", "20" }
                        },
                    }
                },
                new
                {
                    timeframe = new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-1), DateTime.Now),
                    value     = new List <Dictionary <string, string> >
                    {
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group1" }, { "result", "30" }
                        },
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group2" }, { "result", "40" }
                        },
                    }
                }
            };

            var expectedResponse = new
            {
                result = expectedResults
            };

            var client = CreateQueryTestKeenClient(queryParameters, expectedResponse);

            var actualResult = (await client.Queries.Metric(
                                    queryParameters.Analysis,
                                    queryParameters.EventCollection,
                                    queryParameters.TargetProperty,
                                    queryParameters.GroupBy,
                                    queryParameters.Timeframe,
                                    queryParameters.Interval));

            var expectedSdkResult = expectedResults.Select((intervals) =>
            {
                return(new QueryIntervalValue <IEnumerable <QueryGroupValue <string> > >(
                           intervals.value.Select((group) => new QueryGroupValue <string>(
                                                      group["result"],
                                                      group[queryParameters.GroupBy])),
                           intervals.timeframe.Start,
                           intervals.timeframe.End
                           ));
            });

            // Use JArray objects as a way to normalize types here, since the
            // concrete types won't match for the QueryInternalValue IEnumerable implementation.
            Assert.That(JArray.FromObject(actualResult), Is.EquivalentTo(JArray.FromObject(expectedSdkResult)));
        }
        public async Task Query_SimpleSelectUniqueGroupByInterval_Success()
        {
            var queryParameters = new QueryParameters()
            {
                Analysis       = QueryType.SelectUnique(),
                TargetProperty = "someProperty",
                Timeframe      = QueryRelativeTimeframe.ThisNHours(2),
                Interval       = QueryInterval.EveryNHours(1),
                GroupBy        = "someGroupProperty"
            };

            string resultsJson = @"[
                {
                    ""timeframe"": {
                        ""start"": ""2017-10-14T00:00:00.000Z"",
                        ""end"": ""2017-10-15T00:00:00.000Z""
                    },
                    ""value"": [
                        {
                            ""someGroupProperty"": ""group1"",
                            ""result"": [
                                ""10"",
                                ""20""
                            ]
                        },
                        {
                            ""someGroupProperty"": ""group2"",
                            ""result"": [
                                ""30"",
                                ""40""
                            ]
                        }
                    ]
                },
                {
                    ""timeframe"": {
                        ""start"": ""2017-10-15T00:00:00.000Z"",
                        ""end"": ""2017-10-16T00:00:00.000Z""
                    },
                    ""value"": [
                        {
                            ""someGroupProperty"": ""group1"",
                            ""result"": [
                                ""50"",
                                ""60""
                            ]
                        },
                        {
                            ""someGroupProperty"": ""group2"",
                            ""result"": [
                                ""70"",
                                ""80""
                            ]
                        }
                    ]
                }
            ]";

            var expectedResults = JArray.Parse(resultsJson);

            var expectedResponse = new
            {
                result = expectedResults
            };

            var client = CreateQueryTestKeenClient(queryParameters, expectedResponse);

            var actualResults = await client.Queries.Metric(
                queryParameters.Analysis,
                queryParameters.EventCollection,
                queryParameters.TargetProperty,
                queryParameters.GroupBy,
                queryParameters.Timeframe,
                queryParameters.Interval);

            Assert.AreEqual(expectedResults.Count(), actualResults.Count());
            var actualEnumerator = actualResults.GetEnumerator();

            foreach (var expected in expectedResults)
            {
                actualEnumerator.MoveNext();
                var actual = actualEnumerator.Current;
                // Validate the interval is correct
                Assert.AreEqual(DateTime.Parse(expected["timeframe"]["start"].Value <string>()), actual.Start);
                Assert.AreEqual(DateTime.Parse(expected["timeframe"]["end"].Value <string>()), actual.End);

                // Validate the results for the group within the time interval
                Assert.AreEqual(expected["value"].Count(), actual.Value.Count());
                var actualGroupResultEnumerator = actual.Value.GetEnumerator();
                foreach (var expectedGroupResult in expected["value"])
                {
                    actualGroupResultEnumerator.MoveNext();
                    var actualGroupResult = actualGroupResultEnumerator.Current;
                    Assert.AreEqual(expectedGroupResult[queryParameters.GroupBy].Value <string>(), actualGroupResult.Group);
                    Assert.AreEqual(string.Join(",", expectedGroupResult["result"].Values <string>()), actualGroupResult.Value);
                }
            }
        }
        public async Task Query_SimpleCountGroupByInterval_Success()
        {
            var queryParameters = new QueryParameters()
            {
                TargetProperty = "someProperty",
                Timeframe      = QueryRelativeTimeframe.ThisNHours(2),
                Interval       = QueryInterval.EveryNHours(1),
                GroupBy        = "someGroupProperty"
            };

            var expectedResults = new[]
            {
                new
                {
                    timeframe = new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-2), DateTime.Now.AddHours(-1)),
                    value     = new List <Dictionary <string, string> >
                    {
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group1" }, { "result", "10" }
                        },
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group2" }, { "result", "20" }
                        },
                    }
                },
                new
                {
                    timeframe = new QueryAbsoluteTimeframe(DateTime.Now.AddHours(-1), DateTime.Now),
                    value     = new List <Dictionary <string, string> >
                    {
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group1" }, { "result", "30" }
                        },
                        new Dictionary <string, string> {
                            { queryParameters.GroupBy, "group2" }, { "result", "40" }
                        },
                    }
                }
            };

            var expectedResponse = new
            {
                result = expectedResults
            };

            var client = CreateQueryTestKeenClient(queryParameters, expectedResponse);

            var actualResults = await client.Queries.Metric(
                queryParameters.Analysis,
                queryParameters.EventCollection,
                queryParameters.TargetProperty,
                queryParameters.GroupBy,
                queryParameters.Timeframe,
                queryParameters.Interval);

            Assert.AreEqual(expectedResults.Count(), actualResults.Count());
            var actualEnumerator = actualResults.GetEnumerator();

            foreach (var expected in expectedResults)
            {
                actualEnumerator.MoveNext();
                var actual = actualEnumerator.Current;
                // Validate the interval is correct
                Assert.AreEqual(expected.timeframe.Start, actual.Start);
                Assert.AreEqual(expected.timeframe.End, actual.End);

                // Validate the results for the group within the time interval
                Assert.AreEqual(expected.value.Count, actual.Value.Count());
                var actualGroupResultEnumerator = actual.Value.GetEnumerator();
                foreach (var expectedGroupResult in expected.value)
                {
                    actualGroupResultEnumerator.MoveNext();
                    var actualGroupResult = actualGroupResultEnumerator.Current;
                    Assert.AreEqual(expectedGroupResult[queryParameters.GroupBy], actualGroupResult.Group);
                    Assert.AreEqual(expectedGroupResult["result"], actualGroupResult.Value);
                }
            }
        }