Exemplo n.º 1
0
 private static void AddOnDuplicate(this IList <object> args, TsDuplicatePolicy?policy)
 {
     if (policy.HasValue)
     {
         args.Add(CommandArgs.ON_DUPLICATE);
         args.Add(policy.Value.AsArg());
     }
 }
 internal TimeSeriesInformation(long totalSamples, long memoryUsage, TimeStamp firstTimeStamp, TimeStamp lastTimeStamp, long retentionTime, long chunkCount, long chunkSize, IReadOnlyList <TimeSeriesLabel> labels, string sourceKey, IReadOnlyList <TimeSeriesRule> rules, TsDuplicatePolicy?policy)
 {
     TotalSamples   = totalSamples;
     MemoryUsage    = memoryUsage;
     FirstTimeStamp = firstTimeStamp;
     LastTimeStamp  = lastTimeStamp;
     RetentionTime  = retentionTime;
     ChunkCount     = chunkCount;
     Labels         = labels;
     SourceKey      = sourceKey;
     Rules          = rules;
     // backwards compatible with RedisTimeSeries < v1.4
     MaxSamplesPerChunk = chunkSize / 16;
     ChunkSize          = chunkSize;
     // configure what to do on duplicate sample > v1.4
     DuplicatePolicy = policy;
 }
        /// <summary>
        /// Append (or create and append) a new sample to the series.
        /// </summary>
        /// <param name="db">StackExchange.Redis IDatabase instance</param>
        /// <param name="key">Key name for timeseries</param>
        /// <param name="timestamp">TimeStamp to add. UNIX timestamp of the sample. * can be used for automatic timestamp (using the system clock)</param>
        /// <param name="value">Numeric data value of the sample.</param>
        /// <param name="retentionTime">Optional: Maximum age for samples compared to last event time (in milliseconds)</param>
        /// <param name="labels">Optional: Collaction of label-value pairs that represent metadata labels of the key</param>
        /// <param name="uncompressed">Optional: Adding this flag will keep data in an uncompressed form</param>
        /// <param name="chunkSizeBytes">Optional: Each time-series uses chunks of memory of fixed size for time series samples.
        /// You can alter the default TSDB chunk size by passing the chunk_size argument (in Bytes)</param>
        /// <param name="duplicatePolicy">Optioal: overwrite key and database configuration for DUPLICATE_POLICY</param>
        /// <returns>The timestamp value of the new sample</returns>
        public static TimeStamp TimeSeriesAdd(this IDatabase db, string key, TimeStamp timestamp, double value, long?retentionTime = null, IReadOnlyCollection <TimeSeriesLabel> labels = null, bool?uncompressed = null, long?chunkSizeBytes = null, TsDuplicatePolicy?duplicatePolicy = null)
        {
            var args = BuildTsAddArgs(key, timestamp, value, retentionTime, labels, uncompressed, chunkSizeBytes, duplicatePolicy);

            return(ParseTimeStamp(db.Execute(TS.ADD, args)));
        }
        /// <summary>
        /// Create a new time-series.
        /// </summary>
        /// <param name="db">StackExchange.Redis IDatabase instance</param>
        /// <param name="key">Key name for timeseries</param>
        /// <param name="retentionTime">Optional: Maximum age for samples compared to last event time (in milliseconds)</param>
        /// <param name="labels">Optional: Collaction of label-value pairs that represent metadata labels of the key</param>
        /// <param name="uncompressed">Optional: Adding this flag will keep data in an uncompressed form</param>
        /// <param name="chunkSizeBytes">Optional: Each time-series uses chunks of memory of fixed size for time series samples.
        /// You can alter the default TSDB chunk size by passing the chunk_size argument (in Bytes)</param>
        /// <param name="duplicatePolicy">Optinal: Define handling of duplicate samples behavior (avalible for RedisTimeseries >= 1.4)</param>
        /// <returns>If the operation executed successfully</returns>
        public static bool TimeSeriesCreate(this IDatabase db, string key, long?retentionTime = null, IReadOnlyCollection <TimeSeriesLabel> labels = null, bool?uncompressed = null, long?chunkSizeBytes = null, TsDuplicatePolicy?duplicatePolicy = null)
        {
            var args = BuildTsCreateArgs(key, retentionTime, labels, uncompressed, chunkSizeBytes, duplicatePolicy);

            return(ParseBoolean(db.Execute(TS.CREATE, args)));
        }
        private static TimeSeriesInformation ParseInfo(RedisResult result)
        {
            long      totalSamples = -1, memoryUsage = -1, retentionTime = -1, chunkSize = -1, chunkCount = -1;
            TimeStamp firstTimestamp = null, lastTimestamp = null;
            IReadOnlyList <TimeSeriesLabel> labels = null;
            IReadOnlyList <TimeSeriesRule>  rules  = null;
            string            sourceKey            = null;
            TsDuplicatePolicy?duplicatePolicy      = null;

            RedisResult[] redisResults = (RedisResult[])result;
            for (int i = 0; i < redisResults.Length; ++i)
            {
                string label = (string)redisResults[i++];
                switch (label)
                {
                case "totalSamples":
                    totalSamples = (long)redisResults[i];
                    break;

                case "memoryUsage":
                    memoryUsage = (long)redisResults[i];
                    break;

                case "retentionTime":
                    retentionTime = (long)redisResults[i];
                    break;

                case "chunkCount":
                    chunkCount = (long)redisResults[i];
                    break;

                case "chunkSize":
                    chunkSize = (long)redisResults[i];
                    break;

                case "maxSamplesPerChunk":
                    // If the property name is maxSamplesPerChunk then this is an old
                    // version of RedisTimeSeries and we used the number of samples before ( now Bytes )
                    chunkSize = chunkSize * 16;
                    break;

                case "firstTimestamp":
                    firstTimestamp = ParseTimeStamp(redisResults[i]);
                    break;

                case "lastTimestamp":
                    lastTimestamp = ParseTimeStamp(redisResults[i]);
                    break;

                case "labels":
                    labels = ParseLabelArray(redisResults[i]);
                    break;

                case "sourceKey":
                    sourceKey = (string)redisResults[i];
                    break;

                case "rules":
                    rules = ParseRuleArray(redisResults[i]);
                    break;

                case "duplicatePolicy":
                    // Avalible for > v1.4
                    duplicatePolicy = ParsePolicy(redisResults[i]);
                    break;
                }
            }

            return(new TimeSeriesInformation(totalSamples, memoryUsage, firstTimestamp,
                                             lastTimestamp, retentionTime, chunkCount, chunkSize, labels, sourceKey, rules, duplicatePolicy));
        }