refactor service code

This commit is contained in:
Simon Larsen
2023-10-23 15:58:16 +01:00
parent 03f9c36f06
commit 2bb4086fd1
2 changed files with 41 additions and 64 deletions

View File

@@ -11,11 +11,11 @@ import BadRequestException from 'Common/Types/Exception/BadRequestException';
import Span from 'Model/AnalyticsModels/Span';
import Log from 'Model/AnalyticsModels/Log';
import OneUptimeDate from 'Common/Types/Date';
import KeyValueNestedModel from 'Model/AnalyticsModels/NestedModels/KeyValueNestedModel';
import SpanService from 'CommonServer/Services/SpanService';
import LogService from 'CommonServer/Services/LogService';
import ObjectID from 'Common/Types/ObjectID';
import { JSONArray, JSONObject } from 'Common/Types/JSON';
import OTelIngestService from '../Service/OTelIngest';
// Load proto file for OTel
// Create a root namespace
@@ -109,37 +109,9 @@ router.post(
dbSpan.name = span['name'] as string;
dbSpan.kind = span['kind'] as string;
// We need to convert this to date.
const attributes: JSONArray = span[
'attributes'
] as JSONArray;
const dbattributes: Array<KeyValueNestedModel> = [];
for (const attribute of attributes) {
const dbattribute: KeyValueNestedModel =
new KeyValueNestedModel();
dbattribute.key = attribute['key'] as string;
const value: JSONObject = attribute[
'value'
] as JSONObject;
if (value['stringValue']) {
dbattribute.stringValue = value[
'stringValue'
] as string;
}
if (value['intValue']) {
dbattribute.numberValue = value[
'intValue'
] as number;
}
dbattributes.push(dbattribute);
}
dbSpan.attributes = dbattributes;
dbSpan.attributes = OTelIngestService.getKeyValues(
span['attributes'] as JSONArray
);
dbSpans.push(dbSpan);
}
@@ -244,38 +216,9 @@ router.post(
dbLog.spanId = log['spanId'] as string;
// We need to convert this to date.
const attributes: JSONArray = log[
'attributes'
] as JSONArray;
if (attributes) {
const dbattributes: Array<KeyValueNestedModel> = [];
for (const attribute of attributes) {
const dbattribute: KeyValueNestedModel =
new KeyValueNestedModel();
dbattribute.key = attribute['key'] as string;
const value: JSONObject = attribute[
'value'
] as JSONObject;
if (value['stringValue']) {
dbattribute.stringValue = value[
'stringValue'
] as string;
}
if (value['intValue']) {
dbattribute.numberValue = value[
'intValue'
] as number;
}
dbattributes.push(dbattribute);
}
dbLog.attributes = dbattributes;
}
dbLog.attributes = OTelIngestService.getKeyValues(
log['attributes'] as JSONArray
);
dbLogs.push(dbLog);
}

View File

@@ -0,0 +1,34 @@
import { JSONArray, JSONObject } from 'Common/Types/JSON';
import KeyValueNestedModel from 'Model/AnalyticsModels/NestedModels/KeyValueNestedModel';
export default class OTelIngestService {
public static getKeyValues(items: JSONArray): Array<KeyValueNestedModel> {
// We need to convert this to date.
const attributes: JSONArray = items;
if (attributes) {
const dbattributes: Array<KeyValueNestedModel> = [];
for (const attribute of attributes) {
const dbattribute: KeyValueNestedModel =
new KeyValueNestedModel();
dbattribute.key = attribute['key'] as string;
const value: JSONObject = attribute['value'] as JSONObject;
if (value['stringValue']) {
dbattribute.stringValue = value['stringValue'] as string;
}
if (value['intValue']) {
dbattribute.numberValue = value['intValue'] as number;
}
dbattributes.push(dbattribute);
}
return dbattributes;
}
return [];
}
}