Skip to content

Commit

Permalink
#9 create sort for prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixNazarov committed Jul 20, 2024
1 parent 1d26dab commit 3e6787b
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 11 deletions.
2 changes: 2 additions & 0 deletions client/src/stores/config/mapping.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface Mapping extends BaseEntity {

description: string
field_name: string
field_order?: string
desc?: boolean
connection_name: string
}

Expand Down
1 change: 1 addition & 0 deletions client/src/stores/prompt.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Prompt {
value: string
name?: string
preview?: boolean
sort_value?: any

auditData?: {
audit: PromptAudit,
Expand Down
2 changes: 1 addition & 1 deletion client/src/views/Tables/Edit/InputItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default defineComponent({
default_type: this.default_type,
default: this.default_,
})
await RouterService.goToTableItem('macro', result.id)
await RouterService.goToTableItem('input', result.id)
this.loadingSave = false
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/views/Tables/Edit/MappingEntityItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default defineComponent({
entity_id: this.entity_id_,
id: this.id,
})
await RouterService.goToTableItem('output', result.id)
await RouterService.goToTableItem('mapping-entity', result.id)
this.loadingSave = false
}
}
Expand Down
17 changes: 15 additions & 2 deletions client/src/views/Tables/Edit/MappingItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default defineComponent({
description: mapping?.description,
field_name: mapping?.field_name,
connection_name: mapping?.connection_name,
field_order: mapping?.field_order,
desc: mapping?.desc,
loadingSave: false
}
Expand All @@ -47,9 +49,11 @@ export default defineComponent({
field: this.field,
description: this.description,
field_name: this.field_name,
connection_name: this.connection_name
connection_name: this.connection_name,
field_order: this.field_order,
desc: this.desc
})
await RouterService.goToTableItem('output', result.id)
await RouterService.goToTableItem('mapping', result.id)
this.loadingSave = false
}
}
Expand Down Expand Up @@ -87,6 +91,15 @@ export default defineComponent({
hint="Description of the remote product" persistent-hint/>
</VCol>
</VRow>
<VRow>
<VCol>
<VTextField density="compact" label="Order Field" v-model="field_order" variant="outlined"
hint="Field for sort" persistent-hint/>
</VCol>
<VCol>
<VSwitch v-model="desc" label="Descending" density="compact"/>
</VCol>
</VRow>
<VRow>
<VCol>
<VBtn :loading="loadingSave" class="mr-2" color="success" text="Save" @click.prevent="save"/>
Expand Down
31 changes: 28 additions & 3 deletions client/src/views/Workplace/Menu/WorkplaceMenuView.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<script lang="ts">
import {defineComponent} from 'vue'
import {usePromptStore} from "../../../stores/prompt.store.ts";
import {useMappingStore} from "../../../stores/config/mapping.store.ts";
import {Prompt, usePromptStore} from "../../../stores/prompt.store.ts";
import {Mapping, useMappingStore} from "../../../stores/config/mapping.store.ts";
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome'
import {useSettingsStore} from "../../../stores/config/settings.store.ts";
export function hashCode(str: string | number | undefined): number {
const newStr = String(str)
let h: number = 0;
for (let i = 0; i < newStr.length; i++) {
h = 31 * h + newStr.charCodeAt(i);
}
return h & 0xFFFFFFFF
}
export default defineComponent({
name: "WorkplaceMenuView",
components: {FontAwesomeIcon},
Expand All @@ -28,6 +37,22 @@ export default defineComponent({
opened(newVal) {
this.settingsStore.menuOpenedItems = newVal
}
},
methods: {
sortPrompts(mapping: Mapping, prompts: Prompt[]) {
if (!mapping.field_order) {
return prompts.sort((a, b) => {
return hashCode(a.name) - hashCode(b.name)
})
}
return prompts.sort((a, b) => {
if (mapping.desc) {
return hashCode(b.sort_value) - hashCode(a.sort_value)
} else {
return hashCode(a.sort_value) - hashCode(b.sort_value)
}
})
}
}
})
</script>
Expand Down Expand Up @@ -75,7 +100,7 @@ export default defineComponent({
v-if="promptStore.loadings.loadAll"
></VSkeletonLoader>
<VListItem
v-for="prompt in promptStore.promptsByMapping(mapping.id)"
v-for="prompt in sortPrompts(mapping, promptStore.promptsByMapping(mapping.id))"
@click.prevent="$emit('selectPrompt', prompt)"
>
{{ prompt.name }}
Expand Down
1 change: 1 addition & 0 deletions server/promptadmin/api/dto/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ class Prompt(BaseModel):
id: int
value: str
name: str | None
sort_value: str | int | None = None
6 changes: 4 additions & 2 deletions server/promptadmin/api/routers/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ async def load_mapping(mapping: Mapping) -> list[Prompt]:
return []

mapping_name = f', {mapping.field_name}' if mapping.field_name else ''
order_name = f', {mapping.field_order}' if mapping.field_order else ''

row = await conn.fetch(f'SELECT id, {mapping.field} {mapping_name} FROM {mapping.table}')
row = await conn.fetch(f'SELECT id, {mapping.field} {mapping_name} {order_name} FROM {mapping.table}')

result = []
for i in row:
Expand All @@ -50,7 +51,8 @@ async def load_mapping(mapping: Mapping) -> list[Prompt]:
field=mapping.field,
id=i['id'],
value=i[mapping.field],
name=i.get(mapping.field_name)
name=i.get(mapping.field_name),
sort_value=i.get(mapping.field_order)
)
)
return result
Expand Down
4 changes: 2 additions & 2 deletions server/promptadmin/data/entity/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class MappingData(BaseModel):

description: str
field_name: str | None = None
# field_order: str | None = None
# desc: bool = False
field_order: str | None = None
desc: bool = False
connection_name: str


Expand Down
2 changes: 2 additions & 0 deletions server/promptadmin/data/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ create table pa_mapping
field varchar(128) not null,
description varchar(1000) not null,
field_name varchar(128),
field_order varchar(128),
"desc" boolean default false,
connection_name varchar(128) not null
);

Expand Down

0 comments on commit 3e6787b

Please sign in to comment.