Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add entry point to request node to get monitoring information #191

Merged
merged 24 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/data-access/src/data-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Bluebird from 'bluebird';
import { EventEmitter } from 'events';

import Block from './block';
import IgnoredLocation from './ignored-location';
import IntervalTimer from './interval-timer';
import TransactionIndex from './transaction-index';

Expand All @@ -31,6 +32,8 @@ export interface IDataAccessOptions {
* Defaults to DEFAULT_INTERVAL_TIME.
*/
synchronizationIntervalTime?: number;

ignoredLocation?: IgnoredLocation;
}

/**
Expand All @@ -40,11 +43,14 @@ export default class DataAccess implements DataAccessTypes.IDataAccess {
// Transaction index, that allows storing and retrieving transactions by channel or topic, with time boundaries.
// public for test purpose
public transactionIndex: DataAccessTypes.ITransactionIndex;

// boolean to store the initialization state
protected isInitialized: boolean = false;
// Storage layer
private storage: StorageTypes.IStorage;

private ignoredLocation: IgnoredLocation;
vrolland marked this conversation as resolved.
Show resolved Hide resolved

// The function used to synchronize with the storage should be called periodically
// This object allows to handle the periodical call of the function
private synchronizationTimer: IntervalTimer;
Expand All @@ -70,6 +76,7 @@ export default class DataAccess implements DataAccessTypes.IDataAccess {
*/
public constructor(storage: StorageTypes.IStorage, options?: IDataAccessOptions) {
const defaultOptions: IDataAccessOptions = {
ignoredLocation: new IgnoredLocation(),
logger: new Utils.SimpleLogger(),
synchronizationIntervalTime: DEFAULT_INTERVAL_TIME,
transactionIndex: new TransactionIndex(),
Expand All @@ -87,6 +94,7 @@ export default class DataAccess implements DataAccessTypes.IDataAccess {
5,
);
this.transactionIndex = options.transactionIndex!;
this.ignoredLocation = options.ignoredLocation!;

this.logger = options.logger!;
}
Expand Down Expand Up @@ -440,6 +448,33 @@ export default class DataAccess implements DataAccessTypes.IDataAccess {
this.synchronizationTimer.stop();
}

/**
* Gets information of the data indexed
*
* @param detailed if true get the list of the files hash
vrolland marked this conversation as resolved.
Show resolved Hide resolved
*/
public async _getInformation(detailed: boolean = false): Promise<any> {
this.checkInitialized();

// last transaction timestamp retrieved
const lastLocationTimestamp = await this.transactionIndex.getLastTransactionTimestamp();
const listIndexedLocation = await this.transactionIndex.getIndexedLocation();
const listIgnoredLocation = await this.ignoredLocation.getIgnoredLocation();

return {
filesIgnored: {
count: Object.keys(listIgnoredLocation).length,
list: detailed ? listIgnoredLocation : undefined,
},
filesRetrieved: {
count: listIndexedLocation.length,
lastTimestamp: lastLocationTimestamp,
list: detailed ? listIndexedLocation : undefined,
},
lastSynchronizationTimestamp: this.lastSyncStorageTimestamp,
};
}

/**
* Check the format of the data, extract the topics from it and push location indexed with the topics
*
Expand Down Expand Up @@ -468,6 +503,8 @@ export default class DataAccess implements DataAccessTypes.IDataAccess {
await this.transactionIndex.addTransaction(entry.id, block.header, entry.meta.timestamp);
} catch (e) {
parsingErrorCount++;
// Index ignored Location
await this.ignoredLocation.pushReasonByLocation(entry.id, e.message);
benjlevesque marked this conversation as resolved.
Show resolved Hide resolved
this.logger.debug(`Error: can't parse content of the dataId (${entry.id}): ${e}`, [
'synchronization',
]);
Expand Down
98 changes: 98 additions & 0 deletions packages/data-access/src/ignored-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as Keyv from 'keyv';

/**
* Class used to store the block's reason indexed by location
vrolland marked this conversation as resolved.
Show resolved Hide resolved
*/
export default class ReasonsByIgnoredLocationIndex {
/**
* reason by location
* maps dataId => reason
*/
private reasonsByIgnoredLocation: Keyv<string>;

private listIgnoredLocation: Keyv<string[]>;

/**
* reasonByLocationTransactionIndex constructor
* @param store a Keyv store to persist the index to
vrolland marked this conversation as resolved.
Show resolved Hide resolved
*/
public constructor(store?: Keyv.Store<any>) {
this.reasonsByIgnoredLocation = new Keyv<string>({
namespace: 'reasonsByIgnoredLocation',
store,
});

this.listIgnoredLocation = new Keyv<string[]>({
namespace: 'listIgnoredLocation',
store,
});
}

/**
* Function to push reason indexed by location
*
* @param dataId dataId of the block
* @param reason reason to be ignored
*/
public async pushReasonByLocation(dataId: string, reason: string): Promise<void> {
if (!(await this.reasonsByIgnoredLocation.get(dataId))) {
await this.reasonsByIgnoredLocation.set(dataId, reason);
await this.updateListDataId(dataId);
}
}

/**
* Function to update reason indexed by location
*
* @param dataId dataId of the block
* @param reason reason to be ignored
*/
public async removeReasonByLocation(dataId: string): Promise<void> {
await this.reasonsByIgnoredLocation.delete(dataId);
}

/**
* Function to get reason from location
*
* @param dataId location to get the reason from
* @returns reason of the location, null if not found
*/
public async getReasonFromLocation(dataId: string): Promise<string | null> {
const reason: string | undefined = await this.reasonsByIgnoredLocation.get(dataId);
return reason ? reason : null;
}

/**
* Get the list of data ids stored
*
* @returns the list of data ids stored
*/
public async getIgnoredLocation(): Promise<any> {
const listDataId: string[] | undefined = await this.listIgnoredLocation.get('list');

if (!listDataId) {
return {};
}
const result: any = {};
for (const dataId of Array.from(listDataId)) {
result[dataId] = await this.reasonsByIgnoredLocation.get(dataId);
}

return result;
}

/**
* Update the list of data ids stored
*
* @param dataId data id to add to the list
* @returns
*/
private async updateListDataId(dataId: string): Promise<void> {
let listDataIds: string[] | undefined = await this.listIgnoredLocation.get('list');
if (!listDataIds) {
listDataIds = [];
}
listDataIds.push(dataId);
await this.listIgnoredLocation.set('list', listDataIds);
}
}
36 changes: 36 additions & 0 deletions packages/data-access/src/transaction-index/transaction-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ export default class TransactionIndex implements DataAccessTypes.ITransactionInd
// Will be used to get the data from timestamp boundaries
private timestampByLocation: TimestampByLocation;

private indexedLocation: Keyv<string[]>;

/**
* Constructor of TransactionIndex
* @param store a Keyv store to persist the index to
*/
constructor(store?: Keyv.Store<any>) {
this.timestampByLocation = new TimestampByLocation(store);
this.locationByTopic = new LocationByTopic(store);

this.indexedLocation = new Keyv<string[]>({
namespace: 'indexedLocation',
store,
});
}

// tslint:disable-next-line: no-empty
Expand Down Expand Up @@ -58,6 +65,8 @@ export default class TransactionIndex implements DataAccessTypes.ITransactionInd

// add the timestamp in the index
await this.timestampByLocation.pushTimestampByLocation(dataId, timestamp);

await this.updateIndexedLocation(dataId);
}

