Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(desktop): improve update connection logic #1175

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/database/services/ConnectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import HistoryConnectionEntity from '@/database/models/HistoryConnectionEntity'
import { Repository, MoreThan, LessThan } from 'typeorm'
import { DateUtils } from 'typeorm/util/DateUtils'
import time, { sqliteDateFormat } from '@/utils/time'
import useServices from '@/database/useServices'

export const MoreThanDate = (date: string | Date) => MoreThan(DateUtils.mixedDateToUtcDatetimeString(date))
export const LessThanDate = (date: string | Date) => LessThan(DateUtils.mixedDateToUtcDatetimeString(date))
Expand Down Expand Up @@ -70,7 +71,7 @@ export default class ConnectionService {
} as ConnectionModel
}

public static modelToEntity(data: ConnectionModel): ConnectionEntity {
public static modelToEntity(data: Partial<ConnectionModel>): Partial<ConnectionEntity> {
if (data.properties) {
const {
sessionExpiryInterval,
Expand All @@ -86,8 +87,9 @@ export default class ConnectionService {
if (data.properties.userProperties) {
userProperties = JSON.stringify(data.properties.userProperties)
}
const { properties, ...rest } = data
return {
...data,
...rest,
sessionExpiryInterval,
receiveMaximum,
maximumPacketSize,
Expand Down Expand Up @@ -261,22 +263,15 @@ export default class ConnectionService {
return ConnectionService.entityToModel(saved)
}

public async update(id: string, data: ConnectionModel): Promise<ConnectionModel | undefined> {
const res: ConnectionEntity | undefined = await this.connectionRepository
.createQueryBuilder('cn')
.where('cn.id = :id', { id })
.getOne()
if (!res) {
return
}
// safe it's same data struct in single connection table
const merged: ConnectionEntity = _.merge(res, ConnectionService.modelToEntity(data))
const query: ConnectionEntity | undefined = await this.connectionRepository.save({
...merged,
id,
public async update(id: string, data: ConnectionModel) {
const { willService } = useServices()
const { messages, subscriptions, will, ...rest } = data
await this.connectionRepository.update(id, {
...ConnectionService.modelToEntity(rest),
updateAt: time.getNowDate(),
} as ConnectionEntity)
return query as ConnectionModel
})
will && (await willService.save(will))
return await this.get(id)
}

public async import(data: ConnectionModel[]): Promise<string> {
Expand Down
84 changes: 84 additions & 0 deletions src/database/services/WillService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Service } from 'typedi'
import { InjectRepository } from 'typeorm-typedi-extensions'
import WillEntity from '../models/WillEntity'
import { Repository } from 'typeorm'

@Service()
export default class WillService {
constructor(
@InjectRepository(WillEntity)
private willRepository: Repository<WillEntity>,
) {}

public static modelToEntity(model: WillModel): WillEntity {
const {
id,
properties = {
contentType: '',
},
lastWillPayload = '',
lastWillTopic = '',
lastWillQos = 0,
lastWillRetain = false,
} = model
const data = {
lastWillPayload,
lastWillTopic,
lastWillQos,
lastWillRetain,
willDelayInterval: properties.willDelayInterval,
payloadFormatIndicator: properties.payloadFormatIndicator,
messageExpiryInterval: properties.messageExpiryInterval,
contentType: properties.contentType,
responseTopic: properties.responseTopic,
correlationData: properties.correlationData?.toString(),
}
if (id) {
return { ...data, id }
}
return { ...data }
}

public static entityToModel(entity: WillEntity): WillModel {
const {
id,
lastWillTopic,
lastWillPayload,
lastWillQos,
lastWillRetain,
willDelayInterval,
payloadFormatIndicator,
messageExpiryInterval,
contentType,
responseTopic,
correlationData,
userProperties,
} = entity
return {
id: id,
lastWillTopic: lastWillTopic,
lastWillPayload: lastWillPayload,
lastWillQos: lastWillQos,
lastWillRetain: lastWillRetain,
properties: {
willDelayInterval: willDelayInterval,
payloadFormatIndicator: payloadFormatIndicator,
messageExpiryInterval: messageExpiryInterval,
contentType: contentType,
responseTopic: responseTopic,
correlationData: correlationData,
userProperties: userProperties ? JSON.parse(userProperties) : undefined,
},
}
}

public async get(id: string) {
const query = await this.willRepository.findOne(id)
return query && WillService.entityToModel(query)
}

public async save(will: WillModel) {
const entity = WillService.modelToEntity(will)
return await this.willRepository.save(entity)
}
}
3 changes: 3 additions & 0 deletions src/database/useServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Container } from 'typedi'
import ConnectionService from './services/ConnectionService'
import WillService from './services/WillService'
import SettingService from './services/SettingService'
import CollectionService from './services/CollectionService'
import SubscriptionService from './services/SubscriptionService'
Expand All @@ -10,6 +11,7 @@ import ScriptService from './services/ScriptService'

export default function useServices() {
const connectionService = Container.get(ConnectionService)
const willService = Container.get(WillService)
const settingService = Container.get(SettingService)
const collectionService = Container.get(CollectionService)
const subscriptionService = Container.get(SubscriptionService)
Expand All @@ -19,6 +21,7 @@ export default function useServices() {
const scriptService = Container.get(ScriptService)
return {
connectionService,
willService,
settingService,
collectionService,
subscriptionService,
Expand Down
2 changes: 1 addition & 1 deletion src/views/connections/ConnectionForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ export default class ConnectionForm extends Vue {
} else {
// update a exisit connection
if (data.id) {
res = await connectionService.updateWithCascade(data.id, {
res = await connectionService.update(data.id, {
...data,
updateAt: time.getNowDate(),
})
Expand Down
5 changes: 1 addition & 4 deletions src/views/connections/ConnectionInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ export default class ConnectionInfo extends Vue {
}
const { connectionService } = useServices()
if (this.connection.id) {
const res: ConnectionModel | undefined = await connectionService.updateWithCascade(
this.connection.id,
this.connection,
)
const res = await connectionService.update(this.connection.id, this.connection)
if (res) {
this.$emit('handleConnect', this.connection)
}
Expand Down
10 changes: 2 additions & 8 deletions src/views/connections/ConnectionsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Getter, Action } from 'vuex-class'
import Contextmenu from '@/components/Contextmenu.vue'
import { ipcRenderer } from 'electron'
Expand All @@ -154,8 +154,6 @@ import time from '@/utils/time'
},
})
export default class ConnectionsList extends Vue {
@Prop({ required: true }) public connectionCount!: number

@Action('UNREAD_MESSAGE_COUNT_INCREMENT') private unreadMessageIncrement!: (payload: UnreadMessage) => void
@Action('SET_CONNECTIONS_TREE') private setConnectionsTree!: (payload: ConnectionTreeState) => void

Expand Down Expand Up @@ -192,10 +190,6 @@ export default class ConnectionsList extends Vue {
this.selectedCollection = null
}
}
@Watch('connectionCount')
private handleConnectionCountChange(val: number, oldVal: number) {
this.loadData(val === 1 && oldVal === 0)
}
@Watch('$route.params.id')
private handleConnectionIdChanged(id: string) {
this.$nextTick(() => {
Expand Down Expand Up @@ -353,7 +347,7 @@ export default class ConnectionsList extends Vue {
await flushCurSequenceId(this.treeData)
}

private async loadData(firstLoad: boolean = false) {
public async loadData(firstLoad: boolean = false) {
firstLoad && (this.isLoadingData = true)
const { collectionService } = useServices()
const treeData: ConnectionModelTree[] = (await collectionService.getAll()) ?? []
Expand Down
10 changes: 7 additions & 3 deletions src/views/connections/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="connections">
<div class="left-list">
<ConnectionsList :connectionCount="connectionCount" @delete="onDelete" @reload="loadData(true, false)" />
<ConnectionsList ref="connectionList" @delete="onDelete" @reload="loadData(true, false)" />
</div>
<div class="connections-view">
<template v-if="isLoadingData">
Expand Down Expand Up @@ -49,7 +49,6 @@ import { getDefaultRecord } from '@/utils/mqttUtils'
export default class Connections extends Vue {
private isEmpty: boolean = false
private isLoadingData: boolean = false
private connectionCount: number = 0
private currentConnection: ConnectionModel = { ...getDefaultRecord() }

@Watch('$route.params.id')
Expand Down Expand Up @@ -99,7 +98,7 @@ export default class Connections extends Vue {
}
const { connectionService } = useServices()
const connections: ConnectionModel[] | [] = (await connectionService.getAll()) ?? []
this.connectionCount = connections.length
this.refreshConnectionList()
this.isLoadingData = false
if (connections.length && loadLatest) {
const leatestId = await connectionService.getLeatestId()
Expand Down Expand Up @@ -135,6 +134,11 @@ export default class Connections extends Vue {
connectionsDetailRef.removeConnection(data)
}

private refreshConnectionList(firstLoad = false) {
const connectionListRef = this.$refs.connectionList as ConnectionsList
connectionListRef.loadData(firstLoad)
}

private created() {
this.loadData(false, true)
}
Expand Down