Skip to content

Commit

Permalink
Merge pull request #72 from geoadmin/feature/assets-67-update-elastic…
Browse files Browse the repository at this point in the history
…search-index

#67 update elasticsearch index local
  • Loading branch information
ga-ebp authored Apr 11, 2024
2 parents 8a19b62 + 5035bea commit 607eb3f
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 162 deletions.
1 change: 1 addition & 0 deletions apps/server-asset-sg/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export default {
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/server-asset-sg',
coverageReporters: ['json', 'text', 'cobertura', 'lcov'],
};
3 changes: 2 additions & 1 deletion apps/server-asset-sg/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "apps/server-asset-sg/jest.config.ts",
"passWithNoTests": true
"passWithNoTests": true,
"codeCoverage": true
}
},
"gen-prisma-client": {
Expand Down
107 changes: 107 additions & 0 deletions apps/server-asset-sg/src/app/contact-edit/contact-edit.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Test, TestingModule } from '@nestjs/testing';
import { left, right } from 'fp-ts/Either';

import { Contact, PatchContact } from '@asset-sg/shared';

import { PrismaService } from '../prisma/prisma.service';

import { ContactEditService } from './contact-edit.service';


jest.mock('../prisma/prisma.service');

const mockPatchContact: PatchContact = {
name: 'John Doe',
street: '123 Main Street',
houseNumber: '1A',
plz: '12345',
locality: 'City',
country: 'Country',
telephone: '123-456-7890',
email: '[email protected]',
website: 'www.example.com',
contactKindItemCode: '123'
};
const mockContact: Contact = {
id: 1,
contactKindItemCode: '123',
name: 'John Doe',
street: '123 Main Street',
houseNumber: '1A',
plz: '12345',
locality: 'City',
country: 'Country',
telephone: '123-456-7890',
email: '[email protected]',
website: 'www.example.com'
};

const mockPrisma = {
contact: {}
};


describe('ContactEditService', () => {
let service: ContactEditService;
let prismaService: PrismaService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ContactEditService,
{
provide: PrismaService,
useValue: mockPrisma,
},
],
}).compile();

service = module.get<ContactEditService>(ContactEditService);
prismaService = module.get<PrismaService>(PrismaService);

});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should create a contact', async () => {
prismaService.contact.create = jest.fn().mockResolvedValue(mockContact);

const result = await service.createContact(mockPatchContact)();

expect(result).toEqual(right(mockContact));
expect(prismaService.contact.create).toHaveBeenCalledWith({ data: mockPatchContact });
});

it('should update a contact', async () => {
const mockContactId = 1;

prismaService.contact.update = jest.fn().mockResolvedValue(mockPatchContact);
prismaService.contact.findFirstOrThrow = jest.fn().mockResolvedValue(mockContact);

const result = await service.updateContact(mockContactId, mockContact)();

expect(result).toEqual(right(mockContact));
expect(prismaService.contact.update).toHaveBeenCalledWith({ where: { contactId: mockContactId }, data: mockContact });
});

it('should handle errors when creating a contact', async () => {
prismaService.contact.create = jest.fn().mockRejectedValue(new Error('Test error'));

const result = await service.createContact(mockContact)();

expect(result).toEqual(left(new Error()));
expect(prismaService.contact.create).toHaveBeenCalledWith({ data: mockContact });
});

it('should handle errors when updating a contact', async () => {
const mockContactId = 1;
prismaService.contact.update = jest.fn().mockRejectedValue(new Error('Test error'));

const result = await service.updateContact(mockContactId, mockContact)();

expect(result).toEqual(left(new Error()));
expect(prismaService.contact.update).toHaveBeenCalledWith({ where: { contactId: mockContactId }, data: mockContact });
});
});
160 changes: 5 additions & 155 deletions apps/server-asset-sg/src/app/scripts/export-to-elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const date: D.Decoder<unknown, Date> = D.fromGuard(dateGuard, 'Date');
export const dateIdFromDate = (d: Date) => (d.getFullYear() * 10000 + (d.getMonth() + 1) * 100 + d.getDate()) as DateId;
export const DateIdFromDate = pipe(date, D.map(dateIdFromDate));

const index = 'asset-swissgeol-asset';
const index = 'swissgeol_asset_asset';

const main = async () => {
try {
Expand All @@ -41,34 +41,11 @@ const main = async () => {
}
: {}),
};
// console.log({ options });
console.log({ options });

const client = new Client(options);

// console.log('connected');

// console.log({ client });

const exists = await client.indices.exists({ index });
if (exists) {
await client.indices.delete({ index });
}

await client.indices.create({
index,
mappings: {
properties: {
assetId: { type: 'integer' },
createDateId: { type: 'integer' },
authorIds: { type: 'integer' },
assetKindItemCode: { type: 'keyword' },
languageItemCode: { type: 'keyword' },
usageCode: { type: 'keyword' },
manCatLabelItemCodes: { type: 'keyword' },
sgsId: { type: 'keyword' },
},
},
});
console.log('connected');

const prisma = new PrismaClient();

Expand Down Expand Up @@ -173,141 +150,16 @@ const main = async () => {
),
);

// const assets = pipe(
// dataset,
// NEA.group(EqRawAssetByAssetId),
// NEA.fromArray,
// O.map(
// flow(
// A.filter(a => a.length > 1),
// A.splitAt(3),
// a => a[0],
// ),
// ),
// );
// const dataset = await prisma.asset.findMany({
// // where: { assetId: { lt: 10 } },
// // where: { AND: [{ assetId: { gte: 0 } }, { assetId: { lte: 32768 } }] },
// take: 32000,
// select: {
// assetId: true,
// titlePublic: true,
// assetContacts: {
// where: { role: 'author' },
// select: {
// contact: {
// select: {
// name: true,
// },
// },
// },
// },
// },
// });
// const dataset = await prisma.asset.findMany({
// select: {
// assetId: true,
// titlePublic: true,
// // internalUses: {
// // select: {
// // isAvailable: true,
// // },
// // },
// },
// // include: {
// // languageItem: true,
// // },
// });
//
//

