-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEAP10_notebook.rmd
More file actions
119 lines (102 loc) · 4.1 KB
/
Copy pathEAP10_notebook.rmd
File metadata and controls
119 lines (102 loc) · 4.1 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
---
title: "EAP 10 Notebook"
output:
html_document:
df_print: paged
---
This notebook features preliminary counts ahead of the EAP 10 Report. As of 2/10/2021.
```{r, include = FALSE}
library(tidyverse)
library(odbc)
```
### Count of Monitored SMPs by Functional Type
This includes Continuous Water Level Monitoring only.
```{r}
#database connection
con <- odbc::dbConnect(odbc::odbc(), "mars_testing")
#user can specify end date
end_date <- '2021-01-01'
#select asset type, count of unique SMP IDs, and whether public or private
#based on deployments, joined with other tables/views
monitored_smp_count_query <- paste0(
"SELECT sfc.asset_type,
COUNT(DISTINCT(lvl.smp_id)),
own.public
FROM
leveldata_time_per_location lvl
LEFT JOIN fieldwork.ow_ownership own on lvl.ow_uid = own.ow_uid
LEFT JOIN public.smpid_facilityid_componentid sfc on lvl.smp_id = sfc.smp_id
WHERE sfc.component_id IS NULL
AND lvl.smp_id IS NOT NULL
GROUP BY sfc.asset_type, own.public")
monitored_smp_count_df <- odbc::dbGetQuery(con, monitored_smp_count_query)
#spread to put private and public counts in different columns but same row
#add to get total Public + Private for each type
monitored_smp_count <- monitored_smp_count_df %>%
tidyr::pivot_wider(names_from = public, values_from = count, values_fill = 0) %>%
dplyr::rename("Functional Type" = "asset_type", "Private" = "0", "Public" = "1") %>%
dplyr::select(1, 3, 2) %>%
dplyr::mutate("Total" = Private + Public)
print(monitored_smp_count)
#write.csv(monitored_smp_count, "C:/Users/nicholas.manna/Documents/R/EAP10/monitored_smp_count.csv")
```
### Count and average duration of short and long term deployments (working on this)
### Public Breakdown
```{r}
public_breakdown <- dbGetQuery(con, "with cte as(
select lvl.ow_uid, lvl.smp_id, lvl.ow_suffix, lvl.time_days, d.term
from leveldata_time_per_location lvl
left join fieldwork.deployment_full d
on lvl.ow_uid = d.ow_uid
where collection_dtime_est is not null
and term != 'SRT'
and type = 'LEVEL'
and public = TRUE
group by lvl.ow_uid, lvl.smp_id, lvl.ow_suffix, lvl.time_days, d.term
order by ow_uid desc)
select sfc.asset_type,
cte.term,
count(cte.smp_id),
avg(cte.time_days)
FROM cte
LEFT JOIN public.smpid_facilityid_componentid sfc on cte.smp_id = sfc.smp_id
WHERE sfc.component_id IS NULL
group by sfc.asset_type, cte.term")
public_breakdown_wide <- public_breakdown %>%
tidyr::pivot_wider(names_from = term, values_from = c(count, avg), values_fill = 0) %>%
# dplyr::rename("Functional Type" = "asset_type", "No. Short Term Monitoring Deployments" = "count_Short",
# "No. Long Term Monitoring Deployments" = "count_Long",
# "Average Duration of Short Term Monitoring Deployments (Days)" = "avg_Short",
# "Average Duration of Long Term Monitoring Deployments (Days)" = "avg_Long", ) %>%
dplyr::select(1, 2, 4, 3, 5)
print(public_breakdown_wide)
write.csv(public_breakdown_wide, "C:/Users/nicholas.manna/Documents/R/EAP10/public_breakdown.csv")
```
### Private breakdown
```{r}
private_breakdown <- dbGetQuery(con, "with cte as(
select lvl.ow_uid, lvl.smp_id, lvl.ow_suffix, lvl.time_days, d.term
from leveldata_time_per_location lvl
left join fieldwork.deployment_full d
on lvl.ow_uid = d.ow_uid
where collection_dtime_est is not null
and term != 'SRT'
and type = 'LEVEL'
and public = FALSE
group by lvl.ow_uid, lvl.smp_id, lvl.ow_suffix, lvl.time_days, d.term
order by ow_uid desc)
select sfc.asset_type,
cte.term,
count(cte.smp_id),
avg(cte.time_days)
FROM cte
LEFT JOIN public.smpid_facilityid_componentid sfc on cte.smp_id = sfc.smp_id
WHERE sfc.component_id IS NULL
group by sfc.asset_type, cte.term")
private_breakdown_wide <- private_breakdown %>%
tidyr::pivot_wider(names_from = term, values_from = c(count, avg), values_fill = 0) %>%
#dplyr::rename("Functional Type" = "asset_type", "No. Short Term Monitoring Deployments" = "count_Short",
# "Average Duration of Short Term Monitoring Deployments (Days)" = "avg_Short")
print(private_breakdown_wide)
write.csv(private_breakdown_wide, "C:/Users/nicholas.manna/Documents/R/EAP10/private_breakdown.csv")
```