(tenant_id: &Option<Uuid>)
| 871 | |
| 872 | #[cfg(feature = "postgres")] |
| 873 | pub async fn get_active_inactive(tenant_id: &Option<Uuid>) -> Result<DevicesActiveInactive, Error> { |
| 874 | diesel::sql_query(r#" |
| 875 | with device_active_inactive as ( |
| 876 | select |
| 877 | make_interval(secs => dp.uplink_interval) * 1.5 as uplink_interval, |
| 878 | d.last_seen_at as last_seen_at |
| 879 | from |
| 880 | device d |
| 881 | inner join device_profile dp |
| 882 | on d.device_profile_id = dp.id |
| 883 | inner join application a |
| 884 | on d.application_id = a.id |
| 885 | where |
| 886 | $1 is null or a.tenant_id = $1 |
| 887 | ) |
| 888 | select |
| 889 | coalesce(sum(case when last_seen_at is null then 1 end), 0) as never_seen_count, |
| 890 | coalesce(sum(case when (now() - uplink_interval) > last_seen_at then 1 end), 0) as inactive_count, |
| 891 | coalesce(sum(case when (now() - uplink_interval) <= last_seen_at then 1 end), 0) as active_count |
| 892 | from |
| 893 | device_active_inactive |
| 894 | "#) |
| 895 | .bind::<diesel::sql_types::Nullable<fields::sql_types::Uuid>, _>(tenant_id.map(fields::Uuid::from)) |
| 896 | .get_result(&mut get_async_db_conn().await?).await |
| 897 | .map_err(|e| Error::from_diesel(e, "".into())) |
| 898 | } |
| 899 | |
| 900 | #[cfg(feature = "sqlite")] |
| 901 | pub async fn get_active_inactive(tenant_id: &Option<Uuid>) -> Result<DevicesActiveInactive, Error> { |
no test coverage detected