// const dataset2 = dataset.map(x => ({
// assetId: x.assetId,
// titlePublic: x.titlePublic,
// authors: x.assetContacts.map(y => y.contact.name),
// }));

// console.log((dataset as any).filter((a: any) => !a.authorName));
// console.log(dataset.length);
//
// console.log(JSON.stringify(assets, null, 4));

if (E.isRight(assets)) {
// console.log(assets.right.slice(1, 10));
// console.log(assets.right.filter(a => a.assetId === 38981)[0]);
const operations = assets.right.flatMap(doc => [
{ index: { _index: 'swissgeol_asset_asset', _id: doc.assetId } },
doc,
]);
// console.log(operations[0]);

// console.log(assets.right.filter(a => a.assetId === 34153)[0]);

// console.log(operations);
//

const bulkResponse = await client.bulk({ refresh: true, operations });
console.log(bulkResponse);
//
// const SearchResults = D.struct({
// hits: D.struct({
// hits: D.array(
// D.struct({
// fields: D.struct({
// assetId: D.array(D.number),
// }),
// }),
// ),
// }),
// aggregations: D.struct({
// authors: D.struct({
// buckets: D.array(D.struct({ key: D.string, doc_count: D.number })),
// }),
// }),
// });

// const foo = pipe(
// TE.tryCatch(
// () =>
// client.search({
// // size: 10000,
// query: {
// bool: {
// must: {
// query_string: {
// query: 'Schenker',
// fields: ['titlePublic', 'titleOriginal', 'contactNames'],
// },
// },
// },
// },
// aggs: {
// authorIds: { terms: { field: 'authorIds' } },
// minCreateDate: { min: { field: 'createDate' } },
// maxCreateDate: { max: { field: 'createDate' } },
// },
// fields: [
// 'assetId',
// 'titlePublic',
// 'titleOriginal',
// 'createDate',
// 'authorIds',
// 'contactNames',
// ],
// _source: false,
// }),
// unknownToError,
// ),
// // TE.chainW(flow(SearchResults.decode, E.mapLeft(D.draw), TE.fromEither)),
// // TE.mapLeft(e => console.log(JSON.stringify(e, null, 4))),
// );

// foo().then(a => {
// if (a._tag === 'Right') {
// console.log(a.right.aggregations);
// }
// });
// console.log('hi', JSON.stringify(results, null, 4));
} else {
} else {
console.log(D.draw(assets.left));
}
} catch (e) {
Expand All @@ -316,5 +168,3 @@ const main = async () => {
};

main().then(() => console.log('done'));

// client.indices.delete({ index: 'asset' });
1 change: 0 additions & 1 deletion development/init/elasticsearch/index
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PUT /swissgeol_asset_asset


PUT /swissgeol_asset_asset/_mapping
{
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion libs/asset-editor/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default {
transform: {
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$|ol)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</ng-container>
</div>
<table class="asset-details">
<caption></caption>
<tr>
<th translate="search.file" class="icon-height"></th>
<td>
Expand Down Expand Up @@ -173,6 +174,7 @@
<td>
<ng-container *ngIf="rdAssetDetail.value.statusWorks.length === 0">&ndash;</ng-container>
<table class="status-works">
<caption></caption>
<tr *rxFor="let statusWork of rdAssetDetail.value.statusWorks">
<th>{{ statusWork.statusWorkDate | assetSgDate }}</th>
<td>{{ statusWork.statusWork | valueItemName }}</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
background-color: #87d7e6;
color: #357183;
}
&:active:not(focus) {
&:active:not(:focus) {
box-shadow: none;
background-color: variables.$cyan-06;
color: variables.$white;
Expand Down Expand Up @@ -110,7 +110,7 @@
&:focus {
color: variables.$red;
}
&:active:not(focus) {
&:active:not(:focus) {
color: variables.$dark-red;
}
}
Expand All @@ -135,7 +135,7 @@
&:focus {
color: variables.$red;
}
&:active:not(focus) {
&:active:not(:focus) {
color: variables.$dark-red;
}
}
Expand Down
17 changes: 17 additions & 0 deletions libs/core/src/lib/ngrx-store-logger/ngrx-store-logger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ActionReducer } from '@ngrx/store';

import { storeLogger } from './ngrx-store-logger';

describe('storeLogger', () => {

it('should log state and action', () => {
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
const metaReducer = storeLogger<string>();
const actionReducer: ActionReducer<string, any> = (state: string | undefined, action: any) => { return state || ''; };

Check warning on line 10 in libs/core/src/lib/ngrx-store-logger/ngrx-store-logger.spec.ts

View workflow job for this annotation

GitHub Actions / Build and run tests

Unexpected any. Specify a different type

Check warning on line 10 in libs/core/src/lib/ngrx-store-logger/ngrx-store-logger.spec.ts

View workflow job for this annotation

GitHub Actions / Build and run tests

'action' is defined but never used

Check warning on line 10 in libs/core/src/lib/ngrx-store-logger/ngrx-store-logger.spec.ts

View workflow job for this annotation

GitHub Actions / Build and run tests

Unexpected any. Specify a different type

metaReducer(actionReducer)('state', { type: 'test' });

expect(consoleLogSpy).toHaveBeenCalled();
});

});
Loading

0 comments on commit 607eb3f

Please sign in to comment.