Skip to content

Commit

Permalink
HARMONY-1956: Remove duplicates from label list (#657)
Browse files Browse the repository at this point in the history
* HARMONY-1956: Remove duplicates from label list
  • Loading branch information
indiejames authored Nov 13, 2024
1 parent 8b2eb47 commit 151b14b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
3 changes: 2 additions & 1 deletion services/harmony/app/models/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ export async function getLabelsForUser(
): Promise<string[]> {
const query = trx(`${USERS_LABELS_TABLE}`)
.select('value')
.orderBy('updatedAt', 'desc')
.groupBy('value')
.orderByRaw(`max("${USERS_LABELS_TABLE}"."updatedAt") desc`)
.limit(count)
.modify((queryBuilder) => {
if (!isAdminRoute) {
Expand Down
2 changes: 1 addition & 1 deletion services/harmony/test/helpers/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { stub } from 'sinon';
import util from 'util';
import db from '../../app/util/db';

export const tables = ['jobs', 'work_items', 'workflow_steps', 'job_links', 'user_work', 'job_errors', 'batches', 'batch_items'];
export const tables = ['jobs', 'work_items', 'workflow_steps', 'job_links', 'user_work', 'job_errors', 'batches', 'batch_items', 'raw_labels', 'jobs_raw_labels', 'users_labels'];

// eslint-disable-next-line @typescript-eslint/no-var-requires
const exec = util.promisify(require('child_process').exec);
Expand Down
49 changes: 48 additions & 1 deletion services/harmony/test/labels/label_crud.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
import { expect } from 'chai';
import { profanity } from '@2toad/profanity';
import { hookTransaction } from '../helpers/db';
import { hookTransaction, truncateAll } from '../helpers/db';
import { buildJob, getFirstJob } from '../helpers/jobs';
import { addJobsLabels, deleteJobsLabels } from '../helpers/labels';
import hookServersStartStop from '../helpers/servers';
import db from '../../app/util/db';
import env from '../../app/util/env';
import { stub } from 'sinon';
import { getLabelsForUser } from '../../app/models/label';

describe('Get Labels', function () {
const joeJob = buildJob({ username: 'joe' });
const jillJob = buildJob({ username: 'jill' });
hookServersStartStop({ skipEarthdataLogin: false });
before(async function () {
await truncateAll();
const trx = await db.transaction();
await joeJob.save(trx);
await jillJob.save(trx);
await trx.commit();
});

describe('When getting labels using the admin route', function () {
describe('When multiple users use the same label', function () {
it('the label only appears once in the returned list', async function () {
await addJobsLabels(this.frontend, [joeJob.jobID], ['foo', 'bar'], 'joe');
await addJobsLabels(this.frontend, [jillJob.jobID], ['foo', 'boo'], 'jill');
// get up to ten labels across all users
const labels = await getLabelsForUser(
db,
'adam',
10,
true,
);
expect(labels).deep.equal(['foo', 'boo', 'bar']);
});
});
});

describe('When multiple users add labels', function () {
it('returns the most recently used labels', async function () {
await addJobsLabels(this.frontend, [joeJob.jobID], ['one'], 'joe');
await addJobsLabels(this.frontend, [joeJob.jobID], ['two'], 'joe');
await addJobsLabels(this.frontend, [jillJob.jobID], ['three', 'four'], 'jill');
// get up to three labels across all users
const labels = await getLabelsForUser(
db,
'adam',
3,
true,
);
expect(labels).deep.equal(['three', 'four', 'two']);
});
});
});

describe('Job label CRUD', function () {
const envLabelsAllowListStub = stub(env, 'labelsAllowList').get(() => 'butt');
Expand Down

0 comments on commit 151b14b

Please sign in to comment.