예제 #1
0
 public override string ToString()
 {
     if (_tags == null || _tags.Count == 0)
     {
         return(MeasurementName);
     }
     return(MeasurementName.EscapeMeasurementName() + "," + _tags.FormatTags());
 }
        /// <summary>
        /// Returns the string representing the point in InfluxDB Line protocol
        /// </summary>
        /// <returns></returns>
        /// <see cref="https://docs.influxdata.com/influxdb/v0.12/write_protocols/line/"/>
        public string ConvertToInfluxLineProtocol()
        {
            if (Fields.Count == 0)
            {
                throw new InvalidOperationException("InfluxDB needs atleast one field in a line");
            }
            if (String.IsNullOrWhiteSpace(MeasurementName))
            {
                throw new InvalidOperationException("InfluxDB needs a measurement name to accept a point");
            }

            var line = new StringBuilder();

            line.AppendFormat("{0}", MeasurementName.EscapeChars(comma: true, space: true));

            if (Tags.Count > 0)
            {
                line.AppendFormat(",{0}", String.Join(",", Tags.Select(t => new StringBuilder().AppendFormat("{0}={1}", t.Key.EscapeChars(comma: true, equalSign: true, space: true), t.Value.EscapeChars(comma: true, equalSign: true, space: true)))));
            }

            var    tType = typeof(T);
            string fields;

            if (tType == typeof(string))
            {
                //string needs escaping, but = is allowed in value
                fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}=\"{1}\"", v.Key.EscapeChars(comma: true, equalSign: true, space: true), v.Value.ToString().EscapeChars(doubleQuote: true))));
            }
            else if (tType == typeof(long) || tType == typeof(int))
            {
                //int needs i suffix
                fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}i", v.Key.EscapeChars(comma: true, equalSign: true, space: true), v.Value)));
            }
            else if (tType == typeof(bool))
            {
                //bool is okay with True or False
                fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}", v.Key.EscapeChars(comma: true, equalSign: true, space: true), v.Value)));
            }
            else if (tType == typeof(double))
            {
                ////double has to have a . as decimal seperator for Influx
                fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}", v.Key.EscapeChars(comma: true, equalSign: true, space: true), String.Format(new CultureInfo("en-US"), "{0}", v.Value))));
            }
            else if (typeof(IInfluxValueField).IsAssignableFrom(tType))
            {
                fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}", v.Key.EscapeChars(comma: true, equalSign: true, space: true), v.Value.ToString())));
            }
            else
            {
                throw new ArgumentException(tType + " is not supported by this library at this point");
            }

            line.AppendFormat(" {0} {1}", fields,
                              Timestamp > 0? Timestamp.ToEpoch(Precision) :(UtcTimestamp != DateTime.MinValue ? UtcTimestamp.ToEpoch(Precision) : DateTime.UtcNow.ToEpoch(Precision)));

            return(line.ToString());
        }
예제 #3
0
        protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (localContext == null)
            {
                throw new ArgumentNullException(nameof(localContext));
            }

            string   aiSetupJson = AiSetupJson.Get(context);
            AiLogger aiLogger    = new AiLogger(aiSetupJson, localContext.OrganizationService, localContext.TracingService,
                                                localContext.WorkflowExecutionContext, null, localContext.WorkflowExecutionContext.WorkflowCategory);

            string name             = Name.Get(context);
            string measurementName  = MeasurementName.Get(context);
            double measurementValue = MeasurementValue.Get(context);

            string measurementNameValidationResult = AiEvent.ValidateMeasurementName(measurementName);

            if (!string.IsNullOrEmpty(measurementNameValidationResult))
            {
                localContext.TracingService.Trace(measurementNameValidationResult);
                LogSuccess.Set(context, false);
                return;
            }

            Dictionary <string, double> measurements =
                new Dictionary <string, double> {
                { measurementName, Convert.ToDouble(measurementValue) }
            };

            bool logSuccess = aiLogger.WriteEvent(name, measurements);

            LogSuccess.Set(context, logSuccess);
        }