<commit_before>import report from "gatsby-cli/lib/reporter"
import { getConfigStore, getGatsbyVersion } from "gatsby-core-utils"
import latestVersion from "latest-version"
import getDayOfYear from "date-fns/getDayOfYear"

const feedbackKey = `feedback.disabled`
const lastDateKey = `feedback.lastRequestDate`

// This function is designed to be used by `gatsby feedback --disable`
// and `gatsby feedback --enable`. This key is used to determine
// if a user is allowed to be solicited for feedback
export function setFeedbackDisabledValue(enabled: boolean): void {
  getConfigStore().set(feedbackKey, enabled)
}

// Print the feedback request to the user
export function showFeedbackRequest(): void {
  getConfigStore().set(lastDateKey, Date.now())
  report.log(
    `\n\nHello! Will you help Gatsby improve by taking a four question survey?\nIt takes less than five minutes and your ideas and feedback will be very helpful.`
  )
  report.log(`\nGive us your feedback here: https://gatsby.dev/feedback\n\n`)
}

// We are only showing feedback requests to users in if they pass a few checks:
// 1. They pass a Math.random() check. This is a skateboard version of not sending out all requests in one day.
// 2. They haven't disabled the feedback mechanism
// 3. They don't have the environment variable to disable feedback present
// 4. It's been at least 3 months since the last feedback request
// 5. They are on the most recent version of Gatsby
export async function userPassesFeedbackRequestHeuristic(): Promise<boolean> {
  // Heuristic 1
  // This is spreading the request volume over the quarter.
  // We are grabbing a randomNumber within the spread of a first day
  // of a quarter, to the last day
  const currentQuarter = Math.floor((new Date().getMonth() + 3) / 3)
  const randomNumber = Math.floor(
    Math.random() *
      // One quarter year in days (roughly)
      (30 * 3)
  )
  const randomNumberWithinQuarter = randomNumber * currentQuarter

  if (randomNumberWithinQuarter !== getDayOfYear(new Date())) {
    return false
  }

  // Heuristic 2
  if (getConfigStore().get(feedbackKey) === true) {
    return false
  }

  // Heuristic 3
  if (process.env.GATSBY_FEEDBACK_DISABLED === `1`) {
    return false
  }

  // Heuristic 4
  const lastDateValue = getConfigStore().get(lastDateKey)
  // 4.a if the user has never received the feedback request, this is undefined<commit_msg>chore(gatsby): Increase chances of seeing feedback request 5x (#22393)<commit_after>import report from "gatsby-cli/lib/reporter"
import { getConfigStore, getGatsbyVersion } from "gatsby-core-utils"
import latestVersion from "latest-version"
import getDayOfYear from "date-fns/getDayOfYear"

const feedbackKey = `feedback.disabled`
const lastDateKey = `feedback.lastRequestDate`

// This function is designed to be used by `gatsby feedback --disable`
// and `gatsby feedback --enable`. This key is used to determine
// if a user is allowed to be solicited for feedback
export function setFeedbackDisabledValue(enabled: boolean): void {
  getConfigStore().set(feedbackKey, enabled)
}

// Print the feedback request to the user
export function showFeedbackRequest(): void {
  getConfigStore().set(lastDateKey, Date.now())
  report.log(
    `\n\nHello! Will you help Gatsby improve by taking a four question survey?\nIt takes less than five minutes and your ideas and feedback will be very helpful.`
  )
  report.log(`\nGive us your feedback here: https://gatsby.dev/feedback\n\n`)
}

const randomChanceToBeTrue = (): boolean => {
  // This is spreading the request volume over the quarter.
  // We are grabbing a randomNumber within the spread of a first day
  // of a quarter, to the last day
  const currentQuarter = Math.floor((new Date().getMonth() + 3) / 3)
  const randomNumber = Math.floor(
    Math.random() *
      // One quarter year in days (roughly)
      (30 * 3)
  )
  const randomNumberWithinQuarter = randomNumber * currentQuarter

  return randomNumberWithinQuarter === getDayOfYear(new Date())
}

// We are only showing feedback requests to users in if they pass a few checks:
// 1. They pass a Math.random() check. This is a skateboard version of not sending out all requests in one day.
// 2. They haven't disabled the feedback mechanism
// 3. They don't have the environment variable to disable feedback present
// 4. It's been at least 3 months since the last feedback request
// 5. They are on the most recent version of Gatsby
export async function userPassesFeedbackRequestHeuristic(): Promise<boolean> {
  // Heuristic 1
  // We originally wrote this to have a single chance of hitting.
  // We wanted to up the chance by 5x, so this is our crude - temporary -
  // way of giving the user 5 chances to passing.
  const randomlyPassingHeuristic =
    randomChanceToBeTrue() ||
    randomChanceToBeTrue() ||
    randomChanceToBeTrue() ||
    randomChanceToBeTrue() ||
    randomChanceToBeTrue()

  if (!randomlyPassingHeuristic) {
    return false
  }

  // Heuristic 2
  if (getConfigStore().get(feedbackKey) === true) {
    return false
  }

  // Heuristic 3
  if (process.env.GATSBY_FEEDBACK_DISABLED === `1`) {
    return false
  }

  // Heuristic 4
  const lastDateValue = getConfigStore().get(lastDateKey)
  // 4.a if the user has never received the feedback request, this is undefined
{'fraction_non_alphanumeric': 0.09133709981167608, 'fraction_numerical': 0.007909604519774011, 'mean_word_length': 3.7760791366906474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1066128', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1501, 'n_words': 607}