public IList<QueueWithTopEnqueuedJobsDto> Queues()
        {
            var s = _queueProviders
                        .Select(qProvider => qProvider.GetJobQueueMonitoringApi(ConnectionString))
                        .SelectMany(x => x.GetQueues())
                        .ToList();


            const int MaxFirstJobs = 5;

            var result = new List<QueueWithTopEnqueuedJobsDto>();
            foreach (var queue in s)
            {

                var monitoring = _queueProviders.GetProvider(queue).GetJobQueueMonitoringApi(ConnectionString);


                var qw = new QueueWithTopEnqueuedJobsDto()
                {
                    Fetched = 0,
                    FirstJobs = EnqueuedJobs(queue, 0, MaxFirstJobs),
                    Length = monitoring.GetEnqueuedAndFetchedCount(queue).EnqueuedCount.Value,
                    Name = queue
                };


                result.Add(qw);


            }

            return result;
            //return new List<QueueWithTopEnqueuedJobsDto>();
        }
        public IList<ServerDto> Servers()
        {

            Logger.Trace(DateTime.Now.ToLongTimeString() + " enter Servers()");


            return UsingTable<Entities.Server, IList<ServerDto>>(servers =>
            {

                var result = new List<ServerDto>();

                foreach (var server in servers)
                {
                    var data = JobHelper.FromJson<ServerData>(server.Data);

                    result.Add(new ServerDto
                    {
                        Name = server.Id,
                        Heartbeat = server.LastHeartbeat,
                        Queues = data.Queues,
                        StartedAt = data.StartedAt.HasValue ? data.StartedAt.Value : DateTime.MinValue,
                        WorkersCount = data.WorkerCount
                    });

                }

                Debug.WriteLine("MySqlMonitoringApi  Servers() returning " + result.Count);

                return result.ToList();

            });

        }
        public JobList<SucceededJobDto> SucceededJobs(int @from, int count)
        {
            return UsingDatabase(db =>
            {

                var jobs = db.GetTable<Entities.Job>()
                    .Where(j=>j.StateName=="Succeeded")
                    .OrderByDescending(j=>j.Id)
                    .Skip(from)
                    .Take(count);

                var list = new List<KeyValuePair<string, SucceededJobDto>>();

                foreach (var sqlJob in jobs)
                {

                    var stateData = sqlJob.ToStateData().Data;

                    var s = new SucceededJobDto()
                    {
                        Job = sqlJob.ToCommonJob(),
                        InSucceededState = true,
                        Result = stateData.ContainsKey("Result") ? stateData["Result"] : null,
                        TotalDuration = stateData.ContainsKey("PerformanceDuration") && stateData.ContainsKey("Latency")
                            ? (long?) long.Parse(stateData["PerformanceDuration"]) +
                              (long?) long.Parse(stateData["Latency"])
                            : null,
                        SucceededAt = sqlJob.GetNullableDateTimeStateDataValue("SucceededAt")
                    };

                    list.Add(new KeyValuePair<string, SucceededJobDto>(
                        sqlJob.Id.ToString(CultureInfo.InvariantCulture), s));

                }

                return new JobList<SucceededJobDto>(list);

            });

        }
        public JobList<EnqueuedJobDto> EnqueuedJobs(string queue, int @from, int perPage)
        {

            var jobQueues = UsingTable<Entities.JobQueue, IEnumerable<Entities.JobQueue>>(
                jQs => jQs
                    .InQueue(queue)
                    .OrderBy(jQ => jQ.Id)
                    .Skip(from)
                    .Take(perPage)
                    .ToArray());


            var r = new List<KeyValuePair<string, EnqueuedJobDto>>();
            foreach (var jQ in jobQueues)
            {
                var job = UsingTable<Entities.Job, Entities.Job>(jobs => jobs.Single(j => j.Id == jQ.JobId));

                var e = new EnqueuedJobDto()
                {
                    EnqueuedAt =
                        job.IsInState(EnqueuedState.StateName)
                            ? job.GetNullableDateTimeStateDataValue("EnqueuedAt")
                            : null,
                    InEnqueuedState = job.IsInState(EnqueuedState.StateName),
                    Job = job.ToJobData().Job,
                    State = job.StateName
                };

                var pair = new KeyValuePair<string, EnqueuedJobDto>(job.Id.ToString(CultureInfo.InvariantCulture), e);
                r.Add(pair);


            }
        
            return new JobList<EnqueuedJobDto>(r);
            //return new JobList<EnqueuedJobDto>(new List<KeyValuePair<string, EnqueuedJobDto>>());
        }