;;; lava-jobs --- Utility functions for accessing a list of LAVA jobs ;; ;; Copyright (C) 2014 Alex Bennée ;; Author: Alex Bennée ;; Maintainer: Alex Bennée ;; Version: 0.1 ;; Homepage: git.linaro.org/people/alex.bennee/lava-mode.git ;; This file is not part of GNU Emacs. ;; This file is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;; ;;; Commentary: ;; ;; This provides the utility functions for adding, removing and ;; manipulating the list of LAVA jobs used by the other modes. ;; ;;; Code: ;; uncomment to debug ;(setq debug-on-error t) ;(setq edebug-all-defs t) ;; Require prerequisites (require 'dash) ;; Variables (defvar lava-job-info nil "Association list of job info hashes, indexed by lava-job-%d symbols.") (defun lava-jobs-make-index (jobspec-or-id) "Return a unique symbol for a given jobspec-or-id for use in the lava-job-info association list." (cond ((symbolp jobspec-or-id) jobspec-or-id) ((numberp jobspec-or-id) (intern (format "lava-job-%d" jobspec-or-id))) ((stringp jobspec-or-id) (intern (if (string-prefix-p "lava-job-" jobspec-or-id) jobspec-or-id (format "lava-job-%s" jobspec-or-id)))))) ;; Usage ; (lava-jobs-make-index 1234) (defun lava-jobs-get-hash (jobspec) "Return the info hash for the lava job however you specify JOBSPEC." (let ((lava-job (lava-jobs-make-index jobspec))) (cdr (assq lava-job lava-job-info)))) (defun lava-jobs-delete-hash (jobspec) "Remove the info hash for `JOBSPEC' from the list." (let ((index (lava-jobs-make-index jobspec))) (setq lava-job-info (assq-delete-all index lava-job-info)))) (defun lava-jobs-create-hash (job-id &optional status) "Add a new hash (if needed) for numeric `JOB-ID' to the list of jobs. The optional `STATUS' sets that field up." (let ((index (lava-jobs-make-index job-id))) (unless (lava-jobs-get-hash index) (let ((hash (make-hash-table :test 'equal)) (status-string (if status status "Submitted"))) (puthash "lava_host" lava-host hash) (puthash "lava_api_token" lava-api-token hash) (puthash "job_id" (format "%d" job-id) hash) (puthash "job_status" status-string hash) (puthash "updated" (current-time) hash) (setq lava-job-info (cons `(,index . ,hash) lava-job-info)))))) (defun lava-jobs-get-field (job-id key) "Return a given field or `NIL' for a given job-id" (let ((info-hash (lava-jobs-get-hash job-id))) (when info-hash (gethash key info-hash)))) (defun lava-job-complete-p (job-id) "Return t if the job is complete." (-contains? '("Complete" "Incomplete") (lava-jobs-get-field job-id "job_status"))) ;; Return all the lava-jobs (defun lava-jobs-get-all-jobs () "Return a list of all LAVA jobs in the system." (-map 'car lava-job-info)) (defun lava-jobs-get-all-on-host (server) "Return a list of all LAVA jobs for a given `SERVER'." (let ((host-regex (rx-to-string `(: bos ,server eos)))) (--filter (string-match-p host-regex (lava-jobs-get-field it "lava_host")) (-map 'car lava-job-info)))) (defun lava-jobs-get-actual-device (job-id) "Return the actual device this job is running on, or nil" (let ((hostcache (or (lava-jobs-get-field job-id "_actual_device_cache") (lava-jobs-get-field job-id "_requested_device_cache")))) (if hostcache (cdr (assoc "hostname" hostcache)) nil))) (defun lava-jobs-find-running-job-by-name (name) "Return a list of running jobs that match the `NAME' as the job description." (-filter (lambda (job) (let* ((info-hash (lava-jobs-get-hash job)) (info-desc (gethash "description" info-hash))) (and info-desc (string-match-p name info-desc)))) (-map 'car lava-job-info))) ; (lava-jobs-find-running-job-by-name "mustang-cloudimg-hacking-session") ;(lava-jobs-find-running-job-by-name "mustang-hacking-session") (defun lava-jobs-filter-by-state (state list-of-jobs) "Return a list of jobs in a given state." (--filter (string-match-p state (lava-jobs-get-field it "job_status")) list-of-jobs)) (defun lava-jobs-find-latest (list-of-jobs) "Return the latest of `LIST-OF-JOBS' by update time." (--max-by (let ((it-time (lava-jobs-get-field it "updated")) (other-time (lava-jobs-get-field it "updated"))) (time-less-p other-time it-time)) list-of-jobs)) ;; (lava-jobs-find-latest (lava-jobs-get-all-jobs)) ;; (lava-jobs-find-latest (lava-jobs-find-running-job-by-name "mustang-hacking-session")) (defun lava-jobs-find-latest-running (list-of-jobs) "Return the latest running job of from `LIST-OF-JOBS'." (let ((running-jobs (lava-jobs-filter-by-state "Running" list-of-jobs))) (when running-jobs (--max-by (let ((it-time (lava-jobs-get-field it "updated")) (other-time (lava-jobs-get-field it "updated"))) (time-less-p other-time it-time)) running-jobs)))) ; (lava-jobs-find-latest-running (lava-jobs-find-running-job-by-name "mustang-hacking-session")) (provide 'lava-jobs) ;;; lava-jobs.el ends here