def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) self.throttle_check(request) SEARCH_COUNT = int(request.GET.get('n', 5)) cloudlet_ip = request.META.get("REMOTE_ADDR") client_location = cost.ip2location(cloudlet_ip) lat1, lon1 = client_location.latitude, client_location.longitude cloudlet_list = list() for cloudlet in self.Meta.queryset: if cloudlet.status != Cloudlet.CLOUDLET_STATUS_RUNNING: continue lat2, lon2 = float(cloudlet.latitude), float(cloudlet.longitude) geo_distance = ip_location.geo_distance(lat1, lon1, lat2, lon2) cloudlet.cost = geo_distance cloudlet_list.append(cloudlet) top_cloudlets = heapq.nlargest(SEARCH_COUNT, cloudlet_list, key=itemgetter('cost')) top_cloudlet_list = [item.search_out() for item in top_cloudlets] object_list = {'cloudlet': top_cloudlet_list} self.log_throttled_access(request) return self.create_response(request, object_list)
def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) self.throttle_check(request) SEARCH_COUNT = int(request.GET.get('n', 5)) cloudlet_ip = request.META.get("REMOTE_ADDR") client_location = cost.ip2location(cloudlet_ip) lat1, lon1 = client_location.latitude, client_location.longitude cloudlet_list = list() for cloudlet in self.Meta.queryset: if cloudlet.status != Cloudlet.CLOUDLET_STATUS_RUNNING: continue lat2, lon2 = float(cloudlet.latitude), float(cloudlet.longitude) geo_distance = ip_location.geo_distance(lat1, lon1, lat2, lon2) cloudlet.cost = geo_distance cloudlet_list.append(cloudlet) top_cloudlets = heapq.nlargest(SEARCH_COUNT, cloudlet_list, key=itemgetter('cost')) top_cloudlet_list = [item.search_out() for item in top_cloudlets] object_list = { 'cloudlet' : top_cloudlet_list } self.log_throttled_access(request) return self.create_response(request, object_list)
def search_nearby_cloudlet(self, client_ip, max_count=10): client_location = self.cost.ip2location(client_ip) if not client_location: return list() lat1, lon1 = client_location.latitude, client_location.longitude search_field = ["latitude", "longitude", "ip_address"] sql_query = "select %s from ds_cloudlet where status = 'RUN'" % (", ".join(search_field)) ret_list = self.db_engine.execute(sql_query) cloudlet_list = list() for item in ret_list: cloudlet_machine = CloudletMachine(search_field, item) lat2, lon2 = float(cloudlet_machine.latitude), float(cloudlet_machine.longitude) geo_distance = ip_location.geo_distance(lat1, lon1, lat2, lon2) cloudlet_machine.cost = geo_distance cloudlet_list.append(cloudlet_machine) top_cloudlets = heapq.nlargest(max_count, cloudlet_list, key=itemgetter('cost')) return top_cloudlets
def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) self.throttle_check(request) SEARCH_COUNT = int(request.GET.get('n', 5)) client_ip = request.GET.get('client_ip', None) latitude = request.GET.get('latitude', None) longitude = request.GET.get('longitude', None) if latitude is not None and longitude is not None: latitude, longitude = float(latitude), float(longitude) else: if client_ip is None or self._is_ip(client_ip) is False: client_ip = request.META.get("REMOTE_ADDR") client_location = cost.ip2location(str(client_ip)) latitude = getattr(client_location, 'latitude', \ CloudletResource.DEFAULT_LATITUDE) longitude = getattr(client_location, 'longitude', \ CloudletResource.DEFAULT_LONGITUDE) cloudlet_list = list() for cloudlet in Cloudlet.objects.all(): if cloudlet.status != Cloudlet.CLOUDLET_STATUS_RUNNING: continue if len(cloudlet.latitude) is 0: cloudlet.latitude = str(CloudletResource.DEFAULT_LATITUDE) if len(cloudlet.longitude) is 0: cloudlet.longitude = str(CloudletResource.DEFAULT_LONGITUDE) geo_distance = ip_location.geo_distance(latitude, longitude, \ float(cloudlet.latitude), float(cloudlet.longitude)) cloudlet.cost = geo_distance cloudlet_list.append(cloudlet) top_cloudlets = heapq.nsmallest(SEARCH_COUNT, cloudlet_list, key=itemgetter('cost')) top_cloudlet_list = [item.search_out() for item in top_cloudlets] object_list = { 'cloudlet' : top_cloudlet_list } self.log_throttled_access(request) return self.create_response(request, object_list)
def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) self.throttle_check(request) SEARCH_COUNT = int(request.GET.get('n', 5)) client_ip = request.GET.get('client_ip', None) latitude = request.GET.get('latitude', None) longitude = request.GET.get('longitude', None) if latitude is not None and longitude is not None: latitude, longitude = float(latitude), float(longitude) else: if client_ip is None or self._is_ip(client_ip) is False: client_ip = request.META.get("REMOTE_ADDR") client_location = cost.ip2location(str(client_ip)) latitude = getattr(client_location, 'latitude', \ CloudletResource.DEFAULT_LATITUDE) longitude = getattr(client_location, 'longitude', \ CloudletResource.DEFAULT_LONGITUDE) cloudlet_list = list() for cloudlet in Cloudlet.objects.all(): if cloudlet.status != Cloudlet.CLOUDLET_STATUS_RUNNING: continue if len(cloudlet.latitude) is 0: cloudlet.latitude = str(CloudletResource.DEFAULT_LATITUDE) if len(cloudlet.longitude) is 0: cloudlet.longitude = str(CloudletResource.DEFAULT_LONGITUDE) geo_distance = ip_location.geo_distance(latitude, longitude, \ float(cloudlet.latitude), float(cloudlet.longitude)) cloudlet.cost = geo_distance cloudlet_list.append(cloudlet) top_cloudlets = heapq.nsmallest(SEARCH_COUNT, cloudlet_list, key=itemgetter('cost')) top_cloudlet_list = [item.search_out() for item in top_cloudlets] object_list = {'cloudlet': top_cloudlet_list} self.log_throttled_access(request) return self.create_response(request, object_list)
def search_nearby_cloudlet(self, client_ip, max_count=10): client_location = self.cost.ip2location(client_ip) if not client_location: return list() lat1, lon1 = client_location.latitude, client_location.longitude search_field = ["latitude", "longitude", "ip_address"] sql_query = "select %s from ds_cloudlet where status = 'RUN'" % ( ", ".join(search_field)) ret_list = self.db_engine.execute(sql_query) cloudlet_list = list() for item in ret_list: cloudlet_machine = CloudletMachine(search_field, item) lat2, lon2 = float(cloudlet_machine.latitude), float( cloudlet_machine.longitude) geo_distance = ip_location.geo_distance(lat1, lon1, lat2, lon2) cloudlet_machine.cost = geo_distance cloudlet_list.append(cloudlet_machine) top_cloudlets = heapq.nlargest(max_count, cloudlet_list, key=itemgetter('cost')) return top_cloudlets