summaryrefslogtreecommitdiff
path: root/lava-jobs.el
blob: bc662ef4a0e124269a03adbfda4e9f1c778557b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;;; lava-jobs --- Utility functions for accessing a list of LAVA jobs
;;
;; Copyright (C) 2014 Alex Bennée

;; Author: Alex Bennée <alex.bennee@linaro.org>
;; Maintainer: Alex Bennée <alex.bennee@linaro.org>
;; 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 <http://www.gnu.org/licenses/>.
;;
;;; 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 `JOB-ID' to the list of jobs."
  (let ((index (lava-jobs-make-index job-id)))
    (unless (lava-job-get-hash index)
      (let ((hash (make-hash-table :test 'equal))
            (status-string (if status
                               status
                             "Submitted")))
        (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)))
    (gethash key info-hash)))

;; 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-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