Ejemplo n.º 1
0
        public static LoginStatistics GetStatistics(this OperationsJsonLogger <LoginAttempt> logger, DateTime date)
        {
            var attempts = logger.GetLogRecords(date);

            var totalLoginsCount = attempts.GroupBy(x => x.Login).Count();

            var invalidAttempts = attempts
                                  .Where(x => x.LoginStatus != AirBnbLoginStatus.Sucess)
                                  .GroupBy(x => x.Login)
                                  .ToList();

            var invalidProxies = attempts
                                 .Where(x => x.LoginStatus == AirBnbLoginStatus.ProxyError && !string.IsNullOrEmpty(x.Proxy))
                                 .Select(x => x.Proxy)
                                 .Distinct()
                                 .ToList();

            return(new LoginStatistics()
            {
                ErrorsCount = invalidAttempts.Count(),
                ErrorLogins = invalidAttempts.Select(x => new
                {
                    x.Key,
                    Errors = string.Join(", ", x.GroupBy(l => l.LoginStatus)
                                         .Select(s => string.Format("{0} ({1})", s.Key.ToDescription(), s.Count())).ToList())
                })
                              .ToDictionary(x => x.Key, x => x.Errors),
                LoginCount = totalLoginsCount,
                InvalidProxies = invalidProxies
            });
        }
        /// <summary>
        /// Log which all properties will be updated and by whom
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="pricePushLogger"></param>
        public static void LogPropertiesForAirBnb(List <Property> properties, OperationsJsonLogger <PricePushResult> pricePushLogger)
        {
            List <PricePushResult> pushResults = new List <PricePushResult>();

            foreach (Property property in properties)
            {
                pushResults.Add(new PricePushResult(
                                    Channel.AirBnb,
                                    PricePushLogArea.ParsingCSV,
                                    PricingPushLogType.Information,
                                    string.Empty,
                                    property.LoginAccount.Email,
                                    property.LoginAccount.ProxyAddress[0],
                                    property.AirbnbTitle,
                                    property.AirbnbId,
                                    string.Empty,
                                    string.Empty,
                                    string.Empty,
                                    string.Empty
                                    ));
            }
            pricePushLogger.Log(pushResults);
        }
        /// <summary>
        /// Log which all properties will be updated and by whom
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="pricePushLogger"></param>
        public static void LogPropertiesForStreamLine(List <Property> properties, OperationsJsonLogger <PricePushResult> pricePushLogger)
        {
            List <PricePushResult> pushResults = new List <PricePushResult>();

            foreach (Property property in properties)
            {
                pushResults.Add(new PricePushResult(
                                    Channel.StreamLine,
                                    PricePushLogArea.ParsingCSV,
                                    PricingPushLogType.Information,
                                    string.Empty,
                                    Config.I.StreamLineAccountLogin,
                                    string.Empty,
                                    property.StreamLineHomeName,
                                    property.StreamLineHomeId,
                                    string.Empty,
                                    string.Empty,
                                    string.Empty,
                                    string.Empty
                                    ));
            }
            pricePushLogger.Log(pushResults);
        }
        public static List <Property> ReadPricingData(OperationsJsonLogger <PricePushResult> logger)
        {
            List <Property> properties       = null;
            IFormatProvider culture          = new System.Globalization.CultureInfo("en-US", true);
            int             propertCodeIndex = 0;
            int             seasonStartIndex = 0;
            int             seasonEndIndex   = 0;
            int             minLosStartIndex = 0;
            int             dateStampIndex   = 0;
            int             priceRowIndex    = 7;

            try
            {
                //var lines = File.ReadAllLines(Config.I.AirBnbPricingPushFile);
                var lines = WriteSafeReadAllLines(Config.I.PricingPushFile);
                if (lines.Length > 0)
                {
                    properties = new List <Property>();
                }

                for (int index = 0; index < lines.Length; index++)
                {
                    // Getting the list of properties.
                    if (index == propertCodeIndex)
                    {
                        string[] propertyArray      = lines[index].Split(',');
                        int      propertyCellIndex  = Array.IndexOf(propertyArray, PricingPushCsv.PropertyCode);
                        int      propertyCodesIndex = propertyCellIndex + 1;
                        while (propertyCodesIndex < propertyArray.Length)
                        {
                            if (!string.IsNullOrEmpty(propertyArray[propertyCodesIndex]))
                            {
                                Property property = new Property(propertyArray[propertyCodesIndex], propertyCodesIndex);
                                properties.Add(property);
                            }
                            propertyCodesIndex++;
                        }
                    }

                    // Better than checking whether the index is greater than 1 would be to check if its greater than or equal to 0
                    // The whole purpose is to check whether the line exists or not.
                    // Getting the stream line home names.
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.StreamLineHomeName) >= 0)
                    {
                        string[] streamLineHomeNames = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            property.StreamLineHomeName = streamLineHomeNames[property.PropertyIndex].Trim();
                        }
                    }

                    // Parsing and reading the stream line home id's
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.StreamLineHomeId) >= 0)
                    {
                        string[] streamLineHomeIds = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            property.StreamLineHomeId = streamLineHomeIds[property.PropertyIndex].Trim();
                        }
                    }

                    //Getting the airbnb accounts.
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.AirbnbAccount) >= 0)
                    {
                        string[] airBnbAccountNames = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            //property.AirbnbAccount = airBnbAccountNames[property.PropertyIndex].Trim();
                            property.LoginAccount.Email = airBnbAccountNames[property.PropertyIndex].Trim();
                        }
                    }

                    //Getting the airbnb passswords.
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.AirbnbPassword) >= 0)
                    {
                        string[] airBnbAccountPasswords = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            property.LoginAccount.Password = airBnbAccountPasswords[property.PropertyIndex].Trim();
                        }
                    }

                    //Getting the ProxyIP for the accounts.
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.ProxyIp) >= 0)
                    {
                        string[] proxyIps = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            property.LoginAccount.ProxyAddress = new List <string> {
                                proxyIps[property.PropertyIndex].Trim()
                            };
                            //property.ProxyIP = proxyIps[property.PropertyIndex].Trim();
                        }
                    }

                    //Getting the Airbnb title for the properties.
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.AirbnbTitle) >= 0)
                    {
                        string[] airBnbTitles = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            property.AirbnbTitle = airBnbTitles[property.PropertyIndex].Trim();
                        }
                    }

                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.AirbnbListingId) >= 0)
                    {
                        string[] airBnbListingIds = lines[index].Split(',');
                        foreach (Property property in properties)
                        {
                            property.AirbnbId = airBnbListingIds[property.PropertyIndex].Trim();
                        }
                    }


                    //Getting the price index for the properties.
                    if (Array.IndexOf(lines[index].Split(','), PricingPushCsv.DateStamp) >= 0)
                    {
                        string[] propertyPriceHeadings = lines[index].Split(',');
                        seasonStartIndex = Array.IndexOf(propertyPriceHeadings, PricingPushCsv.SeasonStartDate);
                        seasonEndIndex   = Array.IndexOf(propertyPriceHeadings, PricingPushCsv.SeasonEndDate);
                        dateStampIndex   = Array.IndexOf(propertyPriceHeadings, PricingPushCsv.DateStamp);
                        minLosStartIndex = Array.IndexOf(propertyPriceHeadings, PricingPushCsv.MinimumLOS);
                        priceRowIndex    = index + 1;
                    }

                    // Getting and populating the object with prices.
                    if (index >= priceRowIndex)
                    {
                        string[] prices = lines[index].Split(',');
                        if (string.IsNullOrEmpty(prices[0]))
                        {
                            continue;
                        }

                        try
                        {
                            string dayPrice = string.Empty;
                            foreach (Property property in properties)
                            {
                                Price propertyPrice = new Price();
                                propertyPrice.SeasonStartDate = DateTime.Parse(prices[seasonStartIndex].Trim(), CultureInfo.InvariantCulture);
                                propertyPrice.SeasonEndDate   = DateTime.Parse(prices[seasonEndIndex].Trim(), CultureInfo.InvariantCulture);
                                dayPrice = prices[property.PropertyIndex].Trim(new char[] { '$' });
                                if (!string.IsNullOrEmpty(dayPrice))
                                {
                                    propertyPrice.PropertyPrice = Convert.ToDouble(dayPrice);
                                    //Convert.ToDouble(prices[property.PropertyIndex].Trim(new char[] { '$' }));
                                }
                                propertyPrice.MinimumLos = Convert.ToInt32(prices[minLosStartIndex].Trim());
                                propertyPrice.DateStamp  = DateTime.Parse(prices[dateStampIndex].Trim(), CultureInfo.InvariantCulture);
                                property.Prices.Add(propertyPrice);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Log(new PricePushResult(Channel.Common, PricePushLogArea.ParsingCSV, PricingPushLogType.Error, ex.Message + ", Season Start Date :" + prices[seasonStartIndex].Trim() + ", Season End Date :" + prices[seasonEndIndex].Trim()));
                        }
                    }
                }

                return(properties);
            }
            catch (FileNotFoundException ex)
            {
                logger.Log(new PricePushResult(Channel.Common, PricePushLogArea.CSVFileNotFound, PricingPushLogType.Error, ex.Message));
            }
            catch (Exception ex)
            {
                logger.Log(new PricePushResult(Channel.Common, PricePushLogArea.ParsingCSV, PricingPushLogType.Error, ex.Message));
            }
            return(properties);
        }