Skip to content

Commit

Permalink
BE-823 Convert to TS : app/persistence/... (#180)
Browse files Browse the repository at this point in the history
* BE-823 Convert to TS : app/persistence/...

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>

* BE-823 Convert to TS : app/persistence/...

Fixed errors

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored Sep 23, 2020
1 parent aa870ea commit 74f5f4a
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 109 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

import {PgService} from '../postgreSQL/PgService';
import { helper } from '../../common/helper';

const logger = helper.getLogger('CRUDService');
Expand All @@ -11,8 +11,10 @@ const logger = helper.getLogger('CRUDService');
*
* @class CRUDService
*/
class CRUDService {
constructor(sql) {
export class CRUDService {
sql : PgService;

constructor(sql : PgService) {
this.sql = sql;
}

Expand Down Expand Up @@ -260,7 +262,7 @@ class CRUDService {
async getCurBlockNum(network_name, channel_genesis_hash) {
let curBlockNum;
try {
const row = await this.sql.getRowsBySQlCase(
const row : any = await this.sql.getRowsBySQlCase(
`select max(blocknum) as blocknum from blocks where channel_genesis_hash='${channel_genesis_hash}' and network_name = '${network_name}' `
);

Expand Down Expand Up @@ -421,7 +423,6 @@ class CRUDService {
}
// Orderer BE-303
}
module.exports = CRUDService;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

import {PgService} from '../postgreSQL/PgService';
import { helper } from '../../common/helper';

const logger = helper.getLogger('MetricService');
Expand All @@ -11,8 +11,10 @@ const logger = helper.getLogger('MetricService');
*
* @class MetricService
*/
class MetricService {
constructor(sql) {
export class MetricService {
sql : PgService;

constructor(sql : PgService) {
this.sql = sql;
}

Expand Down Expand Up @@ -154,7 +156,7 @@ class MetricService {
*/
async getOrgsData(network_name, channel_genesis_hash) {
const orgs = [];
const rows = await this.sql.getRowsBySQlNoCondition(
const rows : any = await this.sql.getRowsBySQlNoCondition(
`select distinct on (mspid) mspid from peer where channel_genesis_hash='${channel_genesis_hash}' and network_name='${network_name}'`
);
for (let i = 0, len = rows.length; i < len; i++) {
Expand Down Expand Up @@ -192,24 +194,24 @@ class MetricService {
* @memberof MetricService
*/
async getStatusGenerate(network_name, channel_genesis_hash) {
let chaincodeCount = await this.getChaincodeCount(
let chaincodeCount : any = await this.getChaincodeCount(
network_name,
channel_genesis_hash
);
if (!chaincodeCount) {
chaincodeCount = 0;
}
let txCount = await this.getTxCount(network_name, channel_genesis_hash);
let txCount : any = await this.getTxCount(network_name, channel_genesis_hash);
if (!txCount) {
txCount = 0;
}
txCount.c = txCount.c ? txCount.c : 0;
let blockCount = await this.getBlockCount(network_name, channel_genesis_hash);
let blockCount : any = await this.getBlockCount(network_name, channel_genesis_hash);
if (!blockCount) {
blockCount = 0;
}
blockCount.c = blockCount.c ? blockCount.c : 0;
let peerCount = await this.getPeerlistCount(
let peerCount : any = await this.getPeerlistCount(
network_name,
channel_genesis_hash
);
Expand Down Expand Up @@ -641,5 +643,3 @@ class MetricService {
return this.sql.getRowsBySQlQuery(sqlQuery);
}
}

module.exports = MetricService;
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/
import {PgService} from '../postgreSQL/PgService';

/**
*
*
* @class UserDataService
*/
class UserDataService {
export class UserDataService {
sql : PgService;
userModel : any;
/**
* Creates an instance of UserDataService.
* @param {*} sql
* @memberof UserDataService
*/
constructor(sql) {
constructor(sql : PgService) {
this.sql = sql;
}

Expand Down Expand Up @@ -101,5 +104,3 @@ class UserDataService {
});
}
}

module.exports = UserDataService;
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/
const PgService = require('./PgService');

import {PgService} from './PgService';
import {MetricService} from '../fabric/MetricService';
import {CRUDService} from '../fabric/CRUDService';
import {UserDataService} from '../fabric/UserDataService';
/**
*
*
* @class Persist
*/
class Persist {
pgservice : PgService;
metricservice : MetricService;
crudService : CRUDService;
userdataservice : UserDataService;

constructor(pgconfig) {
this.pgservice = new PgService(pgconfig);
this.metricservice = null;
Expand All @@ -22,7 +29,7 @@ class Persist {
* @param {*} metricservice
* @memberof Persist
*/
setMetricService(metricservice) {
setMetricService(metricservice : MetricService) {
this.metricservice = metricservice;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* limitations under the License.
*/
import { helper } from '../../common/helper';
import { Client } from 'pg';
import { Sequelize, Optional } from 'sequelize';

const { Client } = require('pg');
const Sequelize = require('sequelize');
const fs = require('fs');

const logger = helper.getLogger('PgService');
Expand All @@ -30,7 +30,11 @@ const logger = helper.getLogger('PgService');
*
* @class PgService
*/
class PgService {
export class PgService {
pgconfig : any;
userModel : any;
client : Client;

/**
* Creates an instance of PgService.
* @param {*} pgconfig
Expand Down Expand Up @@ -103,7 +107,7 @@ class PgService {
*/
async handleDisconnect() {
try {
this.client.on('error', err => {
this.client.on('error', (err : NodeJS.ErrnoException) => {
logger.error('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
Expand Down Expand Up @@ -517,7 +521,7 @@ class PgService {
* @param datatype limit the page limit.
*
*/
getRowsBySQlNoCondition(sqlcharacter, limit) {
getRowsBySQlNoCondition(sqlcharacter, limit? ) : Promise<any>{
/* eslint-disable */
const _self = this;
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -574,75 +578,4 @@ class PgService {
});
}

/**
*
* @param sql
* @param key
* @returns {Promise}
*
*/
getSQL2Map(sql, key) {
const _self = this;
return new Promise((resolve, reject) => {
_self.client.query(sql, (err, res) => {
if (err) {
reject(err);
return;
}

logger.debug(`The solution is: ${res.rows.length} `);

const keymap = new Map();

for (let ind = 0; ind < res.rows.length; ind++) {
logger.debug(`The ind value is: ${res.rows[ind].id} `);
keymap.set(res.rows[ind][key], res.rows[ind]);
}

resolve(keymap);
});
});
}

/**
*
*
* @param unknown_type sql
* @param unknown_type key
* @return unknown
*/
getSQL2Map4Arr(sql, key) {
const _self = this;
return new Promise((resolve, reject) => {
_self.client.query(sql, (err, rows) => {
if (err) {
reject(err);
return;
}

// Logger.debug( `The solution is: ${rows.length } ` );
logger.debug(' the getSqlMap ');

const keymap = new Map();

for (let ind = 0; ind < rows.length; ind++) {
const keyvalue = rows[ind][key];
let arrvalue = [];

if (keymap.has(keyvalue)) {
arrvalue = keymap.get(keyvalue);
arrvalue.push(rows);
} else {
arrvalue.push(rows);
}

keymap.set(keyvalue, arrvalue);
}

resolve(keymap);
});
});
}
}

module.exports = PgService;
8 changes: 3 additions & 5 deletions app/platform/fabric/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import * as path from 'path';
import {helper} from '../../common/helper';
import {User} from './models/User';
import {MetricService} from '../../persistence/fabric/MetricService';
import {CRUDService} from '../../persistence/fabric/CRUDService';
import {UserDataService} from '../../persistence/fabric/UserDataService';

const fs = require('fs-extra');

Expand All @@ -14,11 +17,6 @@ const ExplorerError = require('../../common/ExplorerError');
const logger = helper.getLogger('Platform');
const FabricUtils = require('./utils/FabricUtils.js');
const ExplorerListener = require('../../sync/listener/ExplorerListener');

const CRUDService = require('../../persistence/fabric/CRUDService');
const MetricService = require('../../persistence/fabric/MetricService');
const UserDataService = require('../../persistence/fabric/UserDataService');

const fabric_const = require('./utils/FabricConst').fabric.const;
const explorer_error = require('../../common/ExplorerMessage').explorer.error;

Expand Down
2 changes: 0 additions & 2 deletions app/platform/fabric/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,3 @@ export class User {
return this.userJson;
}
}

// module.exports = User;
8 changes: 4 additions & 4 deletions app/platform/fabric/sync/SyncPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
*/

import * as path from 'path';
import {helper} from '../../../common/helper';
import {MetricService} from '../../../persistence/fabric/MetricService';
import {CRUDService} from '../../../persistence/fabric/CRUDService';

const fs = require('fs-extra');

const SyncService = require('../sync/SyncService');
const FabricUtils = require('../utils/FabricUtils');
const FabricEvent = require('./FabricEvent');
const FabricConfig = require('../FabricConfig');

import {helper} from '../../../common/helper';

const logger = helper.getLogger('SyncPlatform');
const ExplorerError = require('../../../common/ExplorerError');

const CRUDService = require('../../../persistence/fabric/CRUDService');
const MetricService = require('../../../persistence/fabric/MetricService');

const fabric_const = require('../utils/FabricConst').fabric.const;
const explorer_mess = require('../../../common/ExplorerMessage').explorer;
Expand Down
Loading

0 comments on commit 74f5f4a

Please sign in to comment.