Anonymous avatar Anonymous committed 4c26fc6

Dynamically load heat maps.

Comments (1)

Files changed (8)

HamOntFire.Core/Domain/EventIndex.cs

         }
     }
 
+    public class Events_DistinctTypes : AbstractIndexCreationTask<Event, Events_DistinctTypes.ReduceResult>
+    {
+        public class ReduceResult
+        {
+            public string Type { get; set; }
+        }
+
+        public Events_DistinctTypes()
+        {
+            Map = events => from doc in events
+                            select new { Type = doc.Type };
+            Reduce = results => from eventCount in results
+                                group eventCount by eventCount.Type
+                                into g
+                                select new { Type = g.Key };
+            Sort(result => result.Type, SortOptions.String);
+        }
+    }
+
     public class Events_ByTweetIdSortByTweetId : AbstractIndexCreationTask<Event>
     {
         public Events_ByTweetIdSortByTweetId()

HamOntFire.Core/MapGenerator.cs

             _session = session;
         }
 
-        public Bitmap Generate()
+        public Bitmap Generate(string type, int ward)
         {
             var td = new TweetManager(_session);
-            var events = td.GetEvents();
+            var events = td.GetEvents(type, ward);
 
             // Create new memory bitmap
             //var bMap = new Bitmap(HeatMapWidth, HeatMapHeight);

HamOntFire.Core/TweetManager.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Linq.Expressions;
 using System.Net;
 using System.Text;
 using HamOntFire.Core.Domain;
             return @events;
         }
 
-        public List<Event> GetEvents()
+        public List<Event> GetEvents(string type, int ward)
         {
-            var @events = _session.Query<Event>().Take(1000).ToList();
-            return @events;
+            // Build WHERE
+            var where = BuildWhere(type);
+
+            IEnumerable<Event> @events = _session.Query<Event>().Take(1000);
+            if (where != null)
+                @events = @events.Where( where );
+            return @events.ToList();
+        }
+
+        private static Func<Event, bool> BuildWhere(string type)
+        {
+            if (type == null)
+                return null;
+            return e => e.Type == type;
         }
 
         public void SaveChanges()

HamOntFire.Web/Controllers/StatisticsController.cs

             // Average # of Units per Event Type
             vm.AverageUnitsPerEventType = RavenSession.Query<Events_UnitsPerType.ReduceResult, Events_UnitsPerType>()
                 .OrderByDescending(x => x.UnitsPerType).ToList();
+
+            var types = RavenSession.Query<Events_DistinctTypes.ReduceResult, Events_DistinctTypes>()
+                .OrderBy(x => x.Type).Take(100);
+            foreach (var type in types)
+            {
+                vm.EventTypes.Add( new SelectListItem() { Text = type.Type });
+            }
             
             return View( vm );
         }
 
         [OutputCache(NoStore = true, Location = OutputCacheLocation.None)]
-        public FileStreamResult HeatMap()
+        public FileStreamResult HeatMap(string type=null)
         {
             var mapGen = new MapGenerator(RavenSession);
-            var bmap = mapGen.Generate();
+            var bmap = mapGen.Generate(type, 0);
             var ms = new MemoryStream();
             bmap.Save(ms, ImageFormat.Jpeg);
             ms.Position = 0;
 
     public class StatisticsViewModel
     {
+        public StatisticsViewModel()
+        {
+            EventTypes = new List<SelectListItem>();
+        }
+
         public List<Events_Count.ReduceResult> EventCountByType { get; set; }
         public List<Events_UnitsPerType.ReduceResult> AverageUnitsPerEventType { get; set; }
+        public List<SelectListItem> EventTypes { get; set; } 
     }
 
     public static class JsonExtension

HamOntFire.Web/Global.asax.cs

         {
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
+            routes.MapRoute("HeatMap",
+                            "Statistics/HeatMap/{type}",
+                            new {controller = "Statistics", action = "HeatMap", type = (string)null});
+
             routes.MapRoute(
                 "Default", // Route name
                 "{controller}/{action}/{id}", // URL with parameters

HamOntFire.Web/HamOntFire.Web.csproj

     </Content>
     <Content Include="Web.Debug.config">
       <DependentUpon>Web.config</DependentUpon>
+      <SubType>Designer</SubType>
     </Content>
     <Content Include="Web.Release.config">
       <DependentUpon>Web.config</DependentUpon>

HamOntFire.Web/Scripts/Map.js

 var unseenCount = 0;
 
 function initialize() {
+    // For statistics page only:
+    $('.ddl').change(function () {
+        $('#heatMap').attr('src', '/Statistics/HeatMap/' + $(this).val());
+    });
+    
+    // For the google maps stuff
     var hamilton = new google.maps.LatLng(43.261837, -79.93721);
     var options = {
         zoom: 12,
 
     map = new google.maps.Map(document.getElementById('map_canvas'), options);
     google.maps.event.addListenerOnce(map, 'idle', loadData);
+
 }
 
 function loadData() {

HamOntFire.Web/Views/Statistics/Index.cshtml

 </div>
 
 <h3>Heat Map</h3>
-<img src="@Url.Action("HeatMap")" alt="some image" />
+
+Filter By Type: @Html.DropDownList("ddlEventTypes", Model.EventTypes, new {@class="ddl"})
+<br/>
+<img src="@Url.Action("HeatMap")" id="heatMap" alt="Heat Map" />
 
 <script type="text/javascript">
 var model = @Html.Raw( Model.EventCountByType.ToJson() );
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.