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

Sync topic tree from connection #1800

Merged
merged 3 commits into from
Nov 13, 2024
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
3 changes: 3 additions & 0 deletions src/assets/scss/atom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@
.text-primary {
color: var(--color-main-green);
}
.text-tips {
color: var(--color-text-tips);
}
.bg-secondary {
background-color: var(--color-background-secondary);
}
Expand Down
1 change: 0 additions & 1 deletion src/components/ImportScript.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import { Getter } from 'vuex-class'
import fs from 'fs'
import { remote } from 'electron'
import MyDialog from './MyDialog.vue'
import useServices from '@/database/useServices'
import { ElLoadingComponent } from 'element-ui/types/loading'

@Component({
Expand Down
63 changes: 63 additions & 0 deletions src/database/services/MessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { Service } from 'typedi'
import { InjectRepository } from 'typeorm-typedi-extensions'
import MessageEntity from '../models/MessageEntity'
import { Repository } from 'typeorm'
import ConnectionEntity from '../models/ConnectionEntity'
import ConnectionService from './ConnectionService'

@Service()
export default class MessageService {
constructor(
// @ts-ignore
@InjectRepository(MessageEntity)
private messageRepository: Repository<MessageEntity>,
// @ts-ignore
@InjectRepository(ConnectionEntity)
private connectionRepository: Repository<ConnectionEntity>,
) {}

public static modelToEntity(model: MessageModel, connectionId: string | undefined): MessageEntity {
Expand Down Expand Up @@ -322,4 +327,62 @@ export default class MessageService {

return transform ? transform(messages) : (messages as unknown as T[])
}

/**
* Retrieves message topic node statistics for a given connection.
* @param connectionId - The ID of the connection to retrieve statistics for.
* @returns A promise that resolves to an object containing the connection model and topic statistics,
* or null if the connection is not found.
*/
public async getMessageTopicNodeStats(connectionId: string): Promise<{
connection: ConnectionModel
topicStats: Array<TopicNodeStats>
} | null> {
// Get connection info
const connectionEntity = await this.connectionRepository.findOne(connectionId)
if (!connectionEntity) {
return null
}
const connectionModel = ConnectionService.entityToModel(connectionEntity)

// Get topic statistics
const topicStats: TopicNodeStats[] = await this.messageRepository
.createQueryBuilder('msg')
.select(['msg.topic as msgTopic', 'COUNT(msg.id) as msgCount', 'MAX(msg.createAt) as lastTime'])
.where('msg.connectionId = :connectionId', { connectionId })
.andWhere('msg.out = :out', { out: false }) // Only get received messages
.groupBy('msg.topic')
.orderBy('lastTime', 'DESC')
.getRawMany()

// Get latest messages
const latestMessagesModels = await this.messageRepository
.createQueryBuilder('msg')
.innerJoin(
(qb) =>
qb
.select('sub.topic', 'topic')
.addSelect('MAX(sub.createAt)', 'maxTime')
.from(MessageEntity, 'sub')
.where('sub.connectionId = :connectionId', { connectionId })
.andWhere('sub.out = :out', { out: false })
.groupBy('sub.topic'),
'latest',
'msg.topic = latest.topic AND msg.createAt = latest.maxTime',
)
.where('msg.connectionId = :connectionId', { connectionId })
.andWhere('msg.out = :out', { out: false })
.getMany()
.then((msgs) => msgs.map((msg) => MessageService.entityToModel(msg)))

topicStats.forEach((topicStat) => {
const latestMessage = latestMessagesModels.find((msg) => msg.topic === topicStat.msgTopic)
topicStat.latestMessage = latestMessage
})

return {
connection: connectionModel,
topicStats,
}
}
}
Loading
Loading