entity-csv.ss 6.26 KB
Newer Older
Dave Griffiths's avatar
Dave Griffiths committed
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
;; Naked on Pluto Copyright (C) 2010 Aymeric Mansoux, Marloes de Valk, Dave Griffiths
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program 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 Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

#lang scheme

(require
 "../web/scripts/utils.ss"
 "../web/scripts/sql.ss"
 "ktv.ss"
 "ktv-list.ss"
 "entity-values.ss"
 "entity-get.ss")

(provide (all-defined-out))

(define (csv-titles db table entity-type)
  (foldl
   (lambda (kt r)
     (if (equal? r "") (string-append "\"" (ktv-key kt) "\"")
         (string-append r ", \"" (ktv-key kt) "\"")))
   "id "
   (get-attribute-ids/types db table entity-type)))

dave griffiths's avatar
dave griffiths committed
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
(define (csv-old db table entity-type)
  (let ((s (db-select
         db (string-append
             "select entity_id, unique_id from "
             table "_entity where entity_type = ?") entity-type)))
    (msg "CSV ------------------------------>" entity-type)
    (msg s)
    (if (null? s)
	;; nothing here, just return titles
	(csv-titles db table entity-type)
	(foldl
	 (lambda (res r)
	   (msg res)
	   (let ((entity (get-entity-for-csv db table (vector-ref res 0))))
	     (string-append
	      r "\n"
	      (foldl
	       (lambda (ktv r)
		 (msg ktv)
		 (cond
		  ((equal? (ktv-key ktv) "unique_id") r)
		  ((null? (ktv-value ktv))
		   (msg "value not found in csv for " (ktv-key ktv))
		   (string-append r ", NULL"))
		  ;; dereferences lists of ids
		  ((and
		    (> (string-length (ktv-key ktv)) 8)
		    (equal? (substring (ktv-key ktv) 0 8) "id-list-"))
		   (let ((ids (string-split (ktv-value ktv) '(#\,))))
		     (if (null? ids)
			 (string-append r ", \"\"")
			 (string-append r ", \"" (get-entity-names db "sync"  "\"")))))
		  ;; look for unique ids and dereference them
		  ((and
		    (> (string-length (ktv-key ktv)) 3)
		    (equal? (substring (ktv-key ktv) 0 3) "id-")
		    (not (equal? (ktv-value ktv) "none")))
		   (msg "looking up name")
		   (msg ktv)
		   (let ((name (get-entity-name db "sync" (ktv-value ktv))))
		     (if (null? name)
			 "\"nobody\""
			 (string-append r ", \"" name "\""))))
		  (else
		   (string-append r ", \"" (stringify-value-url ktv) "\""))))
	       (vector-ref res 1) ;; unique_id
	       entity))))
	 (csv-titles db table entity-type)
	 (cdr s)))))

Dave Griffiths's avatar
Dave Griffiths committed
86
(define (csv db table entity-type)
dave griffiths's avatar
dave griffiths committed
87
  (let ((s (db-select
Dave Griffiths's avatar
Dave Griffiths committed
88 89
         db (string-append
             "select entity_id, unique_id from "
dave griffiths's avatar
dave griffiths committed
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
             table "_entity where entity_type = ?") entity-type)))
    (msg "CSV ------------------------------>" entity-type)
    (if (null? s)
	;; nothing here, just return titles
	(csv-titles db table entity-type)
	(foldl
	 (lambda (res r)
	   (let ((entity (get-entity-for-csv db table (vector-ref res 0))))
	     (string-append
	      r "\n"
	      (foldl
	       (lambda (ktv r)
		 (cond
		  ((equal? (ktv-key ktv) "unique_id") r)
		  ((null? (ktv-value ktv))
		   (msg "value not found in csv for " (ktv-key ktv))
		   (string-append r ", NULL"))
		  ;; dereferences lists of ids
		  (else
		   (string-append r ", \"" (stringify-value-url ktv) "\""))))
	       (vector-ref res 1) ;; unique_id
	       entity))))
	 (csv-titles db table entity-type)
	 (cdr s)))))

;; exporting human editable reports

(define (deref-entity db entity)
  (foldl
   (lambda (ktv r)
     (append
      r
      (list
       (ktv-key ktv)
       (cond
        ;; dereferences lists of ids
        ((and
          (> (string-length (ktv-key ktv)) 8)
          (equal? (substring (ktv-key ktv) 0 8) "id-list-"))
         (get-entity-names db "sync" (string-split (ktv-value ktv) '(#\,))))
        ;; look for unique ids and dereference them
        ((and
          (> (string-length (ktv-key ktv)) 3)
          (equal? (substring (ktv-key ktv) 0 3) "id-"))
         (get-entity-name db "sync" (ktv-value ktv)))
        (else
         (ktv-value ktv))))))
   '()
   entity))


(define (csv-convert col)
  (if (number? col) (number->string col)
      (if (string? col) col
          (begin
            (msg "csvify found:" col) "oops"))))

;; convert list of lists into comma seperated columns
;; and newline seperated rows
(define (csvify l)
  (foldl
   (lambda (row r)
     (let ((row-text
            (foldl
             (lambda (col r)
               (let ((converted (csv-convert col)))
                 (if (equal? r "")
                     converted
                     (string-append r ", " converted))))
             "" row)))
       (msg row-text)
       (string-append r row-text "\n")))
   "" l))


(define (ktv-filter ktv-list key)
  (filter
   (lambda (ktv)
     (not (equal? (ktv-key ktv) key)))
   ktv-list))

(define (ktv-filter-many ktv-list key-list)
  (foldl
   (lambda (key r)
     (ktv-filter r key))
   ktv-list
   key-list))

;; meant to be general, but made for pup focal reports
;(define (export-csv db table parent-entity entity-types)
;  (let* ((focal (get-entity db "sync" (get-entity-id db "sync" (ktv-get parent-entity "id-focal-subject"))))
;         (pack (get-entity db "sync" (get-entity-id db "sync" (ktv-get focal "pack-id")))))
;    (csvify
;     (cons
;      '("time" "user" "pack" "subject" "observation type" "key" "value" "key" "value")
;      (sort
;       (foldl
;        (lambda (entity-type r)
;          (append
;           r (map
;              (lambda (entity)
;                (append
;                 (list
;                  (ktv-get entity "time")
;                  (ktv-get entity "user")
;                 (ktv-get pack "name")
;                 (ktv-get focal "name")
;                 entity-type)
;                 (deref-entity 
;		  db (ktv-filter-many
;		      entity (list "user" "unique_id" "parent" "time")))))
;              (db-all-with-parent
;               db table entity-type
;               (ktv-get parent-entity "unique_id")))))
;        '()
;        entity-types)
;      (lambda (a b)
;        (string<? (car a) (car b))))))))