Ejemplo n.º 1
0
        public async Task <TraceIpReport> GetTraceReport(string ip)
        {
            // Start fetching data
            using (ITraceIpApi traceIpApi = new MeliTraceIpApi())
            {
                // Ip2Country API
                Ip2Country ipCountry = await traceIpApi.Ip2Country(ip);

                if (ipCountry == null)
                {
                    // We return null to signal a problem with the Ip or Country
                    return(null);
                }

                TraceIpReport report = GetReportFromCache(ipCountry.CountryCode);
                if (report == null)
                {
                    // Get country info  from api
                    CountryInfo countryInfo = await traceIpApi.RestCountries(ipCountry.CountryCode);

                    if (countryInfo == null)
                    {
                        // We return null to signal a problem with the Ip or Country Code
                        return(null);
                    }

                    // Currency converter - Asynchronus calling, while we calculate distance data
                    Task <string> currencyTask = Helper.CurrencyConverter(traceIpApi, countryInfo.Currencies);
                    double        distance     = Helper.CalculateDistance(countryInfo.Latlng); // Distance from Buenos Aires

                    // Wait for task to finish
                    currencyTask.Wait();
                    string currenciesRate = currencyTask.Result;

                    // Build MeliTrace report
                    ITraceIpBuilder builder = new MeliTraceIpBuilder();
                    builder.AppendCountry(countryInfo.Name, countryInfo.NativeName, countryInfo.Alpha2Code).
                    AppendCurrencyRate(currenciesRate).
                    AppendDistance(distance).
                    AppendTimeZone(countryInfo.Timezones).
                    AppendLanguage(countryInfo.Languages);

                    // Print report
                    report = builder.TraceIpReport;
                }

                // We increment the amount of "hits" that we got from the Ip and update the stats
                report.Hits++;

                ////////////////////////////////////////////
                // Save / Update to database (Redis)
                UpdateTraceReport(report);

                // Update stats needed by stats services
                UpdateStats(report);

                return(report);
            }
        }
Ejemplo n.º 2
0
        private void UpdateStats(TraceIpReport report)
        {
            long?distance = _statsRepositorie.GetCountryDistance(report.CountryCode);

            if (distance == null)
            {
                _statsRepositorie.AddCountryByDistance(report.CountryCode, report.Distance);
            }

            _statsRepositorie.UpdateAverageDistance(CalculateAverageDistance());
        }
Ejemplo n.º 3
0
        public ActionResult GetFarestStat()
        {
            TraceIpReport report = _traceIpservice.GetReportByFarestCountry();

            if (report != null)
            {
                return(Ok(report.ToString()));
            }
            else
            {
                return(Ok("Theres no data in the TraceIp Report database. Call /api/traceip?... to generate a new report."));
            }
        }
Ejemplo n.º 4
0
        public async Task TestNearest()
        {
            TraceIpReport nearest = _service.GetReportByNearestCountry();

            Assert.IsNotNull(nearest);
            Assert.AreEqual(nearest.CountryCode, "UY");

            // We add Argentina (distance 0), but still nearest country is UY
            Assert.IsNotNull(await _service.GetTraceReport("24.232.255.255"));

            nearest = _service.GetReportByNearestCountry();
            Assert.IsNotNull(nearest);
            Assert.AreEqual(nearest.CountryCode, "UY");
        }
Ejemplo n.º 5
0
        public async Task InitServiceAsync()
        {
            _redis = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true");
            _redis.GetServer(_redis.GetEndPoints()[0]).FlushAllDatabases();
            _service = new TraceIpService(new TraceReportRepositorie(_redis), new StatsRepositorie(_redis));

            string testData = System.IO.File.ReadAllText(@"test_data.txt");
            var    IPs      = testData.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

            foreach (var ip in IPs)
            {
                TraceIpReport report = await _service.GetTraceReport(ip);

                Assert.IsNotNull(report);
            }
        }
Ejemplo n.º 6
0
        public async Task <ActionResult> TraceIp([FromQuery] string ip)
        {
            try
            {
                if (String.IsNullOrEmpty(ip))
                {
                    return(BadRequest("Missing IP."));
                }

                TraceIpReport report = await _traceIpservice.GetTraceReport(ip);

                if (report == null)
                {
                    return(BadRequest("Invalid or missing IP address."));
                }

                return(Ok(report.ToString()));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Ejemplo n.º 7
0
 public MeliTraceIpBuilder()
 {
     this.traceReport = new TraceIpReport();
 }
Ejemplo n.º 8
0
        private void UpdateTraceReport(TraceIpReport report)
        {
            string jsonReport = JsonConvert.SerializeObject(report);

            _traceReportRepositorie.AddTraceReport(report.CountryCode, jsonReport);
        }
Ejemplo n.º 9
0
        public void TestFarest()
        {
            TraceIpReport farest = _service.GetReportByFarestCountry();

            Assert.AreEqual(farest.CountryCode, "CN");
        }