mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-06 05:22:52 +00:00
feat: add minimal connector
This commit is contained in:
69
src/cli.ts
Normal file
69
src/cli.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Bulk} from '@openstapps/api/lib/bulk';
|
||||
import {ConnectorClient as Client} from '@openstapps/api/lib/connectorClient';
|
||||
import {HttpClient} from '@openstapps/api/lib/httpClient';
|
||||
import {SCBulkAddResponse, SCMessage} from '@openstapps/core';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import * as promiseLimit from 'promise-limit';
|
||||
import {MinimalConnector} from '.';
|
||||
|
||||
const api = new Client(new HttpClient(), 'http://localhost:3000');
|
||||
const logger = new Logger();
|
||||
|
||||
async function runConnector() {
|
||||
|
||||
const connector = new MinimalConnector();
|
||||
const items = await connector.getItems();
|
||||
|
||||
if (items.length === 0) {
|
||||
throw new Error('No items fetched.');
|
||||
}
|
||||
|
||||
let bulk: Bulk<SCMessage>;
|
||||
|
||||
try {
|
||||
bulk = await api.bulk<SCMessage>('message', 'minimal-connector');
|
||||
} catch (err) {
|
||||
logger.error('Couldn\'t open bulk.');
|
||||
throw err;
|
||||
}
|
||||
|
||||
// create a concurrency limit
|
||||
const limit = promiseLimit<SCBulkAddResponse>(5);
|
||||
|
||||
try {
|
||||
// index all items with our concurrency limit
|
||||
await Promise.all(items.map((item) => {
|
||||
return limit(() => bulk.add(item));
|
||||
}));
|
||||
} catch (err) {
|
||||
logger.error('Error while indexing items.');
|
||||
throw err;
|
||||
}
|
||||
|
||||
try {
|
||||
await bulk.done();
|
||||
} catch (err) {
|
||||
logger.error('Error while closing bulk');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
runConnector().then(() => {
|
||||
logger.log('Done.');
|
||||
}, (err) => {
|
||||
throw err;
|
||||
});
|
||||
81
src/index.ts
Normal file
81
src/index.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {ConnectorClient as Client} from '@openstapps/api/lib/connectorClient';
|
||||
import {SCMessage} from '@openstapps/core';
|
||||
|
||||
export class MinimalConnector {
|
||||
// Data is mocked inside of getItems()
|
||||
private items: SCMessage[] = [];
|
||||
|
||||
/**
|
||||
* Provides "fetched" items (e.g. messages)
|
||||
*
|
||||
* @returns {Promise<SCMessage[]>}
|
||||
* @memberof MinimalConnector
|
||||
*/
|
||||
async getItems(): Promise<SCMessage[]> {
|
||||
// reset items
|
||||
this.items.length = 0;
|
||||
|
||||
// sample items (messages) "fetched" from some source
|
||||
const importedItems: SCMessage[] = [
|
||||
{
|
||||
audiences: ['students', 'employees'],
|
||||
description: 'Some description 1' ,
|
||||
message: 'Some message 1',
|
||||
name: 'Some name 1',
|
||||
origin: {
|
||||
indexed: (new Date()).toISOString(),
|
||||
name: 'minimal connector',
|
||||
},
|
||||
type: 'message',
|
||||
uid: '',
|
||||
},
|
||||
{
|
||||
audiences: ['students', 'employees'],
|
||||
description: 'Some description 2',
|
||||
message: 'Some message 2',
|
||||
name: 'Some name 2',
|
||||
origin: {
|
||||
indexed: (new Date()).toISOString(),
|
||||
name: 'minimal connector',
|
||||
},
|
||||
type: 'message',
|
||||
uid: '',
|
||||
},
|
||||
{
|
||||
audiences: ['students', 'employees'],
|
||||
description: 'Some description 3',
|
||||
message: 'Some message 3',
|
||||
name: 'Some name 3',
|
||||
origin: {
|
||||
indexed: (new Date()).toISOString(),
|
||||
name: 'minimal connector',
|
||||
},
|
||||
type: 'message',
|
||||
uid: '',
|
||||
},
|
||||
];
|
||||
|
||||
// create a universally unique identifier for each item
|
||||
for (const item of importedItems) {
|
||||
// each uid is generated by a string and the namespace id of your university
|
||||
item.uid = Client.makeUUID(JSON.stringify(item), 'f-u');
|
||||
this.items.push(item);
|
||||
}
|
||||
|
||||
return this.items;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user