<commit_before>/**
 * Delete references to deleted datasets
 */

var updated = 0;

// Match all HarvestJob.items.dataset not found in dataset collection
const pipeline = [
    {$unwind: '$items'},  // One row by item
    {$group: {_id: null, datasetId: {$addToSet: '$items.dataset'}}}, // Distinct Dataset IDs
    {$unwind: '$datasetId'}, // One row by ID
    {$lookup: {from: 'dataset', localField: 'datasetId', foreignField: '_id', as: 'dataset'}}, // Join
    {$match: {'dataset': [] }} // Only keep IDs without match
];

const index = {'items.dataset': 1};

db.harvest_job.createIndex(index);<commit_msg>Purge harvestjobs older than retention policy before cleanup<commit_after>/**
 * Delete references to deleted datasets
 */

var updated = 0;
var nbDays = config.HARVEST_JOBS_RETENTION_DAYS;
var minDate = new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * nbDays);

// Delete jobs older then minDate
var result = db.harvest_job.deleteMany({'created': {'$lt': minDate}});
print(`Deleted ${result.deletedCount} HarvestJobs according to retention policy (${config.HARVEST_JOBS_RETENTION_DAYS} days)`);

// Match all HarvestJob.items.dataset not found in dataset collection
const pipeline = [
    {$unwind: '$items'},  // One row by item
    {$group: {_id: null, datasetId: {$addToSet: '$items.dataset'}}}, // Distinct Dataset IDs
    {$unwind: '$datasetId'}, // One row by ID
    {$lookup: {from: 'dataset', localField: 'datasetId', foreignField: '_id', as: 'dataset'}}, // Join
    {$match: {'dataset': [] }} // Only keep IDs without match
];

const index = {'items.dataset': 1};

db.harvest_job.createIndex(index);
{'fraction_non_alphanumeric': 0.1658446362515413, 'fraction_numerical': 0.008631319358816275, 'mean_word_length': 4.338815789473684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4042884', 'n_tokens_mistral': 531, 'n_tokens_neox': 505, 'n_words': 161}