Skip to content

Commit

Permalink
Merge branch 'crowd-linux' into bugfix/long-transactions-preventing-v…
Browse files Browse the repository at this point in the history
…acuum
  • Loading branch information
epipav authored Aug 14, 2024
2 parents b03151a + fb10c9f commit b8473bf
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
8 changes: 0 additions & 8 deletions backend/src/api/organization/organizationFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.organizationRead)

const segmentId = req.query.segmentId
if (!segmentId) {
await req.responseHandler.error(req, res, {
code: 400,
message: 'Segment ID is required',
})
return
}

const payload = await new OrganizationService(req).findById(req.params.id, segmentId)

await req.responseHandler.success(req, res, payload)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "organizationSegmentsAgg" alter column "segmentId" set not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "organizationSegmentsAgg" alter column "segmentId" drop not null;
17 changes: 11 additions & 6 deletions backend/src/database/repositories/organizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ class OrganizationRepository {
offset: 0,
segmentId,
include: {
aggregates: true,
attributes: true,
lfxMemberships: true,
identities: true,
Expand All @@ -1233,6 +1234,7 @@ class OrganizationRepository {
limit: 1,
offset: 0,
include: {
aggregates: false,
attributes: true,
lfxMemberships: true,
identities: true,
Expand Down Expand Up @@ -1681,6 +1683,7 @@ class OrganizationRepository {
segments: false,
attributes: false,
} as {
aggregates: boolean
identities?: boolean
lfxMemberships?: boolean
segments?: boolean
Expand All @@ -1693,10 +1696,10 @@ class OrganizationRepository {

const qx = SequelizeRepository.getQueryExecutor(options, transaction)

const withAggregates = !!segmentId
let segment
if (withAggregates) {
segment = await new SegmentRepository(options).findById(segmentId)
const withAggregates = include.aggregates

if (segmentId) {
const segment = await new SegmentRepository(options).findById(segmentId)

if (segment === null) {
options.log.info('No segment found for organization')
Expand All @@ -1712,8 +1715,8 @@ class OrganizationRepository {
const params = {
limit,
offset,
segmentId,
tenantId: options.currentTenant.id,
segmentId: segment?.id,
}

const filterString = RawQueryParser.parseFilters(
Expand Down Expand Up @@ -1744,7 +1747,9 @@ class OrganizationRepository {
FROM organizations o
${
withAggregates
? ` JOIN "organizationSegmentsAgg" osa ON osa."organizationId" = o.id AND osa."segmentId" = $(segmentId)`
? ` JOIN "organizationSegmentsAgg" osa ON osa."organizationId" = o.id AND ${
segmentId ? `osa."segmentId" = $(segmentId)` : `osa."segmentId" IS NULL`
}`
: ''
}
WHERE 1=1
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/organizationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ export default class OrganizationService extends LoggerBase {
'tags',
'logo',
],
include: { identities: true, lfxMemberships: true },
include: { aggregates: true, identities: true, lfxMemberships: true },
},
this.options,
)
Expand All @@ -1064,7 +1064,7 @@ export default class OrganizationService extends LoggerBase {
offset,
segmentId: undefined,
fields: ['id', 'displayName', 'isTeamOrganization'],
include: { identities: true, segments: true, lfxMemberships: true },
include: { aggregates: false, identities: true, segments: true, lfxMemberships: true },
},
this.options,
)
Expand Down
12 changes: 9 additions & 3 deletions services/libs/data-access-layer/src/activities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function groupByField(segmentType: SegmentType) {
return 's."parentId"'
case SegmentType.PROJECT_GROUP:
return 's."grandparentId"'
case SegmentType.NO_SEGMENT:
return null
}

throw new Error(`Invalid segment type: ${segmentType}`)
Expand All @@ -21,11 +23,15 @@ export async function getOrgAggregates(
organizationId: string,
groupBy: SegmentType,
): Promise<IDbOrganizationAggregateData[]> {
const segmentColumn = groupByField(groupBy)
const segmentJoinClause = segmentColumn ? 'JOIN segments s ON s.id = a."segmentId"' : ''
const groupByColumns = segmentColumn ? '1, 2, 3' : '1, 3'

return qx.select(
`
SELECT
o."id" AS "organizationId",
${groupByField(groupBy)} AS "segmentId",
${segmentColumn} AS "segmentId",
o."tenantId",
COALESCE(MIN(a.timestamp), '1970-01-01') AS "joinedAt",
MAX(a.timestamp) AS "lastActive",
Expand All @@ -35,9 +41,9 @@ export async function getOrgAggregates(
COALESCE(ROUND(AVG(a.score)), 0) AS "avgContributorEngagement"
FROM activities a
JOIN organizations o ON o."id" = a."organizationId"
JOIN segments s ON s.id = a."segmentId"
${segmentJoinClause}
WHERE a."organizationId" = $(organizationId)
GROUP BY 1, 2, 3
GROUP BY ${groupByColumns}
`,
{
organizationId,
Expand Down
2 changes: 1 addition & 1 deletion services/libs/data-access-layer/src/organizations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface IDbOrgAttributeInput {

export interface IDbOrganizationAggregateData {
organizationId: string
segmentId: string
segmentId?: string
tenantId: string

joinedAt: string
Expand Down
1 change: 1 addition & 0 deletions services/libs/types/src/segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export enum SegmentType {
PROJECT_GROUP = 'projectGroup',
PROJECT = 'project',
SUB_PROJECT = 'subproject',
NO_SEGMENT = 'noSegment',
}

export interface SegmentCriteria extends SearchCriteria {
Expand Down

0 comments on commit b8473bf

Please sign in to comment.