<commit_before>const Query = require('@domoinc/query');
const domo = require('ryuu.js');

module.exports = ngModule => {
  function transactionsAnalyticsService($q) {
    // Private variables
    const _queryBuilder = new Query();
    const _dataset = 'transactions';
    let _totals = undefined;

    // Public API here
    const service = {
      getTotals,
      getTopSellingItem/*,
      getTopGrossingItem*/
    };

    return service;

    //// Functions ////
    // returns info on the total no of transactions, total products sold, and total income
    function getTotals() {
      if (typeof _totals !== 'undefined') {
        return $q.resolve(_totals);
      }

      const deferred = $q.defer();
      const query = _queryBuilder.select(['category', 'quantity', 'price', 'name']).
        groupBy('category', { price: 'sum', quantity: 'sum', name: 'count' });

      _queryDb(query).then(data => {
        let transactionCount = 0;
        let productsSold = 0;
        let income = 0;
        for (let i = 0; i < data.length; i++) {
          transactionCount += data[i].name; //confusing, I know...
          productsSold += data[i].quantity;
          income += data[i].price;
        }
        _totals = {
          transactionCount,
          productsSold,
          income
        };
        deferred.resolve(_totals);
      }, error => {
        deferred.reject(error);
      });
      return deferred.promise;
    }

    // todo: return all top selling?
    function getTopSellingItem(date) {
      const queryBuilder = new Query();
      const query = queryBuilder.select(['name', 'price', 'quantity']).orderBy('quantity', 'desc').limit(10);

      domo.get(query.query('transactions')).then(data => {
        console.log(data);
      });

      if (date) {
        return;
      }
    }

     /*function getTopGrossingItem(date) {
      const queryBuilder = new Query();
      const query = queryBuilder.select(['name', 'total']).groupBy('name').orderBy('total', 'desc').limit(10);
      console.log(query.query('transactions'));

      domo.get(query.query('transactions')).then(data => {
        console.log(data);
      });

      if (date) {
        return;
      }
    }*/


    function _queryDb(query) {
      const deferred = $q.defer();
      domo.get(query.query(_dataset)).then(data => {
        deferred.resolve(data);<commit_msg>refactor(make the other model functions all pretty :3):<commit_after>const Query = require('@domoinc/query');
const domo = require('ryuu.js');

module.exports = ngModule => {
  function transactionsAnalyticsService($q) {
    // Private variables
    const _queryBuilder = new Query();
    const _dataset = 'transactions';
    let _totals = undefined;
    let _topSelling = undefined;

    // Public API here
    const service = {
      getTotals,
      getTopSellingItem/*,
      getTopGrossingItem*/
    };

    return service;

    //// Functions ////
    // returns info on the total no of transactions, total products sold, and total income
    function getTotals() {
      if (typeof _totals !== 'undefined') {
        return $q.resolve(_totals);
      }

      const deferred = $q.defer();
      const query = _queryBuilder.select(['category', 'quantity', 'price', 'name']).
        groupBy('category', { price: 'sum', quantity: 'sum', name: 'count' });

      _queryDb(query).then(data => {
        let transactionCount = 0;
        let productsSold = 0;
        let income = 0;
        for (let i = 0; i < data.length; i++) {
          transactionCount += data[i].name; //confusing, I know...
          productsSold += data[i].quantity;
          income += data[i].price;
        }
        _totals = {
          transactionCount,
          productsSold,
          income
        };
        deferred.resolve(_totals);
      }, error => {
        deferred.reject(error);
      });
      return deferred.promise;
    }

    // todo: return all top selling?
    function getTopSellingItem() {
      if (typeof _topSelling !== 'undefined') {
        return $q.resolve(_topSelling);
      }

      const deferred = $q.defer();
      const query = _queryBuilder.select(['name', 'price', 'quantity']).
        orderBy('quantity', 'desc').limit(10);

      _queryDb(query).then(data => {
        _topSelling = data;
        deferred.resolve(data);
      }, error => {
        deferred.reject(error);
      });

      return deferred.promise;
    }

     /*function getTopGrossingItem(date) {
      const queryBuilder = new Query();
      const query = queryBuilder.select(['name', 'total']).groupBy('name').orderBy('total', 'desc').limit(10);
      console.log(query.query('transactions'));

      domo.get(query.query('transactions')).then(data => {
        console.log(data);
      });

      if (date) {
        return;
      }
    }*/


    function _queryDb(query) {
      const deferred = $q.defer();
      domo.get(query.query(_dataset)).then(data => {
        deferred.resolve(data);
{'fraction_non_alphanumeric': 0.14934409687184663, 'fraction_numerical': 0.0034308779011099897, 'mean_word_length': 2.31062124248497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '264232', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1433, 'n_words': 322}