Skip to content

Commit

Permalink
Merge pull request #62 from treedoc/fix-sort-null
Browse files Browse the repository at this point in the history
Sort and statis handling null values
  • Loading branch information
jianwu authored Jan 2, 2024
2 parents 03f6ff4 + 1e3e92f commit f2dc9ef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/components/DataFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export default {
q.offset = Math.max(0, opt.total - q.limit);
if (opt.query.sort) {
const getFieldValue = (row: any) => {
const v = row[q.sort!];
return v instanceof TDNode && v.value !== undefined ? v.value : v;
let v = row[q.sort!];
v = v instanceof TDNode && v.value !== undefined ? v.value : v;
return v || '';
};

opt.filteredData = _.orderBy(opt.filteredData , getFieldValue, q.order);
opt.filteredData = _.orderBy(opt.filteredData , getFieldValue, q.order);
}
opt.filteredDataAsObjectArray = opt.filteredData.map(r => TableUtil.rowToObject(r, opt, true, true));
opt.columnStatistic = TableUtil.collectColumnStatistics(opt.filteredDataAsObjectArray, opt.columns.filter(c => c.visible).map(c => c.field));
Expand Down
8 changes: 4 additions & 4 deletions src/models/TableUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TableUtil {
let val = row[col];
vals.push(val);
if (val === undefined) // Skip undefined value
continue;
val = '';
if (typeof val !== 'string' && typeof val !== 'number') {
val = JSON.stringify(val);
}
Expand All @@ -51,9 +51,9 @@ export class TableUtil {
vals.sort((a,b) => a - b);
stat.avg = stat.sum / rows.length;
if (stat.avg > 0) { // Calculate percentile only when avg is number
stat.p50 = vals[Math.floor(vals.length * 0.5)];
stat.p90 = vals[Math.floor(vals.length * 0.9)];
stat.p99 = vals[Math.floor(vals.length * 0.99)];
stat.p50 = vals[Math.floor(vals.length * 0.5)] || 0;
stat.p90 = vals[Math.floor(vals.length * 0.9)] || 0;
stat.p99 = vals[Math.floor(vals.length * 0.99)] || 0;
}
stat.valueSortedByCounts = Object.keys(stat.valueCounts).sort((a, b) => stat.valueCounts[b] - stat.valueCounts[a]);
result[col] = stat;
Expand Down

0 comments on commit f2dc9ef

Please sign in to comment.