Exemple #1
0
        private List <Client> SortClients()
        {
            string filePath = _configSettings.JsonFilePath;
            string json     = _fileTools.ReadFile(filePath);

            if (String.IsNullOrEmpty(json))
            {
                throw new Exception("Could not read the file specified in " + filePath);
            }
            else
            {
                List <Client> clients = _jsonTools.DeserializeObjectList <Client>(json);
                if (clients.Count > 0)
                {
                    /*Priorities
                     * 1)Number of Connections
                     * (Divided by a configurable(appsettings.json file) number, so other priorities can apply)
                     * Example if divided by 50: 32 and 54 would have the same priority, but 81 would have a higher priority
                     *
                     * 2)Number of Recommendations
                     * 3)Client being in the United States
                     * 4)Client being in Latin America
                     * 5)Number of Connections (Last Tie Breaker)
                     */

                    try
                    {
                        return(clients.OrderByDescending(item => Math.Round(((
                                                                                 item.NumberOfConnections.HasValue ? (double)item.NumberOfConnections : 0
                                                                                 ) / _configSettings.StepNumberOfConnections), MidpointRounding.ToEven))
                               .ThenByDescending(item => item.NumberOfRecommendations)
                               .ThenBy(item => item.Country.Equals(_configSettings.PreferredCountry) ? 0 : 1)
                               .ThenBy(item => _configSettings.LatamCountries.Contains(item.Country) ? 0 : 1)
                               .ThenByDescending(item => item.NumberOfConnections)
                               .ToList());
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e.Message);
                        throw new Exception("There was an error sorting possible clients");
                    }
                }
                else
                {
                    throw new Exception("Could not parse any clients on the specified file: " + filePath);
                }
            }
        }