<commit_before>package at.droelf.backend.storage.database

import java.awt.Color
import java.util.UUID

import at.droelf.backend.DateTimeUtil
import models._
import org.joda.time._
import parser.gpxtype.{GPXWayPoint, GPXTrack}

import scala.slick.driver.JdbcProfile

class DBStorageService(val profile: JdbcProfile = SlickDBDriver.getDriver) extends DateTimeUtil{

  val db = new DBConnection(profile).dbObject()

  def saveTracks(tracks: List[GPXTrack], dateTimeZone: DateTimeZone, activity: String) = {

    def isOverViewPt(index: Int, gap: Int, numOfPt: Int): Boolean = {
      (index % gap == 0 || index == 0 || index == numOfPt-1)
    }

    def getLocalDates(track: GPXTrack): Seq[LocalDate] = {
      track.trackSegments.map( trkSeg => trkSeg.trackPoints.map( wpt => dateTimeToUtcLocalDateTime(DateTime.parse(wpt.time.get)).toLocalDate)).flatten.distinct
    }

    db.withTransaction{ implicit session =>

      tracks.foreach { track =>
        val id = getRandomId
        Tracks.insertTrack(Track(id, track.name, activity))
        getLocalDates(track).foreach(date => TrackToLocalDates.insertTrackToLocalDate(TrackToLocalDate(id,date)))

        TrackMetaDatas.insertTrackMetaData(gpxTrackToTrackMetaData(id, track))

        track.trackSegments.foreach{ trkSeg =>
          trkSeg.trackPoints.zipWithIndex.foreach{ e =>
            val wpt = e._1
            val time = dateTimeToUtcLocalDateTime(DateTime.parse(wpt.time.get))
            TrackPoints.insertIfNotExists(TrackPoint(id, wpt.latitude, wpt.longitude, wpt.ele.getOrElse(0), time, dateTimeZone, isOverViewPt(e._2,100,trkSeg.trackPoints.length)))
          }
        }
      }
    }
  }

  def getAllTracks() = db.withTransaction{implicit session => Tracks.getAllTracks}

  def getTrackById(trackId: UUID): Option[Track] = db.withTransaction{implicit session => Tracks.getTrackById(trackId)}

  def getTrackByDate(date: LocalDate): Seq[Track] = db.withTransaction{implicit session =>
    TrackToLocalDates.getTrackToLocalDateByDate(date).map(e => Tracks.getTracksById(e.trackId)).flatten
  }<commit_msg>fingers crossed<commit_after>package at.droelf.backend.storage.database

import java.awt.Color
import java.util.UUID

import at.droelf.backend.DateTimeUtil
import models._
import org.joda.time._
import parser.gpxtype.{GPXWayPoint, GPXTrack}

import scala.slick.driver.JdbcProfile

class DBStorageService(val profile: JdbcProfile = SlickDBDriver.getDriver) extends DateTimeUtil{

  val db = new DBConnection(profile).dbObject()

  def saveTracks(tracks: List[GPXTrack], dateTimeZone: DateTimeZone, activity: String) = {

    def isOverViewPt(index: Int, gap: Int, numOfPt: Int): Boolean = {
      (index % gap == 0 || index == 0 || index == numOfPt-1)
    }

    def getLocalDates(track: GPXTrack, timezone: DateTimeZone): Seq[LocalDate] = {
      track.trackSegments.map( trkSeg => trkSeg.trackPoints.map( wpt => utcWithTimeZoneToDateTime(dateTimeToUtcLocalDateTime(DateTime.parse(wpt.time.get)),timezone).toLocalDate)).flatten.distinct
    }

    db.withTransaction{ implicit session =>

      tracks.foreach { track =>
        val id = getRandomId
        Tracks.insertTrack(Track(id, track.name, activity))
        getLocalDates(track, dateTimeZone).foreach(date => TrackToLocalDates.insertTrackToLocalDate(TrackToLocalDate(id,date)))

        TrackMetaDatas.insertTrackMetaData(gpxTrackToTrackMetaData(id, track))

        track.trackSegments.foreach{ trkSeg =>
          trkSeg.trackPoints.zipWithIndex.foreach{ e =>
            val wpt = e._1
            val time = dateTimeToUtcLocalDateTime(DateTime.parse(wpt.time.get))
            TrackPoints.insertIfNotExists(TrackPoint(id, wpt.latitude, wpt.longitude, wpt.ele.getOrElse(0), time, dateTimeZone, isOverViewPt(e._2,100,trkSeg.trackPoints.length)))
          }
        }
      }
    }
  }

  def getAllTracks() = db.withTransaction{implicit session => Tracks.getAllTracks}

  def getTrackById(trackId: UUID): Option[Track] = db.withTransaction{implicit session => Tracks.getTrackById(trackId)}

  def getTrackByDate(date: LocalDate): Seq[Track] = db.withTransaction{implicit session =>
    TrackToLocalDates.getTrackToLocalDateByDate(date).map(e => Tracks.getTracksById(e.trackId)).flatten
  }
{'fraction_non_alphanumeric': 0.11494796594134342, 'fraction_numerical': 0.004257332071901609, 'mean_word_length': 4.227441285537701, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '6612956', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1352, 'n_words': 226}