Ejemplo n.º 1
0
        //public async Task<string> WatchRunStatusAsync(Action<string> progressAction = default, System.Threading.CancellationToken cancelToken = default)
        //{
        //    var api = new RunsApi();
        //    var proj = this.Project;
        //    var simuId = this.RunID;
        //    var run = api.GetRun(proj.Owner.Name, proj.Name, simuId);
        //    var status = run.Status;
        //    var startTime = status.StartedAt;
        //    while (status.FinishedAt <= status.StartedAt)
        //    {
        //        var currentSeconds = Math.Round((DateTime.UtcNow - startTime).TotalSeconds);
        //        // wait 5 seconds before calling api to re-check the status
        //        var isCreatedOrScheduled = status.Status == RunStatusEnum.Created || status.Status == RunStatusEnum.Scheduled;
        //        var totalDelaySeconds = isCreatedOrScheduled ? 3 : 5;
        //        for (int i = 0; i < totalDelaySeconds; i++)
        //        {
        //            // suspended by user
        //            cancelToken.ThrowIfCancellationRequested();

        //            progressAction?.Invoke($"{status.Status}: [{GetUserFriendlyTimeCounter(TimeSpan.FromSeconds(currentSeconds))}]");
        //            await Task.Delay(1000);
        //            currentSeconds++;
        //        }
        //        // suspended by user
        //        cancelToken.ThrowIfCancellationRequested();

        //        // update status
        //        await Task.Delay(1000);
        //        run = api.GetRun(proj.Owner.Name, proj.Name, simuId);
        //        status = run.Status;
        //        //_simulation = new Simulation(proj, simuId);
        //    }
        //    this.Run = run;
        //    // suspended by user
        //    cancelToken.ThrowIfCancellationRequested();

        //    var totalTime = status.FinishedAt - startTime;
        //    var finishMessage = status.Status.ToString();
        //    //progressAction?.Invoke($"Task: {status.Status}");

        //    finishMessage = $"{finishMessage}: [{GetUserFriendlyTimeCounter(totalTime)}]";
        //    progressAction?.Invoke(finishMessage);
        //    return finishMessage;

        //    string GetUserFriendlyTimeCounter(TimeSpan timeDelta)
        //    {
        //        string format = @"hh\:mm\:ss";
        //        if (timeDelta.Days > 0)
        //            format = @"d\ hh\:mm\:ss";
        //        else if (timeDelta.Hours > 0)
        //            format = @"hh\:mm\:ss";
        //        else if (timeDelta.Minutes > 0)
        //            format = @"mm\:ss";
        //        else
        //            format = @"ss";
        //        return timeDelta.ToString(format);
        //    }
        //}



        ///// <summary>
        ///// Download all log for a simulation and combine it into one text format
        ///// </summary>
        ///// <param name="progressAction"></param>
        ///// <param name="cancelToken"></param>
        ///// <returns></returns>
        //public async Task<string> GetSimulationOutputLogAsync(Action<string> progressAction = default, System.Threading.CancellationToken cancelToken = default)
        //{
        //    // get task log ids
        //    if (cancelToken.IsCancellationRequested) return string.Empty;
        //    progressAction?.Invoke($"Getting log IDs");
        //    var proj = this.Project;
        //    var simuId = this.RunID;
        //    var api = new RunsApi();
        //    var job = api.GetRun(proj.Owner.Name, proj.Name, simuId);
        //    var status = job.Status;
        //    if (status.Status == "Running") throw new ArgumentException("Simulation is still running, please wait until it's done!");
        //    var taskDic = status.Steps.OrderBy(_ => _.Value.StartedAt).ToDictionary(_ => _.Key, _ => $"[{_.Key}]\n{_.Value.StartedAt.ToLocalTime()} : {_.Value.Name}");
        //    var taskIDs = taskDic.Keys;

        //    //Download file
        //    if (cancelToken.IsCancellationRequested) return string.Empty;
        //    progressAction?.Invoke($"Downloading logs");

        //    //var url = api.GetSimulationLogs(proj.Owner.Name, proj.Name, simuId).ToString();
        //    var url = "";
        //    if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("Failed to call GetSimulationLogs");
        //    var dir = Path.Combine(Helper.GenTempFolder(), simuId);
        //    var downloadfile = await Helper.DownloadFromUrlAsync(url, dir);


        //    //unzip file
        //    if (cancelToken.IsCancellationRequested) return string.Empty;
        //    progressAction?.Invoke($"Reading logs");
        //    Helper.Unzip(downloadfile, dir, true);



        //    //read logs
        //    if (cancelToken.IsCancellationRequested) return string.Empty;
        //    var taskFiles = Directory.GetFiles(dir, "*.log", SearchOption.AllDirectories);
        //    var totalCount = taskIDs.Count;
        //    var current = 0;
        //    foreach (var logFile in taskFiles)
        //    {
        //        if (cancelToken.IsCancellationRequested) break;

        //        var logID = new DirectoryInfo(Path.GetDirectoryName(logFile)).Name;
        //        if (!taskIDs.Contains(logID)) continue;

        //        var logHeader = taskDic[logID];
        //        var logContent = File.ReadAllText(logFile);
        //        logContent = string.IsNullOrWhiteSpace(logContent) ? "No log available for this task." : logContent;
        //        taskDic[logID] = $"{logHeader} \n{logContent}";
        //        current++;

        //        progressAction?.Invoke($"Reading logs [{current}/{totalCount}]");
        //    }

        //    var fullLog = string.Join("\n\n", taskDic.Values);
        //    return fullLog;
        //}

        //private static async Task<string> DownloadFile(string url, string dir)
        //{
        //    var request = new RestRequest(Method.GET);
        //    var client = new RestClient(url.ToString());
        //    var response = await client.ExecuteAsync(request);
        //    if (response.StatusCode != HttpStatusCode.OK)
        //        throw new Exception($"Unable to download file");

        //    // prep file path
        //    var fileName = Path.GetFileName(url).Split(new[] { '?' })[0];
        //    var tempDir = string.IsNullOrEmpty(dir) ? Path.Combine(Path.GetTempPath(), "Pollination", Path.GetRandomFileName()) : dir;
        //    Directory.CreateDirectory(tempDir);
        //    var file = Path.Combine(tempDir, fileName);

        //    var b = response.RawBytes;
        //    File.WriteAllBytes(file, b);

        //    if (!File.Exists(file)) throw new ArgumentException($"Failed to download {fileName}");
        //    return file;
        //}

        //private static void CheckOutputLogs(RunsApi api, Project proj, string simuId)
        //{
        //    //var api = new RunsApi();
        //    var steps = api.GetRunSteps(proj.Owner.Name, proj.Name, simuId.ToString());
        //    foreach (var item in steps.Resources)
        //    {
        //        var stepLog = api.GetRunStepLogs(proj.Owner.Name, proj.Name, simuId.ToString(), item.Id);
        //        Console.WriteLine(stepLog);
        //    }

        //}

        /// <summary>
        /// Load a RunInfo from a local run's folder.
        /// This folder must contains recipe.json for RecipeInterface, and input.json for input arguments
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static RunInfo LoadFromLocalFolder(string folder)
        {
            RunInfo runInfo = null;

            if (Directory.Exists(folder))
            {
                var recipeFile = Path.Combine(folder, "recipe.json");
                var recipeJson = File.ReadAllText(recipeFile);
                var recipe     = RecipeInterface.FromJson(recipeJson);
                runInfo = new RunInfo(recipe, folder);
            }
            return(runInfo);
        }
Ejemplo n.º 2
0
        public void Init()
        {
            using (WebClient wc = new WebClient())
            {
                // Annual daylight
                var url  = @"https://api.staging.pollination.cloud/recipes/ladybug-tools/annual-daylight/tags/latest";
                var json = wc.DownloadString(url);
                this.annualDaylight = RecipePackage.FromJson(json).Manifest;

                // Daylight factor
                url  = @"https://api.staging.pollination.cloud/recipes/ladybug-tools/daylight-factor/tags/latest";
                json = wc.DownloadString(url);
                this.daylightFactor = RecipePackage.FromJson(json).Manifest;

                // Daylight factor
                url  = @"https://api.staging.pollination.cloud/recipes/ladybug-tools/annual-energy-use/tags/latest";
                json = wc.DownloadString(url);
                this.annualEnergyUse = RecipePackage.FromJson(json).Manifest;
            }

            //var file = @"../../../testResources/RecipePackage.json";
            //var text = File.ReadAllText(file);
            //instance = RecipePackage.FromJson(text);
        }
Ejemplo n.º 3
0
 public RunInfo(Project proj, Run run)
 {
     this.Run     = run;
     this.Project = proj;
     this.Recipe  = this.Run.Recipe;
 }