Skip to content

Commit 4def246

Browse files
Progress.
1 parent 0e90f00 commit 4def246

23 files changed

+185
-64
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
<Compile Include="IReportProgress.cs" />
7373
<Compile Include="IResourceConfig.cs" />
7474
<Compile Include="IResourceConfigVisitor.cs" />
75+
<Compile Include="IResourceStrategy.cs" />
7576
<Compile Include="IShouldProcess.cs" />
76-
<Compile Include="ConcurrentTaskProgress.cs" />
7777
<Compile Include="TimeSlot.cs" />
7878
<Compile Include="StateOperationContext.cs" />
7979
<Compile Include="Compute\ComputeStrategy.cs" />

src/ResourceManager/Common/Commands.Common.Strategies/Compute/ComputeStrategy.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public static ResourceStrategy<TModel> Create<TModel, TOperations>(
2626
string header,
2727
Func<ComputeManagementClient, TOperations> getOperations,
2828
Func<TOperations, GetAsyncParams, Task<TModel>> getAsync,
29-
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync)
29+
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
30+
Func<TModel, int> createTime)
3031
where TModel : Resource
3132
=> ResourceStrategy.Create(
3233
type,
@@ -35,6 +36,7 @@ public static ResourceStrategy<TModel> Create<TModel, TOperations>(
3536
getAsync,
3637
createOrUpdateAsync,
3738
config => config.Location,
38-
(config, location) => config.Location = location);
39+
(config, location) => config.Location = location,
40+
createTime);
3941
}
4042
}

src/ResourceManager/Common/Commands.Common.Strategies/Compute/VirtualMachineStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static class VirtualMachineStrategy
2929
(o, p) => o.GetAsync(
3030
p.ResourceGroupName, p.Name, null, p.CancellationToken),
3131
(o, p) => o.CreateOrUpdateAsync(
32-
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
32+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
33+
_ => 120);
3334

3435
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
3536
this ResourceConfig<ResourceGroup> resourceGroup,

src/ResourceManager/Common/Commands.Common.Strategies/ConcurrentTaskProgress.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/ResourceManager/Common/Commands.Common.Strategies/IReportProgress.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ namespace Microsoft.Azure.Commands.Common.Strategies
1616
{
1717
public interface IProgressReport
1818
{
19-
void Report<TModel>(ResourceConfig<TModel> config, double progress)
20-
where TModel : class;
19+
void Start<TModel>(ResourceConfig<TModel> config)
20+
where TModel : class;
21+
22+
void Done<TModel>(ResourceConfig<TModel> config, double progress)
23+
where TModel : class;
2124
}
2225
}

src/ResourceManager/Common/Commands.Common.Strategies/IResourceConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Microsoft.Azure.Commands.Common.Strategies
1818
{
1919
public interface IResourceConfig : IEntityConfig
2020
{
21+
new IResourceStrategy Strategy { get; }
22+
2123
string ResourceGroupName { get; }
2224

2325
IEnumerable<IEntityConfig> Dependencies { get; }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Microsoft.Azure.Commands.Common.Strategies
2+
{
3+
public interface IResourceStrategy : IEntityStrategy
4+
{
5+
string Type { get; }
6+
}
7+
}

src/ResourceManager/Common/Commands.Common.Strategies/Network/NetworkInterfaceStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static class NetworkInterfaceStrategy
2828
(o, p) => o.GetAsync(
2929
p.ResourceGroupName, p.Name, null, p.CancellationToken),
3030
(o, p) => o.CreateOrUpdateAsync(
31-
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
31+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
32+
_ => 5);
3233

3334
public static ResourceConfig<NetworkInterface> CreateNetworkInterfaceConfig(
3435
this ResourceConfig<ResourceGroup> resourceGroup,

src/ResourceManager/Common/Commands.Common.Strategies/Network/NetworkSecurityGroupPolicy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static class NetworkSecurityGroupStrategy
2929
(o, p) => o.GetAsync(
3030
p.ResourceGroupName, p.Name, null, p.CancellationToken),
3131
(o, p) => o.CreateOrUpdateAsync(
32-
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
32+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
33+
_ => 15);
3334

3435
public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(
3536
this ResourceConfig<ResourceGroup> resourceGroup, string name, int[] openPorts)

src/ResourceManager/Common/Commands.Common.Strategies/Network/NetworkStrategy.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public static ResourceStrategy<TModel> Create<TModel, TOperations>(
2626
string header,
2727
Func<NetworkManagementClient, TOperations> getOperations,
2828
Func<TOperations, GetAsyncParams, Task<TModel>> getAsync,
29-
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync)
29+
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
30+
Func<TModel, int> createTime)
3031
where TModel : Resource
3132
=> ResourceStrategy.Create(
3233
type,
@@ -35,6 +36,7 @@ public static ResourceStrategy<TModel> Create<TModel, TOperations>(
3536
getAsync,
3637
createOrUpdateAsync,
3738
model => model.Location,
38-
(model, location) => model.Location = location);
39+
(model, location) => model.Location = location,
40+
createTime);
3941
}
4042
}

