Skip to content

Commit 87b9def

Browse files
ProgressMap
1 parent 4def246 commit 87b9def

File tree

5 files changed

+85
-18
lines changed

5 files changed

+85
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="IResourceConfigVisitor.cs" />
7575
<Compile Include="IResourceStrategy.cs" />
7676
<Compile Include="IShouldProcess.cs" />
77+
<Compile Include="ProgressMap.cs" />
7778
<Compile Include="TimeSlot.cs" />
7879
<Compile Include="StateOperationContext.cs" />
7980
<Compile Include="Compute\ComputeStrategy.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
18+
namespace Microsoft.Azure.Commands.Common.Strategies
19+
{
20+
public class ProgressMap
21+
{
22+
readonly Dictionary<IResourceConfig, Tuple<TimeSlot, int>> _Map
23+
= new Dictionary<IResourceConfig, Tuple<TimeSlot, int>>();
24+
25+
readonly int _Duration;
26+
27+
public ProgressMap(Dictionary<IResourceConfig, Tuple<TimeSlot, int>> map, int duration)
28+
{
29+
_Map = map;
30+
_Duration = duration;
31+
}
32+
33+
public double Get(IResourceConfig config)
34+
{
35+
var x = _Map.GetOrNull(config);
36+
return x.Item1.GetTaskProgress(x.Item2) / _Duration;
37+
}
38+
}
39+
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
namespace Microsoft.Azure.Commands.Common.Strategies
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Common.Strategies
216
{
317
/// <summary>
418
/// TimeSlot is a node of a singly linked list of TimeSlots.
@@ -32,7 +46,7 @@ public TimeSlot AddTask(int duration)
3246
{
3347
return this;
3448
}
35-
else if (Next == null)
49+
else if (IsLast)
3650
{
3751
Next = new TimeSlot();
3852
Duration = duration;
@@ -54,7 +68,9 @@ public TimeSlot AddTask(int duration)
5468
}
5569

5670
public double GetTaskProgress(int duration)
57-
=> duration <= Duration || Next == null
71+
=> IsLast
72+
? duration
73+
: duration <= Duration
5874
? GetTimeSlotProgress(duration)
5975
: GetTimeSlotProgress(Duration) + Next.GetTaskProgress(duration - Duration);
6076

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
216
using System.Collections.Concurrent;
317
using System.Collections.Generic;
418
using System.Linq;
@@ -7,20 +21,21 @@ namespace Microsoft.Azure.Commands.Common.Strategies
721
{
822
public static class TimeSlotExtensions
923
{
10-
public static Tuple<Dictionary<IResourceConfig, TimeSlot>, int> GetTimeSlonAndDuration<TModel>(
24+
public static ProgressMap GetProgressMap<TModel>(
1125
this ResourceConfig<TModel> config, IState state)
1226
where TModel : class
1327
{
1428
var context = new Context(state);
15-
return Tuple.Create(context.BeginMap, context.GetTimeSlotAndDuration(config).Item2);
29+
var duration = context.GetTimeSlotAndDuration(config).Item2;
30+
return new ProgressMap(context.BeginMap, duration);
1631
}
1732

1833
sealed class Context
1934
{
2035
public TimeSlot Begin { get; } = new TimeSlot();
2136

22-
public Dictionary<IResourceConfig, TimeSlot> BeginMap { get; }
23-
= new Dictionary<IResourceConfig, TimeSlot>();
37+
public Dictionary<IResourceConfig, Tuple<TimeSlot, int>> BeginMap { get; }
38+
= new Dictionary<IResourceConfig, Tuple<TimeSlot, int>>();
2439

2540
readonly IState _State;
2641

@@ -43,8 +58,8 @@ public Tuple<TimeSlot, int> GetTimeSlotAndDuration<TModel>(
4358
.GetTargetDependencies(_State)
4459
.Select(GetTimeSlotAndDurationDispatch)
4560
.Aggregate(Tuple.Create(Begin, 0), (a, b) => a.Item2 > b.Item2 ? a : b);
46-
BeginMap.Add(config, tupleBegin.Item1);
4761
var duration = config.Strategy.CreateTime(_State.Get(config));
62+
BeginMap.Add(config, Tuple.Create(tupleBegin.Item1, duration));
4863
return Tuple.Create(
4964
tupleBegin.Item1.AddTask(duration), tupleBegin.Item2 + duration);
5065
});

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static async Task<IState> UpdateStateAsync<TModel>(
3636
target,
3737
shouldProcess,
3838
progressReport,
39-
config.GetTimeSlonAndDuration(target));
39+
config.GetProgressMap(target));
4040
await context.UpdateStateAsync(config);
4141
return context.Result;
4242
}
@@ -53,20 +53,20 @@ sealed class Context
5353

5454
readonly IProgressReport _ProgressReport;
5555

56-
readonly Tuple<Dictionary<IResourceConfig, TimeSlot>, int> _TimeSlotAndDuration;
56+
readonly ProgressMap _ProgressMap;
5757

5858
public Context(
5959
StateOperationContext operationContext,
6060
IState target,
6161
IShouldProcess shouldProcess,
6262
IProgressReport progressReport,
63-
Tuple<Dictionary<IResourceConfig, TimeSlot>, int> timeSlotAndDuration)
63+
ProgressMap progressMap)
6464
{
6565
_OperationContext = operationContext;
6666
_Target = target;
6767
_ShouldProcess = shouldProcess;
6868
_ProgressReport = progressReport;
69-
_TimeSlotAndDuration = timeSlotAndDuration;
69+
_ProgressMap = progressMap;
7070
}
7171

7272
public async Task UpdateStateAsync<TModel>(ResourceConfig<TModel> config)
@@ -92,11 +92,7 @@ await _OperationContext.GetOrAdd(
9292
_OperationContext.Client,
9393
model,
9494
_OperationContext.CancellationToken);
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);
95+
_ProgressReport.Done(config, _ProgressMap.Get(config));
10096
return result;
10197
}
10298
else

0 commit comments

Comments
 (0)