forked from PWD-MARS/shiny-fieldwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.R
More file actions
1611 lines (1395 loc) · 82.9 KB
/
Copy pathdeploy.R
File metadata and controls
1611 lines (1395 loc) · 82.9 KB
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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Deploy Sensor tab
#Left Sidebar with 3 tables: Future, Active, and Past Deployments
#4/29/21 adding section headers
#Not in logical
`%!in%` <- Negate(`%in%`)
#1.0 UI -----
deployUI <- function(id, label = "deploy", sensor_serial, site_name, site_names, html_req, long_term_lookup, html_warn,
deployment_lookup, research_lookup, priority, future_req, sensor_issue_lookup){
#namespace initialization
ns <- NS(id)
#set this up as a list so it can be read into the app.R UI correctly
list(
tabPanel("Deploy Sensor", value = "deploy_tab",
titlePanel("Deploy Sensor Tab"),
fluidRow(
#1.1 Sidebar ----
#set width of column where sidebar is, relative to full screen
column(width = 4,
sidebarPanel(width = 12,
#put some empty space on top so the calendar shows enough
fluidRow(h5()),
fluidRow(h5()),
fluidRow(
#selectizeInput for smps. server side is fast so this starts empty
#placeholder is necessary
column(6,selectizeInput(ns("smp_id"), future_req(html_req("SMP ID")),
choices = NULL,
options = list(
placeholder = 'Select an Option',
onInitialize = I('function() { this.setValue(""); }')
))),
column(6,selectInput(ns("site_name"), future_req(html_req("Site Name")),
choices = c("", site_names), selected = NULL))),
#If there is no deployment date, given an option for future test priority
conditionalPanel(condition = "input.deploy_date === null",
ns = ns,
fluidRow(
column(6, dateInput(ns("premonitoring_date"), "Pre-Monitoring Inspection Date",
value = as.Date(NA))),
column(6, selectInput(ns("priority"), "Future Test Priority",
choices = c("", priority$field_test_priority), selected = NULL))
)),
selectInput(ns("ready"), "Ready for Deployment?",
choices = c("", "Yes" = 1, "No" = 0, "Unknown" = FALSE), selected = "Unknown")
,
fluidRow(
column(6,selectInput(ns("well_name"), future_req(html_req("Location")),
choices = "")),
column(6,selectInput(ns("sensor_id"), html_req("Sensor ID"),
choices = c("", sensor_serial), selected = NULL))),
fluidRow(
column(6, selectInput(ns("sensor_purpose"), html_req("Sensor Purpose"), choices = c("", deployment_lookup$type), selected = NULL)),
column(6, selectInput(ns("term"), html_req("Term"), choices = c("", long_term_lookup$type), selected = NULL))),
fluidRow(
column(6, selectInput(ns("research"), "Research", choices = c("", research_lookup$type), selected = NULL)),
column(6, selectInput(ns("interval"), html_req("Interval (min)"), choices = c("", 5, 15), selected = NULL))),
fluidRow(
column(6,disabled(dateInput(ns("deploy_date"), html_req("Deployment Date"), value = as.Date(NA)))),
column(6,disabled(dateInput(ns("collect_date"), "Collection Date", value = as.Date(NA))))),
#ask about download error and redeploying sensor once you add a collection date
conditionalPanel(width = 12,
condition = "input.deploy_date && input.sensor_purpose != \"BARO\"",
ns = ns,
HTML("If well is dry, enter 0")),
#ask about manual depth to water measurement during deployment
fluidRow(column(6,
conditionalPanel(width = 12,
condition = "input.deploy_date && input.sensor_purpose != \"BARO\"",
ns = ns,
numericInput(ns("deploy_depth_to_water"),
"Deployment Depth-to-Water (ft)", value = NA, min = 0))),
column(6,
#ask about manual depth to water measurement during collection
conditionalPanel(width = 12,
condition = "input.collect_date && input.sensor_purpose != \"BARO\"",
ns = ns,
numericInput(ns("collect_depth_to_water"),
"Collection Depth-to-Water (ft)", value = NA, min = 0),
))),
#ask if there is a download error
conditionalPanel(width = 12,
condition = "input.collect_date",
ns = ns,
selectInput(ns("download_error"), html_req("Did you encounter a sensor issue?"),
choices = c("", "No" = 0, "Yes" = 1), selected = NULL)),
#if there is a download error, ask if the sensor is broken
conditionalPanel(width = 12,
condition = "input.download_error == '1'",
ns = ns,
checkboxInput(ns("sensor_broken"), "Take Sensor Out of Service?")),
#if sensor is out of service, ask what the issues are and if data should be requested
conditionalPanel(width = 12,
ns = ns,
condition = "input.sensor_broken",
selectInput(ns("issue_one"), html_req("Issue #1"),
choices = c("", sensor_issue_lookup$sensor_issue), selected = NULL),
selectInput(ns("issue_two"), "Issue #2",
choices = c("", sensor_issue_lookup$sensor_issue), selected = NULL),
checkboxInput(ns("request_data"), "Request Data Be Retrieved and Sent to PWD")),
#if there is a collection date, ask if user wants to redeploy
conditionalPanel(width = 12,
condition = "input.collect_date",
ns = ns,
checkboxInput(ns("redeploy"), "Redeploy?"),
h6("Redeploy a sensor in the same well on this collection date")),
#if redeploying and the sensor is broken, put in a new sensor id
conditionalPanel(width = 12,
condition = "input.redeploy & input.sensor_broken",
ns = ns,
selectInput(ns("new_sensor_id"), html_req("New Sensor ID"),
choices = c("", sensor_serial), selected = NULL),
textOutput(ns("new_sensor_warning"))),
#notes for the future deployment or current/past deployment
textAreaInput(ns("notes"), "Notes"),
#if redeploying, enter notes for the new deployment
conditionalPanel(width = 12,
condition = "input.redeploy",
ns = ns,
textAreaInput(ns("redeployment_notes"), "New Deployment Notes")),
#if there is no deployment date, show the "Add Future Deployment" button
conditionalPanel(condition = "input.deploy_date === null",
ns = ns,
actionButton(ns("future_deploy"), "Add Future Deployment"),
actionButton(ns("delete_future"), "Delete Future Deployment")),
htmlOutput(ns("deploy_warning")),
actionButton(ns("deploy_sensor"), "Deploy Sensor"),
actionButton(ns("clear_deploy_fields"), "Clear All Fields"),
#Debug button
#actionButton(ns("BrowserButton"), "Click to Browse"),
#note about requirements
fluidRow(column(12,
HTML(paste(html_req(""), " indicates required field for complete tests.")))),
fluidRow(column(12,
HTML(paste(future_req(""), " indicates required field for future tests.")))),
fluidRow(column(12,
HTML(paste("Check the \"Add Sensor\" tab to see if the Sensor ID you are trying to deploy is actively deployed elsewhere."))))
),
),
#1.2 Tables ----
column(width = 8,
#show tables when an smp id is selected
conditionalPanel(condition = "input.smp_id || input.site_name",
ns = ns,
h4(textOutput(ns("future_text"))),
DTOutput(ns("future_deployment")),
h4(textOutput(ns("active_text"))),
DTOutput(ns("current_deployment")),
h4(textOutput(ns("previous_text"))),
DTOutput(ns("prev_deployment"))
)
)
)
)
)
}
#2.0 Server---------
deployServer <- function(id, parent_session, ow, collect, sensor, poolConn, deployment_lookup, srt, si, cwl_history, smp_id, sensor_issue_lookup, newstyleEqual){
moduleServer(
id,
function(input, output, session){
#2.0.1 Set up ----
#define ns to use in modals
ns <- session$ns
#start reactiveValues for this section/tab
rv <- reactiveValues()
#update SMP IDs
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = character(0), server = TRUE)
#2.0.2 Some dependent inputs ----
#get a site name UID if a site name is selected
rv$site_name_lookup_uid_step <- reactive(odbc::dbGetQuery(poolConn, paste0("select site_name_lookup_uid from fieldwork.tbl_site_name_lookup where site_name = '", input$site_name, "'")) %>% pull())
rv$site_name_lookup_uid <- reactive(if (nchar(input$site_name) > 0) paste0("'", rv$site_name_lookup_uid_step(), "'") else "NULL")
#set smp id to NULL if using site name
rv$smp_id <- reactive(if (nchar(input$smp_id) > 0) paste0("'", input$smp_id, "'") else "NULL")
## well panel
#update sensor id choices based on the hobo list
observe(updateSelectInput(session, inputId = "sensor_id", choices = c("", sensor$sensor_serial()), selected = NULL))
#query ow_suffixes based on smp_id
rv$ow_suffixes <- reactive(odbc::dbGetQuery(poolConn, paste0(
"select ow_suffix from fieldwork.tbl_ow where smp_id = ", rv$smp_id(), " OR site_name_lookup_uid = ", rv$site_name_lookup_uid())) %>% dplyr::pull())
observe(updateSelectInput(session, "well_name", choices = c("", rv$ow_suffixes())))
#set minimum collection date to be same day as deployment date
observe(updateDateInput(session, "collect_date", min = input$deploy_date))
#2.0.3 Tab Name ----
tab_name <- "Deploy Sensor"
#2.1 Coming from other Modules -----------
#upon clicking "deploy at this smp" in add_ow
observeEvent(ow$refresh_deploy(), {
#using shinyjs::reset is too slow and will reset after the selection is updated to the desired SMPID
#want to set this to NULL so that it refreshes the location query but that is not working (1/11/21)
#needs to be set as "", not NULL (also 1/11/21)
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = ow$smp_id(), server = TRUE)
})
#upon adding or removing a site in add_ow
observeEvent(ow$site_name_db(), {
updateSelectInput(session, "site_name", choices = c("", ow$site_name_db()$site_name))
})
#upon clicking "deploy this sensor" in add_sensor
observeEvent(sensor$refresh_serial_no(), {
updateSelectInput(session, "sensor_id", selected = sensor$serial_no())
})
#upon clicking through the dialogue box in srt tab
#add -1 to convert from system to smp lol
#update the smp id and term
observeEvent(srt$refresh_deploy(), {
if(srt$refresh_deploy() > 0){
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = paste0(srt$system_id(), "-1"), server = TRUE)
updateSelectInput(session, "term", selected = "SRT")
}
})
#upon clicking through the dialogue box in SI tab
#update the (smp_id or site name) and term
observeEvent(si$refresh_deploy(), {
if(si$refresh_deploy() > 0){
if(nchar(si$system_id()) > 0){
updateSelectInput(session, "site_name", selected = NULL)
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = paste0(si$system_id(), "-1"), server = TRUE)
updateSelectInput(session, "term", selected = "Special")
}else if(nchar(si$site_name()) > 0){
updateSelectInput(session, "site_name", selected = NULL)
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectInput(session, "site_name", selected = si$site_name())
updateSelectInput(session, "term", selected = "Special")
}
}
})
#upon clicking a row in collection_calendar
observeEvent(collect$deploy_refresh(), {
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectInput(session, "site_name", selected = "")
# need to get through the initial load where length == 0 for collection calendar smp_id (is.na does not work in that case)
# go to either smp id or site name
if(length(collect$cal_smp_id()) > 0){
if(!is.na(collect$cal_smp_id())){
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = collect$cal_smp_id(), server = TRUE)
}else{
updateSelectInput(session, "site_name", selected = collect$site_name())
}
#delay so that the selectizeInput is updated and table is queried before it is searched by R
delay(250,{
rv$active_row <- reactive(which(rv$active_table_db()$deployment_uid == collect$row(), arr.ind = TRUE))
dataTableProxy('current_deployment') %>% selectRows(rv$active_row())
})
}
})
#upon click a row in future table (in collection calendar)
observeEvent(collect$future_deploy_refresh(), {
#print(collect$future_smp_id())
if(length(collect$future_smp_id()) > 0){
if(!is.na(collect$future_smp_id())){
updateSelectInput(session, "site_name", selected = "")
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = collect$future_smp_id(), server = TRUE)
}else{
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectInput(session, "site_name", selected = collect$future_site_name())
}
#delay so that the selectizeInput is updated and table is quereied before it is searched by R
delay(250, {
rv$future_row <- reactive(which(rv$future_table_db()$future_deployment_uid == collect$future_row(), arr.ind = TRUE))
dataTableProxy('future_deployment') %>% selectRows(rv$future_row())
})
}
})
#upon clicking a row in current sites (in history)
observeEvent(cwl_history$active_deploy_refresh(), {
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectInput(session, "site_name", selected = "")
#make sure this exists
#"missing" function might help
#changing order might help
if(length(cwl_history$active_smp_id()) > 0){
if(!is.na(cwl_history$active_smp_id())){
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = cwl_history$active_smp_id(), server = TRUE)
}else{
updateSelectInput(session, "site_name", selected = cwl_history$active_site_name())
}
}
}
)
#upon clicking a row in past sites (in history)
observeEvent(cwl_history$past_deploy_refresh(), {
updateSelectizeInput(session, "smp_id", selected = character(0))
updateSelectInput(session, "site_name", selected = "")
if(length(cwl_history$past_smp_id()) > 0){
if(!is.na(cwl_history$past_smp_id())){
updateSelectizeInput(session, "smp_id", choices = smp_id, selected = cwl_history$past_smp_id(), server = TRUE)
}else{
updateSelectInput(session, "site_name", selected = cwl_history$past_site_name())
}
}
}
)
#2.2 back to this tab ----------\
# Debugging Button
# observeEvent(input$BrowserButton,
# {browser()})
#2.2.1 toggle state depending on inputs
#toggle to make sure that only of SMP ID or Site Name is selected
observe(toggleState("smp_id", condition = nchar(input$site_name) == 0))
observe(toggleState("site_name", condition = nchar(input$smp_id) == 0))
#toggle label for redeployment
rv$redeploy_label <- reactive(if(input$sensor_broken == TRUE){
"Redeploy with Different Sensor"
}else{
"Redeploy with Same Sensor"
})
observe(updateCheckboxInput(session, "redeploy", label = rv$redeploy_label()))
#toggle labels for depth to water / water depth
rv$deploy_depth_to_water_label <- reactive(if(str_detect(input$well_name, "^SW")){
"Deployment Water Depth (ft)"
}else{
"Deployment Depth-to-Water (ft)"
})
rv$collect_depth_to_water_label <- reactive(if(str_detect(input$well_name, "^SW")){
"Collection Water Depth (ft)"
}else{
"Collection Depth-to-Water (ft)"
})
observe(updateNumericInput(session, "deploy_depth_to_water", label = rv$deploy_depth_to_water_label()))
observe(updateNumericInput(session, "collect_depth_to_water", label = rv$collect_depth_to_water_label()))
#toggle label active/previous deployment notes
rv$notes_label <- reactive(if(length(input$collect_date) == 0){
"Notes"
}else{
"Completed Deployment Notes"
})
#toggle future deployment delete button
observe({toggleState(id = "deploy_date", condition = input$ready == 1 )})
observe({toggleState(id = "collect_date", condition = input$ready == 1 )})
observe({toggleState(id = "priority", condition = input$ready != 0)})
observeEvent(input$ready, {
if(input$ready == 0){
updateSelectInput(session, "priority", selected = "Dependent (see Notes)")
updateSelectInput(session, "notes", label = "Note why deployment is not ready")
}else{
updateSelectInput(session, "notes", label = "Notes")
}
})
observe(updateTextAreaInput(session, "notes", label = rv$notes_label()))
#2.2.1 Headers -----
#Get the Project name, combine it with SMP ID, and create a reactive header
rv$smp_and_name_step <- reactive(odbc::dbGetQuery(poolConn, paste0("select smp_id, project_name from external.mat_project_names where smp_id = '", input$smp_id, "'")))
rv$smp_and_name <- reactive(paste(rv$smp_and_name_step()$smp_id[1], rv$smp_and_name_step()$project_name[1]))
rv$future_text <- reactive(if(nchar(input$smp_id) > 0){
paste("Future Deployments at", rv$smp_and_name())
}else{
paste("Future Deployments at", input$site_name)
})
output$future_text <- renderText(
rv$future_text()
)
rv$active_text <- reactive(if(nchar(input$smp_id) > 0){
paste("Active Deployments at", rv$smp_and_name())
}else{
paste("Active Deployments at", input$site_name)
})
output$active_text <- renderText(
rv$active_text()
)
rv$previous_text <- reactive(if(nchar(input$smp_id) > 0){
paste("Previous Deployments at", rv$smp_and_name())
}else{
paste("Previous Deployments at", input$site_name)
})
output$previous_text <- renderText(
rv$previous_text()
)
#2.2.2 Deploy warning -----
rv$deploy_logic <- reactive(
if(
### Condition 1: Check if all the required values are populated
#smp id or site name populated
!((nchar(input$smp_id) > 0 | nchar(input$site_name) > 0) &
#well name and sensor id populated OR sensor purpose is "DATALOGGER" for special weather station (i.e. Phila Zoo)
nchar(input$well_name) > 0 & (nchar(input$sensor_id) > 0 | input$sensor_purpose == "DATALOGGER") &
# sensor input chosen
nchar(input$sensor_purpose) > 0 &
# interval input chosen
nchar(input$interval) > 0 &
# deploy date is chosen
length(input$deploy_date) > 0 &
# term input chosen
nchar(input$term) > 0 &
# no download error
rv$req_dl_error() &
# sensor is not broken, or if it is broken a value is populated
rv$sensor_break()
)) {
# error code
# "e1"
paste0("Populate all required inputs to add new deployment.")
}
else if(
# Condition 2: no download error
nchar(input$download_error) > 0 &&
input$download_error == 1){
# error code
# "e2"
paste0("Can not redploy: Sensor Encountered an issue.")
}
# else if(
# # Condition 3: sensor is broken, or if it is broken a value is populated
# nchar(input$download_error) > 0){
# # error code
# # "e3"
# paste0("Sensor has a download error.")
# }
else if(
#browser()
## Condition 4a: sensor is not currently deployed; or if it is, the deployment does not overlap the current sensor use
# sensor is deployed AND
(input$sensor_id %in% collect$sensor_serial() & nchar(input$smp_id) > 0) &&
# It is not deployed at our current site AND
# smp is within the collection list
((!(is.na(collect$smp_id()[which(collect$sensor_serial() == input$sensor_id)])) &&
input$smp_id != collect$smp_id()[which(collect$sensor_serial() == input$sensor_id)]) ||
# site name is within the collection list
(!(is.na(collect$site_names()[which(collect$sensor_serial() == input$sensor_id)])) &&
input$site_name != collect$site_names()[which(collect$sensor_serial() == input$sensor_id)])) &&
# We are not trying to backfill deployment records for a currently deployed sensor
# check deploy
((length(input$deploy_date) > 0 &&
input$deploy_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)]) ||
# check collect
(length(input$collect_date) > 0 &&
input$collect_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)])))
{
#error code
# "e4"
paste0("Sensor chosen is deployed at another location during the selected timeframe.")
}
else if(
## Condition 4b: sensor is not currently deployed; or if it is, the deployment does not overlap the current sensor use
# sensor is deployed AND
(input$sensor_id %in% collect$sensor_serial() & nchar(input$site_name) > 0) &&
# It is not deployed at our current site AND
((!(is.na(collect$site_names()[which(collect$sensor_serial() == input$sensor_id)])) &&
input$smp_id != collect$smp_id()[which(collect$sensor_serial() == input$sensor_id)]) ||
(!(is.na(collect$site_names()[which(collect$sensor_serial() == input$sensor_id)])) &&
input$site_name != collect$site_names()[which(collect$sensor_serial() == input$sensor_id)])) &&
# We are not trying to backfill deployment records for a currently deployed sensor
# check deploy
((length(input$deploy_date) > 0 &&
input$deploy_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)]) ||
# check collect
(length(input$collect_date) > 0 &&
input$collect_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)])))
{
#error code
# "e4"
paste0("Sensor chosen is deployed at another location during the selected timeframe.")
# print("Sensor chosen is deployed at another location during the selected timeframe.")
}
else if(
## Condition 5 - collection date is populated, redeploy is true, and sensor is not currently deployed
(rv$redeploy() & !(input$sensor_id %in% collect$sensor_serial()))
){
# error code
# "e5"
paste0("The senor chosen is not currently deployed at this well.")
# print("The senor chosen is not currently deployed at this well.")
}
# This is handled within the button toggle logic
# else if(
# ## Condition 6 - a current deployment is selected (this allows redeployment)
# (length(input$current_deployment_rows_selected) > 0 & length(input$collect_date) > 0 &&
# input$redeploy != TRUE)
# ){
# # error code
# # "e6"
# paste0("Must select redeploy to deploy sensor again.")
# # print("Must select redeploy to deploy sensor again.")
#
# }
else if(
## Condition 7 - a previous deployment is selected (this allows redeployment)
length(input$prev_deployment_rows_selected) > 0 & length(input$collect_date) > 0
){
# error code
# "e7"
paste0("Must select redeploy to deploy sensor again.")
# print("Must select redeploy to deploy sensor again.")
}
else if(
# Condition 8 - a sensor is deployed at a different well at this SMP
# sensor is currently deployed
(nchar(input$smp_id) > 0 & input$sensor_id %in% collect$sensor_serial()) &&
# sensor is at the current SMP BUT NOT the current well
(input$smp_id == collect$smp_id()[which(collect$sensor_serial() == input$sensor_id)] &
!(input$well_name %in% rv$active_table_db()$ow_suffix[which(rv$active_table_db()$sensor_serial == input$sensor_id)])) &&
# We are not trying to backfill deployment records for a currently deployed sensor
# check deploy
(((length(input$deploy_date) > 0) && (input$deploy_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)])) ||
# check collect
((length(input$collect_date) > 0) && (input$collect_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)]))))
{
#error code
# "e8"
paste0("The sensor chosen is deployed at another well at this SMP.")
# print("The sensor chosen is deployed at another well at this SMP.")
}
else if(
# Condition 9 - a sensor is deployed at a different well at this site.
# sensor is currently deployed
(nchar(input$site_name) > 0 & input$sensor_id %in% collect$sensor_serial()) &&
# sensor is at the current site BUT NOT the current well
(input$site_name == collect$site_names()[which(collect$sensor_serial() == input$sensor_id)] &
!(input$well_name %in% rv$active_table_db()$ow_suffix[which(rv$active_table_db()$sensor_serial == input$sensor_id)])) &&
# We are not trying to backfill deployment records for a currently deployed sensor
# check deploy
(((length(input$deploy_date) > 0) && (input$deploy_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)])) ||
# check collect
((length(input$collect_date) > 0) && (input$collect_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)]))))
{
#error code
# "e9"
paste0("The sensor chosen is deployed at another well at this site.")
# print("The sensor chosen is deployed at another well at this site.")
}
else if(
# Condition 10 - a sensor is NOT IN GOOD ORDER (the horror!)
input$sensor_id %in% rv$sensor_dt()$sensor_serial &&
rv$sensor_dt()[which(rv$sensor_dt()$sensor_serial == input$sensor_id),]$sensor_status != "Good Order"
)
{
#error code
# "e10"
paste0("The sensor chosen is not in Good Order and may not be deployed.")
}
else{
# "e0"
# paste0("Everything is good")
# print("cowabunga")
}
)
output$deploy_warning <- renderText(paste("<b> <p style = \"color:red\";>", rv$deploy_logic(), "</p> </b>"))
#2.2.3 Toggle states based on inputs -----
#enable/disable deploy sensor button based on whether all inputs (except collect date) are not empty
#logical to add to the toggleState below
#require "download error" if collection date is not 0
rv$req_dl_error <- reactive(if(length(input$collect_date) == 0){
TRUE
}else if(nchar(input$download_error) > 0){
TRUE
}else{
FALSE
})
#set sensor purpose to datalogger and disable sensor id if a "DL" is selected
observeEvent(input$well_name, {
if(startsWith(input$well_name, "DL")){
updateSelectInput(session, "sensor_purpose", selected = "DATALOGGER")
updateSelectInput(session, "sensor_id", selected = NULL)
disable("sensor_id")
}else{
enable("sensor_id")
}
})
#new sensor warning
rv$new_sensor_warning <- reactive(if(nchar(input$new_sensor_id) > 0){
if(input$new_sensor_id %in% collect$sensor_serial()){
if(
(input$smp_id != collect$smp_id()[which(collect$sensor_serial() == input$new_sensor_id)] |
(input$smp_id == collect$smp_id()[which(collect$sensor_serial() == input$new_sensor_id)] &
input$well_name != collect$ow_suffix()[which(collect$sensor_serial() == input$new_sensor_id)]))){
"Sensor is deployed at another location. Search Sensor ID in \"Add Sensor\" tab for more info."
}else{
NULL
}}else{
NULL
}}else{
NULL
}
)
output$new_sensor_warning <- renderText(
rv$new_sensor_warning()
)
#check that if sensor is broken, and you are redeploying, a new sensor is specified to activate button
rv$sensor_break <- reactive(if(input$sensor_broken != TRUE){
TRUE
}else if(input$sensor_broken == TRUE &
(input$redeploy != TRUE | (nchar(input$new_sensor_id) > 0 & !(input$new_sensor_id %in% collect$sensor_serial())))){
TRUE
}else if(input$sensor_broken == TRUE & nchar(input$new_sensor_id == 0)){
FALSE
})
#also check to make sure the sensor is not already deployed, or the sensor has a collection date, or a row selected
#allow editing to a sensor already deployed if date, well, and smp match active deployment
observe({toggleState(id = "deploy_sensor", condition =
if(
### Condition 1: Check if all the required values are populated
# smp id or site name populated
(nchar(input$smp_id) > 0 | nchar(input$site_name) > 0) &
# well name and sensor id populated OR sensor purpose is "DATALOGGER" for special weather station (i.e. Phila Zoo)
nchar(input$well_name) > 0 & (nchar(input$sensor_id) > 0 | input$sensor_purpose == "DATALOGGER") &
# sensor input chosen
nchar(input$sensor_purpose) > 0 &
# interval input chosen
nchar(input$interval) > 0 &
# deploy date is chosen
length(input$deploy_date) > 0 &
# data frequency term is chosen
nchar(input$term) > 0 &
# no download error
rv$req_dl_error() &
# sensor is not broken, or if it is broken a value is populated
rv$sensor_break()
) {
# sensor is currently deployed
(!(input$sensor_id %in% collect$sensor_serial() &
# deployment getting added overlaps with current deployment
(ifelse(length(input$deploy_date) > 0,
input$deploy_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)],
FALSE)
|
ifelse(length(input$collect_date) > 0,
input$collect_date >= collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)],
FALSE))
&
# if it is deployed, it's not at the chosen smp
(input$smp_id != collect$smp_id()[which(collect$sensor_serial() == input$sensor_id)] |
# if it is deployed at the chosen smp, it's not at the same location
(input$smp_id == collect$smp_id()[which(collect$sensor_serial() == input$sensor_id)] &
input$well_name %in% rv$active_table_db()$ow_suffix[which(rv$active_table_db()$sensor_serial == input$sensor_id)]
))) |
## B - collection date is populated, redeploy is true, and sensor is not currently deployed
(length(input$collect_date) > 0 & rv$redeploy() == TRUE & !(input$sensor_id %in% collect$sensor_serial()) &
# make sure if currently deployed, the deployment dates are not for an old deployment
(# deployment getting added overlaps with current deployment
ifelse(length(input$deploy_date) > 0,
collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)] < input$deploy_date,
FALSE)
&
ifelse(length(input$collect_date) > 0,
collect$deploy_dates()[which(collect$sensor_serial() == input$sensor_id)] < input$collect_date,
FALSE)
) ) |
## C - a current deployment is selected (this allows redeployment)
length(input$current_deployment_rows_selected) > 0 |
## D - a previous deployment is selected (this allows redeployment)
length(input$prev_deployment_rows_selected) > 0)
}
else{
FALSE
})})
observe({toggleState(id = "future_deploy", condition = (nchar(input$smp_id) > 0 | nchar(input$site_name) > 0) &
nchar(input$well_name) > 0 & nchar(input$ready) > 0) })
#toggle future deployment delete button
observe({toggleState(id = "delete_future", condition = length(rv$future()) > 0 )})
#2.2.4 prepare inputs ------
## sensor panel
#get sensor purpose lookup uid
# select through different method to avoid issue
rv$purpose <- reactive(deployment_lookup %>% dplyr::filter(type == input$sensor_purpose) %>%
select(sensor_purpose_lookup_uid) %>% pull())
# select through different method to avoid issue
# rv$purpose <- reactive(deployment_lookup$sensor_purpose_lookup_uid[deployment_lookup$type == input$sensor_purpose])
#for future deployment - make inputs equal to NULL if blank
rv$purpose_null <- reactive(if(nchar(input$sensor_purpose) == 0) "NULL" else paste0(rv$purpose()))
rv$inventory_sensors_uid <- reactive(odbc::dbGetQuery(poolConn, paste0(
"SELECT inventory_sensors_uid FROM fieldwork.tbl_inventory_sensors WHERE sensor_serial = '", input$sensor_id, "'"
)))
rv$inventory_sensors_uid_null <- reactive(if(nchar(input$sensor_id) == 0) "NULL" else paste0("'", rv$inventory_sensors_uid(), "'"))
rv$interval_min <- reactive(if(nchar(input$interval) == 0) "NULL" else (paste0("'", input$interval, "'")))
rv$deploy_depth_to_water <- reactive(if(is.na(input$deploy_depth_to_water)) "NULL" else paste0("'", input$deploy_depth_to_water, "'"))
rv$collect_depth_to_water <- reactive(if(is.na(input$collect_depth_to_water)) "NULL" else paste0("'", input$collect_depth_to_water, "'"))
#get term UID; make null if blank
rv$term <- reactive(odbc::dbGetQuery(poolConn, paste0("Select long_term_lookup_uid FROM fieldwork.tbl_long_term_lookup WHERE type = '", input$term, "'")) %>% pull)
rv$term_null <- reactive(if(nchar(input$term) == 0) "NULL" else paste0("'", rv$term(), "'"))
#get research uid
rv$research <- reactive(odbc::dbGetQuery(poolConn, paste0("select research_lookup_uid FROM fieldwork.tbl_research_lookup WHERE type = '", input$research, "'")) %>% pull)
#sanitize notes
rv$notes_step1 <- reactive(gsub('\'', '\'\'', input$notes))
rv$notes <- reactive(if(nchar(rv$notes_step1()) == 0) "NULL" else paste0("'", rv$notes_step1(), "'"))
#sanitize deployment notes
rv$redeployment_notes_step1 <- reactive(gsub('\'', '\'\'', input$redeployment_notes))
rv$redeployment_notes <- reactive(if(nchar(rv$redeployment_notes_step1()) == 0) "NULL" else paste0("'", rv$redeployment_notes_step1(), "'"))
rv$download_error <- reactive(if(nchar(input$download_error) == 0 | length(input$collect_date) == 0){
"NULL"
}else{paste0("'", input$download_error, "'")
})
#reset sensor broken and new sensor id if changing download error to no
observeEvent(input$download_error == 0, {
reset("sensor_broken")
reset("new_sensor_id")
})
#reset new sensor id if sensor broken changed
observeEvent(input$sensor_broken, {
reset("new_sensor_id")
})
#lookup priority uid
rv$priority_lookup_uid_query <- reactive(paste0("select field_test_priority_lookup_uid from fieldwork.tbl_field_test_priority_lookup where field_test_priority = '", input$priority, "'"))
rv$priority_lookup_uid_step <- reactive(dbGetQuery(poolConn, rv$priority_lookup_uid_query()))
rv$priority_lookup_uid <- reactive(if(nchar(input$priority) == 0) "NULL" else paste0("'", rv$priority_lookup_uid_step(), "'"))
#set fields to "NULL" if they are empty
rv$collect_date <- reactive(if(length(input$collect_date) == 0) "NULL" else paste0("'", input$collect_date, "'"))
rv$research_lookup_uid <- reactive(if(length(rv$research()) == 0) "NULL" else paste0("'", rv$research(), "'"))
rv$premonitoring_date <- reactive(if(length(input$premonitoring_date) == 0) "NULL" else paste0("'", input$premonitoring_date, "'"))
rv$ready <- reactive(if(nchar(input$ready) == 0) "NULL" else paste0("'", input$ready, "'"))
#get sensor id for redeployment
rv$new_sensor_id <- reactive(if(nchar(input$new_sensor_id) > 0){
paste0("'", input$new_sensor_id, "'")
}else{
paste0("'", input$sensor_id, "'")
}
)
#get sensor uid from sensor id
rv$new_inventory_sensors_uid <- reactive(odbc::dbGetQuery(poolConn, paste0(
"SELECT inventory_sensors_uid FROM fieldwork.tbl_inventory_sensors WHERE sensor_serial = ", rv$new_sensor_id()
)))
#get sensor issue lookup uid and let it be NULL (for issue #1 and issue #2)
rv$issue_lookup_uid_one <- reactive(sensor_issue_lookup %>% dplyr::filter(sensor_issue == input$issue_one) %>%
select(sensor_issue_lookup_uid) %>% pull())
rv$sensor_issue_lookup_uid_one <- reactive(if(nchar(input$issue_one) == 0) "NULL" else paste0("'", rv$issue_lookup_uid_one(), "'"))
rv$issue_lookup_uid_two <- reactive(sensor_issue_lookup %>% dplyr::filter(sensor_issue == input$issue_two) %>%
select(sensor_issue_lookup_uid) %>% pull())
rv$sensor_issue_lookup_uid_two <- reactive(if(nchar(input$issue_two) == 0) "NULL" else paste0("'", rv$issue_lookup_uid_two(), "'"))
#let checkbox input be NULL if blank (instead of FALSE)
rv$request_data <- reactive(if(input$request_data == TRUE) paste0("'TRUE'") else "NULL")
#future deployments - premonitoring date
#see if there were CETs during the premonitoring date, if so, say READY
observeEvent(input$premonitoring_date, {
if(length(input$premonitoring_date) > 0){
rv$cet_asset_query <- reactive(paste0("SELECT count(*) FROM external.mat_assets_ict_limited WHERE smp_id = '", input$smp_id, "' AND component_id != 'NULL'"))
rv$cet_count_query <- reactive(paste0("select count(*) from fieldwork.viw_capture_efficiency_full
where admin.fun_component_to_smp(component_id) = ", rv$smp_id(), "
and test_date < ", rv$premonitoring_date(), "::timestamp + interval '1 day'
and test_date > ", rv$premonitoring_date(), "::timestamp - interval '30 days'" ))
rv$cet_asset_count <- reactive(dbGetQuery(poolConn, rv$cet_asset_query()) %>% pull())
# print(rv$cet_asset_count())
rv$cet_count <- reactive(dbGetQuery(poolConn, rv$cet_count_query()) %>% pull())
# print(rv$cet_count_query())
# print(rv$cet_count())
cet_good <- rv$cet_asset_count() == rv$cet_count()
if(cet_good == FALSE){
showModal(modalDialog(title = "Add Capture Efficiency Test",
"Add capture efficiency tests from the pre-monitoring inspection?",
modalButton("No"),
actionButton(ns("add_cet"), "Yes", onclick = "window.open('https://pwdrstudio.water.gov/content/157/', '_blank')")))
# enable("ready")
}else{
# enable("ready")
}
} else {
# disable("ready")
}
})
#2.2.5 query and display tables ----
#2.2.5.1 active ------
#query for active deployments
active_table_query <- reactive(if(nchar(input$smp_id) > 0){
paste0(
"SELECT * FROM fieldwork.viw_active_deployments
WHERE smp_id = ", rv$smp_id(), " ORDER BY deployment_dtime_est")
}else{
paste0(
"SELECT * FROM fieldwork.viw_active_deployments
WHERE site_name = '", input$site_name, "' ORDER BY deployment_dtime_est")
})
#create table as a reactive value based on query
rv$active_table_db <- reactive(odbc::dbGetQuery(poolConn, active_table_query())%>%
mutate(across("download_error",
~ case_when(. == 1 ~ "Yes",
. == 0 ~ "No"))))
#select columns to show in app, and rename
rv$active_table <- reactive(rv$active_table_db() %>%
mutate(across(where(is.POSIXct), trunc, "days")) %>%
mutate(across(where(is.POSIXlt), as.character)) %>%
dplyr::select(deployment_dtime_est, ow_suffix, type, term, research, interval_min, sensor_serial, date_80percent, date_100percent) %>%
dplyr::rename("Deploy Date" = "deployment_dtime_est",
"Location" = "ow_suffix", "Purpose" = "type", "Term" = "term",
"Research" = "research", "Interval (min)" = "interval_min", "Sensor ID" = "sensor_serial",
"80% Full Date" = "date_80percent", "100% Full Date" = "date_100percent"))
#render Datatable
output$current_deployment <- renderDT(
rv$active_table(),
selection = "single",
style = 'bootstrap',
class = 'table-responsive, table-condensed',
options = list(dom = 'tp')
)
#2.2.5.2 past ------
#query for previous deployments
old_table_query <- reactive(if(nchar(input$smp_id) > 0){
paste0(
"SELECT * FROM fieldwork.viw_previous_deployments
WHERE smp_id = ", rv$smp_id(), " ORDER BY deployment_dtime_est desc")
}else{
paste0(
"SELECT * FROM fieldwork.viw_previous_deployments
WHERE site_name = '", input$site_name, "' ORDER BY deployment_dtime_est desc")
})
#create table as a reactive value based on query
rv$old_table_db <- reactive(odbc::dbGetQuery(poolConn, old_table_query()))
#select key fields and rename
rv$old_table <- reactive(rv$old_table_db() %>%
mutate(across("download_error",
~ case_when(. == 1 ~ "Yes",
. == 0 ~ "No"))) %>%
mutate(across(where(is.POSIXct), trunc, "days")) %>%
mutate(across(where(is.POSIXlt), as.character)) %>%
dplyr::select(deployment_dtime_est, collection_dtime_est, ow_suffix, type, term, research, interval_min, sensor_serial) %>%
dplyr::rename("Deploy Date" = "deployment_dtime_est", "Location" = "ow_suffix",
"Purpose" = "type", "Term" = "term", "Research" = "research",
"Interval (min)" = "interval_min", "Sensor ID" = "sensor_serial",
"Collection Date" = "collection_dtime_est"))
#render datatable
output$prev_deployment <- renderDT(
rv$old_table(),
selection = "single",
style = 'bootstrap',
class = 'table-responsive, table-condensed',
options = list(dom = 'tp')
)
#2.2.5.3 future -------
#query future table based on smp_id or site_name
future_table_query <- reactive(if(nchar(input$smp_id) > 0){
paste0(
"SELECT * FROM fieldwork.viw_future_deployments_full
WHERE smp_id = ", rv$smp_id(), " ORDER BY ow_suffix asc")
}else{
paste0(
"SELECT * FROM fieldwork.viw_future_deployments_full
WHERE site_name = '", input$site_name, "' ORDER BY ow_suffix asc")
})
rv$future_table_db <- reactive(odbc::dbGetQuery(poolConn, future_table_query()))
#select key fields and rename
rv$future_table <- reactive(rv$future_table_db() %>%
mutate(across(where(is.POSIXct), trunc, "days")) %>%
mutate(across(where(is.POSIXlt), as.character)) %>%
dplyr::select(ow_suffix, type, term, research, interval_min, premonitoring_inspection, field_test_priority) %>%
dplyr::rename("Location" = "ow_suffix",
"Purpose" = "type", "Term" = "term", "Research" = "research",
"Interval (min)" = "interval_min", "Pre-Monitoring Inspection Date" = "premonitoring_inspection",
"Priority" = "field_test_priority"))
#render datatable for future deployments
output$future_deployment <- renderDT(
DT::datatable(
rv$future_table(),
selection = "single",
style = 'bootstrap',
class = 'table-responsive, table-condensed',
options = list(dom = 'tp')) %>%
formatStyle('Pre-Monitoring Inspection Date',
backgroundColor = newstyleEqual(NA, '#CCC916'))
)
#2.2.5.1 sensor query ------
sensor_query <- "SELECT * FROM fieldwork.viw_inventory_sensors_status"
rv$sensor_dt <- reactive(odbc::dbGetQuery(poolConn, sensor_query))
#2.2.6 Editing ---------
#shorten name of selected rows from active and prev deployments tables
rv$active <- reactive(input$current_deployment_rows_selected)
rv$prev <- reactive(input$prev_deployment_rows_selected)
rv$future <- reactive(input$future_deployment_rows_selected)
#when a row in active deployments table is clicked
observeEvent(input$current_deployment_rows_selected, {
#deselect from other tables
dataTableProxy('prev_deployment') %>% selectRows(NULL)
dataTableProxy('future_deployment') %>% selectRows(NULL)
})
#when a row in the previous deployments table is clicked
observeEvent(input$prev_deployment_rows_selected, {
#deselect from other table
dataTableProxy('current_deployment') %>% selectRows(NULL)
dataTableProxy('future_deployment') %>% selectRows(NULL)