Skip to content

Commit

Permalink
Merge pull request #3063 from uselagoon/feature/key_fact_aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobybellwood authored Mar 15, 2022
2 parents aaa276c + ce0ba1f commit b9bb785
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
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

0 comments on commit b9bb785

Please sign in to comment.