src/ResourceManager/Common/Commands.Common.Strategies/Network/PublicIPAddressStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static class PublicIPAddressStrategy
2828
(o, p) => o.GetAsync(
2929
p.ResourceGroupName, p.Name, null, p.CancellationToken),
3030
(o, p) => o.CreateOrUpdateAsync(
31-
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
31+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
32+
_ => 15);
3233

3334
public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
3435
this ResourceConfig<ResourceGroup> resourceGroup,

src/ResourceManager/Common/Commands.Common.Strategies/Network/VirtualNetworkStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static class VirtualNetworkStrategy
2828
(o, p) => o.GetAsync(
2929
p.ResourceGroupName, p.Name, null, p.CancellationToken),
3030
(o, p) => o.CreateOrUpdateAsync(
31-
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
31+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
32+
_ => 15);
3233

3334
public static ResourceConfig<VirtualNetwork> CreateVirtualNetworkConfig(
3435
this ResourceConfig<ResourceGroup> resourceGroup,

src/ResourceManager/Common/Commands.Common.Strategies/ResourceConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public sealed class ResourceConfig<TModel> : IEntityConfig<TModel>, IResourceCon
3333

3434
IEntityStrategy IEntityConfig.Strategy => Strategy;
3535

36+
IResourceStrategy IResourceConfig.Strategy => Strategy;
37+
3638
IResourceConfig IEntityConfig.Resource => this;
3739

3840
public ResourceConfig(

src/ResourceManager/Common/Commands.Common.Strategies/ResourceManager/ResourceGroupStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static class ResourceGroupStrategy
2828
(o, p) => o.GetAsync(p.Name, p.CancellationToken),
2929
(o, p) => o.CreateOrUpdateAsync(p.Name, p.Model, p.CancellationToken),
3030
model => model.Location,
31-
(model, location) => model.Location = location);
31+
(model, location) => model.Location = location,
32+
_ => 3);
3233

3334
public static ResourceConfig<ResourceGroup> CreateResourceGroupConfig(string name)
3435
=> Strategy.CreateConfig(name, name);

src/ResourceManager/Common/Commands.Common.Strategies/ResourceStrategy.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace Microsoft.Azure.Commands.Common.Strategies
2222
{
23-
public sealed class ResourceStrategy<TModel> : IEntityStrategy
23+
public sealed class ResourceStrategy<TModel> : IResourceStrategy
2424
{
2525
public string Type { get; }
2626

@@ -35,20 +35,24 @@ public Func<IClient, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> CreateOrUp
3535

3636
public Action<TModel, string> SetLocation { get; }
3737

38+
public Func<TModel, int> CreateTime { get; }
39+
3840
public ResourceStrategy(
3941
string type,
4042
Func<string, IEnumerable<string>> getId,
4143
Func<IClient, GetAsyncParams, Task<TModel>> getAsync,
4244
Func<IClient, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
4345
Func<TModel, string> getLocation,
44-
Action<TModel, string> setLocation)
46+
Action<TModel, string> setLocation,
47+
Func<TModel, int> createTime)
4548
{
4649
Type = type;
4750
GetId = getId;
4851
GetAsync = getAsync;
4952
CreateOrUpdateAsync = createOrUpdateAsync;
5053
GetLocation = getLocation;
5154
SetLocation = setLocation;
55+
CreateTime = createTime;
5256
}
5357
}
5458

