<commit_before>// @flow

const R = require('ramda');
const { sendToLagoonLogs } = require('@lagoon/commons/src/logs');
const { createTaskTask } = require('@lagoon/commons/src/tasks');
const { pubSub, createEnvironmentFilteredSubscriber } = require('../../clients/pubSub');
const esClient = require('../../clients/esClient');
const sqlClient = require('../../clients/sqlClient');
const {
  knex,
  ifNotAdmin,
  inClauseOr,
  prepare,
  query,
  isPatchEmpty,
} = require('../../util/db');
const Sql = require('./sql');
const EVENTS = require('./events');
const Helpers = require('./helpers');
const projectSql = require('../project/sql');
const environmentSql = require('../environment/sql');
const environmentHelpers = require('../environment/helpers');
const envValidators = require('../environment/validators');

/* ::

import type {ResolversObj} from '../';

*/

const taskStatusTypeToString = R.cond([
  [R.equals('ACTIVE'), R.toLower],
  [R.equals('SUCCEEDED'), R.toLower],
  [R.equals('FAILED'), R.toLower],
  [R.T, R.identity],
]);

const getTasksByEnvironmentId = async (
  { id: eid },
  args,
  {
    credentials: {
      role,
      permissions: { customers, projects },
    },
  },
) => {
  const prep = prepare(
    sqlClient,
    `SELECT
        t.*
      FROM environment e
      JOIN task t on e.id = t.environment
      JOIN project p ON e.project = p.id
      WHERE e.id = :eid
      ${ifNotAdmin(
    role,
    `AND (${inClauseOr([['p.customer', customers], ['p.id', projects]])})`,
  )}
    `,
  );

  const rows = await query(sqlClient, prep({ eid }));

  const newestFirst = R.sort(R.descend(R.prop('created')), rows);

  return newestFirst.map(row => Helpers.injectLogs(row));
};

const getTaskByRemoteId = async (
  root,
  { id },
  {
    credentials: {
      role,
      permissions: { customers, projects },
    },
  },
) => {
  const queryString = knex('task')
    .where('remote_id', '=', id)
    .toString();

  const rows = await query(sqlClient, queryString);
  const task = R.prop(0, rows);

  if (!task) {
    return null;
  }

  if (role !== 'admin') {
    const rowsPerms = await query(sqlClient, Sql.selectPermsForTask(task.id));<commit_msg>Only load task logs if a query requests the logs field<commit_after>// @flow

const R = require('ramda');
const getFieldNames = require('graphql-list-fields');
const { sendToLagoonLogs } = require('@lagoon/commons/src/logs');
const { createTaskTask } = require('@lagoon/commons/src/tasks');
const { pubSub, createEnvironmentFilteredSubscriber } = require('../../clients/pubSub');
const esClient = require('../../clients/esClient');
const sqlClient = require('../../clients/sqlClient');
const {
  knex,
  ifNotAdmin,
  inClauseOr,
  prepare,
  query,
  isPatchEmpty,
} = require('../../util/db');
const Sql = require('./sql');
const EVENTS = require('./events');
const Helpers = require('./helpers');
const projectSql = require('../project/sql');
const environmentSql = require('../environment/sql');
const environmentHelpers = require('../environment/helpers');
const envValidators = require('../environment/validators');

/* ::

import type {ResolversObj} from '../';

*/

const taskStatusTypeToString = R.cond([
  [R.equals('ACTIVE'), R.toLower],
  [R.equals('SUCCEEDED'), R.toLower],
  [R.equals('FAILED'), R.toLower],
  [R.T, R.identity],
]);

const getTasksByEnvironmentId = async (
  { id: eid },
  args,
  {
    credentials: {
      role,
      permissions: { customers, projects },
    },
  },
  info,
) => {
  const prep = prepare(
    sqlClient,
    `SELECT
        t.*
      FROM environment e
      JOIN task t on e.id = t.environment
      JOIN project p ON e.project = p.id
      WHERE e.id = :eid
      ${ifNotAdmin(
    role,
    `AND (${inClauseOr([['p.customer', customers], ['p.id', projects]])})`,
  )}
    `,
  );

  const rows = await query(sqlClient, prep({ eid }));
  const newestFirst = R.sort(R.descend(R.prop('created')), rows);

  const requestedFields = getFieldNames(info);

  return newestFirst.map(row => {
    if (R.contains('logs', requestedFields)) {
      return Helpers.injectLogs(row);
    }

    return {
      ...row,
      logs: null,
    };
  });
};

const getTaskByRemoteId = async (
  root,
  { id },
  {
    credentials: {
      role,
      permissions: { customers, projects },
    },
  },
) => {
  const queryString = knex('task')
    .where('remote_id', '=', id)
    .toString();

  const rows = await query(sqlClient, queryString);
  const task = R.prop(0, rows);

  if (!task) {
    return null;
  }

  if (role !== 'admin') {
    const rowsPerms = await query(sqlClient, Sql.selectPermsForTask(task.id));
{'fraction_non_alphanumeric': 0.1819162710401381, 'fraction_numerical': 0.00043159257660768235, 'mean_word_length': 3.2251595259799455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3944580', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1525, 'n_words': 338}