public async Task <ApiQueryResult> GetAutomationPerformanceStats(CancellationToken cancel)
        {
            try
            {
                var automations = await _mailchimp.Automations.GetAllAsync(new QueryableBaseRequest()
                {
                });

                if (automations == null || automations.Count() == 0)
                {
                    _logger.LogError("No automations found!");
                }

                if (cancel.IsCancellationRequested)
                {
                    return(ApiQueryResult.Cancelled.With(nameof(GetAutomationPerformanceStats)));
                }

                var points = new List <PointData>();
                foreach (var r in automations.Where(x => x.Status == MailChimp.Net.Models.AutomationStatus.Sending))
                {
                    var builder = PointData.Measurement("email_automation_performance")
                                  .Timestamp(DateTime.UtcNow, WritePrecision.S)
                                  .Tag("service", "mailchimp")
                                  .Tag("list_name", r.Recipients.ListName)
                                  .Tag("campaign_name", r.Settings.Title)
                                  .Field("emails_sent", r.EmailsSent)
                                  .Field("clicks_total", r.ReportSummary.Clicks)
                                  .Field("clicks_rate", r.ReportSummary.ClickRate)
                                  .Field("opens_total", r.ReportSummary.Opens)
                                  .Field("opens_unique", r.ReportSummary.UniqueOpens)
                                  .Field("opens_rate", r.ReportSummary.OpenRate)
                                  .Field("clicks_subscribers_unique", r.ReportSummary.SubscriberClicks);

                    points.Add(builder);
                }

                var writeApi = _client.GetWriteApiAsync();
                await writeApi.WritePointsAsync(points, cancel);

                foreach (var p in points)
                {
                    _logger.LogInformation("wrote data for [{0}]", p.ToLineProtocol());
                }

                return(ApiQueryResult.WriteSuccess.With(nameof(GetAutomationPerformanceStats)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "failed to complete {0}", nameof(GetAutomationPerformanceStats));
                return(ApiQueryResult.WriteFailure.With(nameof(GetAutomationPerformanceStats), message: ex.Message));
            }
        }
        private async void SeedInfluxDb()
        {
            CreateMeasurementsToSeed();

            var writeApi = _influxDbClient.GetWriteApiAsync();

            await writeApi.WriteMeasurementsAsync(_influxDbOptions.Bucket, _influxDbOptions.Organization, WritePrecision.S, Measurements);
        }
Exemple #3
0
 public async Task WriteMeasure(IEnvSensorMeasure measure)
 {
     await _influxDBClient
     .GetWriteApiAsync()
     .WriteMeasurementAsync(
         _options.Bucket,
         _options.Organization,
         WritePrecision.Ms,
         measure);
 }
        public static async Task Example(InfluxDBClient influxDbClient)
        {
            //
            // Write Data
            //
            var writeApiAsync = influxDbClient.GetWriteApiAsync();

            //
            //
            // Write by LineProtocol
            //
            await writeApiAsync.WriteRecordAsync("my-bucket", "my-org", WritePrecision.Ns,
                                                 "temperature,location=north value=60.0");

            //
            //
            // Write by Data Point
            //
            var point = PointData.Measurement("temperature")
                        .Tag("location", "west")
                        .Field("value", 55D)
                        .Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);

            await writeApiAsync.WritePointAsync("my-bucket", "my-org", point);

            //
            // Write by POCO
            //
            var temperature = new Temperature {
                Location = "south", Value = 62D, Time = DateTime.UtcNow
            };

            await writeApiAsync.WriteMeasurementAsync("my-bucket", "my-org", WritePrecision.Ns, temperature);

            //
            // Check written data
            //
            var tables = await influxDbClient.GetQueryApi()
                         .QueryAsync("from(bucket:\"my-bucket\") |> range(start: 0)", "my-org");

            tables.ForEach(table =>
            {
                var fluxRecords = table.Records;
                fluxRecords.ForEach(record =>
                {
                    Console.WriteLine($"{record.GetTime()}: {record.GetValue()}");
                });
            });

            influxDbClient.Dispose();
        }
        public async Task GroupPointsByPrecisionSame()
        {
            MockServer
            .Given(Request.Create().WithPath("/api/v2/write").UsingPost())
            .RespondWith(CreateResponse("{}"));

            var writeApi = _influxDbClient.GetWriteApiAsync();
            await writeApi.WritePointsAsync("my-bucket", "my-org", new List <PointData>
            {
                PointData.Measurement("h2o").Tag("location", "coyote_creek").Field("water_level", 9.0D)
                .Timestamp(9L, WritePrecision.S),
                PointData.Measurement("h2o").Tag("location", "coyote_creek").Field("water_level", 10.0D)
                .Timestamp(10L, WritePrecision.S)
            });

            Assert.AreEqual(1, MockServer.LogEntries.Count());

            var request = MockServer.LogEntries.Last();

            Assert.AreEqual(MockServerUrl + "/api/v2/write?org=my-org&bucket=my-bucket&precision=s",
                            request.RequestMessage.AbsoluteUrl);
            Assert.AreEqual("h2o,location=coyote_creek water_level=9 9\nh2o,location=coyote_creek water_level=10 10",
                            request.RequestMessage.Body);
        }
        /// <inheritdoc />
        /// <summary>
        /// Emit a batch of log events, running asynchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        public Task EmitBatchAsync(IEnumerable <Events.LogEvent> batch)
        {
            if (batch == null)
            {
                throw new ArgumentNullException(nameof(batch));
            }

            var logEvents = batch as List <Events.LogEvent> ?? batch.ToList();
            var points    = new List <PointData>(logEvents.Count);

            foreach (var logEvent in logEvents)
            {
                var severity = logEvent.Level.ToSeverity();

                var p = PointData.Measurement(PointName)
                        .Tag(Tags.Level, logEvent.Level.ToString())
                        .Tag(Tags.AppName, _applicationName)
                        .Tag(Tags.Facility, _instanceName)
                        .Tag(Tags.Hostname, Environment.MachineName)
                        .Tag(Tags.Severity, severity.ToString())
                        .Field(Fields.Message, StripSpecialCharacter(logEvent.RenderMessage(_formatProvider)))
                        .Field(Fields.Facility, Fields.Values.Facility)
                        .Field(Fields.ProcId, Process.GetCurrentProcess().Id)
                        .Field(Fields.Severity, severity.ToString())
                        .Field(Fields.Timestamp, logEvent.Timestamp.ToUnixTimeMilliseconds() * 1000000)
                        .Field(Fields.Version, Fields.Values.Version)
                        .Timestamp(logEvent.Timestamp.UtcDateTime, WritePrecision.Ms);

                if (logEvent.Exception != null)
                {
                    p = p.Tag(Tags.ExceptionType, logEvent.Exception.GetType().Name);
                }

                if (_includeFullException && logEvent.Exception != null)
                {
                    p = p.Field(Fields.Exception, logEvent.Exception.ToString());
                }

                points.Add(p);
            }

            //Not handling exceptions and let handle by wrapping class PeriodicBatchingSink
            // -> no async and let base class do the work for waiting
            return(_influxDbClient
                   .GetWriteApiAsync()
                   .WritePointsAsync(_connectionInfo.BucketName, _connectionInfo.OrganizationId, points));
        }
Exemple #7
0
        public async Task <ApiQueryResult> FetchAccountMetrics(CancellationToken cancel)
        {
            try
            {
                foreach (var publisher in _config.AccountNames)
                {
                    // QUERY ALL PACKAGES THAT BELONG TO ACCOUNT

                    var packages   = new List <MyFakePackageClass>(); // this gets produced via some API call
                    var dataPoints = new List <PointData>();

                    // do a fast iteration on the top-level package data
                    foreach (var package in packages)
                    {
                        // record stats
                        var builder = PointData.Measurement("nuget_package_stats")
                                      .Timestamp(DateTime.UtcNow, WritePrecision.S)
                                      .Tag("package", package.PackageName)
                                      .Tag("author", publisher)
                                      .Tag("service", "nuget")
                                      .Field("total_downloads", package.TotalDownloads)
                                      .Field("downloads_per_day", package.DownloadsPerDay);

                        dataPoints.Add(builder);

                        // record stats for every package version pushed by publisher
                        // (you can set a limit on it if you really want to)
                    }

                    await _client.GetWriteApiAsync().WritePointsAsync(dataPoints, cancel);

                    _logger.LogInformation("Successfully wrote [{0}] data points for [{1}]", dataPoints.Count,
                                           publisher);
                }

                return(ApiQueryResult.WriteSuccess.With(nameof(FetchAccountMetrics)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "failed to complete {0}", nameof(FetchAccountMetrics));
                return(ApiQueryResult.WriteFailure.With(nameof(FetchAccountMetrics), message: ex.Message));
            }
        }
        public new async Task SetUp()
        {
            _client = InfluxDBClientFactory.Create(GetInfluxDb2Url(), "my-token");
            _client.SetLogLevel(LogLevel.Body);

            // DateTime(2020, 10, 15, 8, 20, 15, DateTimeKind.Utc)
            const string sensor11 = "sensor,deployment=production,sensor_id=id-1 data=15i 1602750015";
            const string sensor21 = "sensor,deployment=production,sensor_id=id-2 data=15i 1602750015";
            // new DateTime(2020, 11, 15, 8, 20, 15, DateTimeKind.Utc)
            const string sensor12 = "sensor,deployment=production,sensor_id=id-1 data=28i 1605428415";
            const string sensor22 = "sensor,deployment=production,sensor_id=id-2 data=28i 1605428415";
            // new DateTime(2020, 11, 16, 8, 20, 15, DateTimeKind.Utc)
            const string sensor13 = "sensor,deployment=production,sensor_id=id-1 data=12i 1605514815";
            const string sensor23 = "sensor,deployment=production,sensor_id=id-2 data=12i 1605514815";
            // new DateTime(2020, 11, 17, 8, 20, 15, DateTimeKind.Utc)
            const string sensor14 = "sensor,deployment=production,sensor_id=id-1 data=89i 1605601215";
            const string sensor24 = "sensor,deployment=production,sensor_id=id-2 data=89i 1605601215";

            await _client
            .GetWriteApiAsync()
            .WriteRecordsAsync("my-bucket", "my-org", WritePrecision.S,
                               sensor11, sensor21, sensor12, sensor22, sensor13, sensor23, sensor14, sensor24);
        }
Exemple #9
0
 public WriteApiAsync GetWriteApiAsync()
 {
     return(_client.GetWriteApiAsync());
 }