An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Fix rollup checkbox percentage precision and display https://github.com/siyuan-note/siyuan/issues/19435
Danield819202e0ad0
diff --git a/kernel/av/rollup_checkbox_percent_test.go b/kernel/av/rollup_checkbox_percent_test.gonew file mode 100644index 0000000000..2133973351--- /dev/null+++ b/kernel/av/rollup_checkbox_percent_test.go@@ -0,0 +1,61 @@+package av++import (+ "math"+ "testing"+)++func TestRollupCheckboxPercent(t *testing.T) {+ for _, operator := range []CalcOperator{CalcOperatorPercentChecked, CalcOperatorPercentUnchecked} {+ for _, test := range []struct {+ name string+ count, total int+ text string+ }{+ {"empty", 0, 0, ""},+ {"none", 0, 3, "0%"},+ {"third", 1, 3, "33.33%"},+ {"small fraction", 1, 103, "0.97%"},+ {"all", 3, 3, "100%"},+ } {+ t.Run(string(operator)+"/"+test.name, func(t *testing.T) {+ rollup := &ValueRollup{}+ for i := 0; i < test.total; i++ {+ checked := i < test.count+ if operator == CalcOperatorPercentUnchecked {+ checked = !checked+ }+ rollup.Contents = append(rollup.Contents, &Value{Type: KeyTypeCheckbox, Checkbox: &ValueCheckbox{Checked: checked}})+ }+ rollup.calcContents(&RollupCalc{Operator: operator}, &Key{Type: KeyTypeCheckbox})+ if test.total == 0 {+ if len(rollup.Contents) != 0 {+ t.Fatal("empty relation should remain empty")+ }+ return+ }+ value := rollup.Contents[0]+ want := float64(test.count) / float64(test.total) * 100+ if value.Number == nil || !value.Number.IsNotEmpty || math.Abs(value.Number.Content-want) > 1e-10 {+ t.Fatalf("numeric result = %+v, want %v", value.Number, want)+ }+ if value.String(true) != test.text {+ t.Errorf("display = %q, want %q", value.String(true), test.text)+ }+ threshold := &Value{Type: KeyTypeNumber, Number: NewFormattedValueNumber(1, NumberFormatNone)}+ if got := value.filter(threshold, nil, nil, FilterOperatorIsGreater, ""); got != (want > 1) {+ t.Errorf("numeric filter = %v, want %v", got, want > 1)+ }+ column := &TableColumn{BaseInstanceField: &BaseInstanceField{Type: KeyTypeRollup, Calc: &FieldCalc{Operator: CalcOperatorSum}}}+ table := &Table{BaseInstance: &BaseInstance{}, Columns: []*TableColumn{column}, Rows: []*TableRow{{Cells: []*TableCell{{BaseValue: &BaseValue{Value: &Value{Type: KeyTypeRollup, Rollup: rollup}, ValueType: KeyTypeRollup}}}}}}+ for _, calcOperator := range []CalcOperator{CalcOperatorSum, CalcOperatorAverage} {+ column.Calc.Operator = calcOperator+ calcFieldRollup(table, column, 0)+ if got := column.Calc.Result.Number.Content; math.Abs(got-want) > 1e-6 {+ t.Errorf("%s = %v, want %v", calcOperator, got, want)+ }+ }+ })+ }+ }+}diff --git a/kernel/av/value.go b/kernel/av/value.goindex 1678f9b576..2d27d4f697 100644--- a/kernel/av/value.go+++ b/kernel/av/value.go@@ -3174,7 +3174,7 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {}}if 0 < len(r.Contents) {- r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewFormattedValueNumber(float64(countChecked*100/len(r.Contents)), NumberFormatNone)}}+ r.Contents = []*Value{{Type: KeyTypeNumber, Number: newRollupCheckboxPercent(countChecked, len(r.Contents))}}}case CalcOperatorPercentUnchecked:countUnchecked := 0@@ -3186,11 +3186,19 @@ func (r *ValueRollup) calcContents(calc *RollupCalc, destKey *Key) {}}if 0 < len(r.Contents) {- r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewFormattedValueNumber(float64(countUnchecked*100/len(r.Contents)), NumberFormatNone)}}+ r.Contents = []*Value{{Type: KeyTypeNumber, Number: newRollupCheckboxPercent(countUnchecked, len(r.Contents))}}}}}+func newRollupCheckboxPercent(count, total int) *ValueNumber {+ ratio := float64(count) / float64(total)+ // 保留筛选、模板和列底部统计使用的百分数数值,仅将显示文本格式化为百分比。+ number := NewFormattedValueNumber(ratio*100, NumberFormatNone)+ number.FormattedContent = formatNumber(ratio, NumberFormatPercent)+ return number+}+func GetAttributeViewDefaultValue(valueID, keyID, blockID string, typ KeyType, keyDateIsTime bool) (ret *Value) {if "" == valueID {valueID = ast.NewNodeID()