Skip to content

Commit

Permalink
fix update_redis_control and support no-change
Browse files Browse the repository at this point in the history
  • Loading branch information
tom1484 committed Feb 3, 2024
1 parent adeb39a commit 4385632
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
85 changes: 50 additions & 35 deletions editor-server/src/graphql/mutations/control_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ impl ControlMapMutation {
let raw_dancer_data: Vec<DancerData> = sqlx::query_as!(
DancerData,
r#"
SELECT
Dancer.id AS "dancer_id",
Part.type AS "part_type: PartType",
Part.id AS "part_id",
ControlData.frame_id AS "control_data_frame_id"
FROM Dancer
INNER JOIN Part
ON Dancer.id = Part.dancer_id
INNER JOIN ControlData
ON Part.id = ControlData.part_id
WHERE ControlData.frame_id = ?
ORDER BY Dancer.id ASC, Part.id ASC;
"#,
SELECT
Dancer.id AS "dancer_id",
Part.type AS "part_type: PartType",
Part.id AS "part_id",
ControlData.frame_id AS "control_data_frame_id"
FROM Dancer
INNER JOIN Part
ON Dancer.id = Part.dancer_id
INNER JOIN ControlData
ON Part.id = ControlData.part_id
WHERE ControlData.frame_id = ?
ORDER BY Dancer.id ASC, Part.id ASC;
"#,
frame_id
)
.fetch_all(mysql)
Expand Down Expand Up @@ -282,7 +282,7 @@ impl ControlMapMutation {
let effect_id = _data[0];

// check if the effect is valid
if !all_led_effect_ids.contains(&effect_id) {
if effect_id > 0 && !all_led_effect_ids.contains(&effect_id) {
let error_message = format!(
"Effect of dancer #{} part #{} is not a valid effect",
index, _index
Expand Down Expand Up @@ -318,9 +318,9 @@ impl ControlMapMutation {

sqlx::query!(
r#"
UPDATE ControlData
SET color_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
UPDATE ControlData
SET color_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
color_id,
alpha,
Expand All @@ -335,19 +335,34 @@ impl ControlMapMutation {
let effect_id = _data[0];
let alpha = _data[1];

sqlx::query!(
r#"
UPDATE ControlData
SET effect_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
effect_id,
alpha,
frame_id,
part.part_id,
)
.execute(mysql)
.await?;
if effect_id > 0 {
sqlx::query!(
r#"
UPDATE ControlData
SET effect_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
effect_id,
alpha,
frame_id,
part.part_id,
)
.execute(mysql)
.await?;
} else {
sqlx::query!(
r#"
UPDATE ControlData
SET effect_id = NULL, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
alpha,
frame_id,
part.part_id,
)
.execute(mysql)
.await?;
}
}
};
}
Expand All @@ -364,11 +379,11 @@ impl ControlMapMutation {
Some(fade) => {
sqlx::query!(
r#"
UPDATE ControlFrame
SET
fade = ?,
meta_rev = meta_rev + 1
WHERE id = ?;
UPDATE ControlFrame
SET
fade = ?,
meta_rev = meta_rev + 1
WHERE id = ?;
"#,
fade,
frame_id,
Expand Down
27 changes: 7 additions & 20 deletions editor-server/src/utils/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,7 @@ pub async fn update_redis_control(
.await
.map_err(|e| e.to_string())?;

let dancer_controls =
partition_by_field(|dancer_control| dancer_control.id, dancer_controls);

dancer_controls
.into_iter()
.map(|dancer_control| partition_by_field(|part| part.id, dancer_control))
.collect_vec()
partition_by_field(|dancer_control| dancer_control.id, dancer_controls)
};

let redis_key = format!("{}{}", envs.redis_ctrl_prefix, frame.id);
Expand All @@ -279,19 +273,12 @@ pub async fn update_redis_control(
.map(|dancer_control| {
dancer_control
.iter()
.map(|part_controls| {
let part_control = part_controls
.iter()
.find(|part_control| part_control.frame_id == frame.id)
.unwrap_or_else(|| panic!("ControlData {} not found", frame.id));

match part_control.part_type {
PartType::LED => {
PartControl(part_control.effect_id.unwrap_or(-1), part_control.alpha)
}
PartType::FIBER => {
PartControl(part_control.color_id.unwrap(), part_control.alpha)
}
.map(|part_control| match part_control.part_type {
PartType::LED => {
PartControl(part_control.effect_id.unwrap_or(-1), part_control.alpha)
}
PartType::FIBER => {
PartControl(part_control.color_id.unwrap(), part_control.alpha)
}
})
.collect_vec()
Expand Down

0 comments on commit 4385632

Please sign in to comment.