• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang dbconn.AcquireLock函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/karl-chan/dbconn.AcquireLock函数的典型用法代码示例。如果您正苦于以下问题:Golang AcquireLock函数的具体用法?Golang AcquireLock怎么用?Golang AcquireLock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了AcquireLock函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: SelectNextBusesByVehicleStopPairs

/* Returns the expected arrival times given vehicleId and naptanId pairs */
func SelectNextBusesByVehicleStopPairs(vehiclesStopsMap map[string]string) []NextBus {
	var nextBuses []NextBus

	innerQueries := make([]string, 0)

	for vehicleId, naptanId := range vehiclesStopsMap {
		innerQueries = append(innerQueries,
			fmt.Sprintf("SELECT line, bound, naptan_id, vehicle_id, expected_arrival_time, already_departed "+
				"FROM next_buses "+
				"WHERE vehicle_id='%v' AND naptan_id='%v'",
				vehicleId, naptanId))
	}

	dbconn.AcquireLock()
	err := db.Select(&nextBuses, strings.Join(innerQueries, " UNION ALL "))
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	/* Set time zone to UTC */
	for i, v := range nextBuses {
		nextBuses[i].ExpectedArrivalTime = v.ExpectedArrivalTime.In(timezone.Get())
	}
	return nextBuses
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:28,代码来源:selectnextbus.go


示例2: SelectTerminuses

/* Returns the naptan ids of the terminuses of given line bound combinations,
   as two separate maps */
func SelectTerminuses(lineBoundCombinations map[string]map[string]bool) map[string]map[string]string {
	var stopPoints []StopPoint

	innerQueries := make([]string, 0)
	for line, details := range lineBoundCombinations {
		for bound, _ := range details {
			innerQueries = append(innerQueries,
				fmt.Sprintf(
					"(SELECT line_id, bound, naptan_id FROM line_stop_points WHERE line_id='%v' AND bound='%v' ORDER BY stop_seq DESC LIMIT 1)",
					line, bound))
		}
	}

	dbconn.AcquireLock()
	err := db.Select(&stopPoints, strings.Join(innerQueries, " UNION ALL "))
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	result := make(map[string]map[string]string)
	for _, stopPoint := range stopPoints {
		_, exists := result[stopPoint.LineId]
		if !exists {
			result[stopPoint.LineId] = make(map[string]string)
		}
		result[stopPoint.LineId][stopPoint.Bound] = stopPoint.NaptanId
	}
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:33,代码来源:selectlinestoppoints.go


示例3: SelectIntermediateStops

/* Returns intermediate stops between (end points inclusive) fromId and toId of line and direction*/
func SelectIntermediateStops(line, bound, fromId, toId string) []string {
	var stopPoints []StopPoint

	dbconn.AcquireLock()
	err := db.Select(&stopPoints,
		"SELECT naptan_id FROM line_stop_points "+
			"WHERE line_id=$1 AND bound=$2 AND stop_seq >="+
			"(SELECT stop_seq FROM line_stop_points WHERE "+
			"line_id=$1 AND bound=$2 AND naptan_id=$3 "+
			"ORDER BY stop_seq LIMIT 1) "+
			"AND stop_seq <= "+
			"(SELECT stop_seq FROM line_stop_points WHERE "+
			"line_id=$1 AND bound=$2 AND naptan_id=$4 "+
			"ORDER BY stop_seq DESC LIMIT 1) "+
			"ORDER BY stop_seq ASC",
		line, bound, fromId, toId)
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	if len(stopPoints) <= 1 {
		logger.GetLogger().Panicf("No intermediate stops between %v and %v for line %v (%v)",
			fromId, toId, line, bound)
	}

	result := make([]string, len(stopPoints))
	for i, v := range stopPoints {
		result[i] = v.NaptanId
	}
	logger.GetLogger().Info("Get intermediate stops (inclusive) from line_stop_points returned: %v", result)
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:35,代码来源:selectlinestoppoints.go


示例4: SelectNextBuses

/* Returns the next buses that are approaching a stop given a line and bound after a certain time */
func SelectNextBuses(lineBoundCombination map[string]string, naptanId string, arrivingAfter time.Time) []NextBus {
	var nextBuses []NextBus

	innerQueries := make([]string, 0)

	for line, bound := range lineBoundCombination {
		innerQueries = append(innerQueries,
			fmt.Sprintf("SELECT line, bound, naptan_id, vehicle_id, expected_arrival_time, already_departed "+
				"FROM next_buses "+
				"WHERE naptan_id='%v' AND line='%v' AND bound='%v' AND expected_arrival_time>=TIMESTAMP '%v'",
				naptanId, line, bound, toPsqlTimeStamp(arrivingAfter)))
	}

	sql := fmt.Sprintf("SELECT line, bound, naptan_id, vehicle_id, expected_arrival_time, already_departed "+
		"FROM (%v) AS all_next_buses ORDER BY expected_arrival_time",
		strings.Join(innerQueries, " UNION ALL "))

	logger.GetLogger().Info(sql)
	dbconn.AcquireLock()
	err := db.Select(&nextBuses, sql)
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	/* Set time zone to UTC */
	for i, v := range nextBuses {
		nextBuses[i].ExpectedArrivalTime = v.ExpectedArrivalTime.In(timezone.Get())
	}
	return nextBuses
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:33,代码来源:selectnextbus.go


示例5: SelectIntermediateStopsWithIcsCode

/* Returns intermediate stops between (end points inclusive) fromIcs and toIcs of line and direction
   NOTE requires the use of table "stops" */
func SelectIntermediateStopsWithIcsCode(line, bound, fromIcs, toIcs string) ([]string, error) {
	var stopPoints []StopPoint

	dbconn.AcquireLock()
	err := db.Select(&stopPoints,
		"SELECT naptan_id FROM line_stop_points "+
			"WHERE line_id=$1 AND bound=$2 AND stop_seq >="+
			"(SELECT stop_seq FROM line_stop_points JOIN stops ON line_stop_points.naptan_id=stops.id "+
			"WHERE line_id=$1 AND bound=$2 AND icscode=$3 "+
			"ORDER BY stop_seq LIMIT 1) "+
			"AND stop_seq <= "+
			"(SELECT stop_seq FROM line_stop_points JOIN stops ON line_stop_points.naptan_id=stops.id "+
			"WHERE line_id=$1 AND bound=$2 AND icscode=$4 "+
			"ORDER BY stop_seq DESC LIMIT 1) "+
			"ORDER BY stop_seq ASC",
		line, bound, fromIcs, toIcs)
	dbconn.ReleaseLock()

	if err != nil {
		return nil, err
	}

	if len(stopPoints) <= 1 {
		return nil, errors.New(
			fmt.Sprintf("No intermediate stops between %v and %v for line %v (%v)",
				fromIcs, toIcs, line, bound))
	}

	result := make([]string, len(stopPoints))
	for i, v := range stopPoints {
		result[i] = v.NaptanId
	}
	logger.GetLogger().Info("Get intermediate stops (inclusive) with icscode from line_stop_points returned: %v", result)
	return result, nil
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:37,代码来源:selectlinestoppoints.go


示例6: insertRouteDurationsIntoDB

func insertRouteDurationsIntoDB(routeDurations map[string]map[string]map[string]time.Duration,
	tflEstimates map[string]map[string]time.Duration) {
	if len(routeDurations) == 0 {
		logger.GetLogger().Error("Lack of route durations. Cycle skipped...")
		return
	}

	values := make([]string, 0)
	for line, lineDetails := range routeDurations {
		for bound, boundDetails := range lineDetails {
			duration := boundDetails["duration"]
			historicDuration := boundDetails["historicDuration"]
			realTimeDuration := boundDetails["realTimeDuration"]
			tflDuration := tflEstimates[line][bound]

			values = append(values, fmt.Sprintf("('%v', '%v', INTERVAL '%v seconds', INTERVAL '%v seconds', INTERVAL '%v seconds', INTERVAL '%v seconds')",
				line, bound, duration.Seconds(), historicDuration.Seconds(), realTimeDuration.Seconds(), tflDuration.Seconds()))
		}
	}

	valuesCommaSep := strings.Join(values, ",")

	dbconn.AcquireLock()
	db.MustExec("DELETE FROM delayed_routes")
	db.MustExec(
		fmt.Sprintf("INSERT INTO delayed_routes (line, bound, estimate, historic_estimate, real_time_estimate, tfl_estimate) "+
			"VALUES %v", valuesCommaSep))
	dbconn.ReleaseLock()
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:29,代码来源:delayedroutesestimates.go


示例7: SelectDelays

/* Returns a map of current delays
   fromStop => (toStop =>
   		("historicEstimate" => seconds (int64)
   		 "realTimeEstimate" => seconds (int64)
   		 "lastUpdated" => millis in Unix Time (int64))) */
func SelectDelays() map[string]map[string]map[string]int64 {
	var delays []CurrentEstimate

	dbconn.AcquireLock()
	err := db.Select(&delays,
		"SELECT from_id, to_id, last_updated, delay_count, "+
			"EXTRACT (epoch FROM historic_estimate) AS historic_estimate, "+
			"EXTRACT (epoch FROM real_time_estimate) AS real_time_estimate "+
			"FROM current_estimates "+
			"WHERE delay_count >= 3 "+
			"AND last_updated=(SELECT MAX(last_updated) FROM current_estimates)")
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	results := make(map[string]map[string]map[string]int64)
	for _, record := range delays {
		fromStopDetails, exists := results[record.FromStop]
		if !exists {
			fromStopDetails = make(map[string]map[string]int64)
		}
		fromStopDetails[record.ToStop] = map[string]int64{
			"historicEstimate": record.HistoricEstimate,
			"realTimeEstimate": record.RealTimeEstimate,
			"delayCount":       record.DelayCount,
			"lastUpdated":      record.LastUpdated.Unix(),
		}
		results[record.FromStop] = fromStopDetails
	}
	return results
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:38,代码来源:selectcurrentestimates.go


示例8: SelectStopSeq

/* Returns the stop sequence number, given the line, direction and naptanId of the stop
   Should there be ties, an error is returned */
func SelectStopSeq(line, bound, naptanId string) (int, error) {
	var stopPoints []StopPoint

	dbconn.AcquireLock()
	err := db.Select(&stopPoints,
		"SELECT stop_seq FROM line_stop_points WHERE line_id=$1 AND bound=$2 AND naptan_id=$3",
		line, bound, naptanId)
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	if len(stopPoints) == 0 {
		logger.GetLogger().Error("Failed to find stop sequence number for stop %v of line %v (%v)",
			naptanId, line, bound)
		return -1, errors.New(
			fmt.Sprintf("Failed to find stop sequence number for stop %v of line %v (%v)",
				naptanId, line, bound))
	}

	if len(stopPoints) > 1 {
		logger.GetLogger().Error("Expected unique stop sequence number for stop %v of line %v (%v), but got: %v",
			naptanId, line, bound, stopPoints)
		return -1, errors.New(
			fmt.Sprintf("Expected unique stop sequence number for stop %v of line %v (%v), but got: %v",
				naptanId, line, bound, stopPoints))
	}

	result := stopPoints[0].StopSeq
	return result, nil
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:34,代码来源:selectlinestoppoints.go


示例9: createMedian

func createMedian() {
	createFinalMedianSql := "CREATE OR REPLACE FUNCTION _final_median(NUMERIC[]) " +
		"RETURNS NUMERIC AS " +
		"$$ " +
		"SELECT AVG(val) " +
		"FROM ( " +
		"SELECT val " +
		"FROM unnest($1) val " +
		"ORDER BY 1 " +
		"LIMIT  2 - MOD(array_upper($1, 1), 2) " +
		"OFFSET CEIL(array_upper($1, 1) / 2.0) - 1 " +
		") sub; " +
		"$$ " +
		"LANGUAGE 'sql' IMMUTABLE"
	createMedianSql := "CREATE AGGREGATE median(NUMERIC) ( " +
		"SFUNC=array_append, " +
		"STYPE=NUMERIC[], " +
		"FINALFUNC=_final_median, " +
		"INITCOND='{}' " +
		")"

	dbconn.AcquireLock()
	tx, err := db.Begin()
	tx.Exec(createFinalMedianSql)
	tx.Exec(createMedianSql)
	err = tx.Commit()
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Warning(err.Error())
		return
	}
	logger.GetLogger().Info("Function 'median' created successfully")
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:34,代码来源:createfunctions.go


示例10: SelectAllCurrentEstimates

/* Returns all current estimates of bus travel times
   CATCH: Not all stop pairs may be present, especially if estimate is not recently generated
          e.g. routes served by night buses in the day*/
func SelectAllCurrentEstimates() map[string]map[string]time.Duration {
	var currentEstimates []CurrentEstimate

	dbconn.AcquireLock()
	err := db.Select(&currentEstimates,
		"SELECT from_id, to_id, last_updated, "+
			"EXTRACT(epoch FROM estimate) AS estimate "+
			"FROM current_estimates "+
			"WHERE last_updated=(SELECT MAX(last_updated) FROM current_estimates)")
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	result := make(map[string]map[string]time.Duration)
	for _, currentEstimate := range currentEstimates {
		fromStop := currentEstimate.FromStop
		toStop := currentEstimate.ToStop
		estimate := time.Duration(currentEstimate.Estimate) * time.Second

		/* Depending on whether from stop exists or not, decide whether there
		   is need to create a new map in results */
		toStopMap, exists := result[fromStop]
		if !exists {
			toStopMap = make(map[string]time.Duration)
		}
		toStopMap[toStop] = estimate
		result[fromStop] = toStopMap
	}
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:35,代码来源:selectcurrentestimates.go


示例11: replaceTimetable

func replaceTimetable(line, bound, origin string, newTimetable map[time.Weekday]map[selectdb.LocalTime]bool) {
	values := make([]string, 0)
	for weekday, departureTimes := range newTimetable {
		for departureTime, _ := range departureTimes {
			values = append(values, fmt.Sprintf("('%v', '%v', '%v', %v, TIME '%02d:%02d:00')",
				line, bound, origin, int(weekday), departureTime.Hour, departureTime.Minute))
		}
	}

	dbconn.AcquireLock()
	tx := db.MustBegin()
	tx.MustExec(fmt.Sprintf("DELETE FROM timetable WHERE line_id='%v' AND bound='%v' AND naptan_id='%v'", line, bound, origin))

	logger.GetLogger().Info(fmt.Sprintf("INSERT INTO timetable (line_id, bound, naptan_id, weekday, departure_time) VALUES %s",
		strings.Join(values, ",")))
	tx.MustExec(
		fmt.Sprintf("INSERT INTO timetable (line_id, bound, naptan_id, weekday, departure_time) VALUES %s",
			strings.Join(values, ",")))
	err := tx.Commit()
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Error(err.Error())
	}
	logger.GetLogger().Info("Replaced timetable for line %v (%v), stop %v", line, bound, origin)
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:26,代码来源:updatetimetable.go


示例12: SelectAllLineStopPoints

/* Returns sequences of all naptanIds, grouped by line and bound
   map (line => (bound => []naptanIds)) */
func SelectAllLineStopPoints() map[string]map[string][]string {
	var stopPoints []StopPoint

	dbconn.AcquireLock()
	err := db.Select(&stopPoints,
		"SELECT line_id, bound, stop_seq, naptan_id "+
			"FROM line_stop_points "+
			"ORDER BY line_id, bound, stop_seq")
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	result := make(map[string]map[string][]string)
	for _, v := range stopPoints {
		lineDetails, exists := result[v.LineId]
		if !exists {
			lineDetails = make(map[string][]string)
		}

		orderedStopPoints, exists := lineDetails[v.Bound]
		if !exists {
			orderedStopPoints = make([]string, 0)
		}

		orderedStopPoints = append(orderedStopPoints, v.NaptanId)
		lineDetails[v.Bound] = orderedStopPoints
		result[v.LineId] = lineDetails
	}
	logger.GetLogger().Info("Get existing line stop points returned: %v", result)
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:35,代码来源:selectlinestoppoints.go


示例13: SelectTimetable

/* Returns timetable for specified line, bound and stop in the form of a map
   map (weekday => []("hour" => hour
                    "minute" => minute)) */
func SelectTimetable(line, bound, naptanId string) map[time.Weekday]map[LocalTime]bool {
	var timetable []TimetableEntry

	dbconn.AcquireLock()
	err := db.Select(&timetable,
		"SELECT weekday, departure_time FROM timetable WHERE line_id=$1 AND bound=$2 AND naptan_id=$3 ORDER BY weekday,departure_time",
		line, bound, naptanId)
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	result := make(map[time.Weekday]map[LocalTime]bool)
	for _, entry := range timetable {
		weekday := time.Weekday(entry.Weekday)
		weekdayMap, exists := result[weekday]
		if !exists {
			weekdayMap = make(map[LocalTime]bool, 0)
			result[weekday] = weekdayMap
		}
		weekdayMap[LocalTime{Hour: entry.DepartureTime.Hour(), Minute: entry.DepartureTime.Minute()}] = true
	}
	logger.GetLogger().Info("Get timetable for line %v (%v), stop %v returned: %v", line, bound, naptanId, result)
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:29,代码来源:selecttimetable.go


示例14: SelectAvailableRoutesTflPredictions

/* Returns all available routes stored in tfl_predictions_history
   []map ("line" => line,
   		  "bound" => bound,
   		  "fromStop" => fromStop,
   		  "toStop" => toStop)
*/
func SelectAvailableRoutesTflPredictions() []map[string]string {
	type AvailableRoute struct {
		Line     string `db:"line"`
		Bound    string `db:"bound"`
		FromStop string `db:"from_id"`
		ToStop   string `db:"to_id"`
	}

	var availableRoutes []AvailableRoute

	dbconn.AcquireLock()
	err := db.Select(&availableRoutes, "SELECT DISTINCT line, bound, from_id, to_id FROM tfl_predictions_history")
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	result := make([]map[string]string, len(availableRoutes))
	for i, v := range availableRoutes {
		result[i] = map[string]string{
			"line":     v.Line,
			"bound":    v.Bound,
			"fromStop": v.FromStop,
			"toStop":   v.ToStop,
		}
	}
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:35,代码来源:selecttflpredictionshistory.go


示例15: insertExpectedDeparturesIntoDB

/* Inserts expected departures found above into table "expected_departures".
   To supply map of expected departures
   map (vehicleId =>
	("line" => line,
	 "bound" => bound,
	 "naptanId" => naptanId of terminus,
	 "expectedDeparture" => expectedDeparture in TfL format,
	))
*/
func insertExpectedDeparturesIntoDB(expectedDepartures map[string]map[string]string) {
	values := make([]string, 0)
	for vehicleId, details := range expectedDepartures {
		values = append(values,
			fmt.Sprintf("('%v', '%v', '%v', '%v', TIMESTAMP '%v')",
				details["line"], details["bound"], details["naptanId"], vehicleId,
				toPsqlTimeStamp(details["expectedDeparture"])))
	}

	valuesCommaSep := strings.Join(values, ",")

	dbconn.AcquireLock()
	db.MustExec(
		fmt.Sprintf("UPDATE terminus_departures AS entry "+
			"SET line=data.line, bound=data.bound, naptan_id=data.naptan_id, expected_departure_time=data.expected_departure_time, last_updated=DEFAULT "+
			"FROM (VALUES %s) AS data(line, bound, naptan_id, vehicle_id, expected_departure_time) "+
			"WHERE data.vehicle_id=entry.vehicle_id",
			valuesCommaSep))

	db.MustExec(
		fmt.Sprintf("WITH data(line, bound, naptan_id, vehicle_id, expected_departure_time) AS (VALUES %s) "+
			"INSERT INTO terminus_departures(line, bound, naptan_id, vehicle_id, expected_departure_time) "+
			"SELECT data.line, data.bound, data.naptan_id, data.vehicle_id, data.expected_departure_time FROM data WHERE NOT EXISTS "+
			"(SELECT 1 FROM terminus_departures entry "+
			"WHERE data.vehicle_id=entry.vehicle_id)",
			valuesCommaSep))
	dbconn.ReleaseLock()
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:37,代码来源:monitorterminusdepartures.go


示例16: DeleteOldHistory

func DeleteOldHistory(days int) {
	/* Remove data older than days old from journey_history to save space */
	dbconn.AcquireLock()
	_, err := db.Exec(fmt.Sprintf("DELETE FROM journey_history WHERE to_timestamp < NOW() - INTERVAL '%v days'", days))
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	/* Remove data older than days old from skipped_stops_history to save space */
	dbconn.AcquireLock()
	_, err = db.Exec(fmt.Sprintf("DELETE FROM skipped_stops_history WHERE to_timestamp < NOW() - INTERVAL '%v days'", days))
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:19,代码来源:deleteoldhistory.go


示例17: SelectTflPredictions

/* Returns the tfl predictions at the times specified for a given line, bound, origin and destination
   map time.Time => time.Duration */
func SelectTflPredictions(line, bound, fromStop, toStop string, times []time.Time) map[time.Time]time.Duration {
	tflPredictions := make([]int64, len(times))

	dbconn.AcquireLock()
	tx, err := db.Begin()
	for i, targetTime := range times {

		upperBound := targetTime.Add(10 * time.Minute)
		lowerBound := targetTime.Add(-10 * time.Minute)

		logger.GetLogger().Info(fmt.Sprintf("SELECT EXTRACT(epoch FROM predicted_time) FROM ("+
			"(SELECT predicted_time, departure_time FROM tfl_predictions_history "+
			"WHERE line='%v' AND bound='%v' AND from_id='%v' AND to_id='%v' AND departure_time>='%v' AND departure_time <= '%v' ORDER BY departure_time LIMIT 1) "+
			"UNION ALL "+
			"(SELECT predicted_time, departure_time FROM tfl_predictions_history "+
			"WHERE line='%v' AND bound='%v' AND from_id='%v' AND to_id='%v' AND departure_time<'%v' AND departure_time >= '%v' ORDER BY departure_time DESC LIMIT 1) "+
			") AS result ORDER BY abs(EXTRACT(epoch FROM departure_time-'%v')) LIMIT 1",
			line, bound, fromStop, toStop, toPsqlTimeStamp(targetTime), toPsqlTimeStamp(upperBound),
			line, bound, fromStop, toStop, toPsqlTimeStamp(targetTime), toPsqlTimeStamp(lowerBound),
			toPsqlTimeStamp(targetTime)))

		err = tx.QueryRow(
			fmt.Sprintf("SELECT EXTRACT(epoch FROM predicted_time) FROM ("+
				"(SELECT predicted_time, departure_time FROM tfl_predictions_history "+
				"WHERE line='%v' AND bound='%v' AND from_id='%v' AND to_id='%v' AND departure_time>='%v' AND departure_time <= '%v' ORDER BY departure_time LIMIT 1) "+
				"UNION ALL "+
				"(SELECT predicted_time, departure_time FROM tfl_predictions_history "+
				"WHERE line='%v' AND bound='%v' AND from_id='%v' AND to_id='%v' AND departure_time<'%v' AND departure_time >= '%v' ORDER BY departure_time DESC LIMIT 1) "+
				") AS result ORDER BY abs(EXTRACT(epoch FROM departure_time-'%v')) LIMIT 1",
				line, bound, fromStop, toStop, toPsqlTimeStamp(targetTime), toPsqlTimeStamp(upperBound),
				line, bound, fromStop, toStop, toPsqlTimeStamp(targetTime), toPsqlTimeStamp(lowerBound),
				toPsqlTimeStamp(targetTime))).Scan(&tflPredictions[i])
		if err != nil {
			logger.GetLogger().Error(err.Error())
			tflPredictions[i] = 0
		}
	}
	err = tx.Commit()
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}

	result := make(map[time.Time]time.Duration)
	for i, t := range times {
		/* Skip if no recent prediction found (within 20 minute window) */
		tflPrediction := tflPredictions[i]
		if tflPrediction == 0 {
			continue
		}
		result[t] = time.Duration(tflPredictions[i]) * time.Second
	}
	return result
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:57,代码来源:selecttflpredictionshistory.go


示例18: SelectCurrentEstimatesCompensation

/* Returns the time required for each cycle of update process, so that script can compensate for time difference*/
func SelectCurrentEstimatesCompensation() (time.Duration, error) {
	var compensationInSeconds int64

	dbconn.AcquireLock()
	err := db.QueryRow("SELECT EXTRACT(epoch FROM MAX(last_updated)-MIN(last_updated)) " +
		"FROM (SELECT last_updated FROM current_estimates ORDER BY last_updated DESC LIMIT 2) AS lastest").Scan(&compensationInSeconds)
	dbconn.ReleaseLock()

	if err != nil {
		return 0, err
	}
	return time.Duration(compensationInSeconds) * time.Second, nil
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:14,代码来源:selectcurrentestimates.go


示例19: SelectPerformanceMeasure2

func SelectPerformanceMeasure2(startTime time.Time) (map[string]map[string][]map[string]string, error) {
	var performanceMeasures []PerformanceMeasure

	dbconn.AcquireLock()
	err := db.Select(&performanceMeasures, "SELECT line, bound, vehicle_id, from_id, to_id, departure_time, arrival_time, "+
		"EXTRACT(epoch FROM tfl_expected_time) AS tfl_expected_time, "+
		"EXTRACT(epoch FROM predicted_time) AS predicted_time, "+
		"EXTRACT(epoch FROM actual_time) AS actual_time "+
		"FROM performance_measure_2 "+
		"WHERE db_inserted_timestamp>=$1", startTime)
	dbconn.ReleaseLock()

	if err != nil {
		return nil, err
	}

	result := make(map[string]map[string][]map[string]string)

	for _, performanceMeasure := range performanceMeasures {
		/* Insert into results, careful not to overwrite map if already exists */
		boundDetails, boundDetailsExists := result[performanceMeasure.Line]
		if !boundDetailsExists {
			boundDetails = make(map[string][]map[string]string)
		}

		innerSlice, innerSliceExists := boundDetails[performanceMeasure.Bound]
		if !innerSliceExists {
			innerSlice = make([]map[string]string, 0)
		}

		details := make(map[string]string)
		details["fromStop"] = performanceMeasure.FromStop
		details["toStop"] = performanceMeasure.ToStop
		details["tflExpectedTime"] = toPsqlFormatDuration(performanceMeasure.TflExpectedTime)
		details["predictedTime"] = toPsqlFormatDuration(performanceMeasure.PredictedTime)
		details["actualTime"] = toPsqlFormatDuration(performanceMeasure.ActualTime)
		details["departureTime"] = toPsqlFormatTime(performanceMeasure.DepartureTime)
		details["arrivalTime"] = toPsqlFormatTime(performanceMeasure.ArrivalTime)
		details["vehicleId"] = performanceMeasure.VehicleId

		innerSlice = append(innerSlice, details)

		boundDetails[performanceMeasure.Bound] = innerSlice

		result[performanceMeasure.Line] = boundDetails
	}

	return result, nil
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:49,代码来源:selectperformancemeasure.go


示例20: SelectStopWithStopSeq

/* Returns the naptan id of a stop given its line, bound and stop sequence */
func SelectStopWithStopSeq(line, bound string, stopSeq int) string {
	var naptanId string

	dbconn.AcquireLock()
	err := db.QueryRow("SELECT naptan_id "+
		"FROM line_stop_points "+
		"WHERE line_id=$1 "+
		"AND bound=$2 "+
		"AND stop_seq=$3 ", line, bound, stopSeq).Scan(&naptanId)
	dbconn.ReleaseLock()

	if err != nil {
		logger.GetLogger().Panic(err)
	}
	return naptanId
}
开发者ID:karl-chan,项目名称:Active_Delay_Warning_Transport_App,代码行数:17,代码来源:selectlinestoppoints.go



注:本文中的github.com/karl-chan/dbconn.AcquireLock函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang dbconn.ReleaseLock函数代码示例发布时间:2022-05-23
下一篇:
Golang web.Server类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap