private string GetWriteJsonString(IList <MetricEntity> logs) { List <InfluxRecord> data = new List <InfluxRecord>(); foreach (var item in logs) { IEnumerable <string> columns = new string[] { "value" }; IEnumerable <JsonValue> points = new JsonValue[] { new DoubleJsonValue(item.Value) }; if (item.Tags != null && item.Tags.Count > 0) { var tagKeys = item.Tags.Keys.ToArray(); JsonValue[] tagVals = new JsonValue[item.Tags.Count]; columns = Enumerable.Concat(columns, tagKeys); for (int i = 0; i < tagKeys.Length; i++) { var tagVal = item.Tags[tagKeys[i]]; tagVals[i] = new StringJsonValue(tagVal); } points = Enumerable.Concat(points, tagVals); } var record = new InfluxRecord(item.Name, item.Time, columns, points); data.Add(record); } var jsonstr = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); return(jsonstr); }
public bool Evaluate(out IJsonValue value, IJSONDocument document) { if (EvaluationType.Equals(EvaluationType.Constant)) { value = new StringJsonValue((string)Constant); } else { value = this; } return(true); }
/// <summary> /// 描述:异步将LogMetric写入Influxdb数据库 /// 作者:徐明祥 /// 日期:20150531 /// </summary> /// <param name="logs"></param> public void WriteAsync(IList <MetricEntity> logs) { if (logs == null || logs.Count == 0) { return; } List <InfluxRecord> data = new List <InfluxRecord>(); foreach (var item in logs) { IEnumerable <string> columns = new string[] { "value" }; IEnumerable <JsonValue> points = new JsonValue[] { new DoubleJsonValue(item.Value) }; if (item.Tags != null && item.Tags.Count > 0) { var tagKeys = item.Tags.Keys.ToArray(); JsonValue[] tagVals = new JsonValue[item.Tags.Count]; columns = Enumerable.Concat(columns, tagKeys); for (int i = 0; i < tagKeys.Length; i++) { var tagVal = item.Tags[tagKeys[i]]; tagVals[i] = new StringJsonValue(tagVal); } points = Enumerable.Concat(points, tagVals); } var record = new InfluxRecord(item.Name, item.Time, columns, points); data.Add(record); } using (var client = new WebClient()) { var jsonstr = new CollectionJsonValue(data.Select(d => d.Json)).AsJson(); client.UploadStringAsync(this.influxdb, jsonstr); } }
public static IJsonValue Wrap(object value) { if (value is JValue) { value = ((JValue)value).Value; } FieldDataType dataType = JSONType.GetJSONType(value); IJsonValue jsonValue = null; switch (dataType) { case FieldDataType.Array: var values = new List <IJsonValue>(); if (value is System.Collections.ArrayList) { var array = (System.Collections.ArrayList)value; foreach (var obj in array) { values.Add(Wrap(obj)); } } else { var array = (Array)value; foreach (var obj in array) { values.Add(Wrap(obj)); } } jsonValue = new ArrayJsonValue(values.ToArray()); break; case FieldDataType.Bool: jsonValue = new BooleanJsonValue((bool)value); break; case FieldDataType.DateTime: jsonValue = new DateTimeJsonValue((DateTime)value); break; case FieldDataType.Null: jsonValue = new NullValue(); break; case FieldDataType.Number: jsonValue = new NumberJsonValue(value); break; case FieldDataType.Object: if (value.GetType() == typeof(JArray)) { var arr = (JArray)value; var vals = new List <IJsonValue>(); foreach (var obj in arr) { vals.Add(Wrap(obj)); } jsonValue = new ArrayJsonValue(vals.ToArray()); } else if (value.GetType() == typeof(IJsonValue[])) { var arr = (IJsonValue[])value; //var vals = new List<IJsonValue>(); //foreach (var obj in arr) //{ // vals.Add(Wrap(obj)); //} jsonValue = new ArrayJsonValue(arr); //vals.ToArray()); } else { jsonValue = new ObjectJsonValue(Serialize(value)); } break; case FieldDataType.String: jsonValue = new StringJsonValue((string)value); break; } return(jsonValue); }