From patchwork Thu Apr 18 08:42:17 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Senthil Kumaran X-Patchwork-Id: 16187 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qe0-f69.google.com (mail-qe0-f69.google.com [209.85.128.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id AB4C423974 for ; Thu, 18 Apr 2013 08:43:11 +0000 (UTC) Received: by mail-qe0-f69.google.com with SMTP id 2sf4623295qea.4 for ; Thu, 18 Apr 2013 01:42:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:x-beenthere:x-received:received-spf:x-received :x-forwarded-to:x-forwarded-for:delivered-to:x-received:received-spf :mime-version:x-launchpad-project:x-launchpad-branch :x-launchpad-message-rationale:x-launchpad-branch-revision-number :x-launchpad-notification-type:to:from:subject:message-id:date :reply-to:sender:errors-to:precedence:x-generated-by :x-launchpad-hash:x-gm-message-state:x-original-sender :x-original-authentication-results:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe :content-type; bh=XYOAFU4eSqZ2JMA85ASYyD4KhlEBdRmH235DpH2vsnM=; b=McrnqGkQ5nu6ahPueMNF1ukyNwT4jJSY3oHVsI9pNh3q56p0QpLw1QGjKzy6z8fEjI sBzSTmxBYZPYw0nP6gNbgsQxGDpPy5uaNbBkqpK4uISvRz8+cb+kly2SZ33qGI/DgLkZ YkP+Qk4tYyhUrJcdLpdumUg55rzNHr+pDtyccBT9yjg50BWI0LqT1s3lM8i24bNgKmGG X0fY+577lqpMcRjhqKzO6m/g5v8OutJfVVnyx7riCK5QJhpTtKpCsrqGdRp3vj/TmUPM ZxiezvZ8iNDzFnytRMX20ErARYhZHDqffmvuiecXkvovEf0Z3BVhMXG16R5E00FK6inV DVrw== X-Received: by 10.224.207.2 with SMTP id fw2mr7391868qab.6.1366274554729; Thu, 18 Apr 2013 01:42:34 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.0.174 with SMTP id 14ls1361457qef.44.gmail; Thu, 18 Apr 2013 01:42:34 -0700 (PDT) X-Received: by 10.52.17.51 with SMTP id l19mr6509633vdd.118.1366274554578; Thu, 18 Apr 2013 01:42:34 -0700 (PDT) Received: from mail-vb0-x22f.google.com (mail-vb0-x22f.google.com [2607:f8b0:400c:c02::22f]) by mx.google.com with ESMTPS id fd1si6098211vcb.65.2013.04.18.01.42.34 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 18 Apr 2013 01:42:34 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c02::22f is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=2607:f8b0:400c:c02::22f; Received: by mail-vb0-f47.google.com with SMTP id x13so2067966vbb.20 for ; Thu, 18 Apr 2013 01:42:34 -0700 (PDT) X-Received: by 10.220.39.69 with SMTP id f5mr7583318vce.45.1366274554312; Thu, 18 Apr 2013 01:42:34 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.58.127.98 with SMTP id nf2csp112218veb; Thu, 18 Apr 2013 01:42:33 -0700 (PDT) X-Received: by 10.180.24.65 with SMTP id s1mr16529916wif.0.1366274553272; Thu, 18 Apr 2013 01:42:33 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id de2si3354217wjc.17.2013.04.18.01.42.32 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 18 Apr 2013 01:42:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1USkQK-0007E5-Kr for ; Thu, 18 Apr 2013 08:42:32 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id 5EED3E31A5 for ; Thu, 18 Apr 2013 08:42:17 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: lava-scheduler X-Launchpad-Branch: ~linaro-validation/lava-scheduler/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 243 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-validation/lava-scheduler/trunk] Rev 243: Add more xmlrpc APIs to make life easy. Message-Id: <20130418084217.27313.53520.launchpad@ackee.canonical.com> Date: Thu, 18 Apr 2013 08:42:17 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: list X-Generated-By: Launchpad (canonical.com); Revision="16567"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: d65ace69264f85031c48e03df24b12200c788471 X-Gm-Message-State: ALoCoQny81/NYaxmXuQNlJnOcyYSV+t/DrAiQ6ek4Ygo5i/jcvNDE1ltviv9PDvWJD8Y6LlPDxKq X-Original-Sender: noreply@launchpad.net X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400c:c02::22f is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Merge authors: Senthil Kumaran S (stylesen) Related merge proposals: https://code.launchpad.net/~stylesen/lava-scheduler/more-xml-rpc-apis/+merge/158400 proposed by: Senthil Kumaran S (stylesen) review: Approve - Antonio Terceiro (terceiro) review: Needs Fixing - Zygmunt Krynicki (zkrynicki) ------------------------------------------------------------ revno: 243 [merge] committer: Senthil Kumaran branch nick: trunk timestamp: Thu 2013-04-18 14:10:44 +0530 message: Add more xmlrpc APIs to make life easy. modified: lava_scheduler_app/api.py --- lp:lava-scheduler https://code.launchpad.net/~linaro-validation/lava-scheduler/trunk You are subscribed to branch lp:lava-scheduler. To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-scheduler/trunk/+edit-subscription === modified file 'lava_scheduler_app/api.py' --- lava_scheduler_app/api.py 2012-05-09 05:57:15 +0000 +++ lava_scheduler_app/api.py 2013-04-18 00:58:20 +0000 @@ -1,15 +1,17 @@ import xmlrpclib - from simplejson import JSONDecodeError - +from django.db.models import Count from linaro_django_xmlrpc.models import ExposedAPI - from lava_scheduler_app.models import ( Device, DeviceType, JSONDataError, TestJob, ) +from lava_scheduler_app.views import ( + SumIfSQL, + SumIf +) class SchedulerAPI(ExposedAPI): @@ -51,3 +53,148 @@ raise xmlrpclib.Fault(403, "Permission denied.") job.cancel() return True + + def job_output(self, job_id): + """ + Name + ---- + `job_output` (`job_id`) + + Description + ----------- + Get the output of given job id. + + Arguments + --------- + `job_id`: string + Job id for which the output is required. + + Return value + ------------ + This function returns an XML-RPC binary data of output file, provided + the user is authenticated with an username and token. + """ + + if not self.user: + raise xmlrpclib.Fault( + 401, "Authentication with user and token required for this " + "API.") + + try: + job = TestJob.objects.accessible_by_principal(self.user).get( + pk=job_id) + except TestJob.DoesNotExist: + raise xmlrpclib.Fault(404, "Specified job not found.") + + return xmlrpclib.Binary(job.output_file().read()) + + def all_devices(self): + """ + Name + ---- + `all_devices` () + + Description + ----------- + Get all the available devices with their state and type information. + + Arguments + --------- + None + + Return value + ------------ + This function returns an XML-RPC array in which each item is a list of + device hostname, device type and device state. For example: + + [['panda01', 'panda', 'running'], ['qemu01', 'qemu', 'idle']] + """ + + devices = Device.objects.values_list('hostname', + 'device_type__name', + 'status') + devices = [list((x[0], x[1], Device.STATUS_CHOICES[x[2]][1].lower())) + for x in devices] + + return devices + + def all_device_types(self): + """ + Name + ---- + `all_device_types` () + + Description + ----------- + Get all the available device types with their state and count + information. + + Arguments + --------- + None + + Return value + ------------ + This function returns an XML-RPC array in which each item is a dict + which contains name (device type), idle, busy, offline counts. + For example: + + [{'idle': 1, 'busy': 0, 'name': 'panda', 'offline': 0}, + {'idle': 1, 'busy': 0, 'name': 'qemu', 'offline': 0}] + """ + + device_type_list = [] + keys = ['busy', 'name', 'idle', 'offline'] + + device_types = DeviceType.objects.filter(display=True).annotate( + idle=SumIf('device', condition='status=%s' % Device.IDLE), + offline=SumIf('device', condition='status in (%s,%s)' % ( + Device.OFFLINE, Device.OFFLINING)), + busy=SumIf('device', condition='status=%s' % Device.RUNNING), + ).order_by('name') + + for dev_type in device_types: + device_type = {} + for key in keys: + device_type[key] = getattr(dev_type, key) + device_type_list.append(device_type) + + return device_type_list + + def pending_jobs_by_device_type(self): + """ + Name + ---- + `pending_jobs_by_device_type` () + + Description + ----------- + Get number of pending jobs in each device type. + + Arguments + --------- + None + + Return value + ------------ + This function returns a dict where the key is the device type and + the value is the number of jobs pending in that device type. + For example: + + {'qemu': 0, 'panda': 3} + """ + + pending_jobs_by_device = {} + + jobs = TestJob.objects.filter(status=TestJob.SUBMITTED).values_list( + 'requested_device_type_id').annotate( + pending_jobs=(Count('id'))) + pending_jobs_by_device.update(dict(jobs)) + + # Get rest of the devices and put number of pending jobs as 0. + device_types = DeviceType.objects.values_list('name', flat=True) + for device_type in device_types: + if device_type not in pending_jobs_by_device: + pending_jobs_by_device[device_type] = 0 + + return pending_jobs_by_device