Skip to content
Snippets Groups Projects
Commit 4087f324 authored by ml bonhomme's avatar ml bonhomme :bee: Committed by Erwan Rouchet
Browse files

Typescriptify the entity API helpers

parent 5a7ad6da
No related branches found
No related tags found
1 merge request!1542Typescriptify the entity API helpers
import axios from 'axios'
import { unique } from '.'
// Retrieve an entity
export const retrieveEntity = unique(async id => (await axios.get(`/entity/${id}/`)).data)
// List entities in a corpus
export const listCorpusEntities = unique(async ({ id, ...params }) => (await axios.get(`/corpus/${id}/entities/`, { params })).data)
// List all links for an element
export const listElementLinks = unique(async id => (await axios.get(`/element/${id}/links/`)).data)
// List all elements linked to an entity
export const listEntityElements = unique(async ({ id, ...params }) => (await axios.get(`/entity/${id}/elements/`, { params })).data)
// List all entities linked to a transcription
export const listTranscriptionEntities = unique(async ({ id, ...params }) => (await axios.get(`/transcription/${id}/entities/`, { params })).data)
// List a corpus's entity types
export const listCorpusEntityTypes = unique(async ({ id, ...params }) => (await axios.get(`/corpus/${id}/entity-types/`, { params })).data)
// Create a new corpus entity type
export const createCorpusEntityType = async type => (await axios.post('/entity/types/', type)).data
// Edit a corpus entity type
export const updateCorpusEntityType = async ({ id, ...data }) => (await axios.patch(`/entity/types/${id}/`, data)).data
// Delete a corpus entity type
export const deleteCorpusEntityType = unique(async ({ id, ...params }) => (await axios.delete(`/entity/types/${id}/`, { params })).data)
import axios from 'axios'
import { PageNumberPaginationParameters, unique } from '.'
import { PageNumberPagination, UUID } from '@/types'
import { Entity, EntityLight, EntityType, TranscriptionEntity } from '@/types/entity'
// Retrieve an entity
export const retrieveEntity = unique(async (id: UUID): Promise<Entity> => (await axios.get(`/entity/${id}/`)).data)
interface CorpusEntitiesListParameters extends PageNumberPaginationParameters {
name?: string
parent?: UUID
}
// List entities in a corpus
export const listCorpusEntities = unique(async (id: UUID, params: CorpusEntitiesListParameters = {}): Promise<PageNumberPagination<EntityLight>> => (await axios.get(`/corpus/${id}/entities/`, { params })).data)
// List all links for an element
export const listElementLinks = unique(async (id: UUID) => (await axios.get(`/element/${id}/links/`)).data)
// List all elements linked to an entity
export const listEntityElements = unique(async (id: UUID, params: PageNumberPaginationParameters = {}) => (await axios.get(`/entity/${id}/elements/`, { params })).data)
interface TranscriptionEntityListParameters extends PageNumberPaginationParameters {
entity_worker_run?: UUID | false
entity_worker_version?: UUID | false
worker_run?: UUID | false
worker_version?: UUID | false
}
// List all entities linked to a transcription
export const listTranscriptionEntities = unique(async (id: UUID, params: TranscriptionEntityListParameters = {}): Promise<PageNumberPagination<TranscriptionEntity>> => (await axios.get(`/transcription/${id}/entities/`, { params })).data)
// List a corpus's entity types
export const listCorpusEntityTypes = unique(async (id: UUID, params: PageNumberPaginationParameters = {}): Promise<PageNumberPagination<EntityType>> => (await axios.get(`/corpus/${id}/entity-types/`, { params })).data)
type EntityTypeCreatePayload = Omit<EntityType, 'id'>
type EntityTypeUpdatePayload = Partial<EntityTypeCreatePayload>
// Create a new corpus entity type
export const createCorpusEntityType = async (type: EntityTypeCreatePayload): Promise<EntityType> => (await axios.post('/entity/types/', type)).data
// Edit a corpus entity type
export const updateCorpusEntityType = async (id: UUID, data: EntityTypeUpdatePayload): Promise<EntityType> => (await axios.patch(`/entity/types/${id}/`, data)).data
// Delete a corpus entity type
export const deleteCorpusEntityType = unique(async (id: UUID) => (await axios.delete(`/entity/types/${id}/`)).data)
......@@ -34,11 +34,11 @@
<script lang="ts">
import { mapState } from 'pinia'
import { EntityTranscriptionLight, parseEntities } from '@/helpers'
import { TranscriptionEntityLight, parseEntities } from '@/helpers'
import { useDisplayStore } from '@/stores'
import { defineComponent } from 'vue'
import type { PropType } from 'vue'
import { Entity, EntityType } from '@/types'
import { Entity, EntityType } from '@/types/entity'
export default defineComponent({
name: 'Token',
......@@ -57,7 +57,7 @@ export default defineComponent({
required: true
},
children: {
type: Array as PropType<EntityTranscriptionLight[]>,
type: Array as PropType<TranscriptionEntityLight[]>,
default: () => []
},
// Show a warning about an entity overflowing its parent
......
import { EntityTranscription } from '@/types'
import { TranscriptionEntity } from '@/types/entity'
import { orderBy } from 'lodash'
export interface Token {
offset: number,
text: string,
entity?: object,
children: EntityTranscriptionLight[],
children: TranscriptionEntityLight[],
overflow?: boolean
}
export type EntityTranscriptionLight = Omit<EntityTranscription, 'worker_run' | 'worker_version_id' | 'confidence'>
export type TranscriptionEntityLight = Omit<TranscriptionEntity, 'worker_run' | 'worker_version_id' | 'confidence'>
/**
* Parse EntityTranscriptions to place them on a transcription text using a series of Token components.
* Parse TranscriptionEntitys to place them on a transcription text using a series of Token components.
* @param text Transcription text to place the entities on.
* @param entities EntityTranscriptions from the API to place onto the text.
* @param entities TranscriptionEntitys from the API to place onto the text.
* @param textOffset Global offset to apply to the text when parsing only a portion of the text.
* @returns Sets of properties to be used on the Token component for display.
*/
export const parseEntities = (text: string, entities: EntityTranscriptionLight[], textOffset = 0) => {
export const parseEntities = (text: string, entities: TranscriptionEntityLight[], textOffset = 0) => {
/**
* The current position in the text.
* If we go through an entity, this position is set to the end of that entity;
......
......@@ -260,7 +260,7 @@ export const actions = {
async listCorpusEntityTypes ({ state, commit, dispatch }, { corpusId, page = 1 }) {
// Do not start fetching corpus entity types if they have been retrieved already
if (page === 1 && state.corpusEntityTypes[corpusId]) return
const data = await api.listCorpusEntityTypes({ id: corpusId, page })
const data = await api.listCorpusEntityTypes(corpusId, { page })
commit('setCorpusEntityTypes', { corpusId, results: data.results })
if (!data || !data.number || page !== data.number) {
// Avoid any loop
......@@ -272,7 +272,7 @@ export const actions = {
async createCorpusEntityType ({ commit }, { ...type }) {
try {
const data = await api.createCorpusEntityType({ ...type })
const data = await api.createCorpusEntityType(type)
commit('setCorpusEntityTypes', { corpusId: type.corpus, results: [data] })
} catch (err) {
commit('notifications/notify', { type: 'error', text: errorParser(err) }, { root: true })
......@@ -282,7 +282,9 @@ export const actions = {
async updateCorpusEntityType ({ commit }, { corpusId, ...type }) {
try {
const data = await api.updateCorpusEntityType({ id: type.id, ...type })
const id = type.id
delete type.id
const data = await api.updateCorpusEntityType(id, type)
commit('updateCorpusEntityType', { corpusId, data })
} catch (err) {
commit('notifications/notify', { type: 'error', text: errorParser(err) }, { root: true })
......@@ -292,7 +294,7 @@ export const actions = {
async deleteCorpusEntityType ({ commit }, { corpusId, typeId }) {
try {
await api.deleteCorpusEntityType({ id: typeId })
await api.deleteCorpusEntityType(typeId)
commit('removeCorpusEntityType', { corpusId, typeId })
} catch (err) {
commit('notifications/notify', { type: 'error', text: errorParser(err) }, { root: true })
......
import { clone, assign } from 'lodash'
import { errorParser } from '@/helpers'
import * as api from '@/api'
import {
listCorpusEntities,
listElementLinks,
listEntityElements,
listTranscriptionEntities,
retrieveEntity
} from '@/api'
export const initialState = () => ({
// A single entity retrieved for the entity details page
......@@ -57,7 +63,7 @@ export const mutations = {
export const actions = {
async get ({ commit }, payload) {
try {
const data = await api.retrieveEntity(payload.id)
const data = await retrieveEntity(payload.id)
commit('set', data)
} catch (err) {
commit('set', null)
......@@ -65,18 +71,18 @@ export const actions = {
}
},
async list ({ commit }, payload) {
async list ({ commit }, { id, ...payload }) {
try {
commit('setEntities', await api.listCorpusEntities(payload))
commit('setEntities', await listCorpusEntities(id, payload))
} catch (err) {
commit('notifications/notify', { type: 'error', text: errorParser(err) }, { root: true })
throw err
}
},
async listLinks ({ commit }, payload) {
async listLinks ({ commit }, { id }) {
try {
const data = await api.listElementLinks(payload.id)
const data = await listElementLinks(id)
commit('setLinks', data)
} catch (err) {
commit('setLinks', null)
......@@ -84,9 +90,9 @@ export const actions = {
}
},
async listElements ({ commit }, payload) {
async listElements ({ commit }, { id, ...params }) {
try {
const data = await api.listEntityElements(payload)
const data = await listEntityElements(id, params)
commit('setElements', data)
} catch (err) {
commit('setElements', null)
......@@ -98,7 +104,7 @@ export const actions = {
// Init inTranscription on first response
if (page === 1) commit('resetInTranscription', transcriptionId)
try {
const data = await api.listTranscriptionEntities({ id: transcriptionId, page })
const data = await listTranscriptionEntities(transcriptionId, { page })
commit('setInTranscription', { id: transcriptionId, ...data })
// Load other pages
if (data.next) dispatch('listInTranscription', { transcriptionId, page: page + 1 })
......
import {
CorpusLight,
UUID,
WorkerRunSummary
} from '@/types'
export interface EntityMetas {
[property_name: string]: string
}
export interface EntityType {
id: UUID,
name: string,
color: string
}
export interface EntityRole {
id: UUID
parent_name: string
child_name: string
parent_type_id: UUID
child_type_id: UUID
}
export interface EntityLight {
id: UUID
name: string
type: EntityType
metas: EntityMetas | null
validated: boolean
worker_version_id: UUID | null
worker_run: WorkerRunSummary | null
}
export interface EntityLink {
id: UUID
parent: EntityLight
child: EntityLight
role: EntityRole
}
export interface Entity extends EntityLight {
corpus: CorpusLight
children: EntityLink[]
parents: EntityLink[]
}
export interface TranscriptionEntity {
offset: number
length: number
entity: EntityLight
worker_run: WorkerRunSummary | null
worker_version_id: UUID | null
confidence: number
}
\ No newline at end of file
import { METADATA_TYPES, PROCESS_STATES, PROCESS_MODES, DATASET_STATES, CLASSIFICATION_STATES } from '@/config'
import { S3FileStatus, GitRefType, WorkerVersionGPUUsage, WorkerVersionState, ProcessActivityState } from '@/enums'
import { EntityLight } from './entity'
import { ModelVersionLight } from './model'
import { UserLight } from './user'
......@@ -183,35 +184,6 @@ export interface InterpretedDate {
day: number | null
}
export interface EntityMetas {
[property_name: string]: string
}
export interface EntityType {
id: UUID,
name: string,
color: string
}
export interface Entity {
id: UUID
name: string
type: EntityType
metas: EntityMetas | null
validated: boolean
worker_version_id: UUID | null
worker_run: WorkerRunSummary | null
}
export interface EntityTranscription {
offset: number,
length: number,
entity: Entity,
worker_run: WorkerRunSummary | null,
worker_version_id: UUID | null,
confidence: number
}
export interface AllowedMetaData {
id: UUID
type: string
......@@ -246,7 +218,7 @@ export interface MetaDataCreate extends Omit<TypedMetaData, 'id'> {
export interface MetaData extends Omit<TypedMetaData, 'entity_id'> {
dates: InterpretedDate[]
entity: Entity
entity: EntityLight
worker_version: UUID | null
worker_run_id: UUID | null
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment