Skip to content

Commit

Permalink
SAFE-Stack#9 Price range facet
Browse files Browse the repository at this point in the history
  • Loading branch information
theprash committed Feb 7, 2018
1 parent 2f53c10 commit 15471c5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
4 changes: 2 additions & 2 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ Target "ImportData" (fun _ ->
let mode = getBuildParam "DataMode"

log "Downloading transaction data..."
let txns = fetchTransactions 1000

// Insert postcode / geo lookup
log "Downloading geolocation data..."
if (not (fileExists (FullName "ukpostcodes.csv"))) then
let txns = fetchTransactions 1000
log "Downloading geolocation data..."
let archivePath = "ukpostcodes.zip"
do
use wc = new Net.WebClient()
Expand Down
1 change: 1 addition & 0 deletions src/client/pages/Filter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let createFilters dispatch facets =
toFilterCard dispatch "District" facets.Districts
toFilterCard dispatch "Town" facets.Towns
toFilterCard dispatch "Locality" facets.Localities
toFilterCard dispatch "Price range" facets.PriceRanges
]
]
]
3 changes: 2 additions & 1 deletion src/scripts/importdata.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ let fetchTransactions rows =
Build = t.Duration |> BuildType.Parse
Contract = t.``Old/New`` |> ContractType.Parse }
Price = t.Price
DateOfTransfer = t.Date })
DateOfTransfer = t.Date
PriceRange = PriceRange.ofPrice t.Price })
|> Seq.toArray

module FableJson =
Expand Down
33 changes: 25 additions & 8 deletions src/server/Contracts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ type PropertyFilter =
Locality : string option
District : string option
County : string option
MaxPrice : int option
MinPrice : int option }
``Price range`` : string option }
type PropertyType =
| Detached | SemiDetached | Terraced | FlatsMaisonettes | Other
member this.Description =
match this with
| PropertyType.SemiDetached -> "Semi Detatch"
| PropertyType.FlatsMaisonettes -> "Flats / Maisonettes"
| SemiDetached -> "Semi Detatch"
| FlatsMaisonettes -> "Flats / Maisonettes"
| _ -> string this
static member Parse = function "D" -> Some Detached | "S" -> Some SemiDetached | "T" -> Some Terraced | "F" -> Some FlatsMaisonettes | "O" -> Some Other | _ -> None
type BuildType =
| NewBuild | OldBuild
member this.Description =
match this with
| BuildType.OldBuild -> "Old Build"
| BuildType.NewBuild -> "New Build"
| OldBuild -> "Old Build"
| NewBuild -> "New Build"
static member Parse = function "Y" -> NewBuild | _ -> OldBuild

type ContractType =
Expand All @@ -47,18 +46,36 @@ type BuildDetails =
{ PropertyType : PropertyType option
Build : BuildType
Contract : ContractType }
type PriceRange =
| ``P < 200``
| ``P 200 - 300``
| ``P 300 - 400``
| ``P > 400``
member this.Description =
match this with
| ``P < 200`` -> "Less than £200k"
| ``P 200 - 300`` -> "£200k - £300k"
| ``P 300 - 400`` -> "£300k - £400k"
| ``P > 400`` -> "More than £400k"
static member ofPrice price =
if price < 200_000 then ``P < 200``
elif price < 300_000 then ``P 200 - 300``
elif price < 400_000 then ``P 300 - 400``
else ``P > 400``

type PropertyResult =
{ TransactionId : Guid
BuildDetails : BuildDetails
Address : Address
Price : int
DateOfTransfer : DateTime }
DateOfTransfer : DateTime
PriceRange : PriceRange }
type Facets =
{ Towns : string list
Localities : string list
Districts : string list
Counties : string list
Prices : string list }
PriceRanges : string list }
type SearchResponse =
{ Results : PropertyResult array
TotalTransactions : int option
Expand Down
2 changes: 1 addition & 1 deletion src/server/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let appConfig =
let builder =
ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional = true)
.AddJsonFile("appSettings.json", optional = true)
.AddEnvironmentVariables()
.Build()

Expand Down
2 changes: 1 addition & 1 deletion src/server/Routing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let genericSearch (searcher:Search.ISearch) (text, page) next (ctx:HttpContext)
task {
let! properties = searcher.GenericSearch request
return! FableJson.serialize properties next ctx }

let webApp searcher : HttpHandler =
choose [
GET >=>
Expand All @@ -31,4 +32,3 @@ let webApp searcher : HttpHandler =
routef "/property/%s/%i" (fun (postcode, distance) -> searchProperties searcher (postcode, distance, 0))
]
setStatusCode 404 >=> text "Not Found" ]

5 changes: 3 additions & 2 deletions src/server/Search.Azure.fs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ let private toFindPropertiesResponse findFacet count page results =
County = result.County
PostCode = result.PostCode |> Option.ofObj }
Price = result.Price
DateOfTransfer = result.DateOfTransfer })
DateOfTransfer = result.DateOfTransfer
PriceRange = PriceRange.ofPrice result.Price })
TotalTransactions = count
Facets =
{ Towns = findFacet "TownCity"
Localities = findFacet "Locality"
Districts = findFacet "District"
Counties = findFacet "County"
Prices = findFacet "Price" }
PriceRanges = findFacet "PriceRange" }
Page = page }

let applyFilters (filter:PropertyFilter) parameters =
Expand Down
10 changes: 6 additions & 4 deletions src/server/Search.InMemory.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module PropertyMapper.Search.InMemory

open PropertyMapper.Contracts
open Giraffe.Tasks
open System

let private data = lazy ("properties.json" |> System.IO.File.ReadAllText |> FableJson.ofJson)

Expand All @@ -15,7 +16,7 @@ let findByPostcode (request:FindNearestRequest) = task {
Localities = []
Districts = []
Counties = []
Prices = [] } } }
PriceRanges = [] } } }

let findGeneric (request:FindGenericRequest) = task {
let genericFilter =
Expand All @@ -31,7 +32,7 @@ let findGeneric (request:FindGenericRequest) = task {
| None -> fun _ -> true
let facetFilter filter mapper =
match filter with
| Some filter -> mapper >> fun (s:string) -> s.ToUpper() = filter
| Some filter -> mapper >> fun s -> String.Equals(s, filter, StringComparison.OrdinalIgnoreCase)
| None -> fun _ -> true

let matches =
Expand All @@ -41,7 +42,8 @@ let findGeneric (request:FindGenericRequest) = task {
|> Array.filter (facetFilter request.Filter.District (fun r -> r.Address.District))
|> Array.filter (facetFilter request.Filter.Locality (fun r -> r.Address.Locality |> Option.defaultValue ""))
|> Array.filter (facetFilter request.Filter.Town (fun r -> r.Address.TownCity))

|> Array.filter (facetFilter request.Filter.``Price range`` (fun r -> r.PriceRange.Description))

let getFacets mapper = Array.choose mapper >> Array.distinct >> Array.truncate 10 >> Array.toList
return
{ Results = matches |> Array.skip (request.Page * 20) |> Array.truncate 20
Expand All @@ -52,5 +54,5 @@ let findGeneric (request:FindGenericRequest) = task {
Localities = matches |> getFacets (fun m -> m.Address.Locality)
Districts = matches |> getFacets (fun m -> Some m.Address.District)
Counties = matches |> getFacets (fun m -> Some m.Address.County)
Prices = [] } }
PriceRanges = matches |> getFacets (fun m -> Some m.PriceRange.Description) } }
}

0 comments on commit 15471c5

Please sign in to comment.