<commit_before>const promise = require("bluebird");
const options = {
  // Initialization Options
  promiseLib: promise, // use bluebird as promise library
  capSQL: true, // when building SQL queries dynamically, capitalize SQL keywords
};
const pgp = require("pg-promise")(options);
const { SUPPORTED_LANGUAGES } = require("./../constants.js");
const { find } = require("lodash");
const { db, CASE_BY_ID } = require("../api/helpers/db");

const {Translate} = require('@google-cloud/translate').v2;
const translate = new Translate({projectId: process.env.GOOGLE_PROJECT_ID});

// getThings();
getLocalizationData(5243);

function getThings() {
  db.any(`SELECT * FROM things WHERE type IN ('case','method','organization')`)
    .then(function(thingData) {
      thingData.forEach(data => {
        getLocalizationData(data.id);
      });
    })
    .catch(function(error) {
      console.log(error);
    });
}

function getLocalizationData(thingid) {
  db.any(`SELECT * FROM localized_texts WHERE thingid = ${thingid} ORDER BY timestamp DESC LIMIT 1`)
    .then(function(data) {
      data.forEach(data => {
        console.log(data);
        var records = [];
        SUPPORTED_LANGUAGES.forEach(language => {
          // console.log(language);
          // if (language.twoLetterCode !== 'en') {
          //   // TODO: Implement google translate here
          //   records.push({
          //     body: 'test',
          //     title: 'test',
          //     description: 'test',
          //     language: language.twoLetterCode,
          //     thingid: thingid
          //   });
          // }
        });

        // const insert = pgp.helpers.insert(records, ['body', 'title', 'description', 'language', 'thingid'], 'localized_texts');

        // db.none(insert)
        //   .then(function(data) {
        //     console.log(data);
        //   })
        //   .catch(function(error) {
        //     console.log(error);
        //   });
      });
    })
    .catch(function(error) {
      console.log(error);
    });<commit_msg>Finalized script to translate localized fields<commit_after>const promise = require("bluebird");
const options = {
  // Initialization Options
  promiseLib: promise, // use bluebird as promise library
  capSQL: true, // when building SQL queries dynamically, capitalize SQL keywords
};
const pgp = require("pg-promise")(options);
const { SUPPORTED_LANGUAGES } = require("./../constants.js");
const { find } = require("lodash");
const { db, CASE_BY_ID } = require("../api/helpers/db");

const {Translate} = require('@google-cloud/translate').v2;
const translate = new Translate({projectId: process.env.GOOGLE_PROJECT_ID});

getThings();

function getThings() {
  db.any(`SELECT * FROM things WHERE type IN ('case','method','organization')`)
    .then(function(thingData) {
      thingData.forEach(data => {
        getLocalizationData(data.id);
      });
    })
    .catch(function(error) {
      console.log(error);
    });
}

function getLocalizationData(thingid) {
  db.any(`SELECT * FROM localized_texts WHERE thingid = ${thingid} ORDER BY timestamp DESC LIMIT 1`)
    .then(function(data) {
      data.forEach(data => {
        createNewRecord(data, thingid);
      });
    })
    .catch(function(error) {
      console.log(error);
    });
}

async function createNewRecord(data, thingid) {
  var records = [];
  for (var i = 0; i < SUPPORTED_LANGUAGES.length; i++) {
    const language = SUPPORTED_LANGUAGES[i];

    if (language.twoLetterCode !== data.language) {
      const item = {
        body: '',
        title: '',
        description: '',
        language: language.twoLetterCode,
        thingid: thingid
      };

      if (data.body) {
        item.body = await translateText(data.body, language.twoLetterCode);
      }

      if (data.title) {
        item.title = await translateText(data.title, language.twoLetterCode);
      }

      if (data.description) {
        item.description = await translateText(data.description, language.twoLetterCode);
      }

      records.push(item);
    }
  }
  const insert = pgp.helpers.insert(records, ['body', 'title', 'description', 'language', 'thingid'], 'localized_texts');

  db.none(insert)
    .then(function(data) {
      console.log(data);
    })
    .catch(function(error) {
      console.log(error);
    });
{'fraction_non_alphanumeric': 0.15505669983800047, 'fraction_numerical': 0.0020828511918537375, 'mean_word_length': 2.7035132819194514, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, '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': '3372791', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1288, 'n_words': 263}