summaryrefslogtreecommitdiff
path: root/models/nutrient-value.rkt
diff options
context:
space:
mode:
authorMarius Peter <dev@marius-peter.com>2025-12-06 17:44:46 +0100
committerMarius Peter <dev@marius-peter.com>2025-12-06 17:50:39 +0100
commitc7ec194f4a6c3dc879c24d8075bfe05a7687f976 (patch)
treeba2ed86e217d32cdb5f561c1a89610be1eaef139 /models/nutrient-value.rkt
parent92dbc8cba30b5e0f300fb4f98d465b3a0eec16db (diff)
Introduce nutrient-value module.
Diffstat (limited to 'models/nutrient-value.rkt')
-rw-r--r--models/nutrient-value.rkt39
1 files changed, 39 insertions, 0 deletions
diff --git a/models/nutrient-value.rkt b/models/nutrient-value.rkt
new file mode 100644
index 0000000..b5798db
--- /dev/null
+++ b/models/nutrient-value.rkt
@@ -0,0 +1,39 @@
+#lang racket
+
+(provide nutrient-value?
+ maybe-nutrient-value?
+ nutrient-value-hash/c
+ (contract-out [insert-nutrient-values
+ (-> connection? db-id? nutrient-value-hash/c (listof (cons/c symbol? any/c)))]
+ [residuals->nutrient-value-hash
+ (-> (listof residual-vector/c) nutrient-value-hash/c)]))
+
+(require db
+ sql
+ "nutrient.rkt"
+ "utils.rkt")
+
+(define nutrient-value? (and/c real? (>=/c 0)))
+(define maybe-nutrient-value? (or/c nutrient-value? #f))
+(define nutrient-value-hash/c (hash/c nutrient? nutrient-value? #:immutable #t))
+
+;; vector/c id, canonical name, french name, nutrient formula, value (ppm)
+(define residual-vector/c (vector/c db-id? string? string? string? real?))
+
+(define (insert-nutrient-values conn nvs-id nutrient-values)
+ (define nv-rows
+ (for/list ([(n v) (in-hash nutrient-values)])
+ (map value->scalar-expr-ast (list nvs-id (nutrient-id n) v))))
+ (define result
+ (query conn
+ (insert #:into nutrient_values
+ #:columns value_set_id
+ nutrient_id
+ value_ppm
+ #:from (TableExpr:AST ,(make-values*-table-expr-ast nv-rows)))))
+ (simple-result-info result))
+
+(define (residuals->nutrient-value-hash residuals)
+ (for/hash ([r (in-list residuals)])
+ (match-define (vector n-id n-canonical-name n-french-name n-formula value-ppm) r)
+ (values (nutrient n-id n-canonical-name n-french-name n-formula) value-ppm)))
Copyright 2019--2026 Marius PETER