<commit_before>
import { AccountModel } from '@server/models/account/account'
import { ActorModel } from '@server/models/actor/actor'
import { ActorImageModel } from '@server/models/actor/actor-image'
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
import { ServerModel } from '@server/models/server/server'
import { TrackerModel } from '@server/models/server/tracker'
import { UserVideoHistoryModel } from '@server/models/user/user-video-history'
import { ScheduleVideoUpdateModel } from '../../schedule-video-update'
import { TagModel } from '../../tag'
import { ThumbnailModel } from '../../thumbnail'
import { VideoModel } from '../../video'
import { VideoBlacklistModel } from '../../video-blacklist'
import { VideoChannelModel } from '../../video-channel'
import { VideoFileModel } from '../../video-file'
import { VideoLiveModel } from '../../video-live'
import { VideoStreamingPlaylistModel } from '../../video-streaming-playlist'
import { VideoTables } from './video-tables'

type SQLRow = { [id: string]: string | number }

/**
 *
 * Build video models from SQL rows
 *
 */

export class VideoModelBuilder {
  private videosMemo: { [ id: number ]: VideoModel }
  private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
  private videoFileMemo: { [ id: number ]: VideoFileModel }

  private thumbnailsDone: Set<any>
  private historyDone: Set<any>
  private blacklistDone: Set<any>
  private liveDone: Set<any>
  private redundancyDone: Set<any>
  private scheduleVideoUpdateDone: Set<any>

  private trackersDone: Set<string>
  private tagsDone: Set<string>

  private videos: VideoModel[]

  private readonly buildOpts = { raw: true, isNewRecord: false }

  constructor (
    readonly mode: 'get' | 'list',
    readonly tables: VideoTables
  ) {

  }

  buildVideosFromRows (rows: SQLRow[], rowsWebTorrentFiles?: SQLRow[], rowsStreamingPlaylist?: SQLRow[]) {
    this.reinit()

    for (const row of rows) {
      this.buildVideoAndAccount(row)

      const videoModel = this.videosMemo[row.id]

      this.setUserHistory(row, videoModel)
      this.addThumbnail(row, videoModel)

      if (!rowsWebTorrentFiles) {
        this.addWebTorrentFile(row, videoModel)
      }

      if (!rowsStreamingPlaylist) {
        this.addStreamingPlaylist(row, videoModel)
        this.addStreamingPlaylistFile(row)
      }

      if (this.mode === 'get') {
        this.addTag(row, videoModel)
        this.addTracker(row, videoModel)
        this.setBlacklisted(row, videoModel)
        this.setScheduleVideoUpdate(row, videoModel)
        this.setLive(row, videoModel)
      }
    }

    this.grabSeparateWebTorrentFiles(rowsWebTorrentFiles)
    this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)