/**
Expand Down Expand Up @@ -200,4 +209,31 @@ export default class TransactionIndex implements DataAccessTypes.ITransactionInd
return channelIds;
}
}

/**
* the list of indexed location
vrolland marked this conversation as resolved.
Show resolved Hide resolved
*/
public async getIndexedLocation(): Promise<string[]> {
vrolland marked this conversation as resolved.
Show resolved Hide resolved
const listDataIds: string[] | undefined = await this.indexedLocation.get('list');
return listDataIds || [];
}

/**
* Update the list of data ids stored
*
* @param dataId data id to add to the list
* @returns
*/
private async updateIndexedLocation(dataId: string): Promise<void> {
let listDataIds: string[] | undefined = await this.indexedLocation.get('list');
if (!listDataIds) {
listDataIds = [];
}

// push it if not already done
if (!listDataIds.includes(dataId)) {
listDataIds.push(dataId);
await this.indexedLocation.set('list', listDataIds);
}
}
}
50 changes: 49 additions & 1 deletion packages/data-access/test/data-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const defaultTestData: Promise<StorageTypes.IEntriesWithLastTimestamp> = Promise
);

const defaultFakeStorage: StorageTypes.IStorage = {
_getInformation: chai.spy(),
_ipfsAdd: chai.spy(),
append: chai.spy(
(): any => {
Expand Down Expand Up @@ -138,7 +139,7 @@ let clock: sinon.SinonFakeTimers;

// tslint:disable:no-magic-numbers
/* tslint:disable:no-unused-expression */
describe.only('data-access', () => {
describe('data-access', () => {
beforeEach(async () => {
clock = sinon.useFakeTimers();
});
Expand Down Expand Up @@ -492,6 +493,7 @@ describe.only('data-access', () => {

it('cannot persistTransaction() and emit error if confirmation failed', async () => {
const mockStorageEmittingError: StorageTypes.IStorage = {
_getInformation: chai.spy(),
_ipfsAdd: chai.spy(),
append: chai.spy(
(): any => {
Expand Down Expand Up @@ -544,6 +546,50 @@ describe.only('data-access', () => {
});
});

describe('_getInformation', () => {
let dataAccess: any;

beforeEach(async () => {
const fakeStorage = {
...defaultFakeStorage,
read: (param: string): any => {
const dataIdBlock2txFake: StorageTypes.IEntry = {
content: JSON.stringify(blockWith2tx),
id: '1',
meta: { state: StorageTypes.ContentState.CONFIRMED, timestamp: 10 },
};
const result: any = {
dataIdBlock2tx: dataIdBlock2txFake,
};
return result[param];
},
};

dataAccess = new DataAccess(fakeStorage);
await dataAccess.initialize();
});

it('can _getInformation()', async () => {
expect(await dataAccess._getInformation(), 'result with arbitraryTopic1 wrong').to.deep.equal(
{
filesIgnored: { count: 0, list: undefined },
filesRetrieved: { count: 1, lastTimestamp: 10, list: undefined },
lastSynchronizationTimestamp: 0,
},
);
});
it('can _getInformation() with details', async () => {
expect(
await dataAccess._getInformation(true),
'result with arbitraryTopic1 wrong',
).to.deep.equal({
filesIgnored: { count: 0, list: {} },
filesRetrieved: { count: 1, lastTimestamp: 10, list: ['dataIdBlock2tx'] },
lastSynchronizationTimestamp: 0,
});
});
});

it('synchronizeNewDataId() should throw an error if not initialized', async () => {
const dataAccess = new DataAccess(defaultFakeStorage);

Expand Down Expand Up @@ -572,6 +618,7 @@ describe.only('data-access', () => {
_ipfsAdd: chai.spy(),
append: chai.spy(),
getData: (): Promise<StorageTypes.IEntriesWithLastTimestamp> => testDataNotJsonData,
_getInformation: chai.spy(),
initialize: chai.spy(),
read: chai.spy(),
readMany: chai.spy(),
Expand Down Expand Up @@ -693,6 +740,7 @@ describe.only('data-access', () => {
_ipfsAdd: chai.spy(),
append: chai.spy.returns(appendResult),
getData: (): Promise<StorageTypes.IEntriesWithLastTimestamp> => chai.spy(),
_getInformation: chai.spy(),
initialize: chai.spy(),
read: chai.spy(),
readMany: chai.spy(),
Expand Down
Loading