Skip to content

Commit

Permalink
ProgressMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Dec 3, 2017
1 parent 4def246 commit 87b9def
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="IResourceConfigVisitor.cs" />
<Compile Include="IResourceStrategy.cs" />
<Compile Include="IShouldProcess.cs" />
<Compile Include="ProgressMap.cs" />
<Compile Include="TimeSlot.cs" />
<Compile Include="StateOperationContext.cs" />
<Compile Include="Compute\ComputeStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public class ProgressMap
{
readonly Dictionary<IResourceConfig, Tuple<TimeSlot, int>> _Map
= new Dictionary<IResourceConfig, Tuple<TimeSlot, int>>();

readonly int _Duration;

public ProgressMap(Dictionary<IResourceConfig, Tuple<TimeSlot, int>> map, int duration)
{
_Map = map;
_Duration = duration;
}

public double Get(IResourceConfig config)
{
var x = _Map.GetOrNull(config);
return x.Item1.GetTaskProgress(x.Item2) / _Duration;
}
}
}
22 changes: 19 additions & 3 deletions src/ResourceManager/Common/Commands.Common.Strategies/TimeSlot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
namespace Microsoft.Azure.Commands.Common.Strategies
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Common.Strategies
{
/// <summary>
/// TimeSlot is a node of a singly linked list of TimeSlots.
Expand Down Expand Up @@ -32,7 +46,7 @@ public TimeSlot AddTask(int duration)
{
return this;
}
else if (Next == null)
else if (IsLast)
{
Next = new TimeSlot();
Duration = duration;
Expand All @@ -54,7 +68,9 @@ public TimeSlot AddTask(int duration)
}

public double GetTaskProgress(int duration)
=> duration <= Duration || Next == null
=> IsLast
? duration
: duration <= Duration
? GetTimeSlotProgress(duration)
: GetTimeSlotProgress(Duration) + Next.GetTaskProgress(duration - Duration);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
using System;
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -7,20 +21,21 @@ namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class TimeSlotExtensions
{
public static Tuple<Dictionary<IResourceConfig, TimeSlot>, int> GetTimeSlonAndDuration<TModel>(
public static ProgressMap GetProgressMap<TModel>(
this ResourceConfig<TModel> config, IState state)
where TModel : class
{
var context = new Context(state);
return Tuple.Create(context.BeginMap, context.GetTimeSlotAndDuration(config).Item2);
var duration = context.GetTimeSlotAndDuration(config).Item2;
return new ProgressMap(context.BeginMap, duration);
}

sealed class Context
{
public TimeSlot Begin { get; } = new TimeSlot();

public Dictionary<IResourceConfig, TimeSlot> BeginMap { get; }
= new Dictionary<IResourceConfig, TimeSlot>();
public Dictionary<IResourceConfig, Tuple<TimeSlot, int>> BeginMap { get; }
= new Dictionary<IResourceConfig, Tuple<TimeSlot, int>>();

readonly IState _State;

Expand All @@ -43,8 +58,8 @@ public Tuple<TimeSlot, int> GetTimeSlotAndDuration<TModel>(
.GetTargetDependencies(_State)
.Select(GetTimeSlotAndDurationDispatch)
.Aggregate(Tuple.Create(Begin, 0), (a, b) => a.Item2 > b.Item2 ? a : b);
BeginMap.Add(config, tupleBegin.Item1);
var duration = config.Strategy.CreateTime(_State.Get(config));
BeginMap.Add(config, Tuple.Create(tupleBegin.Item1, duration));
return Tuple.Create(
tupleBegin.Item1.AddTask(duration), tupleBegin.Item2 + duration);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task<IState> UpdateStateAsync<TModel>(
target,
shouldProcess,
progressReport,
config.GetTimeSlonAndDuration(target));
config.GetProgressMap(target));
await context.UpdateStateAsync(config);
return context.Result;
}
Expand All @@ -53,20 +53,20 @@ sealed class Context

readonly IProgressReport _ProgressReport;

readonly Tuple<Dictionary<IResourceConfig, TimeSlot>, int> _TimeSlotAndDuration;
readonly ProgressMap _ProgressMap;

public Context(
StateOperationContext operationContext,
IState target,
IShouldProcess shouldProcess,
IProgressReport progressReport,
Tuple<Dictionary<IResourceConfig, TimeSlot>, int> timeSlotAndDuration)
ProgressMap progressMap)
{
_OperationContext = operationContext;
_Target = target;
_ShouldProcess = shouldProcess;
_ProgressReport = progressReport;
_TimeSlotAndDuration = timeSlotAndDuration;
_ProgressMap = progressMap;
}

public async Task UpdateStateAsync<TModel>(ResourceConfig<TModel> config)
Expand All @@ -92,11 +92,7 @@ await _OperationContext.GetOrAdd(
_OperationContext.Client,
model,
_OperationContext.CancellationToken);
var timeSlot = _TimeSlotAndDuration.Item1.GetOrNull(config);
var time = config.Strategy.CreateTime(model);
var progress =
timeSlot.GetTaskProgress(time) / _TimeSlotAndDuration.Item2;
_ProgressReport.Done(config, progress);
_ProgressReport.Done(config, _ProgressMap.Get(config));
return result;
}
else
Expand Down

0 comments on commit 87b9def

Please sign in to comment.