|
| 1 | +using System; |
| 2 | +using System.ComponentModel.Composition; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using GitHub.Models; |
| 9 | +using Task = System.Threading.Tasks.Task; |
| 10 | + |
| 11 | +namespace GitHub.Services |
| 12 | +{ |
| 13 | + [Export(typeof(IUsageService))] |
| 14 | + public class UsageService : IUsageService |
| 15 | + { |
| 16 | + const string StoreFileName = "ghfvs.usage"; |
| 17 | + static readonly Calendar cal = CultureInfo.InvariantCulture.Calendar; |
| 18 | + readonly IGitHubServiceProvider serviceProvider; |
| 19 | + string storePath; |
| 20 | + |
| 21 | + [ImportingConstructor] |
| 22 | + public UsageService(IGitHubServiceProvider serviceProvider) |
| 23 | + { |
| 24 | + this.serviceProvider = serviceProvider; |
| 25 | + } |
| 26 | + |
| 27 | + public bool IsSameDay(DateTimeOffset lastUpdated) |
| 28 | + { |
| 29 | + return lastUpdated.Date == DateTimeOffset.Now.Date; |
| 30 | + } |
| 31 | + |
| 32 | + public bool IsSameWeek(DateTimeOffset lastUpdated) |
| 33 | + { |
| 34 | + return GetIso8601WeekOfYear(lastUpdated) == GetIso8601WeekOfYear(DateTimeOffset.Now); |
| 35 | + } |
| 36 | + |
| 37 | + public bool IsSameMonth(DateTimeOffset lastUpdated) |
| 38 | + { |
| 39 | + return lastUpdated.Month == DateTimeOffset.Now.Month; |
| 40 | + } |
| 41 | + |
| 42 | + public IDisposable StartTimer(Func<Task> callback, TimeSpan dueTime, TimeSpan period) |
| 43 | + { |
| 44 | + return new Timer( |
| 45 | + async _ => |
| 46 | + { |
| 47 | + try { await callback(); } |
| 48 | + catch { /* log.Warn("Failed submitting usage data", ex); */ } |
| 49 | + }, |
| 50 | + null, |
| 51 | + dueTime, |
| 52 | + period); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task<UsageData> ReadLocalData() |
| 56 | + { |
| 57 | + Initialize(); |
| 58 | + |
| 59 | + var json = File.Exists(storePath) ? await ReadAllTextAsync(storePath) : null; |
| 60 | + |
| 61 | + try |
| 62 | + { |
| 63 | + return json != null ? |
| 64 | + SimpleJson.DeserializeObject<UsageData>(json) : |
| 65 | + new UsageData { Model = new UsageModel() }; |
| 66 | + } |
| 67 | + catch |
| 68 | + { |
| 69 | + return new UsageData { Model = new UsageModel() }; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public async Task WriteLocalData(UsageData data) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + Directory.CreateDirectory(Path.GetDirectoryName(storePath)); |
| 78 | + var json = SimpleJson.SerializeObject(data); |
| 79 | + await WriteAllTextAsync(storePath, json); |
| 80 | + } |
| 81 | + catch |
| 82 | + { |
| 83 | + // log.Warn("Failed to write usage data", ex); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void Initialize() |
| 88 | + { |
| 89 | + if (storePath == null) |
| 90 | + { |
| 91 | + var program = serviceProvider.GetService<IProgram>(); |
| 92 | + storePath = Path.Combine( |
| 93 | + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| 94 | + program.ApplicationName, |
| 95 | + StoreFileName); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + async Task<string> ReadAllTextAsync(string path) |
| 100 | + { |
| 101 | + using (var s = File.OpenRead(path)) |
| 102 | + using (var r = new StreamReader(s, Encoding.UTF8)) |
| 103 | + { |
| 104 | + return await r.ReadToEndAsync(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + async Task WriteAllTextAsync(string path, string text) |
| 109 | + { |
| 110 | + using (var s = File.OpenWrite(path)) |
| 111 | + using (var w = new StreamWriter(s, Encoding.UTF8)) |
| 112 | + { |
| 113 | + await w.WriteAsync(text); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // http://blogs.msdn.com/b/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx |
| 118 | + static int GetIso8601WeekOfYear(DateTimeOffset time) |
| 119 | + { |
| 120 | + // Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll |
| 121 | + // be the same week# as whatever Thursday, Friday or Saturday are, |
| 122 | + // and we always get those right |
| 123 | + DayOfWeek day = cal.GetDayOfWeek(time.UtcDateTime); |
| 124 | + if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday) |
| 125 | + { |
| 126 | + time = time.AddDays(3); |
| 127 | + } |
| 128 | + |
| 129 | + // Return the week of our adjusted day |
| 130 | + return cal.GetWeekOfYear(time.UtcDateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments