public async override Task Run() { string groupId = ActivityUI.ExistingStatGroupList.Value; if (ActivityUI.UseNewStatXGroupOption.Selected) { //create at first new StatX group var statGroup = await _statXIntegration.CreateGroup(StatXUtilities.GetStatXAuthToken(AuthorizationToken), ActivityUI.NewStatXGroupName.Value); groupId = statGroup.Id; } var statProperties = new List <KeyValueDTO>(); if (ActivityUI.AvailableStatProperties != null && ActivityUI.AvailableStatProperties.Any()) { statProperties.AddRange(ActivityUI.AvailableStatProperties.Select(x => new KeyValueDTO() { Key = x.Name, Value = x.TextValue }).ToList()); } var statItemsList = new List <KeyValueDTO>(); if (ActivityUI.AvailableStatItemsList != null && ActivityUI.AvailableStatItemsList.Any()) { statItemsList.AddRange(JsonConvert.DeserializeObject <List <KeyValueDTO> >(ActivityUI.AvailableStatItemsList.FirstOrDefault().Value)); } var statDTO = StatXUtilities.CreateStatFromDynamicStatProperties(ActivityUI.StatTypesList.Value, statProperties, statItemsList); await _statXIntegration.CreateStat(StatXUtilities.GetStatXAuthToken(AuthorizationToken), groupId, statDTO); Success(); }
public async Task <PollingDataDTO> Poll(PollingDataDTO pollingData) { if (string.IsNullOrEmpty(pollingData.AdditionalConfigAttributes)) { pollingData.Result = false; return(pollingData); } var attributesObject = JObject.Parse(pollingData.AdditionalConfigAttributes); var groupId = attributesObject["GroupId"]?.ToString(); var statId = attributesObject["StatId"]?.ToString(); if (string.IsNullOrEmpty(groupId) || string.IsNullOrEmpty(statId)) { pollingData.Result = false; return(pollingData); } if (string.IsNullOrEmpty(pollingData.Payload)) { //polling is called for the first time var latestStatWithValues = await GetLatestStatItem(pollingData.AuthToken, groupId, statId); pollingData.Payload = JsonConvert.SerializeObject(latestStatWithValues); } else { var statXCM = JsonConvert.DeserializeObject <StatXItemCM>(pollingData.Payload); var latestStatWithValues = await GetLatestStatItem(pollingData.AuthToken, groupId, statId); //check value by value to see if a difference exist. if (StatXUtilities.CompareStatsForValueChanges(statXCM, latestStatWithValues)) { var eventReportContent = new EventReportCM { EventNames = "StatXValueChange_" + statId.Substring(0, 18), EventPayload = new CrateStorage(Crate.FromContent("StatXValueChange", latestStatWithValues)), Manufacturer = "StatX", ExternalAccountId = pollingData.ExternalAccountId }; pollingData.Payload = JsonConvert.SerializeObject(latestStatWithValues); await _hubReporter.Broadcast(Crate.FromContent("Standard Event Report", eventReportContent)); } } pollingData.Result = true; return(pollingData); }
public override async Task Run() { var statValues = ActivityUI.StatValues.Select(x => new { x.Name, Value = x.TextValue }).ToDictionary(x => x.Name, x => x.Value); if (string.IsNullOrEmpty(ActivityUI.ExistingGroupsList.Value)) { throw new ActivityExecutionException("Update Stat activity run failed!. Activity doesn't have selected Group."); } if (string.IsNullOrEmpty(ActivityUI.ExistingGroupStats.Value)) { throw new ActivityExecutionException("Update Stat activity run failed!. Activity doesn't have selected Stat."); } await _statXIntegration.UpdateStatValue(StatXUtilities.GetStatXAuthToken(AuthorizationToken), ActivityUI.ExistingGroupsList.Value, ActivityUI.ExistingGroupStats.Value, statValues, ActivityUI.StatTitle.TextValue, ActivityUI.StatNotes.TextValue); Success(); }
public override async Task FollowUp() { if (!string.IsNullOrEmpty(ActivityUI.StatTypesList.Value)) { var previousGroup = SelectedStatType; if (string.IsNullOrEmpty(previousGroup) || !string.Equals(previousGroup, ActivityUI.StatTypesList.Value)) { var propertyInfos = StatXUtilities.GetStatTypeProperties(ActivityUI.StatTypesList.Value); ActivityUI.ClearDynamicFields(); foreach (var property in propertyInfos) { var t = property.PropertyType; if (t.IsGenericType && typeof(IList <>).IsAssignableFrom(t.GetGenericTypeDefinition()) || t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList <>))) { //render corresponding FieldList control for properties that are collections ActivityUI.AvailableStatItemsList.Add(CreateFieldListBasedOnStatType()); } else { ActivityUI.AvailableStatProperties.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(property.Name, property.Name, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } SelectedStatType = ActivityUI.StatTypesList.Value; } else { ActivityUI.ClearDynamicFields(); SelectedStatType = string.Empty; } SelectedStatGroup = ActivityUI.ExistingStatGroupList.Value; ActivityUI.ExistingStatGroupList.ListItems = (await _statXIntegration.GetGroups(StatXUtilities.GetStatXAuthToken(AuthorizationToken))) .Select(x => new ListItem { Key = x.Name, Value = x.Id }).ToList(); ActivityUI.ExistingStatGroupList.Value = SelectedStatGroup; }
public override async Task FollowUp() { if (!string.IsNullOrEmpty(ActivityUI.ExistingGroupsList.Value)) { var previousGroup = SelectedGroup; if (string.IsNullOrEmpty(previousGroup) || !string.Equals(previousGroup, ActivityUI.ExistingGroupsList.Value)) { var stats = await _statXIntegration.GetStatsForGroup(StatXUtilities.GetStatXAuthToken(AuthorizationToken), ActivityUI.ExistingGroupsList.Value); if (stats.Any(x => string.IsNullOrEmpty(x.Title))) { StatXUtilities.AddAdvisoryMessage(Storage); } else { if (Storage.CratesOfType <AdvisoryMessagesCM>().FirstOrDefault() != null) { ActivityUI.ExistingGroupStats.ListItems = stats.Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); } Storage.RemoveByLabel("Advisories"); } ActivityUI.ExistingGroupStats.ListItems = stats .Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); var firstStat = stats.FirstOrDefault(); if (firstStat != null) { ActivityUI.ExistingGroupStats.SelectByValue(firstStat.Id); ActivityUI.StatTitle.TextValue = firstStat.Title; ActivityUI.StatNotes.TextValue = firstStat.Notes; var statDTO = firstStat as GeneralStatWithItemsDTO; if (statDTO != null) { if (statDTO.VisualType == StatTypes.PickList) { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser("Current Index", "CurrentIndex", requestUpstream: true, groupLabelText: "Available Stat Properties")); } else { foreach (var item in statDTO.Items) { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(item.Name, item.Name, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } else { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(string.IsNullOrEmpty(firstStat.Title) ? firstStat.Id : firstStat.Title, string.IsNullOrEmpty(firstStat.Title) ? firstStat.Id : firstStat.Title, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } SelectedGroup = ActivityUI.ExistingGroupsList.Value; //refresh statx groups ActivityUI.ExistingGroupsList.ListItems = (await _statXIntegration.GetGroups(StatXUtilities.GetStatXAuthToken(AuthorizationToken))) .Select(x => new ListItem { Key = x.Name, Value = x.Id }).ToList(); ActivityUI.ExistingGroupsList.Value = SelectedGroup; } else { ActivityUI.StatTitle.Value = string.Empty; ActivityUI.StatNotes.Value = string.Empty; ActivityUI.ExistingGroupStats.ListItems.Clear(); ActivityUI.ExistingGroupStats.selectedKey = string.Empty; ActivityUI.ExistingGroupStats.Value = string.Empty; SelectedGroup = string.Empty; } if (!string.IsNullOrEmpty(ActivityUI.ExistingGroupStats.Value)) { var previousStat = SelectedStat; var stats = await _statXIntegration.GetStatsForGroup(StatXUtilities.GetStatXAuthToken(AuthorizationToken), ActivityUI.ExistingGroupsList.Value); if (stats.Any(x => string.IsNullOrEmpty(x.Title))) { StatXUtilities.AddAdvisoryMessage(Storage); } else { if (Storage.CratesOfType <AdvisoryMessagesCM>().FirstOrDefault() != null) { ActivityUI.ExistingGroupStats.ListItems = stats.Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); } Storage.RemoveByLabel("Advisories"); } if (string.IsNullOrEmpty(previousStat) || !string.Equals(previousStat, ActivityUI.ExistingGroupStats.Value)) { var currentStat = stats.FirstOrDefault(x => x.Id == ActivityUI.ExistingGroupStats.Value); if (currentStat != null) { ActivityUI.ClearDynamicFields(); var statDTO = currentStat as GeneralStatWithItemsDTO; if (statDTO != null && statDTO.Items.Any()) { if (statDTO.VisualType == StatTypes.PickList) { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser("Current Index", "CurrentIndex", requestUpstream: true, groupLabelText: "Available Stat Properties")); } else { foreach (var item in statDTO.Items) { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(item.Name, item.Name, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } else { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(string.IsNullOrEmpty(currentStat.Title) ? currentStat.Id : currentStat.Title, string.IsNullOrEmpty(currentStat.Title) ? currentStat.Id : currentStat.Title, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } #region Refresh Stat Items to Track for Changes in app SelectedStat = ActivityUI.ExistingGroupStats.Value; ActivityUI.ExistingGroupStats.ListItems = stats.Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); ActivityUI.ExistingGroupStats.Value = SelectedStat; var statToCheck = stats.FirstOrDefault(x => x.Id == SelectedStat); //check for changes in statValue Array if (statToCheck != null) { ActivityUI.ExistingGroupStats.selectedKey = string.IsNullOrEmpty(statToCheck.Title) ? statToCheck.Id : statToCheck.Title; var statDTO = statToCheck as GeneralStatWithItemsDTO; if (statDTO != null && statDTO.Items.Any()) { var oldStatNames = ActivityUI.StatValues.Select(x => x.Name).ToList(); var newStatNames = new List <string>(); if (statDTO.VisualType != StatTypes.PickList) { newStatNames = statDTO.Items.Select(x => x.Name).ToList(); //recreate new items var newItems = newStatNames.Where(x => !oldStatNames.Contains(x)).ToList(); var oldItemsToDelete = oldStatNames.Where(x => !newStatNames.Contains(x)).ToList(); foreach (var item in oldItemsToDelete.ToList()) { var itemToDelete = ActivityUI.StatValues.FirstOrDefault(x => x.Name == item); if (itemToDelete != null) { ActivityUI.StatValues.Remove(itemToDelete); } } foreach (var item in newItems) { ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(item, item, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } else { var currentTextSource = ActivityUI.StatValues.FirstOrDefault()?.Name; if (currentTextSource != (string.IsNullOrEmpty(statToCheck.Title) ? statToCheck.Id : statToCheck.Title)) { ActivityUI.StatValues?.Clear(); ActivityUI.StatValues.Add(UiBuilder.CreateSpecificOrUpstreamValueChooser(string.IsNullOrEmpty(statToCheck.Title) ? statToCheck.Id : statToCheck.Title, string.IsNullOrEmpty(statToCheck.Title) ? statToCheck.Id : statToCheck.Title, requestUpstream: true, groupLabelText: "Available Stat Properties")); } } } #endregion } else { ActivityUI.StatTitle.Value = string.Empty; ActivityUI.StatNotes.Value = string.Empty; ActivityUI.ClearDynamicFields(); ActivityUI.ExistingGroupStats.ListItems.Clear(); ActivityUI.ExistingGroupStats.selectedKey = string.Empty; ActivityUI.ExistingGroupStats.Value = string.Empty; SelectedStat = string.Empty; } }
public override async Task Initialize() { ActivityUI.ExistingGroupsList.ListItems = (await _statXIntegration.GetGroups(StatXUtilities.GetStatXAuthToken(AuthorizationToken))) .Select(x => new ListItem { Key = x.Name, Value = x.Id }).ToList(); }
public override async Task FollowUp() { if (!string.IsNullOrEmpty(ActivityUI.ExistingGroupsList.Value)) { var previousGroup = SelectedGroup; if (string.IsNullOrEmpty(previousGroup) || !string.Equals(previousGroup, ActivityUI.ExistingGroupsList.Value)) { var stats = await _statXIntegration.GetStatsForGroup(StatXUtilities.GetStatXAuthToken(AuthorizationToken), ActivityUI.ExistingGroupsList.Value); if (stats.Any(x => string.IsNullOrEmpty(x.Title))) { StatXUtilities.AddAdvisoryMessage(Storage); } else { if (Storage.CratesOfType <AdvisoryMessagesCM>().FirstOrDefault() != null) { ActivityUI.ExistingGroupStats.ListItems = stats.Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); } Storage.RemoveByLabel("Advisories"); } ActivityUI.ExistingGroupStats.ListItems = stats .Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); var firstStat = stats.FirstOrDefault(); if (firstStat != null) { ActivityUI.ExistingGroupStats.SelectByValue(firstStat.Id); } CrateSignaller.ClearAvailableCrates(); CrateSignaller.MarkAvailableAtRuntime <StandardPayloadDataCM>(RunTimeCrateLabel) .AddFields(CreateStatValueFields(StatXUtilities.MapToStatItemCrateManifest(firstStat))); } SelectedGroup = ActivityUI.ExistingGroupsList.Value; ActivityUI.ExistingGroupsList.ListItems = (await _statXIntegration.GetGroups(StatXUtilities.GetStatXAuthToken(AuthorizationToken))) .Select(x => new ListItem { Key = x.Name, Value = x.Id }).ToList(); ActivityUI.ExistingGroupsList.Value = SelectedGroup; } else { ActivityUI.ExistingGroupStats.ListItems.Clear(); ActivityUI.ExistingGroupStats.selectedKey = string.Empty; ActivityUI.ExistingGroupStats.Value = string.Empty; SelectedGroup = string.Empty; } if (!string.IsNullOrEmpty(ActivityUI.ExistingGroupStats.Value)) { var previousStat = SelectedStat; var stats = await _statXIntegration.GetStatsForGroup(StatXUtilities.GetStatXAuthToken(AuthorizationToken), ActivityUI.ExistingGroupsList.Value); if (stats.Any(x => string.IsNullOrEmpty(x.Title))) { StatXUtilities.AddAdvisoryMessage(Storage); } else { if (Storage.CratesOfType <AdvisoryMessagesCM>().FirstOrDefault() != null) { ActivityUI.ExistingGroupStats.ListItems = stats.Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); } Storage.RemoveByLabel("Advisories"); } if (string.IsNullOrEmpty(previousStat) || !string.Equals(previousStat, ActivityUI.ExistingGroupStats.Value)) { var currentStat = stats.FirstOrDefault(x => x.Id == ActivityUI.ExistingGroupStats.Value); if (currentStat != null) { CrateSignaller.ClearAvailableCrates(); CrateSignaller.MarkAvailableAtRuntime <StandardPayloadDataCM>(RunTimeCrateLabel).AddFields(CreateStatValueFields(StatXUtilities.MapToStatItemCrateManifest(currentStat))); } } SelectedStat = ActivityUI.ExistingGroupStats.Value; ActivityUI.ExistingGroupStats.ListItems = stats.Select(x => new ListItem { Key = string.IsNullOrEmpty(x.Title) ? x.Id : x.Title, Value = x.Id }).ToList(); ActivityUI.ExistingGroupStats.Value = SelectedStat; EventSubscriptions.Subscriptions?.Clear(); EventSubscriptions.Manufacturer = "StatX"; EventSubscriptions.Add("StatXValueChange_" + SelectedStat.Substring(0, 18)); } else { CrateSignaller.ClearAvailableCrates(); ActivityUI.ExistingGroupStats.ListItems.Clear(); ActivityUI.ExistingGroupStats.selectedKey = string.Empty; ActivityUI.ExistingGroupStats.Value = string.Empty; SelectedStat = string.Empty; } }
public override async Task Initialize() { CrateSignaller.MarkAvailableAtRuntime <StandardPayloadDataCM>(RunTimeCrateLabel); ActivityUI.ExistingGroupsList.ListItems = (await _statXIntegration.GetGroups(StatXUtilities.GetStatXAuthToken(AuthorizationToken))) .Select(x => new ListItem { Key = x.Name, Value = x.Id }).ToList(); }
public async Task UpdateStatValue(StatXAuthDTO statXAuthDTO, string groupId, string statId, Dictionary <string, string> statValues, string title, string notes) { try { var uri = new Uri($"{StatXBaseApiUrl}/groups/{groupId}/stats/{statId}"); //get the stat and look for value var currentStat = await GetStat(statXAuthDTO, groupId, statId); if (currentStat != null) { if (string.IsNullOrEmpty(title)) { title = currentStat.Title; } string response = string.Empty; //process stat types that can update multiple items if (currentStat.VisualType == StatTypes.CheckList || currentStat.VisualType == StatTypes.HorizontalBars) { var statDTO = currentStat as GeneralStatWithItemsDTO; if (statDTO != null) { statDTO.LastUpdatedDateTime = DateTime.UtcNow; statDTO.NotesLastUpdatedDateTime = DateTime.UtcNow; statDTO.Title = title; statDTO.Notes = notes; var tempItems = new List <StatItemValueDTO>(); tempItems.AddRange(statDTO.Items); statDTO.Items.Clear(); if (currentStat.VisualType == StatTypes.CheckList) { statDTO.DynamicJsonIgnoreProperties = new string[] { "visualType", "value", "currentIndex" }; statDTO.Items.AddRange(statValues.Select(x => new StatItemValueDTO() { Name = x.Key, Checked = string.IsNullOrEmpty(x.Value) ? tempItems.FirstOrDefault(l => l.Name == x.Key).Checked : StatXUtilities.ConvertChecklistItemValue(x.Value) }).ToList()); } else { statDTO.DynamicJsonIgnoreProperties = new string[] { "visualType", "checked", "currentIndex" }; statDTO.Items.AddRange(statValues.Select(x => new StatItemValueDTO() { Name = x.Key, Value = string.IsNullOrEmpty(x.Value) ? tempItems.FirstOrDefault(l => l.Name == x.Key).Value : x.Value }).ToList()); } string json = JsonConvert.SerializeObject(statDTO, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DynamicContractResolver(statDTO.DynamicJsonIgnoreProperties) }); response = await _restfulServiceClient.PutAsync(uri, (HttpContent) new StringContent(json), null, GetStatxAPIHeaders(statXAuthDTO)); } } else { var updateStatContent = new GeneralStatDTO { Title = title, Notes = notes, LastUpdatedDateTime = DateTime.UtcNow, NotesLastUpdatedDateTime = DateTime.UtcNow, }; if (currentStat.VisualType == StatTypes.PickList) { int currentIndex = 0; int.TryParse(statValues.First().Value, out currentIndex); updateStatContent.CurrentIndex = currentIndex; updateStatContent.DynamicJsonIgnoreProperties = new[] { "visualType", "value" }; } else { updateStatContent.Value = statValues.First().Value; updateStatContent.DynamicJsonIgnoreProperties = new[] { "visualType", "currentIndex" }; } string json = JsonConvert.SerializeObject(updateStatContent, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DynamicContractResolver(updateStatContent.DynamicJsonIgnoreProperties) }); response = await _restfulServiceClient.PutAsync(uri, (HttpContent) new StringContent(json), null, GetStatxAPIHeaders(statXAuthDTO)); } var jObject = JObject.Parse(response); CheckForExistingErrors(jObject, true); } } catch (RestfulServiceException exception) { var jObject = JObject.Parse(exception.ResponseMessage); JToken errorsToken; if (jObject.TryGetValue("errors", out errorsToken)) { if (errorsToken is JArray) { var firstError = (JObject)errorsToken.First; throw new ApplicationException($"StatX request error: {firstError["message"]?.ToString()}"); } } } }
private async Task <StatXItemCM> GetLatestStatItem(string token, string groupId, string statId) { var latestStat = await _statXIntegration.GetStat(StatXUtilities.GetStatXAuthToken(token), groupId, statId); return(StatXUtilities.MapToStatItemCrateManifest(latestStat)); }