-
-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: user field support filter `Me` * fix: selection filter user 'Me' * fix: `sqlite` dateRangeOfMonths agg func * refactor: remove queryUserId param use `cls` user --------- Co-authored-by: pengap <[email protected]>
- Loading branch information
Showing
79 changed files
with
4,189 additions
and
3,447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
apps/nestjs-backend/src/db-provider/aggregation-query/aggregation-function.abstract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { InternalServerErrorException, Logger } from '@nestjs/common'; | ||
import { StatisticsFunc } from '@teable-group/core'; | ||
import type { Knex } from 'knex'; | ||
import type { IFieldInstance } from '../../features/field/model/factory'; | ||
import type { IAggregationFunctionInterface } from './aggregation-function.interface'; | ||
|
||
export abstract class AbstractAggregationFunction implements IAggregationFunctionInterface { | ||
private logger = new Logger(AbstractAggregationFunction.name); | ||
|
||
protected tableColumnRef: string; | ||
|
||
constructor( | ||
protected readonly knex: Knex, | ||
protected readonly dbTableName: string, | ||
protected readonly field: IFieldInstance | ||
) { | ||
const { dbFieldName } = this.field; | ||
|
||
this.tableColumnRef = `${this.dbTableName}.${dbFieldName}`; | ||
} | ||
|
||
compiler(builderClient: Knex.QueryBuilder, aggFunc: StatisticsFunc) { | ||
const functionHandlers = { | ||
[StatisticsFunc.Empty]: this.empty, | ||
[StatisticsFunc.Filled]: this.filled, | ||
[StatisticsFunc.Unique]: this.unique, | ||
[StatisticsFunc.Max]: this.max, | ||
[StatisticsFunc.Min]: this.min, | ||
[StatisticsFunc.Sum]: this.sum, | ||
[StatisticsFunc.Average]: this.average, | ||
[StatisticsFunc.Checked]: this.checked, | ||
[StatisticsFunc.UnChecked]: this.unChecked, | ||
[StatisticsFunc.PercentEmpty]: this.percentEmpty, | ||
[StatisticsFunc.PercentFilled]: this.percentFilled, | ||
[StatisticsFunc.PercentUnique]: this.percentUnique, | ||
[StatisticsFunc.PercentChecked]: this.percentChecked, | ||
[StatisticsFunc.PercentUnChecked]: this.percentUnChecked, | ||
[StatisticsFunc.EarliestDate]: this.earliestDate, | ||
[StatisticsFunc.LatestDate]: this.latestDate, | ||
[StatisticsFunc.DateRangeOfDays]: this.dateRangeOfDays, | ||
[StatisticsFunc.DateRangeOfMonths]: this.dateRangeOfMonths, | ||
[StatisticsFunc.TotalAttachmentSize]: this.totalAttachmentSize, | ||
}; | ||
const chosenHandler = functionHandlers[aggFunc].bind(this); | ||
|
||
if (!chosenHandler) { | ||
throw new InternalServerErrorException(`Unknown function ${aggFunc} for aggregation`); | ||
} | ||
|
||
const { id: fieldId, isMultipleCellValue } = this.field; | ||
|
||
let rawSql: string = chosenHandler(); | ||
|
||
if (isMultipleCellValue) { | ||
const joinTable = `${fieldId}_mcv`; | ||
|
||
builderClient.with(`${fieldId}_mcv`, this.knex.raw(rawSql)); | ||
builderClient.joinRaw(`, ${this.knex.ref(joinTable)}`); | ||
|
||
rawSql = `MAX(${this.knex.ref(`${joinTable}.value`)})`; | ||
} | ||
|
||
return builderClient.select(this.knex.raw(`${rawSql} AS ??`, [`${fieldId}_${aggFunc}`])); | ||
} | ||
|
||
empty(): string { | ||
return this.knex.raw(`COUNT(*) - COUNT(??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
filled(): string { | ||
return this.knex.raw(`COUNT(??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
unique(): string { | ||
return this.knex.raw(`COUNT(DISTINCT ??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
max(): string { | ||
return this.knex.raw(`MAX(??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
min(): string { | ||
return this.knex.raw(`MIN(??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
sum(): string { | ||
return this.knex.raw(`SUM(??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
average(): string { | ||
return this.knex.raw(`AVG(??)`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
checked(): string { | ||
return this.filled(); | ||
} | ||
|
||
unChecked(): string { | ||
return this.empty(); | ||
} | ||
|
||
percentEmpty(): string { | ||
return this.knex | ||
.raw(`((COUNT(*) - COUNT(??)) * 1.0 / COUNT(*)) * 100`, [this.tableColumnRef]) | ||
.toQuery(); | ||
} | ||
|
||
percentFilled(): string { | ||
return this.knex.raw(`(COUNT(??) * 1.0 / COUNT(*)) * 100`, [this.tableColumnRef]).toQuery(); | ||
} | ||
|
||
percentUnique(): string { | ||
return this.knex | ||
.raw(`(COUNT(DISTINCT ??) * 1.0 / COUNT(*)) * 100`, [this.tableColumnRef]) | ||
.toQuery(); | ||
} | ||
|
||
percentChecked(): string { | ||
return this.percentFilled(); | ||
} | ||
|
||
percentUnChecked(): string { | ||
return this.percentEmpty(); | ||
} | ||
|
||
earliestDate(): string { | ||
return this.min(); | ||
} | ||
|
||
latestDate(): string { | ||
return this.max(); | ||
} | ||
|
||
abstract dateRangeOfDays(): string; | ||
|
||
abstract dateRangeOfMonths(): string; | ||
|
||
abstract totalAttachmentSize(): string; | ||
} |
File renamed without changes.
Oops, something went wrong.