Esempio n. 1
0
 private void HistoryTimeline_ItemSwipe(object sender, UWPToolkit.Controls.ItemSwipeEventArgs e)
 {
     if (e.Direction == SwipeListDirection.Buttom)
     {
         // toggle visibility
         CurrentGrid.Visibility = Visibility.Visible;
         HistoryGrid.Visibility = Visibility.Collapsed;
     }
     HistoryTimeline.ResetSwipe();
 }
Esempio n. 2
0
        public async Task <IActionResult> Test([FromRoute] Guid id)
        {
            var webhook = await _context.Webhooks.SingleOrDefaultAsync(m => m.Id == id);

            var timeline = new HistoryTimeline();

            var payload = new NotificationQueueEntry();

            payload.Type    = NotificationQueueEntry.NotificationType.Timeline;
            payload.Payload = (JObject)JToken.FromObject(timeline);

            QueueSyncService.HandleWebhook(webhook, payload);
            return(NoContent());
        }
Esempio n. 3
0
 public HistoryControl()
 {
     Timeline = new HistoryTimeline();
 }
        private void ProcessMachine(IServiceScope scope, ApplicationDbContext context, MachineQueueEntry item)
        {
            var service = scope.ServiceProvider.GetRequiredService <IMachineService>();

            log.Trace("Scope and context created");

            var machines   = new List <Machine>();
            var histories  = new List <Machine.MachineHistoryItem>();
            var timelines  = new List <HistoryTimeline>();
            var health     = new List <HistoryHealth>();
            var trackables = new List <HistoryTrackable>();

            //clients can send up a "create webhook" payload
            var webhooks = new List <Webhook>();

            log.Trace("Beginning item processing...");

            log.Trace($"Attempting find for {item.Machine.Id}");
            Machine machine = null;

            if (item.Machine.Id != Guid.Empty)
            {
                machine = context.Machines.FirstOrDefault(o => o.Id == item.Machine.Id);
            }

            if (machine == null)
            {
                log.Trace("Machine not found by id");
                if (!string.IsNullOrEmpty(item.Machine.Name))
                {
                    log.Trace($"Searching for machine by name {item.Machine.Name}");
                    machine = context.Machines.FirstOrDefault(o => o.Name == item.Machine.Name);
                }

                if (machine == null)
                {
                    log.Trace($"Machine is still null, so attempting another create");
                    if (item.Machine.Id == Guid.Empty)
                    {
                        item.Machine.Id = Guid.NewGuid();
                    }
                    item.Machine.LastReportedUtc = DateTime.UtcNow;
                    item.Machine.StatusUp        = Machine.UpDownStatus.Up;
                    item.Machine.History.Add(new Machine.MachineHistoryItem
                    {
                        Type = Machine.MachineHistoryItem.HistoryType.Created
                    });
                    var x = service.CreateAsync(item.Machine, new CancellationToken()).Result;
                    machine = item.Machine;
                }
            }

            machine.LastReportedUtc = DateTime.UtcNow;
            machine.StatusUp        = Machine.UpDownStatus.Up;
            machines.Add(machine);

            histories.Add(new Machine.MachineHistoryItem
            {
                MachineId  = machine.Id,
                Type       = item.HistoryType,
                CreatedUtc = DateTime.UtcNow
            });

            log.Trace($"Proc history type: {item.HistoryType}");

            if (item.HistoryType == Machine.MachineHistoryItem.HistoryType.PostedResults)
            {
                if (item.LogDump.Log.Length > 0)
                {
                    log.Trace(item.LogDump.Log);
                }

                var lines = item.LogDump.Log.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                foreach (var line in lines)
                {
                    try
                    {
                        var array = line.Split(Convert.ToChar("|"));

                        var     isReady = false;
                        var     time    = DateTime.UtcNow;
                        string  type    = null;
                        dynamic data    = null;

                        var arrayPartCount = array.GetUpperBound(0);

                        //new with time
                        if (arrayPartCount >= 2)
                        {
                            isReady = true;
                            type    = array[0];
                            time    = Convert.ToDateTime(array[1]);
                            data    = JsonConvert.DeserializeObject(array[2]);
                        }

                        //old no time [Obsolete]
                        else if (arrayPartCount == 1)
                        {
                            isReady = true;
                            type    = array[0];
                            data    = JsonConvert.DeserializeObject(array[1]);
                        }

                        log.Trace($"Processing {type} with {data}");

                        if (isReady)
                        {
                            switch (type)
                            {
                            case "TIMELINE":
                                var timeline = new HistoryTimeline
                                {
                                    Command    = data.Command,
                                    MachineId  = machine.Id,
                                    Handler    = data.Handler,
                                    CommandArg = data.CommandArg,
                                    CreatedUtc = time
                                };

                                if (data.Result != null)
                                {
                                    timeline.Result = data.Result;
                                }

                                timelines.Add(timeline);

                                //add notification
                                this.Queue.Enqueue(
                                    new QueueEntry
                                {
                                    Type    = QueueEntry.Types.Notification,
                                    Payload =
                                        new NotificationQueueEntry
                                    {
                                        Type    = NotificationQueueEntry.NotificationType.Timeline,
                                        Payload = (JObject)JToken.FromObject(timeline)
                                    }
                                });

                                if (data["TrackableId"] != null)
                                {
                                    var trackable = new HistoryTrackable
                                    {
                                        TrackableId = data["TrackableId"],
                                        Command     = data.Command,
                                        MachineId   = machine.Id,
                                        Handler     = data.Handler,
                                        CommandArg  = data.CommandArg,
                                        CreatedUtc  = time
                                    };

                                    if (data.Result != null)
                                    {
                                        trackable.Result = data.Result;
                                    }

                                    trackables.Add(trackable);
                                }
                                break;

                            case "HEALTH":
                                var users  = string.Join(",", data.LoggedOnUsers);
                                var errors = string.Join(",", data.Errors);
                                health.Add(new HistoryHealth
                                {
                                    Permissions   = data.Permssions,
                                    MachineId     = machine.Id,
                                    LoggedOnUsers = users,
                                    Internet      = data.Internet,
                                    ExecutionTime = data.ExecutionTime,
                                    Errors        = errors,
                                    Stats         = data.Stats.ToString(),
                                    CreatedUtc    = time
                                });
                                break;

                            case "WEBHOOKCREATE":
                                try
                                {
                                    log.Info($"processing webhookcreate...");
                                    var hook = JsonConvert.DeserializeObject <WebhookViewModel>(data.ToString());
                                    var h    = new Webhook(hook);
                                    webhooks.Add(h);
                                }
                                catch (Exception e)
                                {
                                    log.Info($"serializing hook failed: {data} : {e})");
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        log.Trace($"Bad line: {e} - {line}");
                    }
                }
            } //endif posted results
            var _ = this.Queue.DequeueAsync(new CancellationToken()).Result;

            if (machines.Count > 0)
            {
                context.Machines.UpdateRange(machines);
                try
                {
                    var i = context.SaveChanges();
                    if (i > 0)
                    {
                        log.Info(
                            $"Queue: {i} (machines: {machines.Count()}");
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }
            if (trackables.Count > 0)
            {
                context.HistoryTrackables.AddRange(trackables);
                try
                {
                    var i = context.SaveChanges();
                    if (i > 0)
                    {
                        log.Info(
                            $"Queue: {i} (Trackables: {trackables.Count()})");
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }
            if (health.Count > 0)
            {
                context.HistoryHealth.AddRange(health);
                try
                {
                    var i = context.SaveChanges();
                    if (i > 0)
                    {
                        log.Info(
                            $"Queue: {i} (Health: {health.Count()})");
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }
            if (timelines.Count > 0)
            {
                context.HistoryTimeline.AddRange(timelines);
                try
                {
                    var i = context.SaveChanges();
                    if (i > 0)
                    {
                        log.Info(
                            $"Queue: {i} (Timeline: {timelines.Count()})");
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }
            if (histories.Count > 0)
            {
                context.HistoryMachine.AddRange(histories);
                try
                {
                    var i = context.SaveChanges();
                    if (i > 0)
                    {
                        log.Info(
                            $"Queue: {i} (History: {histories.Count()}");
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }

            if (webhooks.Count > 0)
            {
                context.Webhooks.AddRange(webhooks);
                try
                {
                    var i = context.SaveChanges();
                    if (i > 0)
                    {
                        log.Info(
                            $"Queue: {i} (Webhooks: {webhooks.Count()}");
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }
        }