Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Mar 16, 2024
1 parent 56006a3 commit e49ec48
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 157 deletions.
26 changes: 10 additions & 16 deletions scalawiki-wlx/src/main/scala/org/scalawiki/wlx/dto/Contest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package org.scalawiki.wlx.dto

import java.time.ZonedDateTime

import com.typesafe.config.{
Config,
ConfigFactory,
ConfigParseOptions,
ConfigResolveOptions
}
import com.typesafe.config.{Config, ConfigFactory, ConfigParseOptions, ConfigResolveOptions}
import org.scalawiki.wlx.stat.rating.RateConfig

import scala.util.Try
Expand All @@ -34,9 +29,9 @@ case class Contest(
config: Option[Config] = None
) extends HasImagesCategory {

def campaign = contestType.code + "-" + country.code
def campaign: String = contestType.code + "-" + country.code

def name = s"${contestType.name} $year" + countryName.fold("")(" in " + _)
def name: String = s"${contestType.name} $year" + countryName.fold("")(" in " + _)

def countryName: Option[String] =
if (country != NoAdmDivision)
Expand All @@ -62,17 +57,17 @@ case class Contest(
def fileTemplate: Option[String] =
uploadConfigs.headOption.map(_.fileTemplate)

def listsHost: Option[String] = {
def listsHost: Option[String] =
uploadConfigs.head.listsHost
.orElse(
country.languageCodes.headOption.map(_ + ".wikipedia.org")
)
}

}

object Contest {

val opts = ConfigParseOptions.defaults.setAllowMissing(false)
private val opts = ConfigParseOptions.defaults.setAllowMissing(false)

def load(name: String): Option[Contest] = {
Try {
Expand Down Expand Up @@ -106,9 +101,8 @@ object Contest {

for (
contestType <- ContestType.byCode(typeStr.toLowerCase);
country <- Country.fromJavaLocales.find(country =>
country.name == countryStr || country.code == countryStr
)
country <- Country.fromJavaLocales
.find(country => country.name == countryStr || country.code == countryStr)
)
yield new Contest(
contestType,
Expand All @@ -133,10 +127,10 @@ object Contest {
Nil
)

def WLMUkraine(year: Int) =
def WLMUkraine(year: Int): Contest =
load("wlm_ua.conf").get.copy(year = year)

def WLEUkraine(year: Int) =
def WLEUkraine(year: Int): Contest =
load("wle_ua.conf").get.copy(year = year)

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ trait HasImagesCategory {
def imagesCategory: String
}

class ContestType(val code: String, val name: String)
extends HasImagesCategory {
class ContestType(val code: String, val name: String) extends HasImagesCategory {
override def imagesCategory: String = "Category:Images from " + name
}

Expand All @@ -14,7 +13,7 @@ object ContestType {
val WLE = new ContestType("wle", "Wiki Loves Earth")
val ESPC = new ContestType("espc", "European Science Photo Competition")

val all = Seq(WLM, WLE, ESPC)
val all: Seq[ContestType] = Seq(WLM, WLE, ESPC)

def byName(name: String): Option[ContestType] =
all.find(_.name == name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ import scala.concurrent.Future
* @param contest
* contest: contest type (WLM/WLE), country, year, etc.
* @param startYear
* the first year contest was held, or the first year that we are interested
* in
* the first year contest was held, or the first year that we are interested in
* @param monumentDb
* cultural/natural monuments database for the contest
* @param currentYearImageDb
* image database for current year's contest
* @param totalImageDb
* image database that holds images of all monuments from the contest,
* regardless of when they where uploaded
* image database that holds images of all monuments from the contest, regardless of when they
* where uploaded
* @param dbsByYear
* image databases split by contest year
* @param monumentDbOld
* monument database at current year's contest start. Used to rate users who
* submitted newly pictured monuments.
*/
case class ContestStat(
contest: Contest,
Expand All @@ -39,45 +35,35 @@ case class ContestStat(
currentYearImageDb: Option[ImageDB] = None,
totalImageDb: Option[ImageDB] = None,
dbsByYear: Seq[ImageDB] = Seq.empty,
monumentDbOld: Option[MonumentDB] = None,
config: Option[StatConfig] = None
) {

val imageDbsByYear = dbsByYear.groupBy(_.contest.year)
val yearSeq = imageDbsByYear.keys.toSeq.sorted
val imageDbsByYear: Map[Int, Seq[ImageDB]] = dbsByYear.groupBy(_.contest.year)
val yearSeq: Seq[Int] = imageDbsByYear.keys.toSeq.sorted

lazy val oldImages: Iterable[Image] = {
for (
total <- totalImageDb;
for {
total <- totalImageDb
current <- currentYearImageDb
)
yield {
val currentImageIds = current.images.flatMap(_.pageId).toSet
total.images.filter(image =>
!currentImageIds.contains(image.pageId.get)
)
}
currentImageIds = current.images.flatMap(_.pageId).toSet
} yield total.images.filterNot(_.pageId.exists(currentImageIds.contains))
}.getOrElse(Nil)

def imageDbByYear(year: Int) = imageDbsByYear.get(year).map(_.head)
def imageDbByYear(year: Int): Option[ImageDB] = imageDbsByYear.get(year).map(_.head)

def mapYears[T](f: ImageDB => T) = {
for (
year <- yearSeq;
def mapYears[T](f: ImageDB => T): Seq[T] =
for {
year <- yearSeq
imageDb <- imageDbByYear(year)
)
yield f(imageDb)
}
} yield f(imageDb)
}

/** Coordinates fetching contest statistics and creating reports/galleries etc.
* Needs refactoring.
/** Coordinates fetching contest statistics and creating reports/galleries etc. Needs refactoring.
*
* @param contest
* contest: contest type (WLM/WLE), country, year, etc.
* @param startYear
* the first year contest was held, or the first year that we are interested
* in
* the first year contest was held, or the first year that we are interested in
* @param monumentQuery
* monuments fetcher
* @param imageQuery
Expand Down Expand Up @@ -117,31 +103,20 @@ class Statistics(
private val currentYear = contest.year

private val contests =
(startYear.getOrElse(currentYear) to currentYear).map(y =>
contest.copy(year = y)
)
(startYear.getOrElse(currentYear) to currentYear).map(y => contest.copy(year = y))

/** Fetches contest data
*
* @param total
* whether to fetch image database that holds images of all monuments from
* the contest, regardless of when they where uploaded
* whether to fetch image database that holds images of all monuments from the contest,
* regardless of when they where uploaded
* @return
* asynchronously returned contest data
*/
def gatherData(total: Boolean): Future[ContestStat] = {

val (monumentDb, monumentDbOld) = (
Some(MonumentDB.getMonumentDb(contest, monumentQuery)),
contest.rateConfig.newObjectRating.map { _ =>
MonumentDB.getMonumentDb(
contest,
monumentQuery,
date =
Some(ZonedDateTime.of(2017, 4, 30, 23, 59, 0, 0, ZoneOffset.UTC))
)
}
)
val monumentDb =
Some(MonumentDB.getMonumentDb(contest, monumentQuery))

for (
byYear <- Future.sequence(contests.map(contestImages(monumentDb)));
Expand All @@ -162,19 +137,13 @@ class Statistics(
)
) yield {
val currentYearImages = byYear.find(_.contest.year == currentYear)

val mDbOld: Option[MonumentDB] = currentYearImages.flatMap(
getOldImagesMonumentDb(monumentDb, monumentDbOld, totalImages, _)
)

ContestStat(
contest,
startYear.getOrElse(contest.year),
monumentDb,
currentYearImages,
totalImages,
byYear,
mDbOld,
Some(config)
)
}
Expand Down Expand Up @@ -204,28 +173,6 @@ class Statistics(
Some(new ImageDB(contest, commons ++ wiki, monumentDb))
}

def getOldImagesMonumentDb(
monumentDb: Option[MonumentDB],
monumentDbOld: Option[MonumentDB],
totalImages: Option[ImageDB],
imageDB: ImageDB
): Option[MonumentDB] = {
for (
mDb <- monumentDb;
mdbOld <- monumentDbOld;
total <- totalImages.orElse(Some(new ImageDB(contest, Seq.empty)))
) yield {
val oldIds = mdbOld.monuments.filter(_.photo.isDefined).map(_.id).toSet ++
total.images
.filterNot(ti =>
imageDB.images.exists(i => i.pageId.exists(ti.pageId.contains))
)
.flatMap(_.monumentId)

new MonumentDB(contest, mDb.monuments.filter(m => oldIds.contains(m.id)))
}
}

def init(total: Boolean): Unit = {
gatherData(total = total)
.map { stat =>
Expand Down Expand Up @@ -275,8 +222,7 @@ object Statistics {
val stat = new Statistics(
contest,
startYear = Some(cfg.years.head),
monumentQuery =
MonumentQuery.create(contest, reportDifferentRegionIds = true),
monumentQuery = MonumentQuery.create(contest, reportDifferentRegionIds = true),
config = Some(cfg),
imageQuery = imageQuery,
imageQueryWiki = Some(imageQueryWiki)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,67 +95,6 @@ class StatisticsSpec(implicit ee: ExecutionEnv)
namePrefix: String,
startId: Int = 1
): Seq[Monument] =
(startId until startId + n).map(i =>
monument(s"$regionId-xxx-000$i", namePrefix + i)
)
(startId until startId + n).map(i => monument(s"$regionId-xxx-000$i", namePrefix + i))

"getOldImagesMonumentDb" should {
val bot = mock[MwBot]
val monumentQuery = mock[MonumentQuery]
val imageQuery = mock[ImageQuery]
val stat =
new Statistics(contest, None, monumentQuery, imageQuery, None, bot)

"be empty" in {
stat.getOldImagesMonumentDb(
None,
None,
None,
new ImageDB(contest, Seq.empty)
) === None

val mDb = new MonumentDB(contest, Seq.empty)
stat.getOldImagesMonumentDb(
Some(mDb),
None,
None,
new ImageDB(contest, Seq.empty)
) === None
}

"have images from old monument db" in {

val images1 = Seq(
Image(
"File:Img11y1f1.jpg",
monumentIds = List("01-xxx-0001"),
author = Some("FromCrimea")
)
)

val images2 = Seq(
Image(
"File:Img11y2f1.jpg",
monumentIds = List("01-xxx-0002"),
author = Some("FromCrimeaOld")
)
)

val withoutPhotos = monuments(3, "01", "Crimea")
val withPhotos = withoutPhotos.head.copy(photo =
Some(images1.head.title)
) +: withoutPhotos.tail
val mDb = new MonumentDB(contest, withPhotos)

val oldMdb = stat.getOldImagesMonumentDb(
Some(mDb),
Some(mDb),
None,
new ImageDB(contest, images2)
)

oldMdb.map(_.monuments) === Some(Seq(withPhotos.head))
}

}
}

0 comments on commit e49ec48

Please sign in to comment.