[Racket] Ferti hydroponic nutrient solver, redux.
Update crop rotation migration and model.
Changed files
db/migrations.rkt
@@ -148,12 +148,7 @@
148
148
#:columns [id integer #:not-null]
149
149
;; ISO8601 date
150
150
[rotation_date text #:not-null]
151
Removed:
[nutrient_measurement_id integer]
152
151
#:constraints (primary-key id)
153
Removed:
(foreign-key nutrient_measurement_id
154
Removed:
#:references (nutrient_measurements id)
155
Removed:
#:on-delete
156
Removed:
#:set-null)
157
152
(unique rotation_date))))
158
153
159
154
(define-migration
models/crop-rotation.rkt
@@ -4,12 +4,9 @@
4
4
crop-rotation?
5
5
crop-rotation-id
6
6
(rename-out [crop-rotation-rotation-date crop-rotation-date]
7
Removed:
[crop-rotation-requirement-proportions crop-rotation-requirements]
8
Removed:
[crop-rotation-nutrient-measurement-id crop-rotation-measurement-id])
7
Added:
[crop-rotation-requirement-proportions crop-rotation-requirements])
9
8
(contract-out [create-crop-rotation!
10
Removed:
(->* (string? requirement-proportion-hash/c)
11
Removed:
(#:nutrient-measurement exact-nonnegative-integer?)
12
Removed:
crop-rotation?)]
9
Added:
(-> string? requirement-proportion-hash/c crop-rotation?)]
13
10
[get-crop-rotations (-> (listof crop-rotation?))]
14
11
[get-crop-rotation
15
12
(->* () (#:id crop-rotation-id? #:date string?) (or/c crop-rotation? #f))]
@@ -23,13 +20,7 @@
23
20
"nutrient.rkt"
24
21
"crop-requirement.rkt")
25
22
26
Removed:
(struct crop-rotation (id rotation-date requirement-proportions nutrient-measurement-id)
27
Removed:
#:transparent
28
Removed:
#:guard (λ (id rotation-date requirement-proportions nutrient-measurement-id _)
29
Removed:
(values id
30
Removed:
rotation-date
31
Removed:
requirement-proportions
32
Removed:
(if (sql-null? nutrient-measurement-id) #f nutrient-measurement-id))))
23
Added:
(struct crop-rotation (id rotation-date requirement-proportions) #:transparent)
33
24
34
25
(define crop-rotation-id? exact-nonnegative-integer?)
35
26
(define crop-rotation-or-id/c (or/c crop-rotation? crop-rotation-id?))
@@ -38,23 +29,15 @@
38
29
(define (->cr-id cr-or-id)
39
30
(match cr-or-id
40
31
[(? crop-rotation-id? id) id]
41
Removed:
[(crop-rotation id _ _ _) id]
32
Added:
[(crop-rotation id _ _) id]
42
33
[#f (error '->nt-id "#f can not be converted to an id")]))
43
34
44
35
;; CREATE
45
36
46
Removed:
(define (create-crop-rotation! rotation-date
47
Removed:
requirement-proportions
48
Removed:
#:nutrient-measurement [nutrient-measurement-id #f])
37
Added:
(define (create-crop-rotation! rotation-date requirement-proportions)
49
38
(or (get-crop-rotation #:date rotation-date)
50
39
(with-tx
51
Removed:
(if nutrient-measurement-id
52
Removed:
(query-exec (current-conn)
53
Removed:
(insert #:into crop_rotations
54
Removed:
#:set [rotation_date ,rotation-date]
55
Removed:
[nutrient_measurement_id ,nutrient-measurement-id]))
56
Removed:
(query-exec (current-conn)
57
Removed:
(insert #:into crop_rotations #:set [rotation_date ,rotation-date])))
40
Added:
(query-exec (current-conn) (insert #:into crop_rotations #:set [rotation_date ,rotation-date]))
58
41
(define cr-id
59
42
(query-value (current-conn)
60
43
(select id #:from crop_rotations #:where (= rotation_date ,rotation-date))))
@@ -76,27 +59,23 @@
76
59
(define (residuals->requirement-proportion-hash residuals)
77
60
(for/hash ([r (in-list residuals)])
78
61
(match-define (vector requirement-id proportion) r)
79
Removed:
(values requirement-id proportion)))
62
Added:
(values (get-crop-requirement #:id requirement-id) proportion)))
80
63
81
64
(define (grouped-row->crop-rotation grouped-row)
82
Removed:
(match-define (vector cr-id cr-rotation-date cr-nutrient-measurement-id residuals) grouped-row)
83
Removed:
(crop-rotation cr-id
84
Removed:
cr-rotation-date
85
Removed:
(residuals->requirement-proportion-hash residuals)
86
Removed:
cr-nutrient-measurement-id))
65
Added:
(match-define (vector cr-id cr-rotation-date residuals) grouped-row)
66
Added:
(crop-rotation cr-id cr-rotation-date (residuals->requirement-proportion-hash residuals)))
87
67
88
68
(define (get-crop-rotations)
89
69
(define grouped-rows
90
70
(query-rows (current-conn)
91
71
(select cr.id
92
72
cr.rotation_date
93
Removed:
cr.nutrient_measurement_id
94
73
crr.crop_requirement_id
95
74
crr.proportion_percent
96
75
#:from (TableExpr:AST ,joined)
97
76
#:order-by cr.rotation_date
98
77
#:desc)
99
Removed:
#:group '#(0 1 2)))
78
Added:
#:group '#(0 1)))
100
79
(map grouped-row->crop-rotation grouped-rows))
101
80
102
81
(define (get-crop-rotation #:id [cr-id #f] #:date [rotation-date #f])
@@ -111,14 +90,13 @@
111
90
(query-rows (current-conn)
112
91
(select cr.id
113
92
cr.rotation_date
114
Removed:
cr.nutrient_measurement_id
115
93
crr.crop_requirement_id
116
94
crr.proportion_percent
117
95
#:from (TableExpr:AST ,joined)
118
96
#:where (ScalarExpr:AST ,where)
119
97
#:order-by cr.rotation_date
120
98
#:desc)
121
Removed:
#:group '#(0 1 2)))
99
Added:
#:group '#(0 1)))
122
100
(match grouped-rows
123
101
['() #f]
124
102
[(list grouped-row) (grouped-row->crop-rotation grouped-row)]