Skip to content

Commit

Permalink
IR-6300 Enhance engine settings handling with data type parsing and u…
Browse files Browse the repository at this point in the history
…pdates (#861)

* Enhance engine settings handling with data type parsing and updates

* fix tests : add dataType parameter to createEngineSetting function

* replace old migration with new data type column addition for engine settings

* Update engine settings migration to handle boolean value conversion
  • Loading branch information
mshafiqmk authored Dec 19, 2024
1 parent 34cd9a2 commit cb1b959
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { HiMinus, HiPlusSmall } from 'react-icons/hi2'
import { useFind, useMutation } from '@ir-engine/common'
import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, helmVersionPath } from '@ir-engine/common/src/schema.type.module'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { useHookstate } from '@ir-engine/hyperflux'
import { Button, Select } from '@ir-engine/ui'
import Accordion from '@ir-engine/ui/src/primitives/tailwind/Accordion'
Expand Down Expand Up @@ -99,6 +100,7 @@ const HelmTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRefOb
return helmMutation.create({
key,
category: 'helm',
dataType: getDataType(setting[key]),
value: setting[key],
type: 'private'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Infinite Reality Engine. All Rights Reserved.
import { useFind, useMutation } from '@ir-engine/common'
import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { EngineSettingData, EngineSettingType, engineSettingPath } from '@ir-engine/common/src/schema.type.module'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { useHookstate } from '@ir-engine/hyperflux'
import { Button, Input } from '@ir-engine/ui'
import PasswordInput from '@ir-engine/ui/src/components/tailwind/PasswordInput'
Expand Down Expand Up @@ -97,6 +98,7 @@ const MetabaseTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableR
key,
category: 'metabase',
value: setting[key],
dataType: getDataType(setting[key]),
type: 'private'
})
} else if (settingInDb.value !== setting[key]) {
Expand All @@ -105,6 +107,7 @@ const MetabaseTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableR
key,
category: 'metabase',
value: setting[key],
dataType: getDataType(setting[key]),
type: 'private'
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { HiMinus, HiPlusSmall } from 'react-icons/hi2'
import { useFind, useMutation } from '@ir-engine/common'
import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { EngineSettingData, EngineSettingType, engineSettingPath } from '@ir-engine/common/src/schema.type.module'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { useHookstate } from '@ir-engine/hyperflux'
import { Button, Input } from '@ir-engine/ui'
import PasswordInput from '@ir-engine/ui/src/components/tailwind/PasswordInput'
Expand Down Expand Up @@ -104,13 +105,15 @@ const ServerTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRef
key,
category: 'server',
value: settings[key],
dataType: getDataType(settings[key]),
type: 'private'
})
} else if (settingInDb.value !== settings[key]) {
operations.push(
engineSettingMutation.patch(settingInDb.id, {
key,
category: 'server',
dataType: getDataType(settings[key]),
value: settings[key],
type: 'private'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { HiMinus, HiPlusSmall } from 'react-icons/hi2'
import { useFind, useMutation } from '@ir-engine/common'
import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath } from '@ir-engine/common/src/schema.type.module'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { useHookstate } from '@ir-engine/hyperflux'
import { Button, Input } from '@ir-engine/ui'
import PasswordInput from '@ir-engine/ui/src/components/tailwind/PasswordInput'
Expand Down Expand Up @@ -83,13 +84,15 @@ const ZendeskTab = forwardRef(({ open }: { open: boolean }, ref: React.MutableRe
return zendeskMutation.create({
key,
category: 'zendesk',
dataType: getDataType(setting[key]),
value: setting[key],
type: 'private'
})
}
return zendeskMutation.patch(settingInDb.id, {
key,
category: 'zendesk',
dataType: getDataType(setting[key]),
value: setting[key],
type: 'private'
})
Expand Down
24 changes: 18 additions & 6 deletions packages/common/src/schemas/setting/engine-setting.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,41 @@ export const engineSettingSchema = Type.Object(
})
),
updatedAt: Type.String({ format: 'date-time' }),
createdAt: Type.String({ format: 'date-time' })
createdAt: Type.String({ format: 'date-time' }),
dataType: StringEnum(['string', 'integer', 'boolean'])
},
{ $id: 'EngineSetting', additionalProperties: false }
)
export interface EngineSettingType extends Static<typeof engineSettingSchema> {}

// Schema for creating new entries
export const engineSettingDataSchema = Type.Pick(engineSettingSchema, ['key', 'value', 'type', 'category'], {
$id: 'EngineSettingData'
})
export const engineSettingDataSchema = Type.Pick(
engineSettingSchema,
['key', 'value', 'type', 'category', 'dataType'],
{
$id: 'EngineSettingData'
}
)
export interface EngineSettingData extends Static<typeof engineSettingDataSchema> {}

// Schema for updating existing entries
export const engineSettingPatchSchema = Type.Partial(
Type.Pick(engineSettingSchema, ['key', 'value', 'type', 'category']),
Type.Pick(engineSettingSchema, ['key', 'value', 'type', 'category', 'dataType']),
{
$id: 'EngineSettingPatch'
}
)
export interface EngineSettingPatch extends Static<typeof engineSettingPatchSchema> {}

// Schema for allowed query properties
export const engineSettingQueryProperties = Type.Pick(engineSettingSchema, ['id', 'key', 'value', 'type', 'category'])
export const engineSettingQueryProperties = Type.Pick(engineSettingSchema, [
'id',
'key',
'value',
'type',
'category',
'dataType'
])
export const engineSettingQuerySchema = Type.Intersect(
[
querySyntax(engineSettingQueryProperties),
Expand Down
71 changes: 71 additions & 0 deletions packages/common/src/utils/dataTypeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
CPAL-1.0 License
The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/ir-engine/ir-engine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is Infinite Reality Engine.
The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Infinite Reality Engine team.
All portions of the code written by the Infinite Reality Engine team are Copyright © 2021-2023
Infinite Reality Engine. All Rights Reserved.
*/

import { EngineSettingType } from '../schema.type.module'

/**
* Determines the data type of a given value.
*
* @param value - The value to determine the data type of.
* @returns The data type of the value, which can be 'string', 'boolean', or 'integer'.
*/
export const getDataType = (value: any): EngineSettingType['dataType'] => {
let dataType = 'string'
const normalizedValue = value.toLowerCase()
if (normalizedValue === 'true' || value.toLowerCase() === 'false') {
dataType = 'boolean'
} else if (isValidInteger(value)) {
dataType = 'integer'
}
return dataType as EngineSettingType['dataType']
}

/**
* Parses a string value based on the specified data type.
*
* @param value - The string value to be parsed.
* @param dataType - The data type to which the value should be parsed.
* It can be 'string', 'boolean', or 'integer'.
* @returns The parsed value in the appropriate data type.
*/
export const parseValue = (value: string, dataType: EngineSettingType['dataType']): any =>
dataType === 'string'
? value
: dataType === 'boolean'
? value === 'true'
: dataType === 'integer'
? Number(value)
: value

function isValidInteger(value) {
// Ensure the value is a non-empty string and does not contain non-numeric characters
if (typeof value !== 'string' || value.trim() === '') return false

// Convert the string to a number
const number = Number(value)

// Check if it's an integer and not NaN
return Number.isInteger(number)
}
8 changes: 5 additions & 3 deletions packages/server-core/src/appconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ import { identityProviderPath } from '@ir-engine/common/src/schemas/user/identit
import { loginPath } from '@ir-engine/common/src/schemas/user/login.schema'

import { HookContext } from '@feathersjs/feathers'
import { instanceSignalingPath, projectsPath } from '@ir-engine/common/src/schema.type.module'
import { EngineSettingType, instanceSignalingPath, projectsPath } from '@ir-engine/common/src/schema.type.module'
import { jwtPublicKeyPath } from '@ir-engine/common/src/schemas/user/jwt-public-key.schema'
import { parseValue } from '@ir-engine/common/src/utils/dataTypeUtils'
import { createHash } from 'crypto'
import {
APPLE_SCOPES,
Expand Down Expand Up @@ -481,15 +482,16 @@ chargebeeInst.configure({
* @param value - The value to set for the nested configuration.
* @param category - The category of the configuration.
*/
export function updateNestedConfig(appConfig: Record<string, any>, key: string, value: string, category: string) {
export function updateNestedConfig(appConfig: Record<string, any>, setting: EngineSettingType) {
const { key, value, dataType, category } = setting
const keys = key.split('.')
if (keys.length !== 2) {
return
}
if (!appConfig[category][keys[0]]) {
appConfig[category][keys[0]] = {}
}
appConfig[category][keys[0]][keys[1]] = value
appConfig[category][keys[0]][keys[1]] = parseValue(value, dataType)
}

export default config
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { v4 as uuidv4 } from 'uuid'

import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, EngineSettingType } from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { getDateTimeSql } from '@ir-engine/common/src/utils/datetime-sql'
import appConfig from '@ir-engine/server-core/src/appconfig'
import appRootPath from 'app-root-path'
Expand All @@ -54,7 +55,7 @@ export async function seed(knex: Knex): Promise<void> {
],
'chargebee'
)
const zendeskSettingSeedData: EngineSettingType[] = await Promise.all(
const zendeskSettingSeedData = await generateSeedData(
[
{
key: EngineSettings.Zendesk.Name,
Expand All @@ -68,14 +69,8 @@ export async function seed(knex: Knex): Promise<void> {
key: EngineSettings.Zendesk.Kid,
value: process.env.ZENDESK_KID || ''
}
].map(async (item) => ({
...item,
id: uuidv4(),
type: 'private' as EngineSettingType['type'],
category: 'zendesk' as EngineSettingType['category'],
createdAt: await getDateTimeSql(),
updatedAt: await getDateTimeSql()
}))
],
'zendesk'
)

const coilSeedData = await generateSeedData(
Expand Down Expand Up @@ -220,6 +215,7 @@ export async function seed(knex: Knex): Promise<void> {
...chargebeeSettingSeedData,
...coilSeedData,
...metabaseSeedData,
...serverSeedData,
...redisSeedData,
...zendeskSettingSeedData,
...helmSeedData
Expand Down Expand Up @@ -251,6 +247,7 @@ export async function generateSeedData(
items.map(async (item) => ({
...item,
id: uuidv4(),
dataType: getDataType(item.value),
type: type,
category: category,
createdAt: await getDateTimeSql(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
engineSettingPath,
EngineSettingType
} from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { parseValue } from '@ir-engine/common/src/utils/dataTypeUtils'
import { Application } from '@ir-engine/server-core/declarations'
import appConfig, { updateNestedConfig } from '../../appconfig'
import { EngineSettingService } from './engine-setting.class'
Expand Down Expand Up @@ -63,9 +64,9 @@ export default (app: Application): void => {
args.forEach((setting) => {
if (appConfig[setting.category]) {
if (setting.key.includes('.')) {
updateNestedConfig(appConfig, setting.key, setting.value, setting.category)
updateNestedConfig(appConfig, setting)
} else {
appConfig[setting.category][setting.key] = setting.value
appConfig[setting.category][setting.key] = parseValue(setting.value, setting.dataType)
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Infinite Reality Engine. All Rights Reserved.

import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, EngineSettingType } from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { getDateTimeSql } from '@ir-engine/common/src/utils/datetime-sql'
import type { Knex } from 'knex'
import { v4 as uuidv4 } from 'uuid'
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function up(knex: Knex): Promise<void> {
].map(async (item) => ({
...item,
id: uuidv4(),
dataType: getDataType(item.value),
type: 'private' as EngineSettingType['type'],
category: 'task-server' as EngineSettingType['category'],
createdAt: await getDateTimeSql(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Infinite Reality Engine. All Rights Reserved.

import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, EngineSettingType } from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { getDateTimeSql } from '@ir-engine/common/src/utils/datetime-sql'
import type { Knex } from 'knex'
import { v4 as uuidv4 } from 'uuid'
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function up(knex: Knex): Promise<void> {
].map(async (item) => ({
...item,
id: uuidv4(),
dataType: getDataType(item.value),
type: 'private' as EngineSettingType['type'],
category: 'chargebee' as EngineSettingType['category'],
createdAt: await getDateTimeSql(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Infinite Reality Engine. All Rights Reserved.

import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, EngineSettingType } from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { getDateTimeSql } from '@ir-engine/common/src/utils/datetime-sql'
import type { Knex } from 'knex'
import { v4 as uuidv4 } from 'uuid'
Expand Down Expand Up @@ -58,6 +59,7 @@ export async function up(knex: Knex): Promise<void> {
}
].map(async (item) => ({
...item,
dataType: getDataType(item.value),
id: uuidv4(),
type: 'private' as EngineSettingType['type'],
category: 'coil' as EngineSettingType['category'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Infinite Reality Engine. All Rights Reserved.

import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, EngineSettingType } from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { getDateTimeSql } from '@ir-engine/common/src/utils/datetime-sql'
import type { Knex } from 'knex'
import { v4 as uuidv4 } from 'uuid'
Expand Down Expand Up @@ -59,6 +60,7 @@ export async function up(knex: Knex): Promise<void> {
].map(async (item) => ({
...item,
id: uuidv4(),
dataType: getDataType(item.value),
type: 'private' as EngineSettingType['type'],
category: 'zendesk' as EngineSettingType['category'],
createdAt: await getDateTimeSql(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Infinite Reality Engine. All Rights Reserved.

import { EngineSettings } from '@ir-engine/common/src/constants/EngineSettings'
import { engineSettingPath, EngineSettingType } from '@ir-engine/common/src/schemas/setting/engine-setting.schema'
import { getDataType } from '@ir-engine/common/src/utils/dataTypeUtils'
import { getDateTimeSql } from '@ir-engine/common/src/utils/datetime-sql'
import type { Knex } from 'knex'
import { v4 as uuidv4 } from 'uuid'
Expand Down Expand Up @@ -66,6 +67,7 @@ export async function up(knex: Knex): Promise<void> {
].map(async (item) => ({
...item,
id: uuidv4(),
dataType: getDataType(item.value),
type: 'private' as EngineSettingType['type'],
category: 'metabase' as EngineSettingType['category'],
createdAt: await getDateTimeSql(),
Expand Down
Loading

0 comments on commit cb1b959

Please sign in to comment.