Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod models;
pub mod mutations;
pub mod queries;

use std::sync::Arc;
Expand Down
30 changes: 18 additions & 12 deletions src/graphql/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,28 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize)]
pub struct StreakWithMemberId {
#[serde(rename = "memberId")]
pub member_id: i32,
#[serde(rename = "currentStreak")]
pub current_streak: i32,
#[serde(rename = "maxStreak")]
pub max_streak: i32,
pub struct StatusOnDate {
#[serde(rename = "isSent")]
pub is_sent: bool,
#[serde(rename = "onBreak")]
pub on_break: bool,
}

#[derive(Clone, Debug, Deserialize)]
pub struct Streak {
pub struct StatusStreak {
#[serde(rename = "currentStreak")]
pub current_streak: i32,
pub current_streak: Option<i32>,
#[serde(rename = "maxStreak")]
pub max_streak: i32,
pub max_streak: Option<i32>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct MemberStatus {
#[serde(rename = "onDate")]
pub on_date: Option<StatusOnDate>,
pub streak: Option<StatusStreak>,
#[serde(rename = "consecutiveMisses")]
pub consecutive_misses: Option<i32>,
}

#[derive(Clone, Debug, Deserialize)]
Expand All @@ -42,10 +49,9 @@ pub struct Member {
pub name: String,
#[serde(rename = "discordId")]
pub discord_id: String,
#[serde(default)]
pub streak: Vec<Streak>, // Note that Root will NOT have multiple Streak elements but it may be an empty list which is why we use a vector here
pub track: Option<String>,
pub year: i32,
pub status: Option<MemberStatus>,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down
149 changes: 0 additions & 149 deletions src/graphql/mutations.rs

This file was deleted.

83 changes: 27 additions & 56 deletions src/graphql/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,51 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use anyhow::{anyhow, Context};
use chrono::Local;
use chrono::{Local, NaiveDate};
use serde_json::Value;
use tracing::debug;

use crate::graphql::models::{AttendanceRecord, Member};

use super::{models::StreakWithMemberId, GraphQLClient};
use super::GraphQLClient;

impl GraphQLClient {
pub async fn fetch_members(&self) -> anyhow::Result<Vec<Member>> {
pub async fn fetch_member_data(&self, date: NaiveDate) -> anyhow::Result<Vec<Member>> {
let query = r#"
{
members {
query($date: NaiveDate!) {
allMembers {
memberId
name
discordId
groupId
streak {
currentStreak
maxStreak
status {
onDate(date: $date) {
isSent
onBreak
}
streak {
currentStreak,
maxStreak
}
consecutiveMisses
}
track
}
}"#;
year
}
}"#;

debug!("Sending query {}", query);

let variables = serde_json::json!({
"date": date.format("%Y-%m-%d").to_string()
});

debug!("With variables: {}", variables);

let response = self
.http()
.post(self.root_url())
.json(&serde_json::json!({"query": query}))
.json(&serde_json::json!({"query": query, "variables":variables}))
.send()
.await
.context("Failed to successfully post request")?;
Expand All @@ -65,7 +80,7 @@ impl GraphQLClient {
debug!("Response: {}", response_json);
let members = response_json
.get("data")
.and_then(|data| data.get("members"))
.and_then(|data| data.get("allMembers"))
.and_then(|members| members.as_array())
.ok_or_else(|| {
anyhow::anyhow!(
Expand Down Expand Up @@ -128,48 +143,4 @@ impl GraphQLClient {
);
Ok(attendance)
}

pub async fn fetch_streaks(&self) -> anyhow::Result<Vec<StreakWithMemberId>> {
let query = r#"
{
streaks {
memberId
currentStreak
maxStreak
}
}
"#;

debug!("Sending query {}", query);
let response = self
.http()
.post(self.root_url())
.json(&serde_json::json!({"query": query}))
.send()
.await
.context("Failed to successfully post request")?;

if !response.status().is_success() {
return Err(anyhow!(
"Server responded with an error: {:?}",
response.status()
));
}

let response_json: serde_json::Value = response
.json()
.await
.context("Failed to serialize response")?;

debug!("Response: {}", response_json);
let streaks = response_json
.get("data")
.and_then(|data| data.get("streaks"))
.and_then(|streaks| {
serde_json::from_value::<Vec<StreakWithMemberId>>(streaks.clone()).ok()
})
.context("Failed to parse streaks data")?;

Ok(streaks)
}
}
5 changes: 0 additions & 5 deletions src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,5 @@ pub const RESEARCH_ROLE_ID: u64 = 1298553855474270219;
pub const DEVOPS_ROLE_ID: u64 = 1298553883169132554;
pub const WEB_ROLE_ID: u64 = 1298553910167994428;

// Channel IDs for status updates
pub const SYSTEMS_CHANNEL_ID: u64 = 1378426650152271902;
pub const MOBILE_CHANNEL_ID: u64 = 1378685538835365960;
pub const WEB_CHANNEL_ID: u64 = 1378685360133115944;
pub const AI_CHANNEL_ID: u64 = 1343489220068507649;
pub const STATUS_UPDATE_CHANNEL_ID: u64 = 764575524127244318;
pub const THE_LAB_CHANNEL_ID: u64 = 1208438766893670451;
4 changes: 2 additions & 2 deletions src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use anyhow::Result;
use async_trait::async_trait;
use lab_attendance::PresenseReport;
use serenity::client::Context;
use status_update::StatusUpdateCheck;
use status_update::StatusUpdateReport;
use tokio::time::Duration;

use crate::graphql::GraphQLClient;
Expand All @@ -52,5 +52,5 @@ impl Debug for Box<dyn Task> {
/// Analogous to [`crate::commands::get_commands`], every task that is defined
/// must be included in the returned vector in order for it to be scheduled.
pub fn get_tasks() -> Vec<Box<dyn Task>> {
vec![Box::new(StatusUpdateCheck), Box::new(PresenseReport)]
vec![Box::new(PresenseReport), Box::new(StatusUpdateReport)]
}
Loading