public ReadOnlyCollection<Region> GetRegionInfo() { var dataQuery = new DataQuery(_profileId, DateTime.Today - TimeSpan.FromDays(30), DateTime.Today, "ga:visitors", "ga:region,ga:operatingSystem", "-ga:visitors"); return _service.Query(dataQuery).Entries .Cast<DataEntry>() .Select( entry => new Region(entry.Dimensions[0].Value) { MacUsers = entry.Dimensions[1].Value == "Macintosh" ? entry.Metrics[0].IntegerValue : 0, WindowsUsers = entry.Dimensions[1].Value == "Windows" ? entry.Metrics[0].IntegerValue : 0, }) .Aggregate(new Dictionary<string, Region>(), (dict, region) => { Region existing; if (!dict.TryGetValue(region.Name, out existing)) { existing = new Region(region.Name); dict[region.Name] = existing; } existing.MacUsers += region.MacUsers; existing.WindowsUsers += region.WindowsUsers; return dict; }, dict => dict.Values.ToList().AsReadOnly()); }
public void SyncRegion() { ProgressUpdateArgs e = new ProgressUpdateArgs(); // Region Table // // Select data from FHGLocal // Select data from Local DB // Compare and update/insert. _UnitOfWorkAPL = new UnitOfWork(_sessionHelper.GetSessionFactory("APL")); _UnitOfWorkFHG = new UnitOfWork(_sessionHelper.GetSessionFactory("FHG")); DataLayer.Repository<int, CusRegion> _fhgRegionRepo = new Repository<int, CusRegion>(_UnitOfWorkFHG.Session); Repository<Guid, Region> _aplRegionRepo = new Repository<Guid, Region>(_UnitOfWorkAPL.Session); Repository<Guid, State> _aplStateRepo = new Repository<Guid, State>(_UnitOfWorkAPL.Session); Repository<Guid, Brand> _aplBrandRepo = new Repository<Guid, Brand>(_UnitOfWorkAPL.Session); var regionAPLDB = _aplRegionRepo.All().ToList(); var regionFHG = _fhgRegionRepo.All().ToList(); int itemCount = 1; // Add Missing data foreach (CusRegion c in regionFHG) { e.TotalWorkItems = regionFHG.Count(); e.CurrentWorkItem = itemCount; e.StatusString = "Syncing Regions - (" + itemCount.ToString() + "/" + e.TotalWorkItems.ToString() + ")"; this.ProgressUpdate(e); bool found = false; foreach (Region s in regionAPLDB) { if (s.Name == c.Regionname) { found = true; break; } } if (!found) { Region r = new Region(); r.Name = c.Regionname; r.Active = !c.Archived.HasValue ? false : !(bool)c.Archived; r.State = _aplStateRepo.FilterBy(s => s.Name == c.CusState.Fullname).FirstOrDefault(); r.Brand = _aplBrandRepo.FilterBy(x => x.FhgBrandId == c.Brandid).FirstOrDefault(); _aplRegionRepo.Add(r); } itemCount++; } // Remove extra data. foreach (Region r in regionAPLDB) { bool found = false; foreach (CusRegion cr in regionFHG) { if (r.Name == cr.Regionname) { found = true; break; } } if (!found) { _aplRegionRepo.Delete(r); } } _UnitOfWorkAPL.Commit(); _UnitOfWorkFHG.Commit(); }