feat: calculations (#4473)

* feat: initial calculation controller

* fix: entities

* feat: calculations

* fix: review comments and support floats

* fix: abstract business logic into calculations service

* fix: clean calculation entities after merge

* feat: react to changes to row/cell/field_type

* chore: changes after merging main

* feat: handle delete field

* test: add grid calculations tests

* fix: add validation + format numbers

* refactor: get cell number

* chore: bump collab

* chore: fix clippy

* chore: update docs

* chore: update docs

* chore: fmt

* chore: fix flutter

* chore: collab rev

* fix: cleanup and hover to show

* fix: localization

* test: add basic rust test

* fix: clippy

* fix: support updating calculation on duplicate row

---------

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Mathias Mogensen
2024-02-03 17:52:38 +01:00
committed by GitHub
parent 274742e334
commit 5cbc8b1e18
65 changed files with 2456 additions and 220 deletions

View File

@ -0,0 +1,87 @@
use std::sync::Arc;
use crate::database::calculations_test::script::{CalculationScript::*, DatabaseCalculationTest};
use collab_database::fields::Field;
use flowy_database2::entities::{CalculationType, FieldType, UpdateCalculationChangesetPB};
#[tokio::test]
async fn calculations_test() {
let mut test = DatabaseCalculationTest::new().await;
let expected_sum = 25.00000;
let expected_min = 1.00000;
let expected_average = 5.00000;
let expected_max = 14.00000;
let expected_median = 3.00000;
let view_id = &test.view_id;
let number_fields = test
.fields
.clone()
.into_iter()
.filter(|field| field.field_type == FieldType::Number as i64)
.collect::<Vec<Arc<Field>>>();
let field_id = &number_fields.first().unwrap().id;
let calculation_id = "calc_id".to_owned();
let scripts = vec![
// Insert Sum calculation first time
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Sum,
},
},
AssertCalculationValue {
expected: expected_sum,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Min,
},
},
AssertCalculationValue {
expected: expected_min,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Average,
},
},
AssertCalculationValue {
expected: expected_average,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Max,
},
},
AssertCalculationValue {
expected: expected_max,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id),
calculation_type: CalculationType::Median,
},
},
AssertCalculationValue {
expected: expected_median,
},
];
test.run_scripts(scripts).await;
}

View File

@ -0,0 +1,2 @@
mod calculation_test;
mod script;

View File

@ -0,0 +1,74 @@
use tokio::sync::broadcast::Receiver;
use flowy_database2::entities::UpdateCalculationChangesetPB;
use flowy_database2::services::database_view::DatabaseViewChanged;
use crate::database::database_editor::DatabaseEditorTest;
pub enum CalculationScript {
InsertCalculation {
payload: UpdateCalculationChangesetPB,
},
AssertCalculationValue {
expected: f64,
},
}
pub struct DatabaseCalculationTest {
inner: DatabaseEditorTest,
recv: Option<Receiver<DatabaseViewChanged>>,
}
impl DatabaseCalculationTest {
pub async fn new() -> Self {
let editor_test = DatabaseEditorTest::new_grid().await;
Self {
inner: editor_test,
recv: None,
}
}
pub fn view_id(&self) -> String {
self.view_id.clone()
}
pub async fn run_scripts(&mut self, scripts: Vec<CalculationScript>) {
for script in scripts {
self.run_script(script).await;
}
}
pub async fn run_script(&mut self, script: CalculationScript) {
match script {
CalculationScript::InsertCalculation { payload } => {
self.recv = Some(
self
.editor
.subscribe_view_changed(&self.view_id())
.await
.unwrap(),
);
self.editor.update_calculation(payload).await.unwrap();
},
CalculationScript::AssertCalculationValue { expected } => {
let calculations = self.editor.get_all_calculations(&self.view_id()).await;
let calculation = calculations.items.first().unwrap();
assert_eq!(calculation.value, format!("{:.5}", expected));
},
}
}
}
impl std::ops::Deref for DatabaseCalculationTest {
type Target = DatabaseEditorTest;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::ops::DerefMut for DatabaseCalculationTest {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

View File

@ -1,4 +1,5 @@
mod block_test;
mod calculations_test;
mod cell_test;
mod database_editor;
mod field_settings_test;