mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
refactor: Improve error handling and formatting in DatabaseService
This commit is contained in:
@@ -53,14 +53,14 @@ import TableColumnType from 'Common/Types/AnalyticsDatabase/TableColumnType';
|
||||
export default class AnalyticsDatabaseService<
|
||||
TBaseModel extends AnalyticsBaseModel
|
||||
> extends BaseService {
|
||||
public modelType!: { new(): TBaseModel };
|
||||
public modelType!: { new (): TBaseModel };
|
||||
public database!: ClickhouseDatabase;
|
||||
public model!: TBaseModel;
|
||||
public databaseClient!: ClickhouseClient;
|
||||
public statementGenerator!: StatementGenerator<TBaseModel>;
|
||||
|
||||
public constructor(data: {
|
||||
modelType: { new(): TBaseModel };
|
||||
modelType: { new (): TBaseModel };
|
||||
database?: ClickhouseDatabase | undefined;
|
||||
}) {
|
||||
super();
|
||||
@@ -332,7 +332,7 @@ export default class AnalyticsDatabaseService<
|
||||
WHERE TRUE `.append(whereStatement);
|
||||
/* eslint-enable prettier/prettier */
|
||||
|
||||
if (countBy.groupBy && Object.keys(countBy.groupBy).length > 0){
|
||||
if (countBy.groupBy && Object.keys(countBy.groupBy).length > 0) {
|
||||
statement.append(
|
||||
SQL`
|
||||
GROUP BY `.append(
|
||||
@@ -344,18 +344,18 @@ export default class AnalyticsDatabaseService<
|
||||
if (countBy.limit) {
|
||||
statement.append(SQL`
|
||||
LIMIT ${{
|
||||
value: Number(countBy.limit),
|
||||
type: TableColumnType.Number,
|
||||
}}
|
||||
value: Number(countBy.limit),
|
||||
type: TableColumnType.Number,
|
||||
}}
|
||||
`);
|
||||
}
|
||||
|
||||
if (countBy.skip) {
|
||||
statement.append(SQL`
|
||||
OFFSET ${{
|
||||
value: Number(countBy.skip),
|
||||
type: TableColumnType.Number,
|
||||
}}
|
||||
value: Number(countBy.skip),
|
||||
type: TableColumnType.Number,
|
||||
}}
|
||||
`);
|
||||
}
|
||||
logger.info(`${this.model.tableName} Count Statement`);
|
||||
@@ -374,22 +374,18 @@ export default class AnalyticsDatabaseService<
|
||||
|
||||
const databaseName: string =
|
||||
this.database.getDatasourceOptions().database!;
|
||||
let groupByStatement: Statement | null = null;
|
||||
|
||||
if (findBy.groupBy && Object.keys(findBy.groupBy).length > 0) {
|
||||
|
||||
// overwrite select object
|
||||
findBy.select = {
|
||||
...findBy.groupBy,
|
||||
};
|
||||
|
||||
groupByStatement = this.statementGenerator.toGroupByStatement(
|
||||
findBy.groupBy
|
||||
);
|
||||
}
|
||||
|
||||
let groupByStatement: Statement | null = null;
|
||||
|
||||
if (findBy.groupBy && Object.keys(findBy.groupBy).length > 0) {
|
||||
// overwrite select object
|
||||
findBy.select = {
|
||||
...findBy.groupBy,
|
||||
};
|
||||
|
||||
groupByStatement = this.statementGenerator.toGroupByStatement(
|
||||
findBy.groupBy
|
||||
);
|
||||
}
|
||||
|
||||
const select: { statement: Statement; columns: Array<string> } =
|
||||
this.statementGenerator.toSelectStatement(findBy.select!);
|
||||
@@ -402,20 +398,34 @@ export default class AnalyticsDatabaseService<
|
||||
|
||||
const statement: Statement = SQL``;
|
||||
|
||||
statement.append(SQL`
|
||||
SELECT `.append(select.statement));
|
||||
statement.append(
|
||||
SQL`
|
||||
SELECT `.append(select.statement)
|
||||
);
|
||||
statement.append(SQL`
|
||||
FROM ${databaseName}.${this.model.tableName}`);
|
||||
statement.append(SQL`
|
||||
WHERE TRUE `).append(whereStatement)
|
||||
statement
|
||||
.append(
|
||||
SQL`
|
||||
WHERE TRUE `
|
||||
)
|
||||
.append(whereStatement);
|
||||
|
||||
if (groupByStatement) {
|
||||
statement.append(SQL`
|
||||
GROUP BY `).append(groupByStatement)
|
||||
statement
|
||||
.append(
|
||||
SQL`
|
||||
GROUP BY `
|
||||
)
|
||||
.append(groupByStatement);
|
||||
}
|
||||
|
||||
statement.append(SQL`
|
||||
ORDER BY `).append(sortStatement)
|
||||
statement
|
||||
.append(
|
||||
SQL`
|
||||
ORDER BY `
|
||||
)
|
||||
.append(sortStatement);
|
||||
|
||||
statement.append(SQL`
|
||||
LIMIT ${{
|
||||
@@ -430,8 +440,6 @@ export default class AnalyticsDatabaseService<
|
||||
}}
|
||||
`);
|
||||
|
||||
|
||||
|
||||
/* eslint-enable prettier/prettier */
|
||||
|
||||
logger.info(`${this.model.tableName} Find Statement`);
|
||||
@@ -596,8 +604,8 @@ export default class AnalyticsDatabaseService<
|
||||
statement instanceof Statement
|
||||
? statement
|
||||
: {
|
||||
query: statement, // TODO remove and only accept Statements
|
||||
}
|
||||
query: statement, // TODO remove and only accept Statements
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -699,16 +707,16 @@ export default class AnalyticsDatabaseService<
|
||||
|
||||
const onCreate: OnCreate<TBaseModel> = createBy.props.ignoreHooks
|
||||
? {
|
||||
createBy: {
|
||||
data: data,
|
||||
props: createBy.props,
|
||||
},
|
||||
carryForward: [],
|
||||
}
|
||||
createBy: {
|
||||
data: data,
|
||||
props: createBy.props,
|
||||
},
|
||||
carryForward: [],
|
||||
}
|
||||
: await this._onBeforeCreate({
|
||||
data: data,
|
||||
props: createBy.props,
|
||||
});
|
||||
data: data,
|
||||
props: createBy.props,
|
||||
});
|
||||
|
||||
data = onCreate.createBy.data;
|
||||
|
||||
@@ -810,7 +818,8 @@ export default class AnalyticsDatabaseService<
|
||||
await Promise.allSettled(promises);
|
||||
} else {
|
||||
logger.warn(
|
||||
`Realtime is not initialized. Skipping emitModelEvent for ${this.getModel().tableName
|
||||
`Realtime is not initialized. Skipping emitModelEvent for ${
|
||||
this.getModel().tableName
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -813,9 +813,10 @@ class DatabaseService<TBaseModel extends BaseModel> extends BaseService {
|
||||
distinctOn,
|
||||
}: CountBy<TBaseModel>): Promise<PositiveNumber> {
|
||||
try {
|
||||
|
||||
if(groupBy && Object.keys(groupBy).length > 0) {
|
||||
throw new BadDataException('Group By is not supported for countBy');
|
||||
if (groupBy && Object.keys(groupBy).length > 0) {
|
||||
throw new BadDataException(
|
||||
'Group By is not supported for countBy'
|
||||
);
|
||||
}
|
||||
|
||||
if (!skip) {
|
||||
|
||||
@@ -12,9 +12,8 @@ import SortOrder from 'Common/Types/BaseDatabase/SortOrder';
|
||||
const ServiceDelete: FunctionComponent<PageComponentProps> = (
|
||||
_props: PageComponentProps
|
||||
): ReactElement => {
|
||||
|
||||
const modelId: ObjectID = Navigation.getLastParamAsObjectID(1);
|
||||
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
<AnalyticsModelTable<Metric>
|
||||
@@ -27,7 +26,7 @@ const ServiceDelete: FunctionComponent<PageComponentProps> = (
|
||||
pluralName="Metrics"
|
||||
name="Metrics"
|
||||
isViewable={true}
|
||||
sortBy='name'
|
||||
sortBy="name"
|
||||
sortOrder={SortOrder.Ascending}
|
||||
cardProps={{
|
||||
title: 'Metrics',
|
||||
@@ -37,8 +36,8 @@ const ServiceDelete: FunctionComponent<PageComponentProps> = (
|
||||
groupBy={{
|
||||
name: true,
|
||||
}}
|
||||
onViewPage={async (_item: Metric)=>{
|
||||
return Promise.resolve(new Route(""));
|
||||
onViewPage={async (_item: Metric) => {
|
||||
return Promise.resolve(new Route(''));
|
||||
}}
|
||||
query={{
|
||||
projectId: DashboardNavigation.getProjectId(),
|
||||
@@ -55,7 +54,7 @@ const ServiceDelete: FunctionComponent<PageComponentProps> = (
|
||||
},
|
||||
title: 'Name',
|
||||
type: FieldType.Text,
|
||||
}
|
||||
},
|
||||
]}
|
||||
columns={[
|
||||
{
|
||||
@@ -64,7 +63,7 @@ const ServiceDelete: FunctionComponent<PageComponentProps> = (
|
||||
},
|
||||
title: 'Name',
|
||||
type: FieldType.Text,
|
||||
}
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Fragment>
|
||||
|
||||
Reference in New Issue
Block a user