🎨 Add Rollup column to database table view https://github.com/siyuan-note/siyuan/issues/9958

This commit is contained in:
Daniel 2024-01-01 15:14:52 +08:00
parent 416e50fc37
commit 239dbe5d2c
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 55 additions and 53 deletions

View file

@ -2105,8 +2105,8 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
if !uniqueValues[content] { if !uniqueValues[content.String()] {
uniqueValues[content] = true uniqueValues[content.String()] = true
countUniqueValues++ countUniqueValues++
} }
} }

View file

@ -147,10 +147,14 @@ func (value *Value) String() string {
} }
return strings.Join(ret, " ") return strings.Join(ret, " ")
case KeyTypeRollup: case KeyTypeRollup:
if nil == value.Rollup { if nil == value.Rollup || nil == value.Rollup.Contents {
return "" return ""
} }
return strings.Join(value.Rollup.Contents, " ") var ret []string
for _, v := range value.Rollup.Contents {
ret = append(ret, v.String())
}
return strings.Join(ret, " ")
default: default:
return "" return ""
} }
@ -468,7 +472,7 @@ type ValueRelation struct {
} }
type ValueRollup struct { type ValueRollup struct {
Contents []string `json:"contents"` Contents []*Value `json:"contents"`
} }
func (r *ValueRollup) RenderContents(calc *RollupCalc) { func (r *ValueRollup) RenderContents(calc *RollupCalc) {
@ -479,115 +483,113 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc) {
switch calc.Operator { switch calc.Operator {
case CalcOperatorNone: case CalcOperatorNone:
case CalcOperatorCountAll: case CalcOperatorCountAll:
r.Contents = []string{strconv.Itoa(len(r.Contents))} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(len(r.Contents)))}}
case CalcOperatorCountValues: case CalcOperatorCountValues:
r.Contents = []string{strconv.Itoa(len(r.Contents))} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(len(r.Contents)))}}
case CalcOperatorCountUniqueValues: case CalcOperatorCountUniqueValues:
countUniqueValues := 0 countUniqueValues := 0
uniqueValues := map[string]bool{} uniqueValues := map[string]bool{}
for _, v := range r.Contents { for _, v := range r.Contents {
if !uniqueValues[v] { if _, ok := uniqueValues[v.String()]; !ok {
uniqueValues[v] = true uniqueValues[v.String()] = true
countUniqueValues++ countUniqueValues++
} }
} }
r.Contents = []string{strconv.Itoa(countUniqueValues)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countUniqueValues))}}
case CalcOperatorCountEmpty: case CalcOperatorCountEmpty:
countEmpty := 0 countEmpty := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "" == v { if "" == v.String() {
countEmpty++ countEmpty++
} }
} }
r.Contents = []string{strconv.Itoa(countEmpty)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countEmpty))}}
case CalcOperatorCountNotEmpty: case CalcOperatorCountNotEmpty:
countNonEmpty := 0 countNonEmpty := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
countNonEmpty++ countNonEmpty++
} }
} }
r.Contents = []string{strconv.Itoa(countNonEmpty)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countNonEmpty))}}
case CalcOperatorPercentEmpty: case CalcOperatorPercentEmpty:
countEmpty := 0 countEmpty := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "" == v { if "" == v.String() {
countEmpty++ countEmpty++
} }
} }
r.Contents = []string{strconv.Itoa(countEmpty*100/len(r.Contents)) + "%"} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countEmpty * 100 / len(r.Contents)))}}
case CalcOperatorPercentNotEmpty: case CalcOperatorPercentNotEmpty:
countNonEmpty := 0 countNonEmpty := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
countNonEmpty++ countNonEmpty++
} }
} }
r.Contents = []string{strconv.Itoa(countNonEmpty*100/len(r.Contents)) + "%"} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countNonEmpty * 100 / len(r.Contents)))}}
case CalcOperatorSum: case CalcOperatorSum:
sum := 0.0 sum := 0.0
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
n, _ := strconv.ParseFloat(v, 64) n, _ := strconv.ParseFloat(v.String(), 64)
sum += n sum += n
} }
} }
r.Contents = []string{strconv.FormatFloat(sum, 'f', -1, 64)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(sum)}}
case CalcOperatorAverage: case CalcOperatorAverage:
sum := 0.0 sum := 0.0
count := 0 count := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
n, _ := strconv.ParseFloat(v, 64) n, _ := strconv.ParseFloat(v.String(), 64)
sum += n sum += n
count++ count++
} }
} }
r.Contents = []string{strconv.FormatFloat(sum/float64(count), 'f', -1, 64)} if 0 < count {
r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(sum / float64(count))}}
}
case CalcOperatorMedian: case CalcOperatorMedian:
numbers := []float64{} var numbers []float64
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
n, _ := strconv.ParseFloat(v, 64) n, _ := strconv.ParseFloat(v.String(), 64)
numbers = append(numbers, n) numbers = append(numbers, n)
} }
} }
sort.Float64s(numbers) sort.Float64s(numbers)
if 0 < len(numbers) { if 0 < len(numbers) {
if 0 == len(numbers)%2 { r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(numbers[len(numbers)/2])}}
r.Contents = []string{strconv.FormatFloat((numbers[len(numbers)/2-1]+numbers[len(numbers)/2])/2, 'f', -1, 64)}
} else {
r.Contents = []string{strconv.FormatFloat(numbers[len(numbers)/2], 'f', -1, 64)}
}
} }
case CalcOperatorMin: case CalcOperatorMin:
min := math.MaxFloat64 min := math.MaxFloat64
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
n, _ := strconv.ParseFloat(v, 64) n, _ := strconv.ParseFloat(v.String(), 64)
if n < min { if n < min {
min = n min = n
} }
} }
} }
r.Contents = []string{strconv.FormatFloat(min, 'f', -1, 64)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(min)}}
case CalcOperatorMax: case CalcOperatorMax:
max := -math.MaxFloat64 max := -math.MaxFloat64
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
n, _ := strconv.ParseFloat(v, 64) n, _ := strconv.ParseFloat(v.String(), 64)
if n > max { if n > max {
max = n max = n
} }
} }
} }
r.Contents = []string{strconv.FormatFloat(max, 'f', -1, 64)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(max)}}
case CalcOperatorRange: case CalcOperatorRange:
min := math.MaxFloat64 min := math.MaxFloat64
max := -math.MaxFloat64 max := -math.MaxFloat64
for _, v := range r.Contents { for _, v := range r.Contents {
if "" != v { if "" != v.String() {
n, _ := strconv.ParseFloat(v, 64) n, _ := strconv.ParseFloat(v.String(), 64)
if n < min { if n < min {
min = n min = n
} }
@ -596,38 +598,38 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc) {
} }
} }
} }
r.Contents = []string{strconv.FormatFloat(max-min, 'f', -1, 64)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(max - min)}}
case CalcOperatorChecked: case CalcOperatorChecked:
countChecked := 0 countChecked := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "√" == v { if "√" == v.String() {
countChecked++ countChecked++
} }
} }
r.Contents = []string{strconv.Itoa(countChecked)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countChecked))}}
case CalcOperatorUnchecked: case CalcOperatorUnchecked:
countUnchecked := 0 countUnchecked := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "√" != v { if "√" != v.String() {
countUnchecked++ countUnchecked++
} }
} }
r.Contents = []string{strconv.Itoa(countUnchecked)} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countUnchecked))}}
case CalcOperatorPercentChecked: case CalcOperatorPercentChecked:
countChecked := 0 countChecked := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "√" == v { if "√" == v.String() {
countChecked++ countChecked++
} }
} }
r.Contents = []string{strconv.Itoa(countChecked*100/len(r.Contents)) + "%"} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countChecked * 100 / len(r.Contents)))}}
case CalcOperatorPercentUnchecked: case CalcOperatorPercentUnchecked:
countUnchecked := 0 countUnchecked := 0
for _, v := range r.Contents { for _, v := range r.Contents {
if "√" != v { if "√" != v.String() {
countUnchecked++ countUnchecked++
} }
} }
r.Contents = []string{strconv.Itoa(countUnchecked*100/len(r.Contents)) + "%"} r.Contents = []*Value{{Type: KeyTypeNumber, Number: NewValueNumber(float64(countUnchecked * 100 / len(r.Contents)))}}
} }
} }

