<commit_before>'use strict'

const Record = require('../schemas/Record')
const dateWithOffset = require('../utils/dateWithOffset')

const {
	DURATIONS_INTERVAL,
	DURATIONS_LIMIT,
	// DURATIONS_TYPE_AVERAGE,
	DURATIONS_TYPE_DETAILED
} = require('../constants/durations')

const getDetailed = async (id) => {

	// The time that elapsed between the creation and updating of records.
	const projectDuration = {
		$project: {
			duration: {
				$subtract: [ '$updated', '$created' ]
			}
		}
	}

	// Ackee tracks durations in an interval, but some durations are between
	// possible interval times. This could be caused by a network delay or the
	// browser who's throttling JS execution. This step rounds all durations
	// down to the nearest possible interval to get rid of inaccuracy.
	const projectInterval = {
		$project: {
			duration: {
				$multiply: [
					{
						$floor: [
							{
								$divide: [ '$duration', DURATIONS_INTERVAL ]
							}
						]
					},
					DURATIONS_INTERVAL
				]
			}
		}
	}

	// Visits below the tracking interval will have a duration of zero. That's
	// incorrect as visitors spent time on the site, but just not enough. This
	// step sets the minimum duration to the half of the tracking interval.
	// This value is a compromise that doesn't influence the average too much.
	const projectMinInterval = {
		$project: {
			duration: {
				$cond: {
					if: {
						$lt: [ '$duration', DURATIONS_INTERVAL ]
					},
					then: DURATIONS_INTERVAL / 2,
					else: '$duration'
				}
			}
		}
	}

	// Some visitors keep sites open in the background. Their duration is often
	// way above the limit. This distorts the average and should be omitted.
	const matchLimit = {
		$match: {
			duration: {
				$lt: DURATIONS_LIMIT
			}
		}
	}

	const averageEntries = await Record.aggregate([
		{
			$match: {
				domainId: id,
				created: {
					$gte: dateWithOffset(-7)
				}
			}
		},
		projectDuration,
		projectInterval,
		projectMinInterval,
		matchLimit,
		{
			$group: {
				_id: null,
				average: {
					$avg: '$duration'
				}
			}
		},
		{
			$project: {
				average: {
					$ceil: [ '$average' ]
				}
			}
		}
	])

	const detailedEntries = await Record.aggregate([
		{
			$match: {
				domainId: id,
				created: {
					$gte: dateWithOffset(-7)
				}
			}
		},
		projectDuration,<commit_msg>Fix issue when there's no data<commit_after>'use strict'

const Record = require('../schemas/Record')
const dateWithOffset = require('../utils/dateWithOffset')

const {
	DURATIONS_INTERVAL,
	DURATIONS_LIMIT,
	// DURATIONS_TYPE_AVERAGE,
	DURATIONS_TYPE_DETAILED
} = require('../constants/durations')

const getDetailed = async (id) => {

	// The time that elapsed between the creation and updating of records.
	const projectDuration = {
		$project: {
			duration: {
				$subtract: [ '$updated', '$created' ]
			}
		}
	}

	// Ackee tracks durations in an interval, but some durations are between
	// possible interval times. This could be caused by a network delay or the
	// browser who's throttling JS execution. This step rounds all durations
	// down to the nearest possible interval to get rid of inaccuracy.
	const projectInterval = {
		$project: {
			duration: {
				$multiply: [
					{
						$floor: [
							{
								$divide: [ '$duration', DURATIONS_INTERVAL ]
							}
						]
					},
					DURATIONS_INTERVAL
				]
			}
		}
	}

	// Visits below the tracking interval will have a duration of zero. That's
	// incorrect as visitors spent time on the site, but just not enough. This
	// step sets the minimum duration to the half of the tracking interval.
	// This value is a compromise that doesn't influence the average too much.
	const projectMinInterval = {
		$project: {
			duration: {
				$cond: {
					if: {
						$lt: [ '$duration', DURATIONS_INTERVAL ]
					},
					then: DURATIONS_INTERVAL / 2,
					else: '$duration'
				}
			}
		}
	}

	// Some visitors keep sites open in the background. Their duration is often
	// way above the limit. This distorts the average and should be omitted.
	const matchLimit = {
		$match: {
			duration: {
				$lt: DURATIONS_LIMIT
			}
		}
	}

	const averageEntries = await Record.aggregate([
		{
			$match: {
				domainId: id,
				created: {
					$gte: dateWithOffset(-7)
				}
			}
		},
		projectDuration,
		projectInterval,
		projectMinInterval,
		matchLimit,
		{
			$group: {
				_id: null,
				average: {
					$avg: '$duration'
				}
			}
		},
		{
			$project: {
				average: {
					$ceil: [ '$average' ]
				}
			}
		}
	])

	// No need to continue when there're no entries
	if (averageEntries.length === 0) return []

	const detailedEntries = await Record.aggregate([
		{
			$match: {
				domainId: id,
				created: {
					$gte: dateWithOffset(-7)
				}
			}
		},
		projectDuration,
{'fraction_non_alphanumeric': 0.11258139046418819, 'fraction_numerical': 0.0014702793530770846, 'mean_word_length': 2.567041198501873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5363944', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1517, 'n_words': 454}