Example #1
0
        // TODO: There is no sanitization on message data.
        private async Task ProcessAdvertClick(Event evnt)
        {
            Trace.WriteLine("ProcessEvent");

            int result;

            // Database
            // TODO: Refactor to own method.
            // TODO: Automapper.
            // TODO: SQL connection shouldn't be initialized this way.
            using (var sql = new SqlConnection(_conn))
            {
                await sql.OpenAsync();//.ConfigureAwait(false);

                // TODO: Sanitize this input (the default '0' for null isn't good)
                // TODO: Should handle bad campaign or flight ids
                // TODO: Parse methods will throw on error, convert to TryParse.
                // TODO: Events should be isolated into it's own DB
                result = await sql.InsertAsync(new DatabaseEvent
                {
                    Time = evnt.Time.FromUnixTimeMilliseconds(),

                    UserId = Guid.Parse(evnt.UserId),
                    UserAgent = evnt.UserAgent,
                    UserLanguage = evnt.UserLanguage,
                    UserHostAddress = evnt.UserHostAddress == null ? null : IPAddress.Parse(evnt.UserHostAddress).GetAddressBytes(),
                    UserProxyAddress = evnt.UserProxyAddress == null ? null : IPAddress.Parse(evnt.UserProxyAddress).GetAddressBytes(),

                    Referer = evnt.Referer,

                    Flight = string.IsNullOrWhiteSpace(evnt.Flight) ? 0 : int.Parse(evnt.Flight),
                    Destination = string.IsNullOrWhiteSpace(evnt.Destination) ? 0 : int.Parse(evnt.Destination)
                });//.ConfigureAwait(false);

                sql.Close();
            }

            if (result < 1)
            {
                // TODO: Log error and raise hell
                throw new NotImplementedException();
            }

            // SignalR
            // TODO: [Optimization] If there are no clients connected to SignalR, don't push event message.
            // TODO: Refactor to own method (singleton or simpleinjector).
            // TODO: Automapper(?).
            // BUG: Needs to send click cost (should cache flights in memory and pull click cost [for speed])
            Trace.WriteLine("Sending _eventHubContext");
            _eventHubContext.Clients
                            .All
                            .newEvent(new EmberEvent
                            {
                                Id = result,

                                Time = evnt.Time.FromUnixTimeMilliseconds(),

                                UserId = evnt.UserId,
                                UserAgent = evnt.UserAgent,
                                UserLanguage = evnt.UserLanguage,
                                UserHostAddress = evnt.UserHostAddress,
                                UserProxyAddress = evnt.UserProxyAddress,

                                // TODO: Fix this, should pull from the Flight
                                UserClickCost = 0.25M,

                                Referer = evnt.Referer,

                                Flight = evnt.Flight,
                                Destination = evnt.Destination
                            });
            Trace.WriteLine("Sent _eventHubContext");
        }
Example #2
0
 private async Task ProcessEvent(Event evnt)
 {
     switch (evnt.Type)
     {
         case EventType.AdvertClick:
         {
             await ProcessAdvertClick(evnt);//.ConfigureAwait(false);
             break;
         }
         case EventType.LanderClick:
         {
             break;
         }
         case EventType.LanderView:
         {
             break;
         }
     }
 }