<commit_before>package models

import (
	"log"
	"time"

	"github.com/brnstz/bus/internal/etc"
)

const (
	// midnightSecs is what a depature_sec value of midnight looks like. We
	// use this for trying to find departures after midnight.
	midnightSecs = 86400

	// maximum departures to return in a result
	MaxDepartures = 5
)

type Departure struct {
	Time         time.Time `json:"time" db:"-"`
	DepartureSec int       `json:"-" db:"departure_sec"`
	TripID       string    `json:"trip_id" db:"trip_id"`
	ServiceID    string    `json:"service_id" db:"service_id"`
	Live         bool      `json:"live" db:"-" upsert:"omit"`

	// CompassDir is the direction to the next stop
	CompassDir float64 `json:"compass_dir" db:"-" upsert:"omit"`

	baseTime time.Time `json:"-" db:"-"`
}

func (d *Departure) Initialize() error {
	d.Time = d.baseTime.Add(time.Second * time.Duration(d.DepartureSec))

	return nil
}

type SortableDepartures []*Departure

func (d SortableDepartures) Len() int {
	return len(d)
}

func (d SortableDepartures) Less(i, j int) bool {
	return d[i].Time.Before(d[j].Time)
}

func (d SortableDepartures) Swap(i, j int) {
	d[i], d[j] = d[j], d[i]
}

func getDepartures(agencyID, routeID, stopID, serviceID string, minSec int, base time.Time) (d []*Departure, err error) {
	q := `
		SELECT departure_sec, trip_id, service_id
		FROM   scheduled_stop_time
		WHERE  
			   agency_id  =	$1 AND
			   route_id   =	$2 AND
			   stop_id    =	$3 AND
			   service_id =	$4 AND

			   departure_sec    >= $5

		ORDER BY departure_sec LIMIT $6
	`

	err = etc.DBConn.Select(&d, q, agencyID, routeID, stopID, serviceID,
		minSec, MaxDepartures)

	if err != nil {
		log.Println("can't get departures", err)
		return
	}

	for _, departure := range d {
		departure.baseTime = base
		err = departure.Initialize()
		if err != nil {
			log.Println("can't init departure", err)
			return
		}
	}

	return
}<commit_msg>remove old getDepartures<commit_after>package models

import "time"

const (
	// midnightSecs is what a depature_sec value of midnight looks like. We
	// use this for trying to find departures after midnight.
	midnightSecs = 86400

	// maximum departures to return in a result
	MaxDepartures = 5
)

type Departure struct {
	Time         time.Time `json:"time" db:"-"`
	DepartureSec int       `json:"-" db:"departure_sec"`
	TripID       string    `json:"trip_id" db:"trip_id"`
	ServiceID    string    `json:"service_id" db:"service_id"`
	Live         bool      `json:"live" db:"-" upsert:"omit"`

	// CompassDir is the direction to the next stop
	CompassDir float64 `json:"compass_dir" db:"-" upsert:"omit"`

	baseTime time.Time `json:"-" db:"-"`
}

func (d *Departure) Initialize() error {
	d.Time = d.baseTime.Add(time.Second * time.Duration(d.DepartureSec))

	return nil
}

type SortableDepartures []*Departure

func (d SortableDepartures) Len() int {
	return len(d)
}

func (d SortableDepartures) Less(i, j int) bool {
	return d[i].Time.Before(d[j].Time)
}

func (d SortableDepartures) Swap(i, j int) {
	d[i], d[j] = d[j], d[i]
}
{'fraction_non_alphanumeric': 0.1319078947368421, 'fraction_numerical': 0.007236842105263158, 'mean_word_length': 3.4918759231905465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2333899', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1115, 'n_words': 311}