fix models

This commit is contained in:
Simon Larsen 2023-10-24 10:46:44 +01:00
parent e34599d18a
commit 0bdab474de
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
5 changed files with 34 additions and 9 deletions

View File

@ -70,15 +70,24 @@ export default class AnalyticsDataModel extends CommonModel {
// check if primary keys are subset of tableColumns
data.primaryKeys.forEach((primaryKey: string) => {
const column: AnalyticsTableColumn | undefined = columns.find((column: AnalyticsTableColumn) => {
return column.key === primaryKey;
});
if (
!columns.find((column: AnalyticsTableColumn) => {
return column.key === primaryKey;
})
!column
) {
throw new BadDataException(
'Primary key ' + primaryKey + ' is not part of tableColumns'
);
}
if(!column.required){
throw new BadDataException(
'Primary key ' + primaryKey + ' is not required. Primary keys must be required.'
);
}
});
this.primaryKeys = data.primaryKeys;

View File

@ -59,6 +59,21 @@ export default class CommonModel {
value = JSON.parse(value);
}
if (
column.type === TableColumnType.Number &&
typeof value === 'string'
) {
value = parseInt(value);
}
// decimal
if (
column.type === TableColumnType.Decimal &&
typeof value === 'string'
) {
value = parseFloat(value);
}
return (this.data[columnName] = value as any);
}
throw new BadDataException('Column ' + columnName + ' does not exist');

View File

@ -215,6 +215,7 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
}
if (column.type === TableColumnType.Date && value instanceof Date) {
value = `parseDateTimeBestEffortOrNull('${OneUptimeDate.toString(
value as Date
)}')`;

View File

@ -51,7 +51,7 @@ export default class Metric extends AnalyticsBaseModel {
key: 'time',
title: 'Time',
description: 'When did the Metric happen?',
required: false,
required: true,
type: TableColumnType.Date,
}),
@ -68,7 +68,7 @@ export default class Metric extends AnalyticsBaseModel {
title: 'Time (in Unix Nano)',
description: 'When did the Metric happen?',
required: false,
type: TableColumnType.Date,
type: TableColumnType.Number,
}),
new AnalyticsTableColumn({
@ -76,7 +76,7 @@ export default class Metric extends AnalyticsBaseModel {
title: 'Start Time (in Unix Nano)',
description: 'When did the Metric happen?',
required: false,
type: TableColumnType.Date,
type: TableColumnType.Number,
}),
new AnalyticsTableColumn({

View File

@ -51,7 +51,7 @@ export default class Metric extends AnalyticsBaseModel {
key: 'time',
title: 'Time',
description: 'When did the Metric happen?',
required: false,
required: true,
type: TableColumnType.Date,
}),
@ -68,7 +68,7 @@ export default class Metric extends AnalyticsBaseModel {
title: 'Time (in Unix Nano)',
description: 'When did the Metric happen?',
required: false,
type: TableColumnType.Date,
type: TableColumnType.Number,
}),
new AnalyticsTableColumn({
@ -76,7 +76,7 @@ export default class Metric extends AnalyticsBaseModel {
title: 'Start Time (in Unix Nano)',
description: 'When did the Metric happen?',
required: true,
type: TableColumnType.Date,
type: TableColumnType.Number,
}),
new AnalyticsTableColumn({