Skip to content

Commit

Permalink
fix(scancompare): add the previous scan fetch
Browse files Browse the repository at this point in the history
add the previous scan fetch to compare with the last scan
  • Loading branch information
Hugo Mota committed Mar 10, 2020
1 parent 5ded1ad commit e4d5c8a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
32 changes: 17 additions & 15 deletions source/components/ScanSummary/ScanSummary.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { format as formatString } from 'util';
import { ConfigurationService, LoggerService, XmlParsingService } from '../../services';
import DataService from '../../services/DataService/DataService';
import { IScanSummaryData } from './IScanSummaryData';
import { LoggerService, XmlParsingService, ConfigurationService } from '../../services';
import { QUERY_PROJECT_DETAILS, QUERY_PROJECT_STATUS, RESPONSE_TEMPLATE } from './constants';
import { QUERY_PREVIOUS_SCAN_ID, QUERY_PROJECT_DETAILS, RESPONSE_TEMPLATE } from './constants';
import ScanSummaryError from './ScanSummaryError';

const cnf = ConfigurationService.getConfig();
Expand All @@ -13,7 +12,7 @@ const timeout = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms));
};

const fetchData = async (scanId: number): Promise<any> => {
const fetchData = async (projectId: number, scanId: number): Promise<any> => {
const ds = DataService.getInstance();

try {
Expand All @@ -25,8 +24,12 @@ const fetchData = async (scanId: number): Promise<any> => {
if (!scanSummaryQueryResult.length) {
throw new ScanSummaryError(ScanSummaryError.MISSING_SCAN_DETAILS, scanId);
}
// const scanTotalsQueryResult = (await ds.executeQuery(formatString(QUERY_PROJECT_STATUS, scanId))) as any[];
const scanTotalsQueryResult = (await ds.executeGetCompareScansSummary(scanId as number, scanId as number)) as any[];

const previousScanIdQueryResult = (await ds.executeQuery(formatString(QUERY_PREVIOUS_SCAN_ID, projectId, scanId))) as any[];
const previousScanId: number = previousScanIdQueryResult.length ? previousScanIdQueryResult[0].scanId : scanId;
log.debug('retrieved previous scan id from database %s', previousScanId);

const scanTotalsQueryResult = (await ds.executeGetCompareScansSummary(scanId as number, previousScanId as number)) as any[];
log.debug('retrieved from database %j', scanTotalsQueryResult);

await ds.disconnect();
Expand Down Expand Up @@ -78,24 +81,22 @@ const fetchData = async (scanId: number): Promise<any> => {
}
};

const fetchDataRetry = async (scanId: number, retries: number): Promise<any> => {
const fetchDataRetry = async (projectId: number, scanId: number, retries: number): Promise<any> => {
try {
if (retries >= 0) {
log.info('Feching data for scanId=%s attempt #%s', scanId, retries);
const scanData = await fetchData(scanId);
log.info('Feching data for projectId=%s scanId=%s attempt #%s', projectId, scanId, retries);
const scanData = await fetchData(projectId, scanId);
log.debug('Feching data for scanId=%s attempt #%s returned %j', scanId, retries, scanData);
if (!scanData) {
await timeout(2000);
return await fetchDataRetry(scanId, retries - 1);
return await fetchDataRetry(projectId, scanId, retries - 1);
}
return scanData;
} else {
throw Error('ABORTING');
}
} catch (e) {
if (retries > 0) {
await timeout(2000);
return await fetchDataRetry(scanId, retries - 1);
return await fetchDataRetry(projectId, scanId, retries - 1);
} else {
log.error('Could not retrieve data for scanId=%s after several attempts', scanId);
return undefined;
Expand All @@ -109,11 +110,12 @@ const summaryReport = async (): Promise<any> => {
}

const scanId = await xml.fetchScanIdAndQuit(cnf.project.xmlReport);
const scanData = await fetchDataRetry(scanId, 4);
const projectId = await xml.fetchProjectIdAndQuit(cnf.project.xmlReport);
const scanData = await fetchDataRetry(projectId, scanId, 4);

log.debug('fetched scan details %j', scanData);
if (scanData) {
log.info('finished retrieving data for scanId=%s projectName="%s"', scanId, scanData.projectName);
log.info('finished retrieving data for projectId=%s scanId=%s projectName="%s"', projectId, scanId, scanData.projectName);
}

return Promise.resolve(scanData);
Expand Down
14 changes: 14 additions & 0 deletions source/components/ScanSummary/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ ORDER BY
Query.Severity DESC;
`;

export const QUERY_PREVIOUS_SCAN_ID = `
SELECT
Id as scanId
FROM
[CxDB].[dbo].[TaskScans]
WHERE
ProjectId = %s
AND Id <= %s
ORDER BY
Id DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROW ONLY
`;

export const RESPONSE_TEMPLATE: IScanSummaryData = {
productVersion: '',
projectId: 0,
Expand Down
1 change: 1 addition & 0 deletions source/services/XmlParsingService/XmlParsingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import XmlParsingServiceImpl from './XmlParsingServiceImpl';

export interface IXmlParsingService {
fetchScanIdAndQuit(xmlReportPath: string): Promise<number>;
fetchProjectIdAndQuit(xmlReportPath: string): Promise<number>;
}

let instance: IXmlParsingService;
Expand Down
26 changes: 25 additions & 1 deletion source/services/XmlParsingService/XmlParsingServiceImpl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IXmlParsingService } from './XmlParsingService';
import lineReader from 'line-reader';
import { LoggerService } from '../LoggerService';
import { IXmlParsingService } from './XmlParsingService';

const log = LoggerService.getLogger('XmlParsingServiceImpl');

Expand Down Expand Up @@ -28,6 +28,22 @@ const getScanIdAndQuit = (path: string): Promise<any> => {
});
};

const getProjectIdAndQuit = (path: string): Promise<any> => {
return new Promise(resolve => {
lineReader.eachLine(path, line => {
if (line.includes('CxXMLResults')) {
const xmlProp = extractMatchFromString(/ProjectId=\"[0-9]*\"/g, line);
const xmlPropValue = extractMatchFromString(/\d+/g, xmlProp ? xmlProp : '');
log.debug('fetched xmlProperty %s', xmlProp);
log.debug('fetched xmlPropertyValue %s', xmlPropValue);
resolve(xmlPropValue);
return false;
}
return true;
});
});
};

export default class XmlParsingServiceImpl implements IXmlParsingService {
public async fetchScanIdAndQuit(_xmlReportPath: string): Promise<number> {
const scanId = await getScanIdAndQuit(_xmlReportPath);
Expand All @@ -36,4 +52,12 @@ export default class XmlParsingServiceImpl implements IXmlParsingService {

return Promise.resolve(scanId ? scanId : 0);
}

public async fetchProjectIdAndQuit(_xmlReportPath: string): Promise<number> {
const projectId = await getProjectIdAndQuit(_xmlReportPath);

log.info('fetched projectId=%s from %s', projectId, _xmlReportPath);

return Promise.resolve(projectId ? projectId : 0);
}
}

0 comments on commit e4d5c8a

Please sign in to comment.