示例#1
0
        public virtual ActionResult RetrieveEvents(string Format, DateTime? Start = null, DateTime? End = null, int? ModuleId = null, List<int> EventTypeIds = null, int? Take = null)
        {
             var logRetriever = new ReadLogContext()
            {
                Start = Start,
                End = End,
                Module = ModuleId,
                EventTypes = EventTypeIds,
                Take = Take
            };
            var results = logRetriever.Query(Database);

            switch (Format.ToLower())
            {
                case "json":
                    {
                        return Json(results, JsonRequestBehavior.AllowGet);
                    }
                case "csv":
                    {
                        return File(results.ToCsv(), "text/csv", "DiscoLogs.csv");
                    }
                default:
                    {
                        throw new ArgumentException("Unknown Format", "Format");
                    }
            }

        }
        protected override void ExecuteTask()
        {
            using (DiscoDataContext database = new DiscoDataContext())
            {
                Status.UpdateStatus(0, "Importing MAC Addresses", "Querying Logs for Details");
                // Load Logs
                var logRetriever = new ReadLogContext()
                {
                    Module = EnrolmentLog.Current.ModuleId,
                    EventTypes = new List<int>() { (int)EnrolmentLog.EventTypeIds.SessionDeviceInfo }
                };
                var results = logRetriever.Query(database);

                Status.UpdateStatus(50, string.Format("Passing {0} logs", results.Count));

                Dictionary<string, Tuple<string, string>> addresses = new Dictionary<string, Tuple<string, string>>();

                foreach (var result in results.OrderBy(r => r.Timestamp))
                    addresses[((string)result.Arguments[1]).ToLower()] = new Tuple<string, string>((string)result.Arguments[4], (string)result.Arguments[5]);

                Status.UpdateStatus(75, string.Format("Importing {0} details", addresses.Count));

                var devices = database.Devices.Include("DeviceDetails").ToList();

                Tuple<string, string> addressResult;
                foreach (var device in devices)
                {
                    if (addresses.TryGetValue(device.SerialNumber.ToLower(), out addressResult))
                    {
                        if (!string.IsNullOrEmpty(addressResult.Item1))
                            device.DeviceDetails.LanMacAddress(device, addressResult.Item1);
                        if (!string.IsNullOrEmpty(addressResult.Item2))
                            device.DeviceDetails.WLanMacAddress(device, addressResult.Item2);
                    }
                }

                Status.UpdateStatus(90, "Saving to Database");

                database.SaveChanges();

                // Finished - Remove Placeholder File
                var requiredFilePath = RequiredFilePath(database);
                if (System.IO.File.Exists(requiredFilePath))
                    System.IO.File.Delete(requiredFilePath);
            }
        }