Beispiel #1
0
def comm_item_buyer(request, item, curr_user):
	"""
	The curr_user is the seller, show him the communication interface
	"""
	if request.method == 'POST':
		form = CommForm(request.POST)
		if form.is_valid():
			msg = form.cleaned_data['message']
			try:
				comm = Comm.objects.get(buyer=curr_user.user_obj, item=item)
			except Comm.DoesNotExist:
				comm = Comm(buyer=curr_user.user_obj, item=item)
				comm.save()
			message = Messages(comm=comm, content=msg, user=curr_user.user_obj)
			message.save()
			form = CommForm()
	else:
		form = CommForm()

	# View Part
	try:
		comm = Comm.objects.get(buyer=curr_user.user_obj, item=item)
		try:
			ret = Messages.objects.filter(comm=comm).order_by('-time')
		except Messages.DoesNotExist:
			ret = []
	except Comm.DoesNotExist:
		ret = []
		comm = Comm(buyer=curr_user.user_obj, item=item)
	return render(request, 'comm.html',{'msgs': ret, 'form': form, 'DealForm': DealForm(), 'comm': comm, 'role': 'buyer', 'item': item, 'CancelForm': CancelForm()})
Beispiel #2
0
    def comm_get(self, request):
        user = check_signed_in()
        comm = Comm.get_by_id(request.id)
        #multiple receivers...

        if comm and (user == comm.sender or user == comm.receiver):
            return Comm.to_message_collection([comm])
        else:
            raise endpoints.NotFoundException('Item %s not found.' %
                                              (request.id, ))
Beispiel #3
0
	def estimate_resp_time(self, queries_per_sec):
		self.comm_req_time, _ = Comm.estimate_query_comm_time(self.src_lat, self.src_lng, self.dc_id, Query.query_req_bytes)
		if math.isnan(self.comm_req_time):
			raise ValueError('self.comm_req_time is nan')
		
		self.query_time = Exec.estimate_query_time(self.dc_id, queries_per_sec)
		if math.isnan(self.query_time):
			raise ValueError('self.quepry_time is nan')
		
		self.comm_resp_time, self.dist_mi = Comm.estimate_query_comm_time(self.src_lat, self.src_lng, self.dc_id, Query.query_resp_bytes)
		if math.isnan(self.comm_resp_time):
			raise ValueError('self.query_time): is nan')		

		self.total_resp_time = self.comm_req_time + self.query_time + self.comm_resp_time
Beispiel #4
0
 def add_comm_post(self, request):
     user = check_signed_in()
     item = Item.get_by_id(long(request.item_id))
     try:
         comm = Comm(subject=request.subject,
                     sender=user.id(),
                     receiver=request.receiver.split(';'),
                     content=request.content,
                     item_id=item,
                     item_title=item.title,
                     price=item.price)
         key = comm.put()
         return BaseMessage(message="OK", code="OK", data=str(key.id()))
     except Exception as e:
         return BaseMessage(message=e.message, code="ERROR", data=e.message)
Beispiel #5
0
def init(args):
    conf = init_conf(args)
    dc = load_dc(conf)

    assert (not conf['read_topo'] or not conf['write_topo_and_done']
            ), "Both read_topo and write_topo_and_done cannot be True!"
    topo = load_topo(conf, dc)

    Comm.set_params(dc, conf['sigmas'][0], conf['sigmas'][1],
                    conf['omegas'][0], conf['omegas'][1], conf['omegas'][2])
    Exec.set_params(dc, conf['betas'][0], conf['betas'][1], conf['gammas'][0],
                    conf['gammas'][1], conf['thetas'][0], conf['thetas'][1],
                    conf['lambda_ms'] / 1000)
    Query.set_params(conf['query_req_bytes'], conf['query_resp_bytes'])
    DataAggregator.set_params(conf, dc, topo)

    return conf, dc, topo
Beispiel #6
0
    def del_comm_post(self, request):
        #delete Users Items?
        user = check_signed_in()
        comm = Comm.get_by_id(request.id)

        if comm and comm.sender == User.query(
                User.email == endpoints.get_current_user()).get():
            comm.delete()
            return BaseMessage(message="OK", code="OK", data="User deleted")
        else:
            raise endpoints.NotFoundException('Item %s not found.' %
                                              (request.id, ))
Beispiel #7
0
def seal(request, item_id, curr_user):
	"""
	When buyer seals the deal
	status = 0 -> 1
	"""
	try:
		item = items.objects.get(id=item_id, is_expired=False, is_sold=False)
	except items.DoesNotExist:
		return render(request, 'error.html', {'error': 'Item Not Found'})

	if request.method == 'POST':
		form = DealForm(request.POST)
		if form.is_valid() and form.cleaned_data.get('deal', False):
			# If sealed by buyer
			if item.user != curr_user.user_obj:
				try:
					comm = Comm.objects.get(buyer=curr_user.user_obj, item=item)
				except Comm.DoesNotExist:
					comm = Comm(buyer=curr_user.user_obj, item=item)
					comm.save()
				if comm.status == 0:
					comm.status = 1
					comm.save()
				else:
					return render(request, 'error.html', {'error': 'You can\'t seal the deal now!'})
				return HttpResponseRedirect(item.get_comm_url())
			else:
				# Current user is the seller
				return render(request, 'error.html', {'error': 'You can\'t purchase your own item!!'})
	return HttpResponseRedirect(item.get_comm_url())
Beispiel #8
0
    def aggregate(levels):
        # levels: [city_aliases, ['county'], ['state'], ['region']]
        L = len(levels)

        conf = DataAggregator.conf
        dc = DataAggregator.dc
        topo = DataAggregator.topo

        max_results = []
        for l in range(L):
            results = []
            entities = topo.loc[topo.type.isin(levels[str(l)])]
            max_usage = pd.DataFrame(data=0.0,
                                     index=dc.index,
                                     columns=['max_usage'])

            # collect incoming data per dc per level for map-reduce exec time
            dc.loc[topo.loc[entities.index, 'dc_id'], 'data_in'] = 0.0
            for index, entity in entities.iterrows():
                dc.loc[entity.dc_id, 'data_in'] += entity.data_in

            for index, entity in entities.iterrows():
                dc1_id = entity.dc_id
                dc_data_in = dc.loc[dc1_id, 'data_in']

                m = 0
                if l < L - 1:
                    # map-reduce: compute based on data_in per dc
                    exec_time = 0.0
                    if l == 0:
                        exec_time += Exec.estimate_map_time(dc1_id, dc_data_in)
                        dc_data_in *= conf['s_m']
                    exec_time += Exec.estimate_reduce_time(dc1_id, dc_data_in)

                    # communcation: compute based on data_in per entity
                    data_out = entity['data_in'] * (conf['s_m'] if l == 0 else
                                                    1.0) * conf['s_r']
                    topo.loc[entity.parent_id, 'data_in'] += data_out
                    dc2_id = topo.loc[entity.parent_id].dc_id

                    comm_time, dist_mi = Comm.estimate_dc_comm_time(
                        dc1_id, dc2_id, data_out)
                    aggr_time = exec_time + comm_time

                    # stats
                    if l == 0:
                        # incoming data to level 0 are all mobile
                        DataAggregator.mobile_data += entity['data_in']
                    if dc1_id == dc2_id:
                        DataAggregator.lan_data += data_out
                    else:
                        DataAggregator.wan_data += data_out

                else:
                    # region DC
                    dc2_id = None
                    exec_time = Exec.estimate_reduce_time(dc1_id, dc_data_in)
                    comm_time = 0.0
                    dist_mi = 0
                    aggr_time = exec_time

                # keep track of max usage of machines per dc for level l
                max_usage.loc[dc1_id, 'max_usage'] = max(
                    max_usage.loc[dc1_id, 'max_usage'], aggr_time)

                # save time results in msec
                result = DataAggregationResult(
                    l, aggr_time, exec_time,
                    comm_time if comm_time is not None else None, entity.name,
                    entity['name'], entity.parent_id,
                    topo.loc[entity.parent_id,
                             'name'] if entity.parent_id != -1 else None,
                    dist_mi, dc1_id, dc.loc[dc1_id,
                                            'name'], dc_data_in, dc.loc[dc1_id,
                                                                        'm'],
                    dc2_id, dc.loc[dc2_id,
                                   'name'] if dc2_id is not None else None)
                results.append(result)

            # stats
            DataAggregator.vm_hours_topo.append(
                sum(dc['m'] * max_usage['max_usage'] / 3600))
            for l in range(L):
                ids = (max_usage['max_usage'] != 0.0) & dc.type.isin(
                    levels[str(l)])
                mh = sum(dc.loc[ids, 'm'] * max_usage.loc[ids, 'max_usage'] /
                         3600)
                DataAggregator.vm_hours_dc[l] += mh
            max_results.append(
                max(results, key=lambda (result): result.aggr_time))

        return max_results
Beispiel #9
0
 def list_comms_get(self, request):
     #missing filter
     return Comm.to_message_collection(Comm.query())