-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.razor
131 lines (120 loc) · 4.38 KB
/
App.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@using NCrontab
@using Microsoft.Extensions.Logging
@using Microsoft.AspNetCore.WebUtilities
@inject ILogger<Index> Logger
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
<label for="crontab-expression">NCrontab Expression:</label>
<div class="input-group mb-3">
<input @onkeyup="SetExpression"
@bind="cronExpression"
@bind:event="oninput"
placeholder="Cron Expression"
class="form-control"
id="crontab-expression" />
<div class="input-group-append">
<button
@onclick="PrintOccurrences"
type="button"
class="btn btn-primary"
disabled="@(!isExpressionValid)">
Print More
</button>
<button @onclick="CopyLink" type="button" class="btn btn-light">Copy Link</button>
</div>
</div>
<label>Occurrences Output:</label>
@if (!string.IsNullOrEmpty(errorMessage))
{
<div class="alert alert-danger" role="alert">
@errorMessage
</div>
}
@if (!isExpression6PartFormat)
{
<div class="alert alert-warning" role="alert">
You are using the classic five-part CRON format. Services such as
Azure Functions and Azure WebJobs require the six-part format.
</div>
}
<ul class="occurrences-list">
@foreach (var occurence in occurrences)
{
<li>
@occurence.ToString("yyyy-MM-dd HH:mm:ss")
</li>
}
</ul>
@code {
private const string DefaultCronExpression = "0 0 12 * */2 Mon";
private DateTime startDate = DateTime.Today;
private string cronExpression = DefaultCronExpression;
private CrontabSchedule cronSchedule = CrontabSchedule.Parse(DefaultCronExpression, new CrontabSchedule.ParseOptions { IncludingSeconds = true });
private List<DateTime> occurrences = new List<DateTime>();
private string errorMessage = string.Empty;
private List<Action> actionsToRunAfterRender = new List<Action>();
private bool isExpression6PartFormat = true;
private bool isExpressionValid = true;
protected override Task OnInitializedAsync()
{
var uri = new Uri(NavigationManager.Uri);
var queryPairs = QueryHelpers.ParseQuery(uri.Query);
if (queryPairs.ContainsKey("expression"))
{
cronExpression = ReadableQueryToCron(queryPairs["expression"].First());
}
SetExpression();
return Task.CompletedTask;
}
protected override Task OnAfterRenderAsync(bool firstRender)
{
foreach (var actionToRun in actionsToRunAfterRender)
{
actionToRun();
}
actionsToRunAfterRender.Clear();
return base.OnAfterRenderAsync(firstRender);
}
private void PrintOccurrences()
{
var mostRecentDate = occurrences.Count == 0 ? startDate : occurrences.Last();
for (int i = 0; i < 10; i++)
{
mostRecentDate = cronSchedule.GetNextOccurrence(mostRecentDate);
occurrences.Add(mostRecentDate);
}
actionsToRunAfterRender.Add(() => JSRuntime.InvokeVoidAsync("scrollToOccurrence", occurrences.Count - 1));
}
private void SetExpression()
{
occurrences.Clear();
isExpression6PartFormat = cronExpression
.Trim()
.Replace(@"\t", " ")
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Count() == 6;
try
{
cronSchedule = CrontabSchedule.Parse(cronExpression, new CrontabSchedule.ParseOptions { IncludingSeconds = isExpression6PartFormat });
errorMessage = string.Empty;
isExpressionValid = true;
PrintOccurrences();
}
catch (CrontabException exception)
{
Logger.LogError(exception, exception.Message);
errorMessage = exception.Message;
isExpression6PartFormat = true;
isExpressionValid = false;
}
}
private void CopyLink()
{
var uri = new Uri(NavigationManager.Uri);
var readableCron = CronToReadableQuery(cronExpression);
var uriToCopy = $"{uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped)}?expression={readableCron}";
JSRuntime.InvokeVoidAsync("copyText", uriToCopy);
}
private string CronToReadableQuery(string query) => query.Replace(' ', '+');
private string ReadableQueryToCron(string query) => query.Replace('+', ' ');
}