Last active
February 21, 2016 22:54
-
-
Save omerfarukz/3ef2004235f715d7a7ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Concurrent; | |
using System.IO; | |
using System.Xml.Serialization; | |
using System.Linq; | |
namespace ometrikSample | |
{ | |
/// </summary> | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
MetricManager mm = new MetricManager(); | |
mm.Track("Initializing"); | |
System.Threading.Thread.Sleep(500); | |
mm.Track("Initialized", TimeSpan.FromMilliseconds(500)); | |
var jobsCount = new Random().Next(10, 50); | |
mm.Track("CurrentJobCount", jobsCount); | |
for (int i = 0; i < jobsCount; i++) | |
{ | |
mm.Track(i.ToString()); | |
System.Threading.Thread.Sleep(100); | |
} | |
Console.WriteLine("Finished"); | |
Console.ReadKey(); | |
} | |
} | |
public interface IMetric | |
{ | |
} | |
[Serializable] | |
public class MetricBase : IMetric | |
{ | |
public Guid Id | |
{ | |
get; | |
set; | |
} | |
public DateTime DateCreated | |
{ | |
get; | |
set; | |
} | |
public string Name | |
{ | |
get; | |
set; | |
} | |
public int? Count | |
{ | |
get; | |
set; | |
} | |
public TimeSpan? Elapsed | |
{ | |
get; | |
set; | |
} | |
public MetricBase() | |
{ | |
DateCreated = DateTime.UtcNow; | |
} | |
} | |
public class MetricManager | |
{ | |
private static MetricMq _metricQueue; | |
public MetricManager() | |
{ | |
_metricQueue = new MetricMq(); | |
} | |
public void Track(string name) | |
{ | |
Track(name, null, null); | |
} | |
public void Track(string name, int count) | |
{ | |
Track(name, count, null); | |
} | |
public void Track(string name, TimeSpan elapsed) | |
{ | |
Track(name, null, elapsed); | |
} | |
public void Track(string name, int? count, TimeSpan? elapsed) | |
{ | |
var newMetric = new MetricBase(); | |
newMetric.Id = Guid.NewGuid(); | |
newMetric.Name = name; | |
newMetric.Elapsed = elapsed; | |
Track(newMetric); | |
} | |
public void Track(MetricBase metric) | |
{ | |
_metricQueue.Enqueue(metric); | |
Console.WriteLine("New metric '{0}' is in queue", metric.Name); | |
} | |
} | |
public class MetricMq | |
{ | |
private string _persistentFolder; | |
private System.Timers.Timer _transferTimer; | |
private XmlSerializer _serializer; | |
public MetricMq() | |
{ | |
_persistentFolder = "metricsQueue/"; | |
_serializer = new XmlSerializer(typeof(MetricBase)); | |
_transferTimer = new System.Timers.Timer(1000); | |
_transferTimer.Elapsed += OnTransferTimerTick; | |
_transferTimer.Enabled = true; | |
} | |
public void Enqueue(MetricBase metric) | |
{ | |
using (var fs = new FileStream(GetMetricWritePath(metric), FileMode.CreateNew)) | |
{ | |
_serializer.Serialize(fs, metric); | |
} | |
} | |
private void OnTransferTimerTick(object sender, System.Timers.ElapsedEventArgs e) | |
{ | |
TransferMetrics(); | |
} | |
void TransferMetrics() | |
{ | |
int batchCount = 3; | |
int currentProcessedCount = 0; | |
var directory = new DirectoryInfo(_persistentFolder); | |
if (directory.Exists) | |
{ | |
var filesEnumerate = directory.EnumerateFiles("*.xml").Take(batchCount).ToArray(); | |
for (int i = 0; i < filesEnumerate.Length; i++) | |
{ | |
if (currentProcessedCount >= batchCount) | |
break; | |
// begin tran | |
// deserialize | |
Console.WriteLine("{0} TRANSFERED", filesEnumerate[i].Name); | |
File.Delete(filesEnumerate[i].FullName); | |
// commit tran | |
++currentProcessedCount; | |
} | |
} | |
Console.WriteLine("{0} items transfered", currentProcessedCount); | |
} | |
private string GetMetricWritePath(MetricBase metric) | |
{ | |
return Path.Combine(_persistentFolder, string.Concat(metric.Id.ToString(), ".xml")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment