Exemple #1
0
def bandwidth(req):
    if req.method == 'POST':
        domain_name = req.POST.get('domain_name')
        start = req.POST.get('start')
        end = req.POST.get('end')
        obj = DiLianManager()
        status, reason, resp = obj.bandwidthMap(domain_name, start, end)
        if status == 200:
            #random = uuid.uuid1()
            #path = settings.MONITOR_IMG % random
            #os.system('rm -f %s/*' % os.path.dirname(path))
            #with open(path, 'wb') as f:
            #    f.write(resp)
            data = []
            for i in Etree.fromstring(resp).findall("date/Product/Traffice"):
                data.append(i.text)
            result = ','.join(data)
        else:
            result = Etree.fromstring(resp).find("Message").text
        return HttpResponse(result)
    else:
        if not req.session.has_key("project_id"):
            return HttpResponseRedirect('/login/')
        else:
            project_id = req.session['project_id']
        username = req.COOKIES.get('username')
        if username == settings.SUPERADMIN:
            domains = Domain.objects.all()
        else:
            domains = Domain.objects.filter(project_id=project_id)
        project_list = req.session['project_list']
        return render_to_response("bandwidth.html", locals())
Exemple #2
0
def bandwidth_csv(req):
    if req.method == 'GET':
        import csv
        domain_name = req.GET.get('domain_name')
        start = req.GET.get('start')
        end = req.GET.get('end')
        obj = DiLianManager()
        status, reason, resp = obj.bandwidthMap(domain_name, start, end)
        if status == 200:
            flows = [utils.bandwidthObj(l) for l in Etree.fromstring(resp).findall('date/Product/Traffice')]
            response = HttpResponse(mimetype="text/csv")
            response['Content-Disposition'] = 'attachment; filename=%s_%s_bandwidth.csv' % (start, end)
            writer = csv.writer(response)
            writer.writerow(["Time", "BandWidth"])
            for f in flows:
                writer.writerow([f.time, f.bandwidth])
            return response
        else:
            result = Etree.fromstring(resp).find("Message").text
            return HttpResponse(result)