Skip to content

Instantly share code, notes, and snippets.

@songxiaofeng1981
Created July 18, 2013 08:17
Show Gist options
  • Save songxiaofeng1981/6027631 to your computer and use it in GitHub Desktop.
Save songxiaofeng1981/6027631 to your computer and use it in GitHub Desktop.
views
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Create your views here.
import decimal
from django.db.models import Sum
from django.http import HttpResponse
import MySQLdb, json, datetime, time
from django.shortcuts import render_to_response
from django.template import RequestContext
from loging.models import Log_totals
from toolsbox.flashcharts import MSLine2D
from toolsbox.timetools import add_day
from utilities import Bills
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def save_created_log_files(request):
return HttpResponse('abc')
def init_time(request):
today = request.POST.get("today")
three = request.POST.get("three")
twoday = request.POST.get("twoday")
if today is not None:
start_time = today
elif three is not None:
start_time = three
elif twoday is not None:
start_time = twoday
else:
start_time = request.POST.get('start_time')
end_time = request.POST.get('end_time')
return end_time, start_time
def role_chart(request):
try:
end_time, start_time = init_time(request)
total_list = get_total_list_by_time(start_time, end_time)
sorted_list_desc = sorted(total_list, reverse=True, key=lambda total: total['day'])
logfiles = add_filed(sorted_list_desc)
logfiles = sorted(logfiles, reverse=True, key=lambda total: total['day'])
print logfiles
return render_to_response('loging/show.html',
{'start_time': start_time, 'end_time': end_time,
'logfiles': logfiles}, context_instance=RequestContext(request))
except Exception, e:
print e
# def get_total_sql_by_day(start_time, end_time):
# try:
# return Log_totals.objects.extra(select={'day': 'DATE_FORMAT(day, "%%Y-%%m-%%d %%H:%%i")'}).values("day").filter(
# day__range=(start_time, end_time)).annotate(receivecount=Sum('receivecount'),
# createcount=Sum('createcount')).order_by("-day")
# except Exception, e:
# print e
def add_filed(total_list):
result = []
for logfile in total_list:
receivecount = logfile['receivecount']
createcount = logfile['createcount']
percent = division(receivecount, createcount) * 100
logfile['percent'] = percent
result.append(logfile)
return result
def division(def_count, real_count):
if real_count == 0:
return 0
try:
return decimal.Decimal(str(round(float(def_count) / real_count, 2)))
except Exception, e:
print e
return 0
def role_chart_date(request):
bill = Bills()
title = '日志文件监控'
start_time = request.GET.get('start_time')
end_time = request.GET.get('end_time')
total_list = get_total_list_by_time(start_time, end_time)
label_list, value_list1, value_list2, value_list3 = [], [], [], []
for item in total_list:
label_list.append({'label': item['day']})
value_list1.append({'value': item['receivecount']})
value_list2.append({'value': item['createcount']})
labelstep = len(label_list) / 12 if len(label_list) > 12 else 1
msline2d = MSLine2D(title, '')
msline2d.chart["labelStep"] = labelstep
msline2d.chart["slantLabels"] = "1"
msline2d.chart["anchorRadius"] = "1.8"
msline2d.chart["anchorSides"] = "20"
msline2d.chart["formatNumber"] = "0"
msline2d.chart["labeldisplay"] = "ROTATE"
msline2d.chart["formatNumberScale"] = "0"
msline2d.dataset = [{"seriesname": u"已接收文件数", "color": "7985B0", "anchorbgcolor": "7985B0", "data": []},
{"seriesname": u"已产生文件数", "color": "00ff00", "anchorbgcolor": "7985B0", "data": []}]
msline2d.categories = {'category': label_list}
msline2d.dataset[0]["data"] = value_list1
msline2d.dataset[1]["data"] = value_list2
msline2d.trendlines = {
"line": [{"startvalue": "0.8", "color": "91C728", "displayvalue": "Target", "showontop": "1"}]}
res = json.dumps(msline2d.to_dict(), ensure_ascii=False, sort_keys=True, indent=4)
return HttpResponse(res)
def get_total_list_by_time(start_time, end_time):
return Log_totals.objects.extra(select={'day': 'DATE_FORMAT(day, "%%Y-%%m-%%d %%H:%%i")'}).values("day").filter(
day__range=(start_time, end_time)).annotate(receivecount=Sum('receivecount'),
createcount=Sum('createcount')).order_by("-day")
def init_time_chart(request):
current_time = datetime.datetime.now()
past_time = add_day(datetime.datetime.now(), -0.125)
start_time = past_time.strftime("%Y-%m-%d %H:%M:%S")
end_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
start_time = request.POST.get('start_time')
if start_time is None:
end_time = request.POST.get('end_time')
return end_time, start_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment