-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from gaurav/clean-up-code
Reorganized code so it's easier to read.
- Loading branch information
Showing
6 changed files
with
275 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
version = 3.5.8 | ||
runner.dialect = scala213 | ||
|
||
# Don't keep the params lined up with each other. | ||
align.openParenDefnSite = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.renci.babel.validator | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import org.renci.babel.validator.model.Compendium | ||
import zio.blocking.Blocking | ||
import zio.{Chunk, ZIO} | ||
|
||
/** Methods in this class can be used to compare results between two compendia. | ||
*/ | ||
object Comparer extends LazyLogging { | ||
|
||
/** Helper method for displaying the percent change between two counts. | ||
*/ | ||
def relativePercentChange(count: Long, countPrev: Long): String = { | ||
val percentChange = (count - countPrev).toDouble / countPrev * 100 | ||
f"${count - countPrev}%+d\t$percentChange%+2.2f%%" | ||
} | ||
|
||
case class LengthComparison(filename: String, count: Long, prevCount: Long) { | ||
val relativePercentChange: String = | ||
Comparer.relativePercentChange(count, prevCount) | ||
override val toString: String = | ||
s"${filename}\t${count}\t${prevCount}\t${relativePercentChange}" | ||
} | ||
|
||
def compareLengths( | ||
filename: String, | ||
summary: Compendium, | ||
prevSummary: Compendium | ||
): ZIO[Blocking, Throwable, LengthComparison] = { | ||
for { | ||
count <- summary.count | ||
prevCount <- prevSummary.count | ||
} yield LengthComparison(filename, count, prevCount) | ||
} | ||
|
||
case class TypeComparison( | ||
filename: String, | ||
types: Chunk[String], | ||
prevTypes: Chunk[String] | ||
) { | ||
val typesSet = types.toSet | ||
val prevTypesSet = types.toSet | ||
val added: Set[String] = typesSet -- prevTypesSet | ||
val deleted: Set[String] = prevTypesSet -- typesSet | ||
val changeString: String = (added.toSeq, deleted.toSeq) match { | ||
case (Seq(), Seq()) => "No change" | ||
case (added, Seq()) => s"Added: ${added}" | ||
case (Seq(), _) => s"Deleted: ${added}" | ||
case (added, deleted) => | ||
s"Added: ${added}, Deleted: ${deleted}" | ||
} | ||
|
||
override val toString: String = | ||
s"${filename}\t${typesSet.mkString(", ")} (${types.length})\t${prevTypesSet | ||
.mkString(", ")} (${prevTypes.length})\t${changeString}" | ||
} | ||
|
||
def compareTypes( | ||
filename: String, | ||
summary: Compendium, | ||
prevSummary: Compendium | ||
): ZIO[Blocking, Throwable, TypeComparison] = { | ||
for { | ||
typesChunk <- (for { | ||
row: Compendium.Record <- summary.records | ||
} yield (row.`type`)).runCollect | ||
typesErrors <- summary.types.catchAll(err => { | ||
logger.error(s"Types error: ${err}") | ||
ZIO.fail(err) | ||
}) | ||
prevTypesChunk <- (for { | ||
row: Compendium.Record <- prevSummary.records | ||
} yield (row.`type`)).runCollect | ||
prevTypesErrors <- prevSummary.types.catchAll(err => { | ||
logger.error(s"prevTypes error: ${err}") | ||
ZIO.fail(err) | ||
}) | ||
} yield { | ||
TypeComparison(filename, typesChunk, prevTypesChunk) | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/main/scala/org/renci/babel/validator/Reporter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.renci.babel.validator | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import org.renci.babel.validator.Validator.Conf | ||
import org.renci.babel.validator.model.{BabelOutput, Compendium} | ||
import zio.ZIO | ||
import zio.blocking.Blocking | ||
import zio.console.Console | ||
import zio.stream.ZStream | ||
|
||
import java.io.{FileOutputStream, PrintStream} | ||
|
||
/** Functions for reporting on the differences between two input files. | ||
*/ | ||
object Reporter extends LazyLogging { | ||
|
||
/** Helper method for displaying the percent change between two counts. | ||
*/ | ||
def relativePercentChange(count: Long, countPrev: Long): String = { | ||
val percentChange = (count - countPrev).toDouble / countPrev * 100 | ||
f"${count - countPrev}%+d\t$percentChange%+2.2f%%" | ||
} | ||
|
||
/** Generic method to determine whether a particular filename should be | ||
* filtered in or out from the results. The algorithm we use is: | ||
* 1. If any `--filtered-in` prefixes are provided, then we exclude | ||
* everything that isn't explicitly filtered in (by starting with one of | ||
* those prefixes in a case-sensitive manner). 2. Otherwise, all | ||
* filenames are allowed EXCEPT those explicitly filtered out by | ||
* `--filtered-out` by starting with one of those prefixes in a | ||
* case-sensitive manner. | ||
*/ | ||
def filterFilename(conf: Conf, filename: String): Boolean = { | ||
val filteredIn = conf.filterIn.getOrElse(List()) | ||
val filteredOut = conf.filterOut.getOrElse(List()) | ||
|
||
if (filteredIn.nonEmpty) { | ||
if (filteredIn.exists(filename.startsWith(_))) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
if (filteredOut.nonEmpty && filteredOut.exists(filename.startsWith(_))) { | ||
return false; | ||
} | ||
|
||
true | ||
} | ||
|
||
/** Given two BabelOutputs, it returns a list of all compendia found in BOTH | ||
* of the BabelOutputs paired together. | ||
* | ||
* TODO: modify this so we return every compendium found in EITHER | ||
* BabelOutput. | ||
*/ | ||
def retrievePairedCompendiaSummaries( | ||
babelOutput: BabelOutput, | ||
babelPrevOutput: BabelOutput | ||
): Seq[(String, Compendium, Compendium)] = { | ||
for { | ||
summary <- babelOutput.compendia | ||
summaryPrev <- babelPrevOutput.compendia | ||
if summaryPrev.filename == summary.filename | ||
} yield { | ||
(summary.filename, summary, summaryPrev) | ||
} | ||
} | ||
|
||
def diffResults(conf: Conf): ZIO[Blocking with Console, Throwable, Unit] = { | ||
val babelOutput = new BabelOutput(conf.babelOutput()) | ||
val babelPrevOutput = new BabelOutput(conf.babelPrevOutput()) | ||
val output = conf.output.toOption match { | ||
case Some(file) => new PrintStream(new FileOutputStream(file)) | ||
case _ => System.out | ||
} | ||
|
||
val pairedSummaries = | ||
retrievePairedCompendiaSummaries(babelOutput, babelPrevOutput) | ||
output.println("Filename\tCount\tPrevCount\tDiff\tPercentageChange") | ||
ZStream | ||
.fromIterable(pairedSummaries) | ||
.mapMParUnordered(conf.nCores()) { | ||
case ( | ||
filename: String, | ||
summary: Compendium, | ||
prevSummary: Compendium | ||
) if filterFilename(conf, filename) => { | ||
|
||
for { | ||
lengthComparison <- Comparer.compareLengths( | ||
filename, | ||
summary, | ||
prevSummary | ||
) | ||
typeComparison <- Comparer.compareTypes( | ||
filename, | ||
summary, | ||
prevSummary | ||
) | ||
} yield { | ||
output.println(lengthComparison.toString) | ||
output.println(typeComparison.toString) | ||
} | ||
} | ||
case (filename: String, _, _) if !filterFilename(conf, filename) => { | ||
logger.info(s"Skipping ${filename}") | ||
ZIO.succeed(()) | ||
} | ||
case abc => | ||
ZIO.fail(new RuntimeException(s"Invalid paired summary: ${abc}")) | ||
} | ||
.runDrain | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.