Skip to content

Commit

Permalink
lastMonth stat
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Oct 20, 2023
1 parent 0ef3410 commit b315784
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MessageBot(val conf: Config) extends ActionLibrary with QueryLibrary {
*/
val talkPageMessage = conf.as[Message]("talk-page")

implicit lazy val bot = MwBot.fromHost(host)
implicit lazy val bot: MwBot = MwBot.fromHost(host)

def run() = {
for (users <- fetchUsers(userListPage))
Expand Down
106 changes: 80 additions & 26 deletions scalawiki-bots/src/main/scala/org/scalawiki/bots/np/TTNReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,103 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook

import java.io.FileInputStream

case class Person(contact: String, ttns: Seq[TTN])

object Person {
def receivers(ttns: Seq[TTN]) = {
ttns.groupBy(_.receiverContact).map {
case (contact, personReceived) => Person(contact, personReceived)
}

object TTNReader {
def main(args: Array[String]): Unit = {
val dir = new File("c:\\wmua\\np")
val files = dir.listFiles().filter(_.getName.endsWith(".xlsx"))
// val file = new File(
// "c:\\wmua\\np\\Специфікація до акту №НП-009788712 від 10.08.2023_14082023183857_3.xlsx")
}
}

val ttns: Seq[TTN] = files.flatMap(readTtns)
val byMonth =
ttns.groupBy(_.month).view.mapValues(_.map(_.cost).sum).toSeq.sortBy(_._1)
val byYear = byMonth.groupMap {
case (month, ttns) => month.split("\\.").head
} { case (month, ttns) => ttns }
val byYearAvg = byYear.view.mapValues(ttns => ttns.sum / ttns.size).toSeq.sortBy(_._1)
case class TTNData(ttns: Seq[TTN]) {

val byMonth =
ttns.groupBy(_.month).view.mapValues(_.map(_.cost).sum).toSeq.sortBy(_._1)
val byYear = byMonth.groupMap {
case (month, ttns) => month.split("\\.").head
} { case (month, ttns) => ttns }

def variousStat(): Unit = {
val byYearAvg =
byYear.view.mapValues(ttns => ttns.sum / ttns.size).toSeq.sortBy(_._1)

val year2023 = ttns.filter(_.year == "2022")
val lastYear = year2023.groupBy(_.receiverContact).view.mapValues(x => x.size).map{
case (contact, count) => (count, contact)
}.groupBy(_._1).view.mapValues{x =>
x.toSeq.map(_._2).distinct.sorted
}.toSeq.sortBy(_._1)
val lastYear: Seq[(Int, Seq[String])] = year2023
.groupBy(_.receiverContact)
.view
.mapValues(x => x.size)
.map {
case (contact, count) => (count, contact)
}
.groupBy(_._1)
.view
.mapValues { x =>
x.toSeq.map(_._2).distinct.sorted
}
.toSeq
.sortBy(_._1)

println(byMonth)
println(byYearAvg)

lastYear.filter(_._1 >= 5 )foreach{case (count, people) => println(s"$count: $people")}
lastYear.filter(_._1 >= 5) foreach {
case (count, people) => println(s"$count: $people")
}

// ttns.filter(x => x.receiverContact.contains("Мамон") && x.year == "2023").sortBy(_.yyMmDd).foreach { t =>
// println(s"${t.date}, ${t.description}, ${t.mass}, ${t.cost}")
// }
// ttns.filter(x => x.receiverContact.contains("Мамон") && x.year == "2023").sortBy(_.yyMmDd).foreach { t =>
// println(s"${t.date}, ${t.description}, ${t.mass}, ${t.cost}")
// }

val distinct = year2023.map(_.receiverContact).distinct.size
val all = year2023.size

println(s"All: $all, distinct: $distinct")
}

}

object TTNReader {
def main(args: Array[String]): Unit = {
val wlmNumbers = WlmContacts.getNumbers

val dir = new File("c:\\wmua\\np")
val ttns2023 = TTNData(readDir(dir)).ttns.filter(_.year == "2023")
val ttnsLastMonth = ttns2023.filter { ttn =>
!ttn.receiverContact.contains("Корбут") &&
ttn.month == "2023.08" // || ttn.month == "2023.09"
}
println("Ttns: " + ttnsLastMonth.size)

val wlmTtns = ttnsLastMonth.filter { ttn =>
wlmNumbers.exists(n => ttn.receiverContact.contains(n))
}
val wlmTtnsWithIndex = wlmTtns.sortBy(_.yyMmDd).zipWithIndex.map(_.swap)
println("wlm ttns: " + wlmTtnsWithIndex.size)
// wlmTtnsWithIndex.foreach(println)
val wlmTtnsNumbers = wlmTtns.map(_.ttn).toSet

val nonWlmTtns = ttnsLastMonth
.filterNot(ttn => wlmTtnsNumbers.contains(ttn.ttn))
.sortBy(_.yyMmDd)
.zipWithIndex
.map(_.swap)

println("nonWlmTtns: " + nonWlmTtns.size)
// nonWlmTtns.foreach(println)
// val receivers = ttnsLastMonth.map(_.receiverContact).distinct.sorted
// println("receivers: " + receivers.size)
// receivers.foreach(println)
}

private def readDir(dir: File): Seq[TTN] = {
val files = dir.listFiles().filter(_.getName.endsWith(".xlsx"))
files.flatMap(readFile)
}

private def readTtns(file: File): Seq[TTN] = {
private def readFile(file: File): Seq[TTN] = {
val fis = new FileInputStream(file)
val workbook = new XSSFWorkbook(fis)
val sheet = workbook.getSheetAt(0)
Expand All @@ -58,7 +114,5 @@ object TTNReader {
}
}

def readWlmPhoneNumbers() = {

}
def readWlmPhoneNumbers() = {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.scalawiki.bots.np

import scala.io.Source
import scala.util.matching.Regex

object WlmContacts {

private val Number = """[ +][\d\-()\s]{10,}""".r
private val Lengths = Set(10, 12)

def main(args: Array[String]): Unit = {
println(getNumbers.size)
getNumbers.foreach(println)
}

def getLines: Seq[String] = {
val source = Source.fromFile("c:\\wmua\\np\\progress.txt")
source.getLines().toSeq
}

def getNumbers: Seq[String] = {
getLines.flatMap(getNumber)
}

def getNumber(line: String): Seq[String] = {
Number
.findAllIn(line)
.toSeq
.map(_.filter(_.isDigit))
.collect {
case n if n.length == 10 => "38" + n
case n if n.length == 12 => n
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.scalawiki.bots.np

import org.specs2.mutable.Specification

class WlmContactsSpec extends Specification{

"WlmContacts" should {
"pick 10 digits" in {
WlmContacts.getNumbers.contains("0932563441") === true
}

"pick 12 digits" in {
WlmContacts.getNumbers.contains("380675488100") === true
}

"pick dashed/spaced/braced digits" in {
WlmContacts.getNumbers.contains("063-370-49-59") === true
WlmContacts.getNumbers.contains("097 535-70-10") === true
WlmContacts.getNumbers.contains("095 664 17 25") === true
WlmContacts.getNumbers.contains("095-0415177") === true
WlmContacts.getNumbers.contains("(097)9398937") === true
WlmContacts.getNumbers.contains("380 (63) 788 27 00") === true
WlmContacts.getNumbers.contains("38 050 801 52 49") === true


}
}
}

0 comments on commit b315784

Please sign in to comment.