Permalink
Browse files

Merge pull request #7 from tempodb/client-cleanup

Client cleanup
  • Loading branch information...
2 parents fbfe1bb + 632b284 commit bcd5adc848d64ed5c160b7abe4d4a969d8a066ae @maddenpj maddenpj committed Jan 25, 2013
View
678 Client/Client.cs
@@ -1,102 +1,103 @@
-using System;
-using System.Collections.Generic;
-using System.Net;
using Client.Model;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Deserializers;
+using System;
+using System.Collections.Generic;
+using System.Net;
+
namespace Client
{
- /// <summary>
- /// Stores the session information for authenticating and accessing TempoDB.
- /// Your api key and secret is required. The Client also allows you to specify
- /// the hostname, port, and protocol (http or https). This is used if you are on
- /// a private cluster. The default hostname and port should work for the standard cluster.
- /// <p/>
- /// All access to data is made through a client instance.
- /// </summary>
- public class Client
- {
- /// <summary>
- /// How often the monitoring thread checks for connections to close. </summary>
- private const int DEFAULT_TIMEOUT_MILLIS = 30000; // 30 seconds
-
- private readonly string _host;
- private readonly string _key;
- private readonly int _port;
- private readonly string _secret;
- private readonly bool _secure;
- private readonly string _version;
-
- private RestClient _restClient;
-
- // private readonly DateTimeFormatter iso8601 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
-
- /// <param name="key"> Api key </param>
- /// <param name="secret"> Api secret </param>
- /// <param name="host"> Hostname of the api server </param>
- /// <param name="port"> Port that the api server is listening on </param>
+ /// <summary>
+ /// Stores the session information for authenticating and accessing TempoDB.
+ /// Your api key and secret is required. The Client also allows you to specify
+ /// the hostname, port, and protocol (http or https). This is used if you are on
+ /// a private cluster. The default hostname and port should work for the standard cluster.
+ /// <p/>
+ /// All access to data is made through a client instance.
+ /// </summary>
+ public class Client
+ {
+ /// <summary>
+ /// How often the monitoring thread checks for connections to close. </summary>
+ private const int DEFAULT_TIMEOUT_MILLIS = 30000; // 30 seconds
+
+ private readonly string _host;
+ private readonly string _key;
+ private readonly int _port;
+ private readonly string _secret;
+ private readonly bool _secure;
+ private readonly string _version;
+
+ private RestClient _restClient;
+
+ // private readonly DateTimeFormatter iso8601 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+
+ /// <param name="key"> Api key </param>
+ /// <param name="secret"> Api secret </param>
+ /// <param name="host"> Hostname of the api server </param>
+ /// <param name="port"> Port that the api server is listening on </param>
/// <param name="version"> The API version</param>
/// <param name="secure"> Uses http if false, https if true </param>
/// <param name="restClient"> Optional Rest Client. </param>
- public Client(string key, string secret, string host = "api.tempo-db.com", int port = 443, string version = "v1", bool secure = true, RestClient restClient = null)
- {
- _key = key;
- _secret = secret;
- _host = host;
- _port = port;
- _version = version;
- _secure = secure;
+ public Client(string key, string secret, string host = "api.tempo-db.com", int port = 443, string version = "v1", bool secure = true, RestClient restClient = null)
+ {
+ _key = key;
+ _secret = secret;
+ _host = host;
+ _port = port;
+ _version = version;
+ _secure = secure;
_restClient = restClient;
- }
-
- /// <summary>
- /// Executes the rest request, where no response is expected.
- /// </summary>
- /// <param name="request">The request to be executed</param>
- public void Execute(RestRequest request)
- {
- RestClient client = GetRestClient();
- IRestResponse response = client.Execute(request);
-
- if (response.StatusCode != HttpStatusCode.OK)
- {
- throw new Exception(string.Format("Service call failed with HTTP response [{0}].",
- Enum.GetName(typeof (HttpStatusCode), response.StatusCode)));
- }
- }
-
- /// <summary>
- /// Executes the rest request, where the JSON response is expected to be deserialized.
- /// </summary>
- /// <typeparam name="T">The type of the response to be deserialized.</typeparam>
- /// <param name="request">The request to be executed.</param>
- /// <returns>The response, deserialized as type T</returns>
- public T Execute<T>(RestRequest request) where T : new()
- {
- RestClient client = GetRestClient();
-
- IRestResponse<T> response = client.Execute<T>(request);
-
- if (response.StatusCode != HttpStatusCode.OK)
- {
- throw new Exception(string.Format("Service call failed with HTTP response [{0}].",
- Enum.GetName(typeof (HttpStatusCode), response.StatusCode)));
- }
- if (response.ResponseStatus != ResponseStatus.Completed)
- {
- throw new Exception(
- string.Format(
- "Response deserialization failed with status [{0} - {1}]. If no response is expected, call Execute() with parameter 'expectJsonRespone=false'",
- Enum.GetName(typeof (ResponseStatus), response.ResponseStatus),
- response.ErrorMessage));
- }
- return response.Data;
- }
-
- private RestClient GetRestClient()
- {
+ }
+
+ /// <summary>
+ /// Executes the rest request, where no response is expected.
+ /// </summary>
+ /// <param name="request">The request to be executed</param>
+ public void Execute(RestRequest request)
+ {
+ RestClient client = GetRestClient();
+ IRestResponse response = client.Execute(request);
+
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ throw new Exception(string.Format("Service call failed with HTTP response [{0}].",
+ Enum.GetName(typeof (HttpStatusCode), response.StatusCode)));
+ }
+ }
+
+ /// <summary>
+ /// Executes the rest request, where the JSON response is expected to be deserialized.
+ /// </summary>
+ /// <typeparam name="T">The type of the response to be deserialized.</typeparam>
+ /// <param name="request">The request to be executed.</param>
+ /// <returns>The response, deserialized as type T</returns>
+ public T Execute<T>(RestRequest request) where T : new()
+ {
+ RestClient client = GetRestClient();
+
+ IRestResponse<T> response = client.Execute<T>(request);
+
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ throw new Exception(string.Format("Service call failed with HTTP response [{0}].",
+ Enum.GetName(typeof (HttpStatusCode), response.StatusCode)));
+ }
+ if (response.ResponseStatus != ResponseStatus.Completed)
+ {
+ throw new Exception(
+ string.Format(
+ "Response deserialization failed with status [{0} - {1}]. If no response is expected, call Execute() with parameter 'expectJsonRespone=false'",
+ Enum.GetName(typeof (ResponseStatus), response.ResponseStatus),
+ response.ErrorMessage));
+ }
+ return response.Data;
+ }
+
+ private RestClient GetRestClient()
+ {
if (_restClient == null)
{
@@ -117,128 +118,122 @@ private RestClient GetRestClient()
{
return _restClient;
}
- }
-
-
- /// <summary>
- /// Creates a new series in the database with a key.
- /// </summary>
- /// <param name="key"> A user-defined key for the series </param>
- /// <returns> A Series </returns>
- public Series CreateSeries(string key = "")
- {
- const string url = "/series/";
-
- var body = new Dictionary<string, string>();
- body.Add("key", key);
- RestRequest request = BuildRequest(url, Method.POST, body);
- return Execute<Series>(request);
- }
-
- /// <summary>
- /// Updates metadata for a Series
- /// </summary>
- /// <param name="series"> The series to update. </param>
- /// <returns> The updated Series </returns>
- public Series UpdateSeries(Series series)
- {
- const string url = "/series/id/{id}/";
- RestRequest request = BuildRequest(url, Method.PUT, series);
- request.AddUrlSegment("id", series.Id);
- return Execute<Series>(request);
- }
-
-
- /// <summary>
- /// Gets a series by Key
- /// </summary>
- /// <returns> A list of Series with the matching Key </returns>
- public Series GetSeriesByKey(string key)
- {
- const string url = "/series/key/{key}";
-
- RestRequest request = BuildRequest(url, Method.GET);
- request.AddUrlSegment("key", key);
-
- return Execute<Series>(request);
- }
-
-
- /// <summary>
- /// Gets a series by Id
- /// </summary>
- /// <returns> The Series that matches the specified Id </returns>
- public Series GetSeriesById(string id)
- {
- const string url = "/series/id/{id}";
-
- RestRequest request = BuildRequest(url, Method.GET);
- request.AddUrlSegment("id", id);
-
- return Execute<Series>(request);
- }
-
- /// <summary>
- /// Lists all of the series in the database, with an optional filter.
- /// </summary>
- /// <param name="filter">Filter criteria to limit the list of results.</param>
- /// <returns> A list of Series containing only those that match the specified filter criteria </returns>
- public List<Series> ListSeries(Filter filter = null)
- {
- if (filter == null)
- {
- filter = new Filter();
- }
-
- const string url = "/series";
- RestRequest request = BuildRequest(url, Method.GET);
- ApplyFilterToRequest(request, filter);
- var result = Execute<List<Series>>(request);
- return result;
- }
-
-
- /// <summary>
- /// Writes a DataSet by id
- /// </summary>
- /// <param name="seriesId"> The id of the series into which the data points will be added</param>
- /// <param name="data"> A list of DataPoints to write </param>
- public void WriteById(string seriesId, IList<DataPoint> data)
- {
- WriteDataPoints(SeriesProperty.Id, seriesId, data);
- }
-
- /// <summary>
- /// Writes a DataSet by key
- /// </summary>
- /// <param name="seriesKey"> The key of the series into which the data points will be added</param>
- /// <param name="data"> A list of DataPoints to write </param>
- public void WriteByKey(string seriesKey, IList<DataPoint> data)
- {
- WriteDataPoints(SeriesProperty.Key, seriesKey, data);
- }
-
-
- private void WriteDataPoints(string seriesProperty, string propertyValue, IList<DataPoint> data)
- {
- const string url = "/series/{property}/{value}/data/";
- RestRequest request = BuildRequest(url, Method.POST, data);
- request.AddUrlSegment("property", seriesProperty);
- request.AddUrlSegment("value", propertyValue);
- Execute(request);
- }
-
-
- /// <summary>
- /// Writes a set of datapoints for different series for the same timestamp
- /// </summary>
- /// <param name="dataSet"> A BulkDataSet to write </param>
- public virtual void WriteBulkData(BulkDataSet dataSet)
- {
- const string url = "/data/";
- RestRequest request = BuildRequest(url, Method.POST, dataSet);
- Execute(request);
- }
+ }
+
+ /// <summary>
+ /// Creates a new series in the database with a key.
+ /// </summary>
+ /// <param name="key"> A user-defined key for the series </param>
+ /// <returns> A Series </returns>
+ public Series CreateSeries(string key = "")
+ {
+ const string url = "/series/";
+
+ var body = new Dictionary<string, string>();
+ body.Add("key", key);
+ RestRequest request = BuildRequest(url, Method.POST, body);
+ return Execute<Series>(request);
+ }
+
+ /// <summary>
+ /// Updates metadata for a Series
+ /// </summary>
+ /// <param name="series"> The series to update. </param>
+ /// <returns> The updated Series </returns>
+ public Series UpdateSeries(Series series)
+ {
+ const string url = "/series/id/{id}/";
+ RestRequest request = BuildRequest(url, Method.PUT, series);
+ request.AddUrlSegment("id", series.Id);
+ return Execute<Series>(request);
+ }
+
+ /// <summary>
+ /// Gets a series by Key
+ /// </summary>
+ /// <returns> A list of Series with the matching Key </returns>
+ public Series GetSeriesByKey(string key)
+ {
+ const string url = "/series/key/{key}";
+
+ RestRequest request = BuildRequest(url, Method.GET);
+ request.AddUrlSegment("key", key);
+
+ return Execute<Series>(request);
+ }
+
+ /// <summary>
+ /// Gets a series by Id
+ /// </summary>
+ /// <returns> The Series that matches the specified Id </returns>
+ public Series GetSeriesById(string id)
+ {
+ const string url = "/series/id/{id}";
+
+ RestRequest request = BuildRequest(url, Method.GET);
+ request.AddUrlSegment("id", id);
+
+ return Execute<Series>(request);
+ }
+
+ /// <summary>
+ /// Lists all of the series in the database, with an optional filter.
+ /// </summary>
+ /// <param name="filter">Filter criteria to limit the list of results.</param>
+ /// <returns> A list of Series containing only those that match the specified filter criteria </returns>
+ public List<Series> ListSeries(Filter filter = null)
+ {
+ if (filter == null)
+ {
+ filter = new Filter();
+ }
+
+ const string url = "/series";
+ RestRequest request = BuildRequest(url, Method.GET);
+ ApplyFilterToRequest(request, filter);
+ var result = Execute<List<Series>>(request);
+ return result;
+ }
+
+ /// <summary>
+ /// Writes a DataSet by id
+ /// </summary>
+ /// <param name="seriesId"> The id of the series into which the data points will be added</param>
+ /// <param name="data"> A list of DataPoints to write </param>
+ public void WriteById(string seriesId, IList<DataPoint> data)
+ {
+ WriteDataPoints(SeriesProperty.Id, seriesId, data);
+ }
+
+ /// <summary>
+ /// Writes a DataSet by key
+ /// </summary>
+ /// <param name="seriesKey"> The key of the series into which the data points will be added</param>
+ /// <param name="data"> A list of DataPoints to write </param>
+ public void WriteByKey(string seriesKey, IList<DataPoint> data)
+ {
+ WriteDataPoints(SeriesProperty.Key, seriesKey, data);
+ }
+
+ private void WriteDataPoints(string seriesProperty, string propertyValue, IList<DataPoint> data)
+ {
+ const string url = "/series/{property}/{value}/data/";
+ RestRequest request = BuildRequest(url, Method.POST, data);
+ request.AddUrlSegment("property", seriesProperty);
+ request.AddUrlSegment("value", propertyValue);
+ Execute(request);
+ }
+
+ /// <summary>
+ /// Writes a set of datapoints for different series for the same timestamp
+ /// </summary>
+ /// <param name="dataSet"> A BulkDataSet to write </param>
+ public virtual void WriteBulkData(BulkDataSet dataSet)
+ {
+ const string url = "/data/";
+ RestRequest request = BuildRequest(url, Method.POST, dataSet);
+ Execute(request);
+ }
/// <summary>
/// Increments a DataSet by id
@@ -260,7 +255,6 @@ public void IncrementByKey(string seriesKey, IList<DataPoint> data)
IncrementDataPoints(SeriesProperty.Key, seriesKey, data);
}
-
private void IncrementDataPoints(string seriesProperty, string propertyValue, IList<DataPoint> data)
{
const string url = "/series/{property}/{value}/increment/";
@@ -270,7 +264,6 @@ private void IncrementDataPoints(string seriesProperty, string propertyValue, IL
Execute(request);
}
-
/// <summary>
/// Increments a set of datapoints for different series for the same timestamp
/// </summary>
@@ -283,10 +276,10 @@ public virtual void IncrementBulkData(BulkDataSet dataSet)
}
/// <summary>
- /// Deletes a range of data by id
- /// </summary>
- /// <param name="seriesId"> The id of the series into which the data points will be deleted</param>
- /// <param name="start"> Start of range </param>
+ /// Deletes a range of data by id
+ /// </summary>
+ /// <param name="seriesId"> The id of the series into which the data points will be deleted</param>
+ /// <param name="start"> Start of range </param>
/// <param name="end"> End of range </param>
public void DeleteById(string seriesId, DateTime start, DateTime end)
{
@@ -315,133 +308,130 @@ private void DeleteDataPoints(string seriesProperty, string propertyValue, DateT
Execute(request);
}
+ private RestRequest BuildRequest(string url, Method method, object body = null)
+ {
+ var request = new RestRequest
+ {
+ Method = method,
+ Resource = url,
+ Timeout = DEFAULT_TIMEOUT_MILLIS,
+ RequestFormat = DataFormat.Json,
+ JsonSerializer = new JsonSerializer()
+ };
+
+ request.AddHeader("Accept-Encoding", "gzip,deflate");
+
+ if (body != null)
+ {
+ request.AddBody(body);
+ }
+
+ return request;
+ }
- private RestRequest BuildRequest(string url, Method method, object body = null)
- {
- var request = new RestRequest
- {
- Method = method,
- Resource = url,
- Timeout = DEFAULT_TIMEOUT_MILLIS,
- RequestFormat = DataFormat.Json,
- JsonSerializer = new JsonSerializer()
- };
-
- request.AddHeader("Accept-Encoding", "gzip,deflate");
-
- if (body != null)
- {
- request.AddBody(body);
- }
-
- return request;
- }
-
- /// <summary>
- /// Reads a DataSet by id
- /// </summary>
- /// <param name="seriesId"> The id of the series </param>
- /// <param name="start"> The start time of the range </param>
- /// <param name="end"> The end time of the range </param>
- /// <param name="interval"> An interval for the rollup. (e.g. 1min, 15min, 1hour, 1day, 1month) </param>
- /// <param name="function"> A function for the rollup. (e.g. min, max, sum, avg, stddev, count) </param>
- /// <returns> A DataSet </returns>
- public virtual DataSet ReadById(string seriesId, DateTime start, DateTime end, string interval = null,
- string function = null)
- {
- return ReadDataSet(SeriesProperty.Id, seriesId, start, end, interval, function);
- }
-
-
- /// <summary>
- /// Reads a DataSet by key
- /// </summary>
- /// <param name="seriesKey"> The key of the series </param>
- /// <param name="start"> The start time of the range </param>
- /// <param name="end"> The end time of the range </param>
- /// <param name="interval"> An interval for the rollup. (e.g. 1min, 15min, 1hour, 1day, 1month) </param>
- /// <param name="function"> A function for the rollup. (e.g. min, max, sum, avg, stddev, count) </param>
- /// <returns> A DataSet </returns>
- public virtual DataSet ReadByKey(string seriesKey, DateTime start, DateTime end, string interval = null,
- string function = null)
- {
- return ReadDataSet(SeriesProperty.Key, seriesKey, start, end, interval, function);
- }
-
- private DataSet ReadDataSet(string seriesProperty, string propertyValue, DateTime start, DateTime end,
- string interval = null, string function = null)
- {
- RestRequest request = BuildRequest("/series/{property}/{value}/data", Method.GET);
-
- request.AddUrlSegment("property", seriesProperty);
- request.AddUrlSegment("value", propertyValue);
-
- AddReadParameters(request, start, end, interval, function);
-
- var result = Execute<DataSet>(request);
- return result;
- }
-
-
- /// <summary>
- /// Reads a list of DataSet by the provided filter and rolluped by the interval
- /// </summary>
- /// <param name="start"> The start time of the range </param>
- /// <param name="end"> The end time of the range </param>
- /// <param name="filter"> A Filter instance to filter the series </param>
- /// <param name="interval"> An interval for the rollup. (e.g. 1min, 15min, 1hour, 1day, 1month) </param>
- /// <param name="function"> A function for the rollup. (e.g. min, max, sum, avg, stddev, count) </param>
- /// <returns> A list of DataSets </returns>
- public virtual IList<DataSet> ReadMultipleSeries(DateTime start, DateTime end, Filter filter, string interval = null,
- string function = null)
- {
- RestRequest request = BuildRequest("/data/", Method.GET);
-
- AddReadParameters(request, start, end, interval, function);
-
- if (filter != null)
- {
- ApplyFilterToRequest(request, filter);
- }
-
- return Execute<List<DataSet>>(request);
- }
-
- private static void AddReadParameters(IRestRequest request, DateTime start, DateTime end, string interval = null,
- string function = null)
- {
- request.AddParameter(QueryStringParameter.Start, TempoDateTimeConvertor.ConvertDateTimeToString(start));
- request.AddParameter(QueryStringParameter.End, TempoDateTimeConvertor.ConvertDateTimeToString(end));
-
- if (!string.IsNullOrEmpty(interval))
- {
- request.AddParameter(QueryStringParameter.Interval, interval);
- }
-
- if (!string.IsNullOrEmpty(function))
- {
- request.AddParameter(QueryStringParameter.Function, function);
- }
- }
-
- private static void ApplyFilterToRequest(IRestRequest request, Filter filter)
- {
- foreach (string id in filter.Ids)
- {
- request.AddParameter("id", id);
- }
- foreach (string key in filter.Keys)
- {
- request.AddParameter("key", key);
- }
- foreach (string tag in filter.Tags)
- {
- request.AddParameter("tag", tag);
- }
- foreach (var attribute in filter.Attributes)
- {
- request.AddParameter(string.Format("attr[{0}]", attribute.Key), attribute.Value);
- }
- }
- }
+ /// <summary>
+ /// Reads a DataSet by id
+ /// </summary>
+ /// <param name="seriesId"> The id of the series </param>
+ /// <param name="start"> The start time of the range </param>
+ /// <param name="end"> The end time of the range </param>
+ /// <param name="interval"> An interval for the rollup. (e.g. 1min, 15min, 1hour, 1day, 1month) </param>
+ /// <param name="function"> A function for the rollup. (e.g. min, max, sum, avg, stddev, count) </param>
+ /// <returns> A DataSet </returns>
+ public virtual DataSet ReadById(string seriesId, DateTime start, DateTime end, string interval = null,
+ string function = null)
+ {
+ return ReadDataSet(SeriesProperty.Id, seriesId, start, end, interval, function);
+ }
+
+ /// <summary>
+ /// Reads a DataSet by key
+ /// </summary>
+ /// <param name="seriesKey"> The key of the series </param>
+ /// <param name="start"> The start time of the range </param>
+ /// <param name="end"> The end time of the range </param>
+ /// <param name="interval"> An interval for the rollup. (e.g. 1min, 15min, 1hour, 1day, 1month) </param>
+ /// <param name="function"> A function for the rollup. (e.g. min, max, sum, avg, stddev, count) </param>
+ /// <returns> A DataSet </returns>
+ public virtual DataSet ReadByKey(string seriesKey, DateTime start, DateTime end, string interval = null,
+ string function = null)
+ {
+ return ReadDataSet(SeriesProperty.Key, seriesKey, start, end, interval, function);
+ }
+
+ private DataSet ReadDataSet(string seriesProperty, string propertyValue, DateTime start, DateTime end,
+ string interval = null, string function = null)
+ {
+ RestRequest request = BuildRequest("/series/{property}/{value}/data", Method.GET);
+
+ request.AddUrlSegment("property", seriesProperty);
+ request.AddUrlSegment("value", propertyValue);
+
+ AddReadParameters(request, start, end, interval, function);
+
+ var result = Execute<DataSet>(request);
+ return result;
+ }
+
+ /// <summary>
+ /// Reads a list of DataSet by the provided filter and rolluped by the interval
+ /// </summary>
+ /// <param name="start"> The start time of the range </param>
+ /// <param name="end"> The end time of the range </param>
+ /// <param name="filter"> A Filter instance to filter the series </param>
+ /// <param name="interval"> An interval for the rollup. (e.g. 1min, 15min, 1hour, 1day, 1month) </param>
+ /// <param name="function"> A function for the rollup. (e.g. min, max, sum, avg, stddev, count) </param>
+ /// <returns> A list of DataSets </returns>
+ public virtual IList<DataSet> ReadMultipleSeries(DateTime start, DateTime end, Filter filter, string interval = null,
+ string function = null)
+ {
+ RestRequest request = BuildRequest("/data/", Method.GET);
+
+ AddReadParameters(request, start, end, interval, function);
+
+ if (filter != null)
+ {
+ ApplyFilterToRequest(request, filter);
+ }
+
+ return Execute<List<DataSet>>(request);
+ }
+
+ private static void AddReadParameters(IRestRequest request, DateTime start, DateTime end, string interval = null,
+ string function = null)
+ {
+ request.AddParameter(QueryStringParameter.Start, TempoDateTimeConvertor.ConvertDateTimeToString(start));
+ request.AddParameter(QueryStringParameter.End, TempoDateTimeConvertor.ConvertDateTimeToString(end));
+
+ if (!string.IsNullOrEmpty(interval))
+ {
+ request.AddParameter(QueryStringParameter.Interval, interval);
+ }
+
+ if (!string.IsNullOrEmpty(function))
+ {
+ request.AddParameter(QueryStringParameter.Function, function);
+ }
+ }
+
+ private static void ApplyFilterToRequest(IRestRequest request, Filter filter)
+ {
+ foreach (string id in filter.Ids)
+ {
+ request.AddParameter("id", id);
+ }
+ foreach (string key in filter.Keys)
+ {
+ request.AddParameter("key", key);
+ }
+ foreach (string tag in filter.Tags)
+ {
+ request.AddParameter("tag", tag);
+ }
+ foreach (var attribute in filter.Attributes)
+ {
+ request.AddParameter(string.Format("attr[{0}]", attribute.Key), attribute.Value);
+ }
+ }
+ }
}
View
107 Client/JsonSerializer.cs
@@ -1,60 +1,59 @@
-using System;
-using Newtonsoft.Json;
+using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
-using RestSharp;
using RestSharp.Deserializers;
using RestSharp.Serializers;
+using RestSharp;
+using System;
+
namespace Client
{
-
- public class JsonDeserializer : IDeserializer
- {
- public T Deserialize<T>(IRestResponse response)
- {
- return JsonConvert.DeserializeObject<T>(response.Content);
- }
-
- public string RootElement { get; set; }
- public string Namespace { get; set; }
- public string DateFormat { get; set; }
- }
-
-
- public class JsonSerializer : ISerializer
- {
- public JsonSerializer()
- {
- ContentType = "application/json";
- }
-
- public string Serialize(object obj)
- {
- return JsonConvert.SerializeObject(obj, new TempoDateTimeConvertor());
- }
-
- public string RootElement { get; set; }
- public string Namespace { get; set; }
- public string DateFormat { get; set; }
- public string ContentType { get; set; }
- }
-
- public class TempoDateTimeConvertor : DateTimeConverterBase
- {
-
- public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
- {
- writer.WriteValue(ConvertDateTimeToString(value));
- }
-
- public static string ConvertDateTimeToString(object value)
- {
- return ((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss.fffzzz");
- }
-
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
- {
- return DateTime.Parse(reader.Value.ToString());
- }
- }
-}
+ public class JsonDeserializer : IDeserializer
+ {
+ public T Deserialize<T>(IRestResponse response)
+ {
+ return JsonConvert.DeserializeObject<T>(response.Content);
+ }
+
+ public string RootElement { get; set; }
+ public string Namespace { get; set; }
+ public string DateFormat { get; set; }
+ }
+
+ public class JsonSerializer : ISerializer
+ {
+ public JsonSerializer()
+ {
+ ContentType = "application/json";
+ }
+
+ public string Serialize(object obj)
+ {
+ return JsonConvert.SerializeObject(obj, new TempoDateTimeConvertor());
+ }
+
+ public string RootElement { get; set; }
+ public string Namespace { get; set; }
+ public string DateFormat { get; set; }
+ public string ContentType { get; set; }
+ }
+
+ public class TempoDateTimeConvertor : DateTimeConverterBase
+ {
+
+ public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ writer.WriteValue(ConvertDateTimeToString(value));
+ }
+
+ public static string ConvertDateTimeToString(object value)
+ {
+ return ((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss.fffzzz");
+ }
+
+ public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return DateTime.Parse(reader.Value.ToString());
+ }
+ }
+}
View
40 Client/Model/BulkDataSet.cs
@@ -1,28 +1,28 @@
+using Newtonsoft.Json;
using System;
using System.Collections.Generic;
-using Newtonsoft.Json;
+
namespace Client.Model
{
+ /// <summary>
+ /// Set of data to send for a bulk write. This encapsulates the timestamp and list of BulkPoints
+ /// </summary>
+ public class BulkDataSet
+ {
+ /// <param name="timestamp"> The timestamp to write the datapoints at </param>
+ /// <param name="data"> A list of BulkPoints to write </param>
- /// <summary>
- /// Set of data to send for a bulk write. This encapsulates the timestamp and list of BulkPoints
- /// </summary>
- public class BulkDataSet
- {
- /// <param name="timestamp"> The timestamp to write the datapoints at </param>
- /// <param name="data"> A list of BulkPoints to write </param>
-
- public BulkDataSet(DateTime timestamp, List<BulkPoint> data)
- {
- Timestamp = timestamp;
- Data = data;
- }
+ public BulkDataSet(DateTime timestamp, List<BulkPoint> data)
+ {
+ Timestamp = timestamp;
+ Data = data;
+ }
- [JsonProperty(PropertyName = "t")]
- public DateTime Timestamp { get; private set; }
+ [JsonProperty(PropertyName = "t")]
+ public DateTime Timestamp { get; private set; }
- [JsonProperty(PropertyName = "data")]
- public List<BulkPoint> Data { get; private set; }
- }
-}
+ [JsonProperty(PropertyName = "data")]
+ public List<BulkPoint> Data { get; private set; }
+ }
+}
View
36 Client/Model/BulkIdPoint.cs
@@ -1,23 +1,23 @@
using Newtonsoft.Json;
+
namespace Client.Model
{
- /// <summary>
- /// Represents a datapoint for a series referenced by id. This class is used to represent
- /// datapoints in a bulk write.
- /// </summary>
- public class BulkIdPoint : BulkPoint
- {
- /// <param name="id"> The id of the Series </param>
- /// <param name="value"> The datapoint value </param>
- public BulkIdPoint(string id, double value)
- {
- Id = id;
- Value = value;
- }
-
- [JsonProperty(PropertyName = "id")]
- public string Id { get; private set; }
- }
+ /// <summary>
+ /// Represents a datapoint for a series referenced by id. This class is used to represent
+ /// datapoints in a bulk write.
+ /// </summary>
+ public class BulkIdPoint : BulkPoint
+ {
+ /// <param name="id"> The id of the Series </param>
+ /// <param name="value"> The datapoint value </param>
+ public BulkIdPoint(string id, double value)
+ {
+ Id = id;
+ Value = value;
+ }
-}
+ [JsonProperty(PropertyName = "id")]
+ public string Id { get; private set; }
+ }
+}
View
36 Client/Model/BulkKeyPoint.cs
@@ -1,23 +1,23 @@
using Newtonsoft.Json;
+
namespace Client.Model
{
- /// <summary>
- /// Represents a datapoint for a series referenced by id. This class is used to represent
- /// datapoints in a bulk write.
- /// </summary>
- public class BulkKeyPoint : BulkPoint
- {
- /// <param name="key"> The key of the Series </param>
- /// <param name="value"> The datapoint value </param>
- public BulkKeyPoint(string key, double value)
- {
- Key = key;
- Value = value;
- }
-
- [JsonProperty(PropertyName = "key")]
- public string Key { get; private set; }
- }
+ /// <summary>
+ /// Represents a datapoint for a series referenced by id. This class is used to represent
+ /// datapoints in a bulk write.
+ /// </summary>
+ public class BulkKeyPoint : BulkPoint
+ {
+ /// <param name="key"> The key of the Series </param>
+ /// <param name="value"> The datapoint value </param>
+ public BulkKeyPoint(string key, double value)
+ {
+ Key = key;
+ Value = value;
+ }
-}
+ [JsonProperty(PropertyName = "key")]
+ public string Key { get; private set; }
+ }
+}
View
26 Client/Model/BulkPoint.cs
@@ -1,18 +1,18 @@
-
using Newtonsoft.Json;
+
namespace Client.Model
{
- /// <summary>
- /// The abstract parent class representing a datapoint used in a bulk write. Bulk
- /// writing allows values for different series to be written for the same timestamp in one
- /// Rest call. The series can be referenced by series id or series key. The two subclasses
- /// represent these two options.
- /// </summary>
+ /// <summary>
+ /// The abstract parent class representing a datapoint used in a bulk write. Bulk
+ /// writing allows values for different series to be written for the same timestamp in one
+ /// Rest call. The series can be referenced by series id or series key. The two subclasses
+ /// represent these two options.
+ /// </summary>
- public abstract class BulkPoint
- {
- [JsonProperty(PropertyName = "v")]
- public double Value { get; set; }
- }
-}
+ public abstract class BulkPoint
+ {
+ [JsonProperty(PropertyName = "v")]
+ public double Value { get; set; }
+ }
+}
View
122 Client/Model/Common.cs
@@ -1,63 +1,63 @@
namespace Client.Model
{
- public static class FoldingFunction
- {
- public static readonly string Count = "count";
- public static readonly string Max = "max";
- public static readonly string Mean = "mean";
- public static readonly string Min = "min";
- public static readonly string StandardDeviation = "stddev";
- public static readonly string Sum = "sum";
- public static readonly string SumOfSquares = "ss";
- }
-
- public static class IntervalParameter
- {
- public static string Days(int days)
- {
- return string.Format("{0}day", days);
- }
-
- public static string Hours(int hours)
- {
- return string.Format("{0}hour", hours);
- }
-
- public static string Minutes(int minutes)
- {
- return string.Format("{0}min", minutes);
- }
-
- public static string Months(int months)
- {
- return string.Format("{0}month", months);
- }
-
- public static string Years(int years)
- {
- return string.Format("{0}year", years);
- }
-
- public static string Raw()
- {
- return "raw";
- }
-
- }
-
- public static class SeriesProperty
- {
- public static readonly string Id = "id";
- public static readonly string Key = "key";
- }
-
- public static class QueryStringParameter
- {
- public static readonly string Id = "id";
- public static readonly string Key = "key";
- public static readonly string Start = "start";
- public static readonly string End = "end";
- public static readonly string Function = "function";
- public static readonly string Interval = "interval";
- }
-}
+ public static class FoldingFunction
+ {
+ public static readonly string Count = "count";
+ public static readonly string Max = "max";
+ public static readonly string Mean = "mean";
+ public static readonly string Min = "min";
+ public static readonly string StandardDeviation = "stddev";
+ public static readonly string Sum = "sum";
+ public static readonly string SumOfSquares = "ss";
+ }
+
+ public static class IntervalParameter
+ {
+ public static string Days(int days)
+ {
+ return string.Format("{0}day", days);
+ }
+
+ public static string Hours(int hours)
+ {
+ return string.Format("{0}hour", hours);
+ }
+
+ public static string Minutes(int minutes)
+ {
+ return string.Format("{0}min", minutes);
+ }
+
+ public static string Months(int months)
+ {
+ return string.Format("{0}month", months);
+ }
+
+ public static string Years(int years)
+ {
+ return string.Format("{0}year", years);
+ }
+
+ public static string Raw()
+ {
+ return "raw";
+ }
+
+ }
+
+ public static class SeriesProperty
+ {
+ public static readonly string Id = "id";
+ public static readonly string Key = "key";
+ }
+
+ public static class QueryStringParameter
+ {
+ public static readonly string Id = "id";
+ public static readonly string Key = "key";
+ public static readonly string Start = "start";
+ public static readonly string End = "end";
+ public static readonly string Function = "function";
+ public static readonly string Interval = "interval";
+ }
+}
View
56 Client/Model/DataPoint.cs
@@ -1,41 +1,42 @@
-using System;
using Newtonsoft.Json;
+using System;
+
namespace Client.Model
{
- /// <summary>
- /// Represents one timestamp/value pair. This class uses a Joda Time DateTime.
- /// </summary>
- public class DataPoint
- {
- public DataPoint()
- {
- }
-
- /// <param name="timestamp"> DateTime representing the data point's timstamp </param>
- /// <param name="value"> The value of the measurement (long or double) </param>
- public DataPoint(DateTime timestamp, double value)
- {
- Timestamp = timestamp;
- Value = value;
- }
+ /// <summary>
+ /// Represents one timestamp/value pair. This class uses a Joda Time DateTime.
+ /// </summary>
+ public class DataPoint
+ {
+ public DataPoint()
+ {
+ }
+
+ /// <param name="timestamp"> DateTime representing the data point's timstamp </param>
+ /// <param name="value"> The value of the measurement (long or double) </param>
+ public DataPoint(DateTime timestamp, double value)
+ {
+ Timestamp = timestamp;
+ Value = value;
+ }
[JsonProperty(PropertyName = "t")]
public DateTime Timestamp { get; set; }
[JsonProperty(PropertyName = "v")]
public double Value { get; set; }
-
- public override string ToString()
- {
- return string.Format("DataPoint({0}, {1})", Timestamp, Value);
- }
+
+ public override string ToString()
+ {
+ return string.Format("DataPoint({0}, {1})", Timestamp, Value);
+ }
public override bool Equals(Object obj)
{
DataPoint other = obj as DataPoint;
- return other != null &&
+ return other != null &&
Timestamp.Equals(other.Timestamp) &&
Value.Equals(other.Value);
}
@@ -44,10 +45,5 @@ public override Int32 GetHashCode()
{
return Timestamp.GetHashCode() ^ Value.GetHashCode();
}
-
- }
-
-
-
-
-}
+ }
+}
View
36 Client/Model/DataSet.cs
@@ -1,23 +1,23 @@
+using Newtonsoft.Json;
using System;
using System.Collections.Generic;
-using Newtonsoft.Json;
+
namespace Client.Model
{
- /// <summary>
- /// Respresents data from a time range of a series. This is essentially a list
- /// of DataPoints with some added metadata. This is the object returned from a query.
- /// The DataSet contains series metadata, the start/end times for the queried range,
- /// a list of the DataPoints and a statistics summary table. The Summary table contains
- /// statistics for the time range (sum, mean, min, max, count, etc.)
- /// </summary>
- public class DataSet
- {
- public Series Series { get; set; }
- public DateTime Start { get; set; }
- public DateTime End { get; set; }
- public List<DataPoint> Data { get; set; }
- public Dictionary<string, double> Summary { get; set; }
- }
-
-}
+ /// <summary>
+ /// Respresents data from a time range of a series. This is essentially a list
+ /// of DataPoints with some added metadata. This is the object returned from a query.
+ /// The DataSet contains series metadata, the start/end times for the queried range,
+ /// a list of the DataPoints and a statistics summary table. The Summary table contains
+ /// statistics for the time range (sum, mean, min, max, count, etc.)
+ /// </summary>
+ public class DataSet
+ {
+ public Series Series { get; set; }
+ public DateTime Start { get; set; }
+ public DateTime End { get; set; }
+ public List<DataPoint> Data { get; set; }
+ public Dictionary<string, double> Summary { get; set; }
+ }
+}
View
167 Client/Model/Filter.cs
@@ -1,94 +1,93 @@
using System.Collections.Generic;
+
namespace Client.Model
{
+ /// <summary>
+ /// Represents a filter on the set of Series. This is used to query a set of series with specific
+ /// properties. A filter can include ids, keys, tags and attributes. An empty filter is created
+ /// and filter predicates are added. For example a filter to return series with keys myagley-1 and
+ /// myagley-2 looks like this:
+ /// <pre>
+ /// {@code
+ /// Filter filter = new Filter();
+ /// filter.addKey("myagley-1");
+ /// filter.addKey("myagley-1");
+ /// }
+ /// </pre>
+ /// </summary>
+ public class Filter
+ {
+ private readonly IList<string> _ids = new List<string>();
+ private readonly IList<string> _keys = new List<string>();
+ private readonly IList<string> _tags = new List<string>();
+ private readonly IDictionary<string, string> _attributes = new Dictionary<string, string>();
- /// <summary>
- /// Represents a filter on the set of Series. This is used to query a set of series with specific
- /// properties. A filter can include ids, keys, tags and attributes. An empty filter is created
- /// and filter predicates are added. For example a filter to return series with keys myagley-1 and
- /// myagley-2 looks like this:
- /// <pre>
- /// {@code
- /// Filter filter = new Filter();
- /// filter.addKey("myagley-1");
- /// filter.addKey("myagley-1");
- /// }
- /// </pre>
- /// </summary>
- public class Filter
- {
- private readonly IList<string> _ids = new List<string>();
- private readonly IList<string> _keys = new List<string>();
- private readonly IList<string> _tags = new List<string>();
- private readonly IDictionary<string, string> _attributes = new Dictionary<string, string>();
-
- /// <summary>
- /// Adds an id to the filter.
- /// </summary>
- /// <param name="id"> The id to add </param>
- public void AddId(string id)
- {
- _ids.Add(id);
- }
-
- /// <summary>
- /// Adds a key to the filter.
- /// </summary>
- /// <param name="key"> The key to add </param>
- public void AddKey(string key)
- {
- _keys.Add(key);
- }
+ /// <summary>
+ /// Adds an id to the filter.
+ /// </summary>
+ /// <param name="id"> The id to add </param>
+ public void AddId(string id)
+ {
+ _ids.Add(id);
+ }
- /// <summary>
- /// Adds a tag to the filter.
- /// </summary>
- /// <param name="tag"> The tag to add </param>
- public void AddTag(string tag)
- {
- _tags.Add(tag);
- }
+ /// <summary>
+ /// Adds a key to the filter.
+ /// </summary>
+ /// <param name="key"> The key to add </param>
+ public void AddKey(string key)
+ {
+ _keys.Add(key);
+ }
- /// <summary>
- /// Adds an attribute to the filter.
- /// </summary>
- /// <param name="key"> The attribute key </param>
- /// <param name="value"> The attribute value </param>
+ /// <summary>
+ /// Adds a tag to the filter.
+ /// </summary>
+ /// <param name="tag"> The tag to add </param>
+ public void AddTag(string tag)
+ {
+ _tags.Add(tag);
+ }
- public void AddAttribute(string key, string value)
- {
- _attributes[key] = value;
- }
+ /// <summary>
+ /// Adds an attribute to the filter.
+ /// </summary>
+ /// <param name="key"> The attribute key </param>
+ /// <param name="value"> The attribute value </param>
- public IList<string> Ids
- {
- get
- {
- return _ids;
- }
- }
- public IList<string> Keys
- {
- get
- {
- return _keys;
- }
- }
- public IList<string> Tags
- {
- get
- {
- return _tags;
- }
- }
- public IDictionary<string, string> Attributes
- {
- get
- {
- return _attributes;
- }
- }
- }
+ public void AddAttribute(string key, string value)
+ {
+ _attributes[key] = value;
+ }
-}
+ public IList<string> Ids
+ {
+ get
+ {
+ return _ids;
+ }
+ }
+ public IList<string> Keys
+ {
+ get
+ {
+ return _keys;
+ }
+ }
+ public IList<string> Tags
+ {
+ get
+ {
+ return _tags;
+ }
+ }
+ public IDictionary<string, string> Attributes
+ {
+ get
+ {
+ return _attributes;
+ }
+ }
+ }
+}
View
59 Client/Model/Series.cs
@@ -1,46 +1,47 @@
+using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Reflection;
-using Newtonsoft.Json;
+
namespace Client.Model
{
- /// <summary>
- /// Respresents metadata associated with the series. Each series has a
- /// globally unique id that is generated by the system and a user defined key.
- /// The key must be unique among all of your series. Each series may have a set
- /// of tags and attributes that can be used to filter series during bulk reads.
- /// Attributes are key/value pairs. Both the key and attribute must be strings.
- /// Tags are keys with no values. Tags must also be strings.
- /// </summary>
- public class Series
- {
+ /// <summary>
+ /// Respresents metadata associated with the series. Each series has a
+ /// globally unique id that is generated by the system and a user defined key.
+ /// The key must be unique among all of your series. Each series may have a set
+ /// of tags and attributes that can be used to filter series during bulk reads.
+ /// Attributes are key/value pairs. Both the key and attribute must be strings.
+ /// Tags are keys with no values. Tags must also be strings.
+ /// </summary>
+ public class Series
+ {
public Series()
{
Name = "";
Attributes = new Dictionary<string, string>();
Tags = new List<string>();
}
- [JsonProperty(PropertyName = "id", Required = Required.Always)]
- public string Id { get; set; }
+ [JsonProperty(PropertyName = "id", Required = Required.Always)]
+ public string Id { get; set; }
+
+ [JsonProperty(PropertyName = "key", Required = Required.Always)]
+ public string Key { get; set; }
- [JsonProperty(PropertyName = "key", Required = Required.Always)]
- public string Key { get; set; }
+ [JsonProperty(PropertyName = "name", Required = Required.Always)]
+ public string Name { get; set; }
- [JsonProperty(PropertyName = "name", Required = Required.Always)]
- public string Name { get; set; }
+ [JsonProperty(PropertyName = "attributes", Required = Required.Always)]
+ public Dictionary<string, string> Attributes { get; set; }
- [JsonProperty(PropertyName = "attributes", Required = Required.Always)]
- public Dictionary<string, string> Attributes { get; set; }
+ [JsonProperty(PropertyName = "tags", Required = Required.Always)]
+ public List<string> Tags { get; set; }
- [JsonProperty(PropertyName = "tags", Required = Required.Always)]
- public List<string> Tags { get; set; }
-
- public override string ToString()
- {
- return string.Format("Series: \n\tid:\t{0}\n\tkey:\t{1}\n\tname:\t{2}\n\ttags:\t{3}\n\tattr:\t{4}", Id, Key, Name, Tags, Attributes);
- }
+ public override string ToString()
+ {
+ return string.Format("Series: \n\tid:\t{0}\n\tkey:\t{1}\n\tname:\t{2}\n\ttags:\t{3}\n\tattr:\t{4}", Id, Key, Name, Tags, Attributes);
+ }
public override bool Equals(object obj)
{
@@ -59,7 +60,5 @@ public override int GetHashCode()
Attributes.GetHashCode() ^
Tags.GetHashCode();
}
-
-
- }
-}
+ }
+}

0 comments on commit bcd5adc

Please sign in to comment.