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

Adds fact summary functionality to environment fact resolver #3063

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
76 changes: 66 additions & 10 deletions services/api/src/resources/fact/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { Sql } from './sql';
import { ResolverFn } from '../index';
import { knex } from '../../util/db';
import { logger } from '../../loggers/logger';
import crypto from 'crypto';
import { Service } from 'aws-sdk';

export const getFactsByEnvironmentId: ResolverFn = async (
{ id: environmentId, environmentAuthz },
{ keyFacts, limit },
{ keyFacts, limit, summary },
{ sqlClientPool, hasPermission }
) => {
const environment = await environmentHelpers(
Expand All @@ -21,18 +23,72 @@ export const getFactsByEnvironmentId: ResolverFn = async (
});
}

const rows = await query(
sqlClientPool,
Sql.selectFactsByEnvironmentId({
environmentId,
keyFacts,
limit
})
);

return R.sort(R.descend(R.prop('created')), rows);
var rows = [];
// If we're summarizing facts, we actually can't pass back fact ids, or limit
if(summary) {
rows = await query(
sqlClientPool,
Sql.selectFactsByEnvironmentId({
environmentId,
keyFacts,
limit: false
})
);

rows = summarizeFacts(rows);

return R.sort(R.ascend(R.prop('name')), rows);

} else {
rows = await query(
sqlClientPool,
Sql.selectFactsByEnvironmentId({
environmentId,
keyFacts,
limit
})
);

return R.sort(R.descend(R.prop('created')), rows);
}

};

const summarizeFacts = (rows) => {
const factSummary = new Map<string, object>();
rows.forEach(element => {
var summaryKey = crypto.createHash('md5')
.update(element['name'])
.update(element['value'])
.update(element['description'])
.digest('hex');

//clear identifying marks ...
element['id'] = null;
element['created'] = null;

const summaryConcat = (head, tail) => {
if(head.length > 0) {
return `${head}, ${tail}`
}
return tail;
}

if(factSummary.has(summaryKey)) {
var f = factSummary.get(summaryKey);
f['source'] = summaryConcat(f['source'], element['source']);
//TODO : after adding service, we need to add csv of the service names here ...
// f['service'] = summaryConcat(f['service'], element['service']);
factSummary.set(summaryKey, f);
} else {
factSummary.set(summaryKey, element);
}
});
return Array.from(factSummary.values());
};


export const getFactReferencesByFactId: ResolverFn = async (
{ id: fid },
args,
Expand Down
2 changes: 1 addition & 1 deletion services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ const typeDefs = gql`
advancedTasks: [AdvancedTaskDefinition]
services: [EnvironmentService]
problems(severity: [ProblemSeverityRating], source: [String]): [Problem]
facts(keyFacts: Boolean, limit: Int): [Fact]
facts(keyFacts: Boolean, limit: Int, summary: Boolean): [Fact]
openshift: Openshift
openshiftProjectPattern: String
kubernetes: Kubernetes
Expand Down