Permalink
Browse files

Add sorting to api

  • Loading branch information...
1 parent 8ce0fdd commit 99d52653a28b4641dac555d5b5b543252b386a65 @Valve Valve committed Mar 6, 2014
Showing with 75 additions and 7 deletions.
  1. +3 −2 Igooana.Demo/MainWindow.xaml.cs
  2. +1 −0 Igooana/Igooana.csproj
  3. +20 −0 Igooana/Query.cs
  4. +21 −5 Igooana/QueryOptions.cs
  5. +30 −0 Igooana/QuerySort.cs
@@ -30,8 +30,9 @@ public partial class MainWindow : Window {
var query = Query
.For(74167085, DateTime.Now.AddDays(-31), DateTime.Now)
- .WithMetrics(Metric.PageTracking.Pageviews + Metric.PageTracking.AvgTimeOnPage + Metric.Session.TimeOnSite)
- .WithDimensions(Dimension.Time.DayOfWeek).Skip(2).Take(3);
+ .WithMetrics(Metric.PageTracking.Pageviews + Metric.Session.Visits).WithDimensions(Dimension.GeoNetwork.Country)
+ .Take(10)
+ .OrderByDescending(Metric.PageTracking.Pageviews + Metric.Session.Visits);
var result = await api.Execute(query);
}
catch (ConnectionException ex) {
View
@@ -79,6 +79,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Query.cs" />
<Compile Include="QueryOptions.cs" />
+ <Compile Include="QuerySort.cs" />
<Compile Include="Response.cs" />
</ItemGroup>
<ItemGroup>
View
@@ -59,6 +59,25 @@ public class Query {
return new Query(profileId, dt1, dt2, metric, dimension, options + new QueryOptions { StartIndex = rows });
}
+ public Query Order(Metric metric) {
+ return OrderCore(new QuerySort(metric.ToString()));
+ }
+ public Query OrderByDescending(Metric metric) {
+ return OrderCore(new QuerySort(metric.ToString(), SortDirection.Descending));
+ }
+ public Query Order(Dimension dimension) {
+ return OrderCore(new QuerySort(dimension.ToString()));
+ }
+
+ public Query OrderByDescending(Dimension dimension) {
+ return OrderCore(new QuerySort(dimension.ToString(), SortDirection.Descending));
+ }
+
+ private Query OrderCore(QuerySort querySort) {
+ var queryOptions = options + new QueryOptions { Sorts = new[] { querySort } };
+ return new Query(profileId, dt1, dt2, metric, dimension, queryOptions);
+ }
+
public override string ToString() {
string dateFormat = "yyyy-MM-dd";
@@ -76,5 +95,6 @@ public class Query {
return builder.ToString();
}
+
}
}
View
@@ -1,12 +1,20 @@
using Igooana.Extensions;
using System;
+using System.Collections.Generic;
using System.Linq;
using System.Reflection;
+using System.Text;
namespace Igooana {
internal class QueryOptions {
+ const int DefaultMaxResult = 1000;
+ const int DefaultStartIndex = 1;
internal static readonly QueryOptions Empty = new QueryOptions();
- internal QueryOptions() { }
+ internal QueryOptions() {
+ StartIndex = DefaultStartIndex;
+ MaxResults = DefaultMaxResult;
+ Sorts = Enumerable.Empty<QuerySort>();
+ }
/// <summary>
/// Maximum number of rows to include in this response.
@@ -20,6 +28,8 @@ internal class QueryOptions {
[GaParam]
public int StartIndex { get; set; }
+ public IEnumerable<QuerySort> Sorts { get; set; }
+
public static QueryOptions operator +(QueryOptions o1, QueryOptions o2) {
Ensure.ArgumentNotNull(o1, "o1");
Ensure.ArgumentNotNull(o2, "o2");
@@ -31,26 +41,32 @@ internal class QueryOptions {
throw new InvalidOperationException("Addition must use two different QueryOption instances");
}
return new QueryOptions {
- MaxResults = MaxResults == default(int) ? other.MaxResults : MaxResults,
- StartIndex = StartIndex == default(int) ? other.StartIndex : StartIndex
+ MaxResults = MaxResults == DefaultMaxResult ? other.MaxResults : MaxResults,
+ StartIndex = StartIndex == DefaultStartIndex ? other.StartIndex : StartIndex,
+ Sorts = Sorts.Concat(other.Sorts)
};
}
public bool IsEmpty {
get {
- return MaxResults == default(int) && StartIndex == default(int);
+ return MaxResults == DefaultMaxResult && StartIndex == DefaultStartIndex && !Sorts.Any();
}
}
public override string ToString() {
if (IsEmpty) {
return "";
}
+ var builder = new StringBuilder();
var urlParams = GetType()
.GetTypeInfo()
.DeclaredProperties
.Where(x => x.IsDefined(typeof(GaParam)))
.Select(x => "{0}={1}".FormatWith(x.Name.Dasherize(), x.GetValue(this)));
- return String.Join("&", urlParams);
+ builder.Append(String.Join("&", urlParams));
+ if (Sorts.Any()) {
+ builder.AppendFormat("&sort={0}", String.Join(",", Sorts.Select(x => x.ToString())));
+ }
+ return builder.ToString();
}
}
}
View
@@ -0,0 +1,30 @@
+using System;
+using System.Linq;
+using Igooana.Extensions;
+namespace Igooana {
+ class QuerySort {
+ private readonly string property;
+ private readonly SortDirection direction;
+
+ public QuerySort(string property, SortDirection direction = SortDirection.Ascending) {
+ this.property = property;
+ this.direction = direction;
+ }
+ public string Property { get { return property; } }
+
+ public SortDirection Direction { get { return direction; } }
+
+ public override string ToString() {
+ var directionChar = Direction == SortDirection.Ascending ? "" : "-";
+ return String.Join(",", Property.Split(new[] { ',' }).Select(x => "{0}{1}".FormatWith(directionChar, x)));
+ }
+ }
+
+
+ /// <summary>
+ /// Sorting order, ascending or descending
+ /// </summary>
+ public enum SortDirection {
+ Ascending, Descending
+ }
+}

0 comments on commit 99d5265

Please sign in to comment.