View file

@ -212,7 +212,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
switch kValues.Key.Type { switch kValues.Key.Type {
case av.KeyTypeRollup: case av.KeyTypeRollup:
kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeRollup, Rollup: &av.ValueRollup{Contents: []string{}}}) kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeRollup, Rollup: &av.ValueRollup{Contents: []*av.Value{}}})
case av.KeyTypeTemplate: case av.KeyTypeTemplate:
kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: ""}}) kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: ""}})
case av.KeyTypeCreated: case av.KeyTypeCreated:
@ -260,7 +260,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
destVal.Number.FormatNumber() destVal.Number.FormatNumber()
} }
kv.Values[0].Rollup.Contents = append(kv.Values[0].Rollup.Contents, destVal.String()) kv.Values[0].Rollup.Contents = append(kv.Values[0].Rollup.Contents, destVal.Clone())
kv.Values[0].Rollup.RenderContents(kv.Key.Rollup.Calc) kv.Values[0].Rollup.RenderContents(kv.Key.Rollup.Calc)
} }
} }
@ -832,7 +832,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
destVal.Number.FormatNumber() destVal.Number.FormatNumber()
} }
cell.Value.Rollup.Contents = append(cell.Value.Rollup.Contents, destVal.String()) cell.Value.Rollup.Contents = append(cell.Value.Rollup.Contents, destVal.Clone())
} }
cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc) cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc)

View file

@ -789,7 +789,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
destVal.Number.FormatNumber() destVal.Number.FormatNumber()
} }
cell.Value.Rollup.Contents = append(cell.Value.Rollup.Contents, destVal.String()) cell.Value.Rollup.Contents = append(cell.Value.Rollup.Contents, destVal.Clone())
} }
cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc) cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc)