def task(): with timed() as t: db = ratlib.db.get_session(bot) stmt = sql.select([ sql.column('eddb_id'), sql.column('distance'), sql.column('remaining'), sql.column('final'), ]).select_from( sql.func.find_route(source.eddb_id, target.eddb_id, maxdistance)).alias() query = (db.query( Starsystem, stmt.c.distance, stmt.c.remaining, stmt.c.final).join( stmt, Starsystem.eddb_id == stmt.c.eddb_id).order_by( stmt.c.remaining.desc())) result = query.all() text = [banner, ''] sysline_fmt = "{jump:5}: {sys.name:30} ({sys.x:.2f}, {sys.y:.2f}, {sys.z:.2f})" travel_fmt = " -> (jump {distance:.2f} LY; {remaining:.2f} LY remaining)" for jump, row in enumerate(result): if not jump: jump = "START" else: text.append( travel_fmt.format(distance=row.distance, remaining=row.remaining)) if row.final: jump = " END" text.append( sysline_fmt.format(jump=jump, sys=row.Starsystem)) success = result[-1].final elapsed = timeutil.format_timedelta(t.delta) text.append('') if success: text.append("Plot completed in {}.".format(elapsed)) else: text.append( "Could not complete plot. Went {} jumps in {}.".format( len(result) - 1, elapsed)) text = "\n".join(text) + "\n" url = post_to_hastebin( text, bot.config.ratbot.hastebin_url or "http://hastebin.com/") + ".txt" if success: return ( "Plot from {source.name} to {target.name} completed: {url}" .format(source=source, target=target, url=url)) else: return ( "Plot from {source.name} to {target.name} failed, partial results at: {url}" .format(source=source, target=target, url=url))
def cmd_sysrefresh(bot, trigger, db=None): """ Refreshes the starsystem database if you have halfop or better. Reports the last refresh time otherwise. -f: Force refresh even if data is stale. Requires op. """ access = ratlib.sopel.best_channel_mode(bot, trigger.nick) privileged = access & (HALFOP | OP) msg = "" if privileged: options = "" if not trigger.group( 2) or trigger.group(2)[0] != '-' else trigger.group(2)[1:] force = 'f' in options and (access & OP) prune = not ('p' in options and (access & OP)) try: refreshed = refresh_database( bot, force=force, prune=prune, callback=lambda: bot.say("Starting starsystem refresh...")) if refreshed: bot.say(refresh_time_stats(bot)) return msg = "Not yet. " except ConcurrentOperationError: bot.say("A starsystem refresh operation is already in progress.") return when = get_status(db).starsystem_refreshed if not when: msg += "The starsystem database appears to have never been initialized." else: when = when.astimezone(datetime.timezone.utc) msg += "The starsystem database was refreshed at {} ({}) or an update is still in progress. It is only allowed every {} seconds.".format( timeutil.format_timestamp(when), timeutil.format_timedelta(when), bot.config.ratbot.edsm_maxage or '<unknown>') bot.say(msg)