From b315784c3b7847974ac381f7f39b806007cccdb3 Mon Sep 17 00:00:00 2001 From: ilya Date: Fri, 20 Oct 2023 16:36:56 +0300 Subject: [PATCH] lastMonth stat --- .../scala/org/scalawiki/bots/MessageBot.scala | 2 +- .../org/scalawiki/bots/np/TTNReader.scala | 106 +++++++++++++----- .../org/scalawiki/bots/np/WlmContacts.scala | 36 ++++++ .../scalawiki/bots/np/WlmContactsSpec.scala | 28 +++++ 4 files changed, 145 insertions(+), 27 deletions(-) create mode 100644 scalawiki-bots/src/main/scala/org/scalawiki/bots/np/WlmContacts.scala create mode 100644 scalawiki-bots/src/test/scala/org/scalawiki/bots/np/WlmContactsSpec.scala diff --git a/scalawiki-bots/src/main/scala/org/scalawiki/bots/MessageBot.scala b/scalawiki-bots/src/main/scala/org/scalawiki/bots/MessageBot.scala index c5f3b58c..ab10c309 100644 --- a/scalawiki-bots/src/main/scala/org/scalawiki/bots/MessageBot.scala +++ b/scalawiki-bots/src/main/scala/org/scalawiki/bots/MessageBot.scala @@ -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)) diff --git a/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/TTNReader.scala b/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/TTNReader.scala index c1aefcee..43d21584 100644 --- a/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/TTNReader.scala +++ b/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/TTNReader.scala @@ -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) @@ -58,7 +114,5 @@ object TTNReader { } } - def readWlmPhoneNumbers() = { - - } + def readWlmPhoneNumbers() = {} } diff --git a/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/WlmContacts.scala b/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/WlmContacts.scala new file mode 100644 index 00000000..286a92a3 --- /dev/null +++ b/scalawiki-bots/src/main/scala/org/scalawiki/bots/np/WlmContacts.scala @@ -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 + } + } + +} diff --git a/scalawiki-bots/src/test/scala/org/scalawiki/bots/np/WlmContactsSpec.scala b/scalawiki-bots/src/test/scala/org/scalawiki/bots/np/WlmContactsSpec.scala new file mode 100644 index 00000000..1e39f4db --- /dev/null +++ b/scalawiki-bots/src/test/scala/org/scalawiki/bots/np/WlmContactsSpec.scala @@ -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 + + + } + } +}