11import bson
22import graphene
33import os
4+ import pygeohash
45import pymongo
56from abc import ABC , abstractmethod
67from dataclasses import dataclass
78
8- from . import SearchBoundingBox , PropertyFilter , PropertiesPage , Statistics
9+ from . import SearchBoundingBox , PropertyFilter , PropertiesPage , Statistics , LocationBoundingBox , Point
910from ..mapper import PropertyMapper , PropertiesPageMapper , LocalStatisticsMapper , PriceStatisticsMapper , \
1011 GlobalStatisticsMapper , StatisticsMapper
1112from ..mongodb import MongoDBConnection , MONGODB_CONNECTION
@@ -189,19 +190,6 @@ def find_statistics_by_filter(self, filter: PropertyFilter) -> Statistics:
189190 }
190191 ])
191192
192- local_statistics = []
193- for result in local_results :
194- price_statistics = PriceStatisticsMapper .map (
195- min_price = result .get ('price' ).get ('min' ),
196- max_price = result .get ('price' ).get ('max' ),
197- avg_price = result .get ('price' ).get ('avg' )
198- )
199- geohash = result .get ('geohash' )
200- local_statistics .append (LocalStatisticsMapper .map (
201- price_statistics = price_statistics ,
202- geohash = geohash
203- ))
204-
205193 global_results = list (global_results )
206194
207195 min_price = None
@@ -220,10 +208,62 @@ def find_statistics_by_filter(self, filter: PropertyFilter) -> Statistics:
220208 )
221209 global_statistics = GlobalStatisticsMapper .map (price_statistics = global_price_statistics )
222210
211+ local_statistics = []
212+ for result in local_results :
213+ price_statistics = PriceStatisticsMapper .map (
214+ min_price = result .get ('price' ).get ('min' ),
215+ max_price = result .get ('price' ).get ('max' ),
216+ avg_price = result .get ('price' ).get ('avg' )
217+ )
218+ geohash = result .get ('geohash' )
219+ local_statistics .append (LocalStatisticsMapper .map (
220+ price_statistics = price_statistics ,
221+ geohash = geohash ,
222+ bounding_box = self .get_bounding_box (geohash ),
223+ score = self .get_score (result .get ('price' ).get ('avg' ), avg_price )
224+ ))
225+
223226 result = StatisticsMapper .map (local_statistics = local_statistics , global_statistics = global_statistics )
224227
225228 return result
226229
230+ @staticmethod
231+ def get_bounding_box (geohash ) -> LocationBoundingBox :
232+
233+ bounding_box = LocationBoundingBox ()
234+ bounding_box .top_right = Point ()
235+ bounding_box .bottom_left = Point ()
236+
237+ if geohash is not None :
238+
239+ decoded = pygeohash .decode_exactly (geohash )
240+ center_latitude = decoded [0 ]
241+ center_longitude = decoded [1 ]
242+ epsilon_latitude = decoded [2 ]
243+ epsilon_longitude = decoded [3 ]
244+
245+ bounding_box .top_right .latitude = center_latitude + epsilon_latitude
246+ bounding_box .top_right .longitude = center_longitude + epsilon_longitude
247+
248+ bounding_box .bottom_left .latitude = center_latitude - epsilon_latitude
249+ bounding_box .bottom_left .longitude = center_longitude - epsilon_longitude
250+
251+ return bounding_box
252+
253+ @staticmethod
254+ def get_score (local_avg , global_avg ):
255+
256+ score = 0
257+ if local_avg is not None and global_avg is not None :
258+ if global_avg * 0.8 <= local_avg <= global_avg * 1.2 :
259+ score = 50
260+ elif local_avg > global_avg :
261+ score = 0
262+ else :
263+ score = 100
264+
265+ return score
266+
227267
228268RESOLVER_MONGODB = MongoDBResolver (
229269 mongodb_client = MONGODB_CLIENT ,
0 commit comments