Skip to content

Commit

Permalink
Added grace branch list-contents. Bug fix in Diff.CLI.fs where we w…
Browse files Browse the repository at this point in the history
…eren't updating local object cache and Grace Status if `grace watch` isn't running. Started experiments with localization.
  • Loading branch information
ScottArbeit committed Dec 25, 2023
1 parent c5657f5 commit 985a75c
Show file tree
Hide file tree
Showing 19 changed files with 876 additions and 345 deletions.
1 change: 1 addition & 0 deletions src/Grace.Actors/Branch.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ open System.Threading.Tasks
open NodaTime
open System.Text.Json
open System.Net.Http.Json
open Grace.Shared.Dto.Reference

module Branch =

Expand Down
8 changes: 5 additions & 3 deletions src/Grace.Actors/Diff.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ module Diff =
task {
let relativeDirectoryPath = getRelativeDirectory relativePath Constants.RootDirectoryPath
//logToConsole $"In DiffActor.getFileStream(); relativePath: {relativePath}; relativeDirectoryPath: {relativeDirectoryPath}; graceIndex.Count: {graceIndex.Count}."
let directory = graceIndex.Item(relativeDirectoryPath)
let directory = graceIndex[relativeDirectoryPath]
let fileVersion = directory.Files.First(fun f -> f.RelativePath = relativePath)
let! uri = getReadSharedAccessSignature repositoryDto fileVersion
logToConsole $"In DiffActor.getFileStream(); uri: {Result.get uri}."
Expand Down Expand Up @@ -239,8 +239,10 @@ module Diff =
return! diffTwoFiles fileStream2 fileStream1
}

do! fileStream1.DisposeAsync()
do! fileStream2.DisposeAsync()
if not <| isNull(fileStream1) then
do! fileStream1.DisposeAsync()
if not <| isNull(fileStream2) then
do! fileStream2.DisposeAsync()

// Create a FileDiff with the DiffPlex results and corresponding Sha256Hash values.
let fileDiff =
Expand Down
159 changes: 97 additions & 62 deletions src/Grace.CLI/Command/Branch.CLI.fs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/Grace.CLI/Command/Common.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open Spectre.Console
open System
open System.CommandLine
open System.CommandLine.Parsing
open System.Globalization
open System.Text.Json
open System.Threading.Tasks
open Spectre.Console.Rendering
Expand Down Expand Up @@ -36,6 +37,11 @@ module Common =
command.AddOption(option)
command

let public Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName

/// Gets the "... ago" text.
let ago = ago Language

module Options =
let correlationId = new Option<String>([|"--correlationId"; "-c"|], IsRequired = false, Description = "CorrelationId for end-to-end tracking <String>.", Arity = ArgumentArity.ExactlyOne)
correlationId.SetDefaultValue(Guid.NewGuid().ToString())
Expand Down
72 changes: 38 additions & 34 deletions src/Grace.CLI/Command/Diff.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ module Diff =
let t2 = progressContext.AddTask($"[{Color.DodgerBlue1}]Creating new directory verions.[/]", autoStart = false)
let t3 = progressContext.AddTask($"[{Color.DodgerBlue1}]Uploading changed files to object storage.[/]", autoStart = false)
let t4 = progressContext.AddTask($"[{Color.DodgerBlue1}]Uploading new directory versions.[/]", autoStart = false)
let t5 = progressContext.AddTask($"[{Color.DodgerBlue1}]Getting {(getDistributedUnionCaseName referenceType).ToLowerInvariant()}.[/]", autoStart = false)
let t6 = progressContext.AddTask($"[{Color.DodgerBlue1}]Sending diff request to server.[/]", autoStart = false)
let t5 = progressContext.AddTask($"[{Color.DodgerBlue1}]Creating a save reference.[/]", autoStart = false)
let t6 = progressContext.AddTask($"[{Color.DodgerBlue1}]Getting {(getDistributedUnionCaseName referenceType).ToLowerInvariant()}.[/]", autoStart = false)
let t7 = progressContext.AddTask($"[{Color.DodgerBlue1}]Sending diff request to server.[/]", autoStart = false)

let mutable rootDirectoryId = DirectoryId.Empty
let mutable rootDirectorySha256Hash = Sha256Hash String.Empty
Expand All @@ -183,6 +184,7 @@ module Diff =
t2.Value <- 100.0
t3.Value <- 100.0
t4.Value <- 100.0
t5.Value <- 100.0
rootDirectoryId <- graceWatchStatus.RootDirectoryId
rootDirectorySha256Hash <- graceWatchStatus.RootDirectorySha256Hash
previousDirectoryIds <- graceWatchStatus.DirectoryIds
Expand All @@ -192,39 +194,52 @@ module Diff =
t0.Value <- 100.0
t1.StartTask()
let! differences = scanForDifferences previousGraceStatus
let! newFileVersions = copyUpdatedFilesToObjectCache t1 differences
t1.Value <- 100.0

t2.StartTask()
let! (updatedGraceStatus, newDirectoryVersions) = getNewGraceStatusAndDirectoryVersions previousGraceStatus differences
do! writeGraceStatusFile updatedGraceStatus
rootDirectoryId <- updatedGraceStatus.RootDirectoryId
rootDirectorySha256Hash <- updatedGraceStatus.RootDirectorySha256Hash
previousDirectoryIds <- updatedGraceStatus.Index.Keys.ToHashSet()
t2.Value <- 100.0

t3.StartTask()
let updatedRelativePaths =
differences.Select(fun difference ->
match difference.DifferenceType with
| Add -> match difference.FileSystemEntryType with | FileSystemEntryType.File -> Some difference.RelativePath | FileSystemEntryType.Directory -> None
| Change -> match difference.FileSystemEntryType with | FileSystemEntryType.File -> Some difference.RelativePath | FileSystemEntryType.Directory -> None
| Delete -> None)
.Where(fun relativePathOption -> relativePathOption.IsSome)
.Select(fun relativePath -> relativePath.Value)

let newFileVersions = updatedRelativePaths.Select(fun relativePath ->
newDirectoryVersions.First(fun dv -> dv.Files.Exists(fun file -> file.RelativePath = relativePath)).Files.First(fun file -> file.RelativePath = relativePath))

let! uploadResult = uploadFilesToObjectStorage newFileVersions (getCorrelationId parseResult)
match! uploadFilesToObjectStorage newFileVersions (getCorrelationId parseResult) with
| Ok returnValue ->
()
| Error error ->
logToAnsiConsole Colors.Error $"Failed to upload changed files to object storage. {error}"
t3.Value <- 100.0

t4.StartTask()
let saveParameters = SaveDirectoryVersionsParameters()
saveParameters.DirectoryVersions <- newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()
let! uploadDirectoryVersions = Directory.SaveDirectoryVersions saveParameters
if (newDirectoryVersions.Count > 0) then
(task {
let saveDirectoryVersionsParameters = SaveDirectoryVersionsParameters()
saveDirectoryVersionsParameters.DirectoryVersions <- newDirectoryVersions.Select(fun dv -> dv.ToDirectoryVersion).ToList()

match! Directory.SaveDirectoryVersions saveDirectoryVersionsParameters with
| Ok returnValue ->
()
| Error error ->
logToAnsiConsole Colors.Error $"Failed to upload new directory versions. {error}"
}).Wait()
t4.Value <- 100.0

t5.StartTask()
if newDirectoryVersions.Count > 0 then
(task {
match! createSaveReference (getRootDirectoryVersion updatedGraceStatus) $"Created during `grace diff {(getDistributedUnionCaseName referenceType).ToLowerInvariant()}` operation." (getCorrelationId parseResult) with
| Ok saveReference ->
()
| Error error ->
logToAnsiConsole Colors.Error $"Failed to create a save reference. {error}"
}).Wait()
t5.Value <- 100.0

// Check for latest reference of the given type from the server.
t5.StartTask()
t6.StartTask()
let getReferencesParameters = GetReferencesParameters(
OwnerId = parameters.OwnerId,
OwnerName = parameters.OwnerName,
Expand Down Expand Up @@ -308,7 +323,7 @@ module Diff =
| Error error ->
logToAnsiConsole Colors.Error $"Error getting latest reference: {Markup.Escape(error.Error)}."
ReferenceDto.Default
t5.Value <- 100.0
t6.Value <- 100.0

let addToOutput (markup: IRenderable) =
markupList.Add markup
Expand All @@ -323,7 +338,7 @@ module Diff =
addToOutput(Markup(String.Empty))

// Sending diff request to server.
t6.StartTask()
t7.StartTask()
//logToAnsiConsole Colors.Verbose $"latestReference.DirectoryId: {latestReference.DirectoryId}; rootDirectoryId: {rootDirectoryId}."
let getDiffParameters = GetDiffParameters(DirectoryId1 = latestReference.DirectoryId, DirectoryId2 = rootDirectoryId)
let! getDiffResult = Diff.GetDiff(getDiffParameters)
Expand Down Expand Up @@ -357,7 +372,7 @@ module Diff =
logToAnsiConsole Colors.Error $"Error submitting diff: {s}"
if parseResult |> json || parseResult |> verbose then
logToAnsiConsole Colors.Verbose (serialize error)
t6.Increment(100.0)
t7.Increment(100.0)
//AnsiConsole.MarkupLine($"[{Colors.Important}]Differences: {differences.Count}.[/]")
//AnsiConsole.MarkupLine($"[{Colors.Error}]{error.Error.EscapeMarkup()}[/]")
})
Expand Down Expand Up @@ -465,6 +480,7 @@ module Diff =
t0.Value <- 100.0
t1.StartTask()
let! differences = scanForDifferences previousGraceStatus
let! newFileVersions = copyUpdatedFilesToObjectCache t1 differences
t1.Value <- 100.0

t2.StartTask()
Expand All @@ -475,18 +491,6 @@ module Diff =
t2.Value <- 100.0

t3.StartTask()
let updatedRelativePaths =
differences.Select(fun difference ->
match difference.DifferenceType with
| Add -> match difference.FileSystemEntryType with | FileSystemEntryType.File -> Some difference.RelativePath | FileSystemEntryType.Directory -> None
| Change -> match difference.FileSystemEntryType with | FileSystemEntryType.File -> Some difference.RelativePath | FileSystemEntryType.Directory -> None
| Delete -> None)
.Where(fun relativePathOption -> relativePathOption.IsSome)
.Select(fun relativePath -> relativePath.Value)

let newFileVersions = updatedRelativePaths.Select(fun relativePath ->
newDirectoryVersions.First(fun dv -> dv.Files.Exists(fun file -> file.RelativePath = relativePath)).Files.First(fun file -> file.RelativePath = relativePath))

let! uploadResult = uploadFilesToObjectStorage newFileVersions (getCorrelationId parseResult)
t3.Value <- 100.0

Expand Down
11 changes: 8 additions & 3 deletions src/Grace.CLI/Command/Repository.CLI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -519,19 +519,24 @@ module Repository =
table.AddColumn(TableColumn(Markup($"[{Colors.Important}]Branch[/]")))
.AddColumn(TableColumn(Markup($"[{Colors.Important}]Updated at[/]")))
.AddColumn(TableColumn(Markup($"[{Colors.Important}]Branch Id[/]")))
.AddColumn(TableColumn(Markup($"[{Colors.Important}]Based on latest promotion[/]")))
.AddColumn(TableColumn(Markup($"[{Colors.Important}]Parent branch[/]"))) |> ignore
let allBranches = returnValue.ReturnValue
let parents = allBranches.Select(fun branch -> {|
branchId = branch.BranchId
branchName = if branch.ParentBranchId = Constants.DefaultParentBranchId then "root"
else allBranches.Where(fun br -> br.BranchId = branch.ParentBranchId).Select(fun br -> br.BranchName).First() |})
else allBranches.Where(fun br -> br.BranchId = branch.ParentBranchId).Select(fun br -> br.BranchName).First()
latestPromotion = if branch.ParentBranchId = Constants.DefaultParentBranchId then branch.LatestPromotion
else allBranches.Where(fun br -> br.BranchId = branch.ParentBranchId).Select(fun br -> br.LatestPromotion).First() |})

let branchesWithParentNames = allBranches.Join(parents, (fun branch -> branch.BranchId), (fun parent -> parent.branchId),
(fun branch parent -> {| branchId = branch.BranchId; branchName = branch.BranchName; updatedAt = branch.UpdatedAt; parentBranchName = parent.branchName |})).OrderBy(fun branch -> branch.parentBranchName, branch.branchName)
(fun branch parent -> {| branchId = branch.BranchId; branchName = branch.BranchName; updatedAt = branch.UpdatedAt; parentBranchName = parent.branchName; basedOnLatestPromotion = (branch.BasedOn = parent.latestPromotion) |})).OrderBy(fun branch -> branch.parentBranchName, branch.branchName)

for br in branchesWithParentNames do
let updatedAt = match br.updatedAt with | Some t -> instantToLocalTime(t) | None -> String.Empty
table.AddRow(br.branchName, updatedAt, $"[{Colors.Deemphasized}]{br.branchId}[/]", br.parentBranchName) |> ignore
table.AddRow(br.branchName, updatedAt, $"[{Colors.Deemphasized}]{br.branchId}[/]",
(if br.basedOnLatestPromotion then $"[{Colors.Added}]Yes[/]" else $"[{Colors.Important}]No[/]"),
br.parentBranchName) |> ignore
AnsiConsole.Write(table)

return intResult
Expand Down
Loading

0 comments on commit 985a75c

Please sign in to comment.