コード例 #1
0
        /// <summary>
        /// Searches for matching telemetry event data in the data store.
        /// </summary>
        /// <param name="filter">Optional filter options to apply to the telemetry event data search.</param>
        public async Task <IEnumerable <TelemetryEvent <TContext> > > SearchDataAsync(FilterOptions filter = null)
        {
            List <TelemetryEvent <TContext> > events = new List <TelemetryEvent <TContext> >();

            try
            {
                using (MySqlConnection connection = new MySqlConnection(this.sqlConnectionString))
                {
                    using (MySqlCommand command = this.CreateTelemetrySearchCommand(connection, filter))
                    {
                        await connection.OpenAsync().ConfigureAwait(false);

                        using (IDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
                        {
                            if (reader != null)
                            {
                                while (reader.Read())
                                {
                                    events.Add(new TelemetryEvent <TContext>(
                                                   eventName: reader.GetString(1),
                                                   correlationId: reader.GetGuid(2),
                                                   timestamp: reader.GetDateTime(0),
                                                   context: JsonConvert.DeserializeObject <TContext>(reader.GetString(3), TelemetryEvent <TContext> .SerializationSettings)));
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                throw;
            }

            return(events as IEnumerable <TelemetryEvent <TContext> >);
        }
コード例 #2
0
        protected MySqlCommand CreateTelemetrySearchCommand(MySqlConnection connection, FilterOptions filter)
        {
            MySqlCommand command = connection.CreateCommand();

            command.CommandType = System.Data.CommandType.Text;
            command.CommandText =
                @"SELECT
                Timestamp,
                EventName,
                CorrelationId,
                Context
                FROM TelemetryEvents 
                WHERE 1";

            if (filter != null && filter.IsSet)
            {
                if (filter.CorrelationId != null)
                {
                    command.CommandText += $" AND CorrelationId = '{filter.CorrelationId}'";
                }

                if (filter.EventName != null)
                {
                    command.CommandText += $" AND EventName = '{filter.EventName}'";
                }

                if (filter.Latest == true)
                {
                    if (filter.EventName != null)
                    {
                        command.CommandText +=
                            $@" AND CorrelationId =
                            (
                                SELECT CorrelationId FROM TelemetryEvents
                                WHERE EventName = '{filter.EventName}'
                                ORDER BY Id DESC
                                LIMIT 1
                            ) ORDER BY TIMESTAMP DESC";
                    }
                    else
                    {
                        command.CommandText +=
                            $@" AND CorrelationId =
                            (
                                SELECT CorrelationId FROM TelemetryEvents
                                ORDER BY Id DESC
                                LIMIT 1
                            ) ORDER BY TIMESTAMP DESC";
                    }
                }
            }
            else
            {
                command.CommandText += " ORDER BY TIMESTAMP DESC LIMIT 300";
            }

            return(command);
        }