private JArray Search(AnalyticsCachedUser user, string query) { JArray accountsArray = new JArray(); foreach (AnalyticsCachedAccount account in user.Accounts) { // Is the account a direct match? bool match1 = account.IsMatch(query); JArray webPropertiesArray = new JArray(); foreach (AnalyticsCachedWebProperty property in account.WebProperties) { // Is the web property a direct match? bool match2 = match1 || property.IsMatch(query); JArray profilesArray = new JArray(); foreach (AnalyticsCachedProfile profile in property.Profiles) { if (match2 || profile.IsMatch(query)) { profilesArray.Add(JObject.FromObject(profile)); } } if (profilesArray.Count > 0) { webPropertiesArray.Add(new JObject { { "id", property.Id }, { "name", property.Name }, { "profiles", profilesArray } }); } } if (webPropertiesArray.Count > 0) { accountsArray.Add(new JObject { { "id", account.Id }, { "name", account.Name }, { "webProperties", webPropertiesArray } }); } } return(accountsArray); }
public object GetAccounts(string query = null, bool cache = true) { TimeSpan lifetime = TimeSpan.FromMinutes(30); JArray usersArray = new JArray(); foreach (AnalyticsClientConfiguration client in DashboardContext.Current.Configuration.Analytics.Clients) { foreach (AnalyticsUserConfiguration user in client.Users) { string path = DashboardContext.Current.MapPath(DashboardConstants.AnalyticsCachePath + "/Users/" + user.Id + ".json"); AnalyticsCachedUser cachedUser; if (cache && System.IO.File.Exists(path) && System.IO.File.GetLastWriteTimeUtc(path) > DateTime.UtcNow.Add(lifetime)) { // Load the user from the disk cachedUser = AnalyticsCachedUser.Load(path); } else { GoogleService service = GoogleService.CreateFromRefreshToken(client.ClientId, client.ClientSecret, user.RefreshToken); var response1 = service.Analytics.Management.GetAccounts(); var response2 = service.Analytics.Management.GetWebProperties(); var response3 = service.Analytics.Management.GetProfiles(); cachedUser = AnalyticsCachedUser.GetFromResponses(user, response1, response2, response3); Directory.CreateDirectory(Path.GetDirectoryName(path)); cachedUser.Save(path); } // Only include accounts/web properties/profiles that match "query" JArray accountsArray = Search(cachedUser, (query ?? "").ToLower()); usersArray.Add(new JObject { { "id", user.Id }, { "email", user.Email }, { "name", user.Name }, { "accounts", accountsArray } }); } } return(usersArray); }