diff --git a/Common/AnalyticsModels/BaseModel.ts b/Common/AnalyticsModels/BaseModel.ts index 7357ae69a0..b2f30df643 100644 --- a/Common/AnalyticsModels/BaseModel.ts +++ b/Common/AnalyticsModels/BaseModel.ts @@ -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; diff --git a/Common/AnalyticsModels/CommonModel.ts b/Common/AnalyticsModels/CommonModel.ts index e098455208..9606936349 100644 --- a/Common/AnalyticsModels/CommonModel.ts +++ b/Common/AnalyticsModels/CommonModel.ts @@ -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'); diff --git a/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts b/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts index 97a3ed7922..bacfa65e90 100644 --- a/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts +++ b/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts @@ -215,6 +215,7 @@ export default class StatementGenerator { } if (column.type === TableColumnType.Date && value instanceof Date) { + value = `parseDateTimeBestEffortOrNull('${OneUptimeDate.toString( value as Date )}')`; diff --git a/Model/AnalyticsModels/MetricHistogram.ts b/Model/AnalyticsModels/MetricHistogram.ts index 21aeda5143..9004b97056 100644 --- a/Model/AnalyticsModels/MetricHistogram.ts +++ b/Model/AnalyticsModels/MetricHistogram.ts @@ -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({ diff --git a/Model/AnalyticsModels/MetricSum.ts b/Model/AnalyticsModels/MetricSum.ts index 86187020fc..58d56ac14b 100644 --- a/Model/AnalyticsModels/MetricSum.ts +++ b/Model/AnalyticsModels/MetricSum.ts @@ -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({