Skip to content

Commit

Permalink
#17 add sync job
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixNazarov committed Jul 31, 2024
1 parent 6c68c02 commit 52ff64a
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 15 deletions.
18 changes: 17 additions & 1 deletion client/src/plugins/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {useOutputStore} from "../stores/config/output.store.ts";
import {usePromptStore} from "../stores/prompt.store.ts";
import {useMacroStore} from "../stores/config/macro.store.ts";
import {useInputStore} from "../stores/config/input.store.ts";
import {useVarsStore} from "../stores/vars.store.ts";


function tableIdProp(id: string): number | undefined {
Expand Down Expand Up @@ -241,7 +242,18 @@ const router = createRouter({
groupId: Number.parseInt(route.params.groupId as string)
}
}
}
},
{
name: 'ProjectVars',
path: 'vars',
component: () => import('../views/Project/ProjectVarView.vue'),
props: route => {
useVarsStore().load(route.params.project as string)
return {
project: route.params.project as string,
}
}
},
]
}
]
Expand Down Expand Up @@ -290,6 +302,10 @@ export class RouterService {
static async goToProjectGroup(project: string, groupId: number | undefined) {
await router.push(`/project/${project}/group/${groupId == undefined ? -1 : groupId}`)
}

static async goToProjectVars(project: string) {
await router.push(`/project/${project}/vars`)
}
}


Expand Down
35 changes: 31 additions & 4 deletions client/src/stores/vars.store.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
import {defineStore} from "pinia";
import {ApiService} from "../api/ApiService.ts";

export interface Variable {
key: string
value: string
}


export const useVarsStore = defineStore({
id: 'vars',
state: () => ({
vars: new Map<string, Map<string, string>>,
loadings: {}
vars: new Map<string, Variable[]>,
loadings: {
load: false
}
}),
getters: {},
getters: {
getByProject: state => {
return (project: string) => {
const vars = state.vars.get(project)
return vars ? vars : []
}
},
},
actions: {
async load(project: string | undefined) {
if (!project) return {}
const vars = this.vars.get(project)
if (vars) {
return vars
}
const loadVars = await ApiService.post<Map<string, string>>('/api/vars/load', {project: project})
this.loadings.load = true
const loadVars = await ApiService.post<Variable[]>('/api/vars/load', {project: project})
this.vars.set(project, loadVars)
this.loadings.load = false
return loadVars
},
async change(project: string, key: string, value: string) {
await ApiService.post('/api/vars/change', {project: project, key: key, value: value})
},
async remove(project: string, key: string) {
await ApiService.post('/api/vars/remove', {project: project, key: key})
await this.load(project)
},
async create(project: string, key: string, value: string) {
await ApiService.post('/api/vars/create', {project: project, key: key, value: value})
}
}
})
5 changes: 5 additions & 0 deletions client/src/views/Project/ProjectMainLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ export default defineComponent({
]
}
]]
},
{
label: 'Variables',
icon: 'pi pi-server',
command: () => RouterService.goToProjectVars(project)
}
]"/>
</VCol>
Expand Down
136 changes: 136 additions & 0 deletions client/src/views/Project/ProjectVarView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<script lang="ts">
import {defineComponent} from 'vue'
import {useVarsStore} from "../../stores/vars.store.ts";
import ProjectMainView from "./ProjectMainLayout.vue";
import {cropText} from "../Utils.ts";
import InputList from "../Tables/Edit/InputList.vue";
import RemoveButton from "../Tables/Edit/Components/RemoveButton.vue";
import {FontAwesomeIcon} from "@fortawesome/vue-fontawesome";
export default defineComponent({
name: "ProjectVarView",
components: {FontAwesomeIcon, RemoveButton, InputList, ProjectMainView},
props: {
project: {
type: String,
required: true
}
},
setup() {
const varsStore = useVarsStore()
return {
varsStore
}
},
data() {
return {
headers: [
{title: 'Key', key: 'key'},
{title: 'Value', key: 'value'},
{title: 'Action', value: 'actions'}
],
newVar: {
key: '',
value: ''
},
loadingSave: false
}
},
methods: {
cropText,
async save(isActive: any) {
this.loadingSave = true
await this.varsStore.create(this.project, this.newVar.key, this.newVar.value)
this.loadingSave = false
isActive.value = false
},
async change(key: string, value: string) {
this.loadingSave = true
await this.varsStore.change(this.project, key, value)
this.loadingSave = false
}
}
})
</script>

