<commit_before>package x7c1.linen.scene.updater

import android.app.Service
import android.content.Intent
import x7c1.linen.database.control.DatabaseHelper
import x7c1.linen.glue.service.ServiceControl
import x7c1.linen.repository.date.Date
import x7c1.linen.repository.dummy.{DummyFactory, TraceableQueue}
import x7c1.linen.repository.loader.crawling.{Implicits, SourceInspector}
import x7c1.linen.repository.loader.schedule.LoaderSchedule
import x7c1.linen.repository.preset.PresetFactory
import x7c1.linen.repository.source.setting.{SettingSource, SettingSourceAccessorFactory}
import x7c1.linen.scene.loader.crawling.LoaderScheduler
import x7c1.wheat.macros.intent.{ExtraNotFound, IntentExpander}
import x7c1.wheat.macros.logger.Log
import x7c1.wheat.modern.formatter.ThrowableFormatter.format
import x7c1.wheat.modern.patch.TaskAsync.async

import scala.concurrent.Future

class UpdaterMethods(
  service: Service with ServiceControl,
  helper: DatabaseHelper,
  queue: TraceableQueue,
  startId: Int){

  import Implicits._

  def execute(intent: Intent) = IntentExpander findFrom intent match {
    case Left(e: ExtraNotFound) => Log error e.toString
    case Left(notFound) => Log info notFound.toString
    case Right(f) => f.apply()
  }
  def createDummies(max: Int): Unit = async {
    Log info "[init]"
    val notifier = new UpdaterServiceNotifier(service, max, Date.current(), startId)
    DummyFactory.createDummies0(service)(max){ n =>
      notifier.notifyProgress(n)
    }
    notifier.notifyDone()
    service stopSelf startId
  }
  def createPresetJp(): Unit = async {
    Log info "[init]"
    new PresetFactory(helper).setupJapanesePresets()
  }
  def createDummySources(channelIds: Seq[Long]) = async {
    Log info s"$channelIds"
  }
  def setupLoaderSchedule(accountId: Long): Unit = Future {
    Log error s"[init]"

    helper.selectorOf[LoaderSchedule] findPresetSchedule accountId matches {
      case Right(Some(schedule)) => LoaderScheduler(service) createOrUpdate schedule
      case Right(None) => Log error s"preset schedule not found"
      case Left(e) => Log error format(e){"[failed]"}
    }
  }
  def loadFromSchedule(scheduleId: Long): Unit = {
    Log info s"schedule:$scheduleId"
  }
  def loadSource(sourceId: Long): Unit = {
    Log info s"[init] source-id: $sourceId"

    val inspector = SourceInspector(helper)
    val future = Future { inspector inspectSource sourceId } map {
      case Right(source) => queue enqueue source
      case Left(error) => Log error error.message
    }
    future onFailure {
      case e => Log error format(e)(s"[error] source(id:$sourceId)")
    }
  }
  def loadChannelSources(channelId: Long, accountId: Long): Unit = async {
    Log info s"[init] channel:$channelId"

    val factory = new SettingSourceAccessorFactory(helper.getReadableDatabase, accountId)
    val inspector = SourceInspector(helper)
    val accessor = factory.create(channelId)

    val settingSources = (0 to accessor.length - 1) flatMap accessor.findAt

    val inspectedSources = settingSources map inspector.inspectSource[SettingSource]
    val targetSources = inspectedSources collect {
      case Right(sources) => sources
    }
    inspectedSources collect {
      case Left(error) => Log error error.message<commit_msg>add logging in case of unexpected failure<commit_after>package x7c1.linen.scene.updater

import android.app.Service
import android.content.Intent
import x7c1.linen.database.control.DatabaseHelper
import x7c1.linen.glue.service.ServiceControl
import x7c1.linen.repository.date.Date
import x7c1.linen.repository.dummy.{DummyFactory, TraceableQueue}
import x7c1.linen.repository.loader.crawling.{Implicits, SourceInspector}
import x7c1.linen.repository.loader.schedule.LoaderSchedule
import x7c1.linen.repository.preset.PresetFactory
import x7c1.linen.repository.source.setting.{SettingSource, SettingSourceAccessorFactory}
import x7c1.linen.scene.loader.crawling.LoaderScheduler
import x7c1.wheat.macros.intent.{ExtraNotFound, IntentExpander}
import x7c1.wheat.macros.logger.Log
import x7c1.wheat.modern.formatter.ThrowableFormatter.format
import x7c1.wheat.modern.patch.TaskAsync.async

import scala.concurrent.Future

class UpdaterMethods(
  service: Service with ServiceControl,
  helper: DatabaseHelper,
  queue: TraceableQueue,
  startId: Int){

  import Implicits._

  def execute(intent: Intent) = IntentExpander findFrom intent match {
    case Left(e: ExtraNotFound) => Log error e.toString
    case Left(notFound) => Log info notFound.toString
    case Right(f) => f.apply()
  }
  def createDummies(max: Int): Unit = async {
    Log info "[init]"
    val notifier = new UpdaterServiceNotifier(service, max, Date.current(), startId)
    DummyFactory.createDummies0(service)(max){ n =>
      notifier.notifyProgress(n)
    }
    notifier.notifyDone()
    service stopSelf startId
  }
  def createPresetJp(): Unit = async {
    Log info "[init]"
    new PresetFactory(helper).setupJapanesePresets()
  }
  def createDummySources(channelIds: Seq[Long]) = async {
    Log info s"$channelIds"
  }
  def setupLoaderSchedule(accountId: Long): Unit = Future {
    Log error s"[init]"

    helper.selectorOf[LoaderSchedule] findPresetSchedule accountId matches {
      case Right(Some(schedule)) => LoaderScheduler(service) createOrUpdate schedule
      case Right(None) => Log error s"preset schedule not found"
      case Left(e) => Log error format(e){"[failed]"}
    }
  } onFailure {
    case e => Log error format(e){"[abort] (unexpected)"}
  }
  def loadFromSchedule(scheduleId: Long): Unit = {
    Log info s"schedule:$scheduleId"
  }
  def loadSource(sourceId: Long): Unit = {
    Log info s"[init] source-id: $sourceId"

    val inspector = SourceInspector(helper)
    val future = Future { inspector inspectSource sourceId } map {
      case Right(source) => queue enqueue source
      case Left(error) => Log error error.message
    }
    future onFailure {
      case e => Log error format(e)(s"[error] source(id:$sourceId)")
    }
  }
  def loadChannelSources(channelId: Long, accountId: Long): Unit = async {
    Log info s"[init] channel:$channelId"

    val factory = new SettingSourceAccessorFactory(helper.getReadableDatabase, accountId)
    val inspector = SourceInspector(helper)
    val accessor = factory.create(channelId)

    val settingSources = (0 to accessor.length - 1) flatMap accessor.findAt

    val inspectedSources = settingSources map inspector.inspectSource[SettingSource]
    val targetSources = inspectedSources collect {
      case Right(sources) => sources
    }
    inspectedSources collect {
      case Left(error) => Log error error.message
{'fraction_non_alphanumeric': 0.10033042955842596, 'fraction_numerical': 0.009312105737458696, 'mean_word_length': 4.4094232331437855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, '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': '1374903', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2018, 'n_words': 480}