Inheritance: IInfluxMeasurement
Example #1
0
        /// <summary>
        /// Gets the whole DB structure for the given databse in Influx.
        /// </summary>
        /// <param name="dbName">Name of the database</param>
        /// <returns>Hierarchical structure, Database<Measurement<Tags,Fields>></returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>
        public async Task <IInfluxDatabase> GetInfluxDBStructureAsync(string dbName)
        {
            var dbStructure = new InfluxDatabase(dbName);
            var fields      = await QueryMultiSeriesAsync(dbName, "SHOW FIELD KEYS");

            foreach (var s in fields)
            {
                var measurement = new InfluxMeasurement(s.SeriesName);
                foreach (var e in s.Entries)
                {
                    measurement.Fields.Add(e.FieldKey);
                }
                dbStructure.Measurements.Add(measurement);
            }

            var tags = await QueryMultiSeriesAsync(dbName, "SHOW TAG KEYS");

            foreach (var t in tags)
            {
                var measurement = dbStructure.Measurements.FirstOrDefault(x => x.Name == t.SeriesName);
                foreach (var e in t.Entries)
                {
                    measurement.Tags.Add(e.TagKey);
                }
            }

            return(dbStructure);
        }
        /// <summary>
        /// Gets the whole DB structure for the given databse in Influx.
        /// </summary>
        /// <param name="dbName">Name of the database</param>
        /// <returns>Hierarchical structure, Database<Measurement<Tags,Fields>></returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>
        public async Task <IInfluxDatabase> GetInfluxDBStructureAsync(string dbName)
        {
            var dbStructure       = new InfluxDatabase(dbName);
            var retentionPolicies = await GetRetentionPoliciesAsync(dbName);

            foreach (var policy in retentionPolicies)
            {
                dbStructure.MeasurementHierarchy.Add(policy, new HashSet <IInfluxMeasurement>());

                var fields = await QueryMultiSeriesAsync(dbName : dbName, retentionPolicy : policy.Name, measurementQuery : "SHOW FIELD KEYS");

                foreach (var s in fields)
                {
                    var measurement = new InfluxMeasurement(s.SeriesName);
                    dbStructure.Measurements.Add(measurement);
                    dbStructure.MeasurementHierarchy[policy].Add(measurement);

                    foreach (var e in s.Entries)
                    {
                        measurement.Fields.Add(e.FieldKey);
                    }

                    measurement.SeriesCount = (await QueryMultiSeriesAsync(dbName: dbName, retentionPolicy: policy.Name, measurementQuery: "SHOW SERIES")).FirstOrDefault().Entries?.Count() ?? -1;

                    var points = (IDictionary <string, object>)(await QueryMultiSeriesAsync(dbName: dbName, retentionPolicy: policy.Name, measurementQuery: $"select count(*) from \"{s.SeriesName}\"")).FirstOrDefault().Entries?.FirstOrDefault();
                    //influx returns point counts for each of the fields. Pick the larget of them as total points in the measurement
                    measurement.PointsCount = measurement.Fields.Select(f => int.Parse(points[$"Count_{f}"].ToString())).Max();
                }

                var tags = await QueryMultiSeriesAsync(dbName : dbName, retentionPolicy : policy.Name, measurementQuery : "SHOW TAG KEYS");

                foreach (var t in tags)
                {
                    var measurement = dbStructure.Measurements.FirstOrDefault(x => x.Name == t.SeriesName);
                    foreach (var e in t.Entries)
                    {
                        measurement.Tags.Add(e.TagKey);
                    }
                }
            }
            return(dbStructure);
        }
        /// <summary>
        /// Gets the whole DB structure for the given databse in Influx.
        /// </summary>
        /// <param name="dbName">Name of the database</param>
        /// <returns>Hierarchical structure, Database<Measurement<Tags,Fields>></returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>
        public async Task<IInfluxDatabase> GetInfluxDBStructureAsync (string dbName)
        {
            var dbStructure = new InfluxDatabase (dbName);
            var fields = await QueryMultiSeriesAsync (dbName, "SHOW FIELD KEYS");
            foreach (var s in fields)
            {
                var measurement = new InfluxMeasurement (s.SeriesName);
                foreach (var e in s.Entries)
                    measurement.Fields.Add (e.FieldKey);
                dbStructure.Measurements.Add (measurement);
            }

            var tags = await QueryMultiSeriesAsync (dbName, "SHOW TAG KEYS");
            foreach (var t in tags)
            {
                var measurement = dbStructure.Measurements.FirstOrDefault (x => x.Name == t.SeriesName);
                foreach (var e in t.Entries)
                    measurement.Tags.Add (e.TagKey);
            }

            return dbStructure;
        }