<commit_before>import _ from 'lodash';

import { split_by_hour_into_data } from '~/util/transforms';
import { getColorFromCategory } from '~/util/color';
import { Category } from '~/util/classes';

// TODO: Move elsewhere
interface Event {
  timestamp: string;
  duration: number;
  data: object;
}

interface HourlyData {
  cat_events: Event[];
}

interface Dataset {
  label: string;
  backgroundColor: string;
  data: number[];
}

export function buildBarchartDataset(
  $store: any,
  data_by_hour: HourlyData[],
  classes: Category[]
): Dataset[] {
  const SEP = '>>>';
  const data = data_by_hour;
  if (data) {
    const category_names: Set<string> = new Set(
      Object.values(data)
        .map(result => {
          return result.cat_events.map(e => e.data['$category'].join(SEP));
        })
        .flat()
    );
    const ds: Dataset[] = [...category_names]
      .map(c_ => {
        const c = $store.getters['categories/get_category'](c_.split(SEP));
        if (c) {
          const values = Object.values(data).map(results => {
            const cat = results.cat_events.find(e => _.isEqual(e.data['$category'], c.name));
            if (cat) return Math.round((cat.duration / (60 * 60)) * 1000) / 1000;
            else return null;
          });
          return {
            label: c.name.join(' > '),
            backgroundColor: getColorFromCategory(c, classes),
            data: values,
          } as Dataset;
        } else {
          // FIXME: This shouldn't happen
          // This may for example happen if one doesn't have an 'Uncategorized' category,
          // as can happen when one upgrades from an old version where there wasn't one in the default classes.
          console.error('missing category:', c_);
        }
      })
      .filter(x => x);
    return ds;
  } else {
    return [];
  }
}

export function buildBarchartDatasetActive($store: any, events_active: Event[]) {
  const data = split_by_hour_into_data(events_active);
  return [
    {
      label: 'Total time',
      backgroundColor: '#6699ff',
      data,
    },
  ];
}<commit_msg>fix: fixed type errors<commit_after>import _ from 'lodash';

import { split_by_hour_into_data } from '~/util/transforms';
import { getColorFromCategory } from '~/util/color';
import { Category } from '~/util/classes';

// TODO: Move elsewhere
interface IEvent {
  timestamp: string;
  duration: number;
  data: Record<string, any>;
}

interface HourlyData {
  cat_events: IEvent[];
}

interface Dataset {
  label: string;
  backgroundColor: string;
  data: number[];
}

export function buildBarchartDataset(
  $store: any,
  data_by_hour: HourlyData[],
  classes: Category[]
): Dataset[] {
  const SEP = '>>>';
  const data = data_by_hour;
  if (data) {
    const category_names: Set<string> = new Set(
      Object.values(data)
        .map(result => {
          return result.cat_events.map(e => e.data['$category'].join(SEP));
        })
        .flat()
    );
    const ds: Dataset[] = [...category_names]
      .map(c_ => {
        const c = $store.getters['categories/get_category'](c_.split(SEP));
        if (c) {
          const values = Object.values(data).map(results => {
            const cat = results.cat_events.find(e => _.isEqual(e.data['$category'], c.name));
            if (cat) return Math.round((cat.duration / (60 * 60)) * 1000) / 1000;
            else return null;
          });
          return {
            label: c.name.join(' > '),
            backgroundColor: getColorFromCategory(c, classes),
            data: values,
          } as Dataset;
        } else {
          // FIXME: This shouldn't happen
          // This may for example happen if one doesn't have an 'Uncategorized' category,
          // as can happen when one upgrades from an old version where there wasn't one in the default classes.
          console.error('missing category:', c_);
        }
      })
      .filter(x => x);
    return ds;
  } else {
    return [];
  }
}

export function buildBarchartDatasetActive($store: any, events_active: IEvent[]) {
  const data = split_by_hour_into_data(events_active);
  return [
    {
      label: 'Total time',
      backgroundColor: '#6699ff',
      data,
    },
  ];
}
{'fraction_non_alphanumeric': 0.1382751247327156, 'fraction_numerical': 0.007602755999049656, 'mean_word_length': 2.476465730800991, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 26, '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': '5176671', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1300, 'n_words': 334}