Skip to content

Commit

Permalink
chore(rethinkdb): TimelineEvent: Phase 3 (#9876)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <[email protected]>
  • Loading branch information
mattkrick authored Jun 27, 2024
1 parent 6796d8c commit 77b56ad
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 84 deletions.
5 changes: 0 additions & 5 deletions packages/server/database/rethinkDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import SuggestedActionTryTheDemo from './types/SuggestedActionTryTheDemo'
import Task from './types/Task'
import TemplateDimension from './types/TemplateDimension'
import TemplateScale from './types/TemplateScale'
import TimelineEvent from './types/TimelineEvent'

export type RethinkSchema = {
AgendaItem: {
Expand Down Expand Up @@ -187,10 +186,6 @@ export type RethinkSchema = {
type: TemplateScale
index: 'teamId'
}
TimelineEvent: {
type: TimelineEvent
index: 'userIdCreatedAt' | 'meetingId'
}
}

export type DBType = {
Expand Down
14 changes: 14 additions & 0 deletions packages/server/dataloader/foreignKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ export const retroReflectionsByGroupId = foreignKeyLoaderMaker(
.execute()
}
)

export const timelineEventsByMeetingId = foreignKeyLoaderMaker(
'timelineEvents',
'meetingId',
async (meetingIds) => {
const pg = getKysely()
return pg
.selectFrom('TimelineEvent')
.selectAll()
.where('meetingId', 'in', meetingIds)
.where('isActive', '=', true)
.execute()
}
)
4 changes: 4 additions & 0 deletions packages/server/dataloader/primaryKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ export const selectRetroReflections = () =>
export const retroReflections = primaryKeyLoaderMaker((ids: readonly string[]) => {
return selectRetroReflections().where('id', 'in', ids).execute()
})

export const timelineEvents = primaryKeyLoaderMaker((ids: readonly string[]) => {
return getKysely().selectFrom('TimelineEvent').selectAll().where('id', 'in', ids).execute()
})
14 changes: 0 additions & 14 deletions packages/server/dataloader/rethinkForeignKeyLoaderMakers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import TimelineEventCheckinComplete from 'parabol-server/database/types/TimelineEventCheckinComplete'
import TimelineEventRetroComplete from 'parabol-server/database/types/TimelineEventRetroComplete'
import getRethink from '../database/rethinkDriver'
import {RDatum} from '../database/stricterR'
import RethinkForeignKeyLoaderMaker from './RethinkForeignKeyLoaderMaker'
Expand Down Expand Up @@ -182,18 +180,6 @@ export const templateDimensionsByTemplateId = new RethinkForeignKeyLoaderMaker(
)
}
)
export const timelineEventsByMeetingId = new RethinkForeignKeyLoaderMaker(
'timelineEvents',
'meetingId',
async (meetingIds) => {
const r = await getRethink()
return r
.table('TimelineEvent')
.getAll(r.args(meetingIds), {index: 'meetingId'})
.filter({isActive: true})
.run() as Promise<TimelineEventCheckinComplete[] | TimelineEventRetroComplete[]>
}
)

export const slackAuthByUserId = new RethinkForeignKeyLoaderMaker(
'slackAuths',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ export const tasks = new RethinkPrimaryKeyLoaderMaker('Task')
export const teamMembers = new RethinkPrimaryKeyLoaderMaker('TeamMember')
export const teamInvitations = new RethinkPrimaryKeyLoaderMaker('TeamInvitation')
export const templateDimensions = new RethinkPrimaryKeyLoaderMaker('TemplateDimension')
export const timelineEvents = new RethinkPrimaryKeyLoaderMaker('TimelineEvent')
15 changes: 5 additions & 10 deletions packages/server/graphql/mutations/archiveTimelineEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {GraphQLID, GraphQLNonNull} from 'graphql'
import {SubscriptionChannel} from 'parabol-client/types/constEnums'
import TimelineEventCheckinComplete from 'parabol-server/database/types/TimelineEventCheckinComplete'
import TimelineEventRetroComplete from 'parabol-server/database/types/TimelineEventRetroComplete'
import getRethink from '../../database/rethinkDriver'
import {TimelineEventEnum} from '../../database/types/TimelineEvent'
import getKysely from '../../postgres/getKysely'
import {getUserId, isTeamMember} from '../../utils/authorization'
Expand All @@ -25,7 +24,6 @@ const archiveTimelineEvent = {
{timelineEventId}: {timelineEventId: string},
{authToken, dataLoader, socketId: mutatorId}: GQLContext
) => {
const r = await getRethink()
const operationId = dataLoader.share()
const subOptions = {mutatorId, operationId}
const viewerId = getUserId(authToken)
Expand Down Expand Up @@ -59,14 +57,11 @@ const archiveTimelineEvent = {
.load(meetingId)
const eventIds = meetingTimelineEvents.map(({id}) => id)
const pg = getKysely()
await Promise.all([
pg
.updateTable('TimelineEvent')
.set({isActive: false})
.where('id', 'in', eventIds)
.execute(),
r.table('TimelineEvent').getAll(r.args(eventIds)).update({isActive: false}).run()
])
await pg
.updateTable('TimelineEvent')
.set({isActive: false})
.where('id', 'in', eventIds)
.execute()
meetingTimelineEvents.map((event) => {
const {id: timelineEventId, userId} = event
publish(
Expand Down
5 changes: 1 addition & 4 deletions packages/server/graphql/mutations/endCheckIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ export default {
)
const timelineEventId = events[0]!.id
const pg = getKysely()
await Promise.all([
pg.insertInto('TimelineEvent').values(events).execute(),
r.table('TimelineEvent').insert(events).run()
])
await pg.insertInto('TimelineEvent').values(events).execute()
if (team.isOnboardTeam) {
const teamLeadUserId = await r
.table('TeamMember')
Expand Down
5 changes: 1 addition & 4 deletions packages/server/graphql/mutations/endSprintPoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ export default {
})
)
const pg = getKysely()
await Promise.all([
pg.insertInto('TimelineEvent').values(events).execute(),
r.table('TimelineEvent').insert(events).run()
])
await pg.insertInto('TimelineEvent').values(events).execute()

const data = {
meetingId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ const bootstrapNewUser = async (
)
.insertInto('TimelineEvent')
.values(joinEvent)
.execute(),
r.table('TimelineEvent').insert(joinEvent).run()
.execute()
])

// Identify the user so user properties are set before any events are sent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default async function createTeamAndLeader(user: IUser, newTeam: ValidNew
r.table('MeetingSettings').insert(meetingSettings).run(),
// denormalize common fields to team member
insertNewTeamMember(user, teamId),
r.table('TimelineEvent').insert(timelineEvent).run(),
addTeamIdToTMS(userId, teamId)
])
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ const safeEndRetrospective = async ({
)
const timelineEventId = events[0]!.id
const pg = getKysely()
await Promise.all([
pg.insertInto('TimelineEvent').values(events).execute(),
r.table('TimelineEvent').insert(events).run()
])
await pg.insertInto('TimelineEvent').values(events).execute()

if (team.isOnboardTeam) {
const teamLeadUserId = await r
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ const safeEndTeamPrompt = async ({
)
const timelineEventId = events[0]!.id
const pg = getKysely()
await Promise.all([
pg.insertInto('TimelineEvent').values(events).execute(),
r.table('TimelineEvent').insert(events).run()
])
await pg.insertInto('TimelineEvent').values(events).execute()
summarizeTeamPrompt(meeting, context)
analytics.teamPromptEnd(completedTeamPrompt, meetingMembers, responses, dataLoader)
checkTeamsLimit(team.orgId, dataLoader)
Expand Down
12 changes: 1 addition & 11 deletions packages/server/graphql/private/mutations/backupOrganization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,7 @@ const backupOrganization: MutationResolvers['backupOrganization'] = async (_sour
r.or(row('teamId').default(null).eq(null), r(teamIds).contains(row('teamId')))
)
.coerceTo('array')
.do((items: RValue) => r.db(DESTINATION).table('SuggestedAction').insert(items)),
timelineEvent: (
r
.table('TimelineEvent')
.filter((row: RDatum) => r(userIds).contains(row('userId'))) as any
)
.filter((row: RValue) =>
r.branch(row('teamId'), r(teamIds).contains(row('teamId')), true)
)
.coerceTo('array')
.do((items: RValue) => r.db(DESTINATION).table('TimelineEvent').insert(items))
.do((items: RValue) => r.db(DESTINATION).table('SuggestedAction').insert(items))
})
}),
meetingIds: r
Expand Down
6 changes: 0 additions & 6 deletions packages/server/graphql/private/mutations/hardDeleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ const hardDeleteUser: MutationResolvers['hardDeleteUser'] = async (
.getAll(r.args(teamIds), {index: 'teamId'})
.filter((row: RValue) => row('createdBy').eq(userIdToDelete))
.delete(),
timelineEvent: r
.table('TimelineEvent')
.between([userIdToDelete, r.minval], [userIdToDelete, r.maxval], {
index: 'userIdCreatedAt'
})
.delete(),
agendaItem: r
.table('AgendaItem')
.getAll(r.args(teamIds), {index: 'teamId'})
Expand Down
35 changes: 17 additions & 18 deletions packages/server/graphql/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import {
MAX_RESULT_GROUP_SIZE
} from '../../../client/utils/constants'
import groupReflections from '../../../client/utils/smartGroup/groupReflections'
import getRethink from '../../database/rethinkDriver'
import {RDatum} from '../../database/stricterR'
import MeetingMemberType from '../../database/types/MeetingMember'
import OrganizationType from '../../database/types/Organization'
import OrganizationUserType from '../../database/types/OrganizationUser'
import SuggestedActionType from '../../database/types/SuggestedAction'
import TimelineEvent from '../../database/types/TimelineEvent'
import getKysely from '../../postgres/getKysely'
import {getUserId, isSuperUser, isTeamMember} from '../../utils/authorization'
import getMonthlyStreak from '../../utils/getMonthlyStreak'
import getRedis from '../../utils/getRedis'
Expand Down Expand Up @@ -217,7 +215,6 @@ const User: GraphQLObjectType<any, GQLContext> = new GraphQLObjectType<any, GQLC
{after, first, teamIds, eventTypes},
{authToken, dataLoader}: GQLContext
) => {
const r = await getRethink()
const viewerId = getUserId(authToken)

// VALIDATE
Expand All @@ -244,21 +241,23 @@ const User: GraphQLObjectType<any, GQLContext> = new GraphQLObjectType<any, GQLC
: accessibleTeamIds

if (viewerId !== id && !isSuperUser(authToken)) return null
const dbAfter = after ? new Date(after) : r.maxval
const events = await r
.table('TimelineEvent')
.between([viewerId, r.minval], [viewerId, dbAfter], {
index: 'userIdCreatedAt'
})
.filter({isActive: true})
.filter((t: RDatum<TimelineEvent>) =>
eventTypes ? r.expr(eventTypes).contains(t('type')) : true
)
.filter((t: RDatum) => r.expr(validTeamIds).contains(t('teamId')))
.orderBy(r.desc('createdAt'))
const dbAfter = after ? new Date(after) : new Date('3000-01-01')
const minVal = new Date(0)

const pg = getKysely()
const events = await pg
.selectFrom('TimelineEvent')
.selectAll()
.where('userId', '=', viewerId)
.where('createdAt', '>', minVal)
.where('createdAt', '<=', dbAfter)
.where('isActive', '=', true)
.where('teamId', 'in', validTeamIds)
.$if(!!eventTypes, (db) => db.where('type', 'in', eventTypes))
.orderBy('createdAt')
.limit(first + 1)
.coerceTo('array')
.run()
.execute()

const edges = events.slice(0, first).map((node) => ({
cursor: node.createdAt,
node
Expand Down

0 comments on commit 77b56ad

Please sign in to comment.