Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Implement FactSchema.{list,get} #32

Merged
merged 3 commits into from Jul 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/resolvers-cassandra/Facts/queries.js
@@ -1,19 +1,82 @@
'use strict';

const Promise = require('promise');
const cassandraConnector = require('../../clients/cassandra/CassandraConnector');
const withRunTime = require('../shared').withRunTime;

/**
* @typedef {id: string, language: string, title: string, tags: string[], date: string, link: string, text: string, sources: string[]} Fact
*/

function cassandraRowToFact(row) {
return {
id: row.id,
language: row.eventlangcode,
title: row.title,
tags: row.detectedkeywords,
date: row.event_time,
link: row.sourceurl,
text: row.messagebody,
sources: row.pipeline && [row.pipeline]
};
}

function appendDefaultFilters(query, params) {
query += ' AND pipeline IN ("TadaWeb", "Bing", "CustomEvent")';
return {query: query, params: params};
}

function makeListQuery(args) {
const tagsCondition = args.tagFilter.map(_ => 'detectedkeywords CONTAINS ?').join(' OR '); // eslint-disable-line no-unused-vars
const query = `SELECT * FROM fortis.events WHERE (${tagsCondition})`;
const params = args.tagFilter.slice();
return appendDefaultFilters(query, params);
}

/**
* @param {{pageSize: number, skip: number, tagFilter: string[]}} args
* @returns {Promise.<{runTime: string, type: string, facts: Array<{id: string, language: string, title: string, tags: string[], date: string, link: string, text: string, sources: string[]}>}>}
* @returns {Promise.<{runTime: string, type: string, facts: Fact[]}>}
*/
function list(args, res) { // eslint-disable-line no-unused-vars
return new Promise((resolve, reject) => {
if (!args || !args.tagFilter || !args.tagFilter.length) return reject('No tags specified to fetch');

const query = makeListQuery(args);
return cassandraConnector.executeQuery(query.query, query.params)
.then(rows => {
const facts = rows.map(cassandraRowToFact);

resolve({
facts: facts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where's my runTime at? =)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a cross-cutting concern that we should implement once for all the resolvers (e.g. as a decorator).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

});
})
.catch(reject);
});
}

function makeGetQuery(args) {
let query = 'SELECT * FROM fortis.events WHERE id = ?';
let params = [args.id];
return appendDefaultFilters(query, params);
}

/**
* @param {{id: string}} args
* @returns {Promise.<{id: string, language: string, title: string, tags: string[], date: string, link: string, text: string, sources: string[]}>}
* @returns {Promise.<Fact>}
*/
function get(args, res) { // eslint-disable-line no-unused-vars
return new Promise((resolve, reject) => {
if (!args || !args.id) return reject('No id specified to fetch');

const query = makeGetQuery(args);
return cassandraConnector.executeQuery(query.query, query.params)
.then(rows => {
if (rows.length > 1) return reject(`Got more ${rows.length} faces with id ${args.id}`);

resolve(cassandraRowToFact(rows[0]));
})
.catch(reject);
});
}

module.exports = {
Expand Down