@@ -61,7 +65,8 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
6165
Func<TOperation, GetAsyncParams, Task<TModel>> getAsync,
6266
Func<TOperation, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
6367
Func<TModel, string> getLocation,
64-
Action<TModel, string> setLocation)
68+
Action<TModel, string> setLocation,
69+
Func<TModel, int> createTime)
6570
where TClient : ServiceClient<TClient>
6671
{
6772
Func<IClient, TOperation> toOperations = client => getOperations(client.GetClient<TClient>());
@@ -71,7 +76,8 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
7176
(client, p) => getAsync(toOperations(client), p),
7277
(client, p) => createOrUpdateAsync(toOperations(client), p),
7378
getLocation,
74-
setLocation);
79+
setLocation,
80+
createTime);
7581
}
7682

7783
public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
@@ -81,7 +87,8 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
8187
Func<TOperation, GetAsyncParams, Task<TModel>> getAsync,
8288
Func<TOperation, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
8389
Func<TModel, string> getLocation,
84-
Action<TModel, string> setLocation)
90+
Action<TModel, string> setLocation,
91+
Func<TModel, int> createTime)
8592
where TClient : ServiceClient<TClient>
8693
=> Create(
8794
type,
@@ -90,6 +97,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
9097
getAsync,
9198
createOrUpdateAsync,
9299
getLocation,
93-
setLocation);
100+
setLocation,
101+
createTime);
94102
}
95103
}

src/ResourceManager/Common/Commands.Common.Strategies/TimeSlot.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public TimeSlot AddTask(int duration)
5454
}
5555

5656
public double GetTaskProgress(int duration)
57-
=> duration <= Duration
58-
? GetProgress(duration)
59-
: GetProgress(Duration) + Next.GetTaskProgress(duration - Duration);
57+
=> duration <= Duration || Next == null
58+
? GetTimeSlotProgress(duration)
59+
: GetTimeSlotProgress(Duration) + Next.GetTaskProgress(duration - Duration);
6060

61-
double GetProgress(double duration)
61+
double GetTimeSlotProgress(double duration)
6262
=> duration / TaskCount;
6363
}
6464
}
Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,65 @@
1-
namespace Microsoft.Azure.Commands.Common.Strategies
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Microsoft.Azure.Commands.Common.Strategies
27
{
3-
class TimeSlotExtensions
8+
public static class TimeSlotExtensions
49
{
5-
// public TimeSlot
10+
public static Tuple<Dictionary<IResourceConfig, TimeSlot>, int> GetTimeSlonAndDuration<TModel>(
11+
this ResourceConfig<TModel> config, IState state)
12+
where TModel : class
13+
{
14+
var context = new Context(state);
15+
return Tuple.Create(context.BeginMap, context.GetTimeSlotAndDuration(config).Item2);
16+
}
17+
18+
sealed class Context
19+
{
20+
public TimeSlot Begin { get; } = new TimeSlot();
21+
22+
public Dictionary<IResourceConfig, TimeSlot> BeginMap { get; }
23+
= new Dictionary<IResourceConfig, TimeSlot>();
24+
25+
readonly IState _State;
26+
27+
readonly ConcurrentDictionary<IResourceConfig, Tuple<TimeSlot, int>> _Map
28+
= new ConcurrentDictionary<IResourceConfig, Tuple<TimeSlot, int>>();
29+
30+
public Context(IState state)
31+
{
32+
_State = state;
33+
}
34+
35+
public Tuple<TimeSlot, int> GetTimeSlotAndDuration<TModel>(
36+
ResourceConfig<TModel> config)
37+
where TModel : class
38+
=> _Map.GetOrAdd(
39+
config,
40+
_ =>
41+
{
42+
var tupleBegin = config
43+
.GetTargetDependencies(_State)
44+
.Select(GetTimeSlotAndDurationDispatch)
45+
.Aggregate(Tuple.Create(Begin, 0), (a, b) => a.Item2 > b.Item2 ? a : b);
46+
BeginMap.Add(config, tupleBegin.Item1);
47+
var duration = config.Strategy.CreateTime(_State.Get(config));
48+
return Tuple.Create(
49+
tupleBegin.Item1.AddTask(duration), tupleBegin.Item2 + duration);
50+
});
51+
52+
Tuple<TimeSlot, int> GetTimeSlotAndDurationDispatch(IResourceConfig config)
53+
=> config.Accept(new GetTimeSlotAndDurationVisitor(), this);
54+
}
55+
56+
sealed class GetTimeSlotAndDurationVisitor :
57+
IResourceConfigVisitor<Context, Tuple<TimeSlot, int>>
58+
{
59+
public Tuple<TimeSlot, int> Visit<TModel>(
60+
ResourceConfig<TModel> config, Context context)
61+
where TModel : class
62+
=> context.GetTimeSlotAndDuration(config);
63+
}
664
}
765
}

