def get_graph_values(items): """ This function just returns the raw data that can be used for drawing the graph. It takes the parameters for the query from request.POST object. """ sfrom = items['wfrom'].split('-') dfrom = date(int(sfrom[0]), int(sfrom[1]), int(sfrom[2])) sto = items['wto'].split('-') dto = date(int(sto[0]), int(sto[1]), int(sto[2])) graph_type = items['graph_type'] aggregation = items['aggregation'] data_type = items['data_type'] graph_values = {} if data_type == 'jobcount': graph_values['ylabel']='Number of jobs' graph_values['title']='Completed jobs per time unit' data_type_arg=Count("pk") data_type_res='pk__count' elif data_type == 'cputime': graph_values['ylabel']='CPU time' graph_values['title']='CPU time of completed jobs' data_type_arg=Sum("cput") data_type_res='cput__sum' elif data_type == 'walltime': graph_values['ylabel']='Wall time' graph_values['title']='Wall time of completed jobs' data_type_arg=Sum("walltime") data_type_res='walltime__sum' queue_pks = [] for ts in BatchServer.objects.all(): for q in items.getlist('server_'+str(ts.pk)): if q.startswith('queue_'): queue_pks.append(q[len('queue_'):]) queue_pks.sort() if aggregation=='day': N = (dto-dfrom).days+1 elif aggregation=='week': dfrom = date.fromordinal(dfrom.toordinal()-dfrom.weekday()) dto = date.fromordinal(dto.toordinal()+(6-dto.weekday())) N = ((dto-dfrom).days+1)/7 else: # month dfrom = date(dfrom.year, dfrom.month, 1) dto = nextmonth(dto) N = (dto.year-dfrom.year)*12+(dto.month-dfrom.month+1) fromdates = [] todates = [] for j in range(0,N): if aggregation=='day': f = date.fromordinal(dfrom.toordinal()+j).isoformat() t = date.fromordinal(dfrom.toordinal()+j+1).isoformat() elif aggregation=='week': f = date.fromordinal(dfrom.toordinal()+j*7).isoformat() t = date.fromordinal(dfrom.toordinal()+(j+1)*7).isoformat() else: if j==0: f = dfrom else: f = todates[-1] t = nextmonth(f) fromdates.append(f) todates.append(t) graph_values['values'] = [] queue_names = {} for q in Queue.objects.all().values('pk','name','server__name'): queue_names[q['pk']]="%s@%s" % (q['name'], q['server__name']) graph_values['queues'] = [] for pk in queue_pks: graph_values['queues'].append(queue_names[int(pk)]) graph_values['queues_colors'] = [] backup_colors = getColorArrayHTML(len(queue_pks)) i = 0 for q in queue_pks: color = Queue.objects.get(pk=q).color if not color: color = backup_colors[i] graph_values['queues_colors'].append(color) i+=1 for j in range(0,N): rr = {} rr['date'] = fromdates[j] out = Job.objects.filter(comp_time__range=(fromdates[j], todates[j])).values('queue__pk').annotate(data_type_arg) od = {} for i in out: od[str(i['queue__pk'])]=i[data_type_res] rr['queues'] = [] for q in queue_pks: if od.has_key(q): rr['queues'].append(od[q]) else: rr['queues'].append(0) graph_values['values'].append(rr) return graph_values
def fairshare(request): """ View info about total used walltime and cputime. Compare it with walltime/cputime per entity (queue, unix group, unix user). """ fs_form = FairshareForm() if not request.POST: return render_to_response_with_config( 'trqacc/jobs_fairshare.html', {'fs_form':fs_form} ) fs_form.data['timescale'] = request.POST['timescale'] fs_form.data['minutes'] = request.POST['minutes'] fs_form.data['entity'] = request.POST['entity'] fs_form.data['server'] = request.POST['server'] fs_form.is_bound = True minutes = int(request.POST['timescale']) if minutes == 0: minutes = int(request.POST['minutes']) endtime = datetime.datetime.now() starttime = datetime.datetime.fromtimestamp(int(endtime.strftime("%s"))-60*minutes) # Job.objects.filter(comp_time__range=("2010-01-01", "2010-01-02")).values('queue__name').annotate(Sum('walltime')) if request.POST['entity']=='0': # Queue entity_name = 'queue__name' entity_color = 'queue__color' elif request.POST['entity']=='1': # Group entity_name = 'job_owner__group__name' entity_color = 'job_owner__group__color' else: # request.POST['entity']=='2': # User entity_name = 'job_owner__name' entity_color = 'job_owner__color' result = [] total_sum = 0 for j in Job.objects.filter(server__pk=request.POST['server'], comp_time__range=(starttime, endtime)).values(entity_name, entity_color).annotate(Sum('walltime')): secs = int(j['walltime__sum']) tstr = "%d %d:%d:%d" % ((secs/86400), (secs/3600)%24, (secs/60)%60, secs%60) result.append({ 'entity_name':j[entity_name], 'walltime__sum':j['walltime__sum'], 'walltime__str':tstr, 'entity_color':j[entity_color] }) total_sum += int(j['walltime__sum']) # check color validity colors_valid = False for r in result: if r['entity_color']: colors_valid = True break if not colors_valid: i = 0 generated_colors = getColorArrayHTML(len(result)) for r in result: r['entity_color'] = generated_colors[i] i += 1 total_str = "%d %d:%d:%d" % ((total_sum/86400), (total_sum/3600)%24, (total_sum/60)%60, total_sum%60) total = {'entity_name':'total', 'walltime__sum':total_sum, 'walltime__str':total_str} for row in result: if total_sum==0: row['percentage'] = 0 else: row['percentage'] = 100*float(row['walltime__sum'])/total_sum return render_to_response_with_config( 'trqacc/jobs_fairshare.html', {'fs_form':fs_form, 'result':result, 'total':total} )