-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMapController.cs
More file actions
116 lines (104 loc) · 3.92 KB
/
MapController.cs
File metadata and controls
116 lines (104 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using DotNetNuke.Instrumentation;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Web.Api;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MaxMind.GeoIP2;
namespace Dnn.WebAnalytics
{
public class MapController : DnnApiController
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(MapController));
private VisitInfoRepo visitRepo = new VisitInfoRepo();
[HttpGet]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[AllowAnonymous]
public HttpResponseMessage Get(int minutes)
{
try
{
List<MapDTO> dtos = new List<MapDTO>();
List<VisitInfo> recent_visits = visitRepo.GetItemsAll()
.Where(i =>
i.date >= DateTime.Now.AddMinutes(-minutes) &&
i.latitude != string.Empty && i.longitude != string.Empty
)
.ToList();
var recent_unique_visits = recent_visits
.GroupBy(i => i.ip)
.Select(i => new
{
id = i.Max(o => o.id)
})
.ToList();
var ids = recent_unique_visits.Select(i => i.id).ToList();
foreach (long id in ids)
{
var visit = visitRepo.GetItemById((int)id); // this may need to be debugged and/or handled better later
if (visit != null)
{
MapDTO mapDTO = new MapDTO()
{
user_id = visit.Visitor.user_id,
latitude = visit.latitude,
longitude = visit.longitude
};
dtos.Add(mapDTO);
}
}
return Request.CreateResponse(HttpStatusCode.OK, dtos);
}
catch (Exception ex)
{
Logger.Error(ex.Message, ex);
Exceptions.LogException(ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
}
}
[HttpGet]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[AllowAnonymous]
public HttpResponseMessage Get()
{
try
{
MapDTO dto = new MapDTO()
{
latitude = "37.09024",
longitude = "-95.712891"
};
string ip_address = this.Request.GetIPAddress();
//ip_address = "104.185.202.20"; // for testing on localhost, should resolve to Atlanta, GA...
using (var objGeoIP2DB = new DatabaseReader(string.Concat(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\GeoIP2-City.mmdb")))
{
try
{
var objGeoIP2 = objGeoIP2DB.City(ip_address);
if (objGeoIP2 != null)
{
dto.latitude = objGeoIP2.Location.Latitude.ToString();
dto.longitude = objGeoIP2.Location.Longitude.ToString();
}
}
catch
{
// IP address cannot be resolved
}
}
return Request.CreateResponse(HttpStatusCode.OK, dto);
}
catch (Exception ex)
{
Logger.Error(ex.Message, ex);
Exceptions.LogException(ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
}
}
}
}