<commit_before>import * as OS from 'os'
import { UAParser } from 'ua-parser-js'
import { StatsDatabase, ILaunchStats, IDailyMeasures } from './stats-database'
import { getDotComAPIEndpoint } from '../api'
import { getVersion } from '../../ui/lib/app-proxy'
import { hasShownWelcomeFlow } from '../welcome'
import { Account } from '../../models/account'

const StatsEndpoint = 'https://central.github.com/api/usage/desktop'

const LastDailyStatsReportKey = 'last-daily-stats-report'

/** How often daily stats should be submitted (i.e., 24 hours). */
const DailyStatsReportInterval = 1000 * 60 * 60 * 24

type DailyStats = { version: string } & ILaunchStats & IDailyMeasures

/** The store for the app's stats. */
export class StatsStore {
  private db: StatsDatabase

  /** Has the user opted out of stats reporting? */
  private optOut: boolean

  public constructor(db: StatsDatabase) {
    this.db = db

    const optOutValue = localStorage.getItem('stats-opt-out')
    if (optOutValue) {
      this.optOut = !!parseInt(optOutValue, 10)
    } else {
      this.optOut = false
    }
  }

  /** Should the app report its daily stats? */
  private shouldReportDailyStats(): boolean {
    const lastDateString = localStorage.getItem(LastDailyStatsReportKey)
    let lastDate = 0
    if (lastDateString && lastDateString.length > 0) {
      lastDate = parseInt(lastDateString, 10)
    }

    if (isNaN(lastDate)) {
      lastDate = 0
    }

    const now = Date.now()
    return now - lastDate > DailyStatsReportInterval
  }

  /** Report any stats which are eligible for reporting. */
  public async reportStats(accounts: ReadonlyArray<Account>) {
    if (this.optOut) { return }

    // Never report stats while in dev or test. They could be pretty crazy.
    if (__DEV__ || process.env.TEST_ENV) {<commit_msg>Create & store a guid<commit_after>import * as OS from 'os'
import { UAParser } from 'ua-parser-js'
import { StatsDatabase, ILaunchStats, IDailyMeasures } from './stats-database'
import { getDotComAPIEndpoint } from '../api'
import { getVersion } from '../../ui/lib/app-proxy'
import { hasShownWelcomeFlow } from '../welcome'
import { Account } from '../../models/account'
import { v4 as generateGUID } from 'uuid'

const StatsEndpoint = 'https://central.github.com/api/usage/desktop'

const LastDailyStatsReportKey = 'last-daily-stats-report'

const StatsGUIDKey = 'stats-guid'

/** How often daily stats should be submitted (i.e., 24 hours). */
const DailyStatsReportInterval = 1000 * 60 * 60 * 24

type DailyStats = { version: string } & ILaunchStats & IDailyMeasures

/** The store for the app's stats. */
export class StatsStore {
  private db: StatsDatabase

  /** Has the user opted out of stats reporting? */
  private optOut: boolean

  /** The GUID for uniquely identifying installations. */
  private readonly guid: string

  public constructor(db: StatsDatabase) {
    this.db = db

    const optOutValue = localStorage.getItem('stats-opt-out')
    if (optOutValue) {
      this.optOut = !!parseInt(optOutValue, 10)
    } else {
      this.optOut = false
    }

    let guid = localStorage.getItem(StatsGUIDKey)
    if (!guid) {
      guid = generateGUID()
      localStorage.setItem(StatsGUIDKey, guid)
    }

    this.guid = guid
  }

  /** Should the app report its daily stats? */
  private shouldReportDailyStats(): boolean {
    const lastDateString = localStorage.getItem(LastDailyStatsReportKey)
    let lastDate = 0
    if (lastDateString && lastDateString.length > 0) {
      lastDate = parseInt(lastDateString, 10)
    }

    if (isNaN(lastDate)) {
      lastDate = 0
    }

    const now = Date.now()
    return now - lastDate > DailyStatsReportInterval
  }

  /** Report any stats which are eligible for reporting. */
  public async reportStats(accounts: ReadonlyArray<Account>) {
    if (this.optOut) { return }

    // Never report stats while in dev or test. They could be pretty crazy.
    if (__DEV__ || process.env.TEST_ENV) {
{'fraction_non_alphanumeric': 0.11545248298462314, 'fraction_numerical': 0.009831106629694983, 'mean_word_length': 3.294372294372294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '5302304', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1168, 'n_words': 359}