    return this.videos
  }

  private reinit () {
    this.videosMemo = {}
    this.videoStreamingPlaylistMemo = {}
    this.videoFileMemo = {}

    this.thumbnailsDone = new Set<number>()
    this.historyDone = new Set<number>()
    this.blacklistDone = new Set<number>()
    this.liveDone = new Set<number>()
    this.redundancyDone = new Set<number>()
    this.scheduleVideoUpdateDone = new Set<number>()

    this.trackersDone = new Set<string>()
    this.tagsDone = new Set<string>()

    this.videos = []
  }

  private grabSeparateWebTorrentFiles (rowsWebTorrentFiles?: SQLRow[]) {
    if (!rowsWebTorrentFiles) return

    for (const row of rowsWebTorrentFiles) {
      const id = row['VideoFiles.id']
      if (!id) continue

      const videoModel = this.videosMemo[row.id]
      this.addWebTorrentFile(row, videoModel)
      this.addRedundancy(row, 'VideoFiles.RedundancyVideos', this.videoFileMemo[id])
    }
  }

  private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
    if (!rowsStreamingPlaylist) return

    for (const row of rowsStreamingPlaylist || []) {
      const id = row['VideoStreamingPlaylists.id']
      if (!id) continue

      const videoModel = this.videosMemo[row.id]

      this.addStreamingPlaylist(row, videoModel)
      this.addStreamingPlaylistFile(row)
      this.addRedundancy(
        row,
        'VideoStreamingPlaylists.RedundancyVideos',
        this.videoStreamingPlaylistMemo[id]
      )<commit_msg>Fix redundancy row grabbing<commit_after>
import { AccountModel } from '@server/models/account/account'
import { ActorModel } from '@server/models/actor/actor'
import { ActorImageModel } from '@server/models/actor/actor-image'
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
import { ServerModel } from '@server/models/server/server'
import { TrackerModel } from '@server/models/server/tracker'
import { UserVideoHistoryModel } from '@server/models/user/user-video-history'
import { ScheduleVideoUpdateModel } from '../../schedule-video-update'
import { TagModel } from '../../tag'
import { ThumbnailModel } from '../../thumbnail'
import { VideoModel } from '../../video'
import { VideoBlacklistModel } from '../../video-blacklist'
import { VideoChannelModel } from '../../video-channel'
import { VideoFileModel } from '../../video-file'
import { VideoLiveModel } from '../../video-live'
import { VideoStreamingPlaylistModel } from '../../video-streaming-playlist'
import { VideoTables } from './video-tables'

type SQLRow = { [id: string]: string | number }

/**
 *
 * Build video models from SQL rows
 *
 */

export class VideoModelBuilder {
  private videosMemo: { [ id: number ]: VideoModel }
  private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
  private videoFileMemo: { [ id: number ]: VideoFileModel }

  private thumbnailsDone: Set<any>
  private historyDone: Set<any>
  private blacklistDone: Set<any>
  private liveDone: Set<any>
  private redundancyDone: Set<any>
  private scheduleVideoUpdateDone: Set<any>

  private trackersDone: Set<string>
  private tagsDone: Set<string>

  private videos: VideoModel[]

  private readonly buildOpts = { raw: true, isNewRecord: false }

  constructor (
    readonly mode: 'get' | 'list',
    readonly tables: VideoTables
  ) {

  }

  buildVideosFromRows (rows: SQLRow[], rowsWebTorrentFiles?: SQLRow[], rowsStreamingPlaylist?: SQLRow[]) {
    this.reinit()

    for (const row of rows) {
      this.buildVideoAndAccount(row)

      const videoModel = this.videosMemo[row.id]

      this.setUserHistory(row, videoModel)
      this.addThumbnail(row, videoModel)

      if (!rowsWebTorrentFiles) {
        this.addWebTorrentFile(row, videoModel)
      }

      if (!rowsStreamingPlaylist) {
        this.addStreamingPlaylist(row, videoModel)
        this.addStreamingPlaylistFile(row)
      }

      if (this.mode === 'get') {
        this.addTag(row, videoModel)
        this.addTracker(row, videoModel)
        this.setBlacklisted(row, videoModel)
        this.setScheduleVideoUpdate(row, videoModel)
        this.setLive(row, videoModel)
      }
    }

    this.grabSeparateWebTorrentFiles(rowsWebTorrentFiles)
    this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)

    return this.videos
  }

  private reinit () {
    this.videosMemo = {}
    this.videoStreamingPlaylistMemo = {}
    this.videoFileMemo = {}

    this.thumbnailsDone = new Set<number>()
    this.historyDone = new Set<number>()
    this.blacklistDone = new Set<number>()
    this.liveDone = new Set<number>()
    this.redundancyDone = new Set<number>()
    this.scheduleVideoUpdateDone = new Set<number>()

    this.trackersDone = new Set<string>()
    this.tagsDone = new Set<string>()

    this.videos = []
  }

  private grabSeparateWebTorrentFiles (rowsWebTorrentFiles?: SQLRow[]) {
    if (!rowsWebTorrentFiles) return

    for (const row of rowsWebTorrentFiles) {
      const id = row['VideoFiles.id']
      if (!id) continue

      const videoModel = this.videosMemo[row.id]
      this.addWebTorrentFile(row, videoModel)
      this.addRedundancy(row, 'VideoFiles', this.videoFileMemo[id])
    }
  }

  private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
    if (!rowsStreamingPlaylist) return

    for (const row of rowsStreamingPlaylist || []) {
      const id = row['VideoStreamingPlaylists.id']
      if (!id) continue

      const videoModel = this.videosMemo[row.id]

      this.addStreamingPlaylist(row, videoModel)
      this.addStreamingPlaylistFile(row)
      this.addRedundancy(row, 'VideoStreamingPlaylists', this.videoStreamingPlaylistMemo[id])
{'fraction_non_alphanumeric': 0.12070192079677496, 'fraction_numerical': 0.0, 'mean_word_length': 3.686111111111111, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '3310146', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2551, 'n_words': 546}