src/ResourceManager/Common/Commands.Common.Strategies/UpdateStateExtensions.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Collections.Generic;
1517
using System.Linq;
1618
using System.Threading;
1719
using System.Threading.Tasks;
@@ -33,7 +35,8 @@ public static async Task<IState> UpdateStateAsync<TModel>(
3335
new StateOperationContext(client, cancellationToken),
3436
target,
3537
shouldProcess,
36-
progressReport);
38+
progressReport,
39+
config.GetTimeSlonAndDuration(target));
3740
await context.UpdateStateAsync(config);
3841
return context.Result;
3942
}
@@ -50,16 +53,20 @@ sealed class Context
5053

5154
readonly IProgressReport _ProgressReport;
5255

56+
readonly Tuple<Dictionary<IResourceConfig, TimeSlot>, int> _TimeSlotAndDuration;
57+
5358
public Context(
5459
StateOperationContext operationContext,
5560
IState target,
5661
IShouldProcess shouldProcess,
57-
IProgressReport progressReport)
62+
IProgressReport progressReport,
63+
Tuple<Dictionary<IResourceConfig, TimeSlot>, int> timeSlotAndDuration)
5864
{
5965
_OperationContext = operationContext;
6066
_Target = target;
6167
_ShouldProcess = shouldProcess;
6268
_ProgressReport = progressReport;
69+
_TimeSlotAndDuration = timeSlotAndDuration;
6370
}
6471

6572
public async Task UpdateStateAsync<TModel>(ResourceConfig<TModel> config)
@@ -80,12 +87,16 @@ await _OperationContext.GetOrAdd(
8087
// call the CreateOrUpdateAsync function for the resource.
8188
if (await _ShouldProcess.ShouldCreate(config, model))
8289
{
83-
_ProgressReport.Report(config, 0.0);
90+
_ProgressReport.Start(config);
8491
var result = await config.CreateOrUpdateAsync(
8592
_OperationContext.Client,
8693
model,
8794
_OperationContext.CancellationToken);
88-
_ProgressReport.Report(config, 1.0);
95+
var timeSlot = _TimeSlotAndDuration.Item1.GetOrNull(config);
96+
var time = config.Strategy.CreateTime(model);
97+
var progress =
98+
timeSlot.GetTaskProgress(time) / _TimeSlotAndDuration.Item2;
99+
_ProgressReport.Done(config, progress);
89100
return result;
90101
}
91102
else

src/ResourceManager/Common/Commands.Common.Strategies/Void.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Microsoft.Azure.Commands.Common.Strategies
1616
{
17-
struct Void
17+
public struct Void
1818
{
1919
}
2020
}

0 commit comments

Comments
 (0)