<template>
<ProjectMainView :project="project">
<VDataTable
:headers="headers"
:items="varsStore.getByProject(project)"
density="compact"
variant="outlined"
:loading="varsStore.loadings.load"
>
<template v-slot:top>
<VToolbar
flat
color="transparent"
density="compact"
>
<VToolbarTitle>Variables {{ project }}</VToolbarTitle>
<VDialog>
<template v-slot:activator="{ props: activatorProps }">
<VBtn
variant="outlined"
dark
text="New Item"
v-bind="activatorProps"
/>
</template>
<template v-slot:default="{ isActive }">
<VCard>
<VCardText>
<VRow>
<VTextField v-model="newVar.key" label="Key"/>
</VRow>
<VRow>
<VTextarea v-model="newVar.value" label="Value"/>
</VRow>
<VRow>
<VBtn :loading="loadingSave" class="mr-2" color="success" text="Save" @click.prevent="save(isActive)"/>
</VRow>
</VCardText>
</VCard>
</template>
</VDialog>
</VToolbar>
</template>
<template v-slot:loading>
<VSkeletonLoader type="table-row@5"></VSkeletonLoader>
</template>
<template v-slot:item.value="{ item }">
{{ cropText(item.value!) }}
</template>
<template v-slot:item.actions="{ item }">
<VDialog>
<template v-slot:activator="{ props: activatorProps }">
<FontAwesomeIcon class="pointer" style="margin-right: 0.5rem" icon="fa-pen" v-bind="activatorProps"/>
</template>
<template v-slot:default="{ isActive }">
<VCard>
<VCardText>
<VRow>
<VTextField v-model="item.key" label="Key"/>
</VRow>
<VRow>
<VTextarea v-model="item.value" label="Value"/>
</VRow>
<VRow>
<VBtn :loading="loadingSave" class="mr-2" color="success" text="Save"
@click.prevent="change(item.key, item.value!)"/>
</VRow>
</VCardText>
</VCard>
</template>
</VDialog>
<RemoveButton variant="icon" :text="cropText(item.value!)"
:remove="() => varsStore.remove(project, item.key)"/>
</template>
</VDataTable>
</ProjectMainView>
</template>

<style scoped>
</style>
4 changes: 3 additions & 1 deletion client/src/views/Workplace/Hint/HintSyncData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export default defineComponent({
const mapping = this.mapping()
if (!mapping) return []
await this.varsStore.load(mapping.connection_name)
this.context = {...this.template_context_default, var: this.varsStore.vars.get(mapping.connection_name)}
const v = {}
this.varsStore.vars.get(mapping.connection_name)?.forEach(vr => v[vr.key] = vr.value)
this.context = {...this.template_context_default, var: v}
},
mapping(): Mapping | undefined {
if (this.prompt) return this.mappingStore.getById(this.prompt.mapping_id)
Expand Down
14 changes: 7 additions & 7 deletions server/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion server/promptadmin_server/api/routers/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,47 @@ class ProjectDto(BaseModel):
project: str


class VarKeyDto(ProjectDto):
key: str


class VarDto(VarKeyDto):
value: str


@router.post('/load')
async def load(project_dto: ProjectDto):
connection = SETTINGS.connections.get(project_dto.project)
if connection is None:
return {}
return await VarService(connection).collect_vars()
return [
{
'key': i[0],
'value': i[1]
}
for i in list((await VarService(connection).collect_vars()).items())
]


@router.post('/create')
async def create(var_dto: VarDto):
connection = SETTINGS.connections.get(var_dto.project)
if connection is None:
return {}
return await VarService(connection).create(var_dto.key, var_dto.value)


@router.post('/remove')
async def remove(var_key_dto: VarKeyDto):
connection = SETTINGS.connections.get(var_key_dto.project)
if connection is None:
return {}
return await VarService(connection).remove(var_key_dto.key)


@router.post('/change')
async def change(var_dto: VarDto):
connection = SETTINGS.connections.get(var_dto.project)
if connection is None:
return {}
return await VarService(connection).change(var_dto.key, var_dto.value)
2 changes: 1 addition & 1 deletion server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ starlette = "^0.37.2"
uvicorn = "^0.29.0"
Jinja2 = "^3.1.2"
itsdangerous = "^2.2.0"
prompt-admin = "^0.1.8"
prompt-admin = "^0.1.9"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.3"
Expand Down

0 comments on commit 52ff64a

Please sign in to comment.