<commit_before>package dao

import java.sql.Timestamp
import java.time.temporal.ChronoUnit._
import java.time.{Clock, Instant}

import _root_.play.api.Logger
import entities.bigclown.{AggregatedBcMeasure, BcMeasure}
import scalikejdbc._

/**
  * Dao for bc measures
  */
class BcMeasureDao(_clock: Clock) {
  implicit val clock = _clock
  def cleanDb():Unit = {
    DB.autoCommit(implicit session => {
      sql"""TRUNCATE TABLE bc_measure""".update().apply()
    })
  }

  def save(message: BcMeasure): Unit = {
    DB.autoCommit(implicit session => {
      sql"""
          INSERT INTO bc_measure (phenomenon, sensor, measure_timestamp, value, unit)
          VALUES (
              ${message.phenomenon},
              ${message.sensor},
              ${new java.sql.Timestamp(message.measureTimestamp.toEpochMilli)},
              ${message.value},
              ${message.unit}
          )
      """
        .update.apply()
    })
  }

  def getSampledMeasures(phenomenon: String, by: TimeGranularity = ByHour): Seq[AggregatedBcMeasure] = {
    DB.readOnly(implicit session => {
      val (extractTime, lastMeasureTimestamp) = TimeGranularity.toExtractAndTime(by)

      sql"""
           SELECT MAX(measure_timestamp) AS ts, AVG(value) AS avg, MIN(value) as min, MAX(value) as max, unit, sensor
           FROM bc_measure
           WHERE phenomenon = ${phenomenon}
           AND measure_timestamp > ${lastMeasureTimestamp}
           GROUP BY EXTRACT(${extractTime} FROM measure_timestamp), unit, sensor
           ORDER BY MAX(measure_timestamp)
        """
        .map(rs => AggregatedBcMeasure(
          sensor = rs.string("sensor"),
          phenomenon = phenomenon,
          measureTimestamp = Instant.ofEpochMilli(rs.timestamp("ts").millis),
          min = rs.double("min"),
          max = rs.double("max"),
          average = rs.double("avg"),
          unit = rs.string("unit")<commit_msg>Round bc measures to 2 decimal places<commit_after>package dao

import java.sql.Timestamp
import java.time.temporal.ChronoUnit._
import java.time.{Clock, Instant}

import _root_.play.api.Logger
import entities.bigclown.{AggregatedBcMeasure, BcMeasure}
import scalikejdbc._

/**
  * Dao for bc measures
  */
class BcMeasureDao(_clock: Clock) {
  implicit val clock = _clock
  def cleanDb():Unit = {
    DB.autoCommit(implicit session => {
      sql"""TRUNCATE TABLE bc_measure""".update().apply()
    })
  }

  def save(message: BcMeasure): Unit = {
    DB.autoCommit(implicit session => {
      sql"""
          INSERT INTO bc_measure (phenomenon, sensor, measure_timestamp, value, unit)
          VALUES (
              ${message.phenomenon},
              ${message.sensor},
              ${new java.sql.Timestamp(message.measureTimestamp.toEpochMilli)},
              ${message.value},
              ${message.unit}
          )
      """
        .update.apply()
    })
  }

  def getSampledMeasures(phenomenon: String, by: TimeGranularity = ByHour): Seq[AggregatedBcMeasure] = {
    DB.readOnly(implicit session => {
      val (extractTime, lastMeasureTimestamp) = TimeGranularity.toExtractAndTime(by)

      sql"""
           SELECT MAX(measure_timestamp) AS ts, ROUND(AVG(value), 2) AS avg, ROUND(MIN(value), 2) as min, ROUND(MAX(value), 2) as max, unit, sensor
           FROM bc_measure
           WHERE phenomenon = ${phenomenon}
           AND measure_timestamp > ${lastMeasureTimestamp}
           GROUP BY EXTRACT(${extractTime} FROM measure_timestamp), unit, sensor
           ORDER BY MAX(measure_timestamp)
        """
        .map(rs => AggregatedBcMeasure(
          sensor = rs.string("sensor"),
          phenomenon = phenomenon,
          measureTimestamp = Instant.ofEpochMilli(rs.timestamp("ts").millis),
          min = rs.double("min"),
          max = rs.double("max"),
          average = rs.double("avg"),
          unit = rs.string("unit")
{'fraction_non_alphanumeric': 0.12145435791645177, 'fraction_numerical': 0.0010314595152140279, 'mean_word_length': 2.6320224719101124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4861374', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1147, 'n_words': 248}