Exemple #1
0
        private static CensusIdentifier ParseIdentifier(
            string censusIdentifierStr)
        {
            CensusIdentifier toReturn = null;

            string[] identifierParts = censusIdentifierStr.Split(
                '-',
                StringSplitOptions.RemoveEmptyEntries);

            if (identifierParts.Length == 3)
            {
                toReturn = new CensusIdentifier()
                {
                    DatasetQueryFileId = identifierParts[0],
                    ParameterName      = identifierParts[1],
                    ParameterValue     = identifierParts[2],
                };
            }

            return(toReturn);
        }
        /// <inheritdoc />
        public async Task <GetCensusResponse> GetCensusAsync(
            GetCensusRequest getCensusRequest,
            CancellationToken cancellationToken)
        {
            GetCensusResponse toReturn = null;

            if (getCensusRequest == null)
            {
                throw new ArgumentNullException(nameof(getCensusRequest));
            }

            CensusIdentifier censusIdentifier =
                getCensusRequest.CensusIdentifier;

            string datasetQueryFileId = censusIdentifier.DatasetQueryFileId;

            this.loggerWrapper.Debug(
                $"Pulling back {nameof(DatasetQueryFile)} with id " +
                $"\"{datasetQueryFileId}\"...");

            DatasetQueryFile datasetQueryFile =
                await this.datasetQueryFilesStorageAdapter.GetDatabaseQueryFileAsync(
                    censusIdentifier.DatasetQueryFileId,
                    cancellationToken)
                .ConfigureAwait(false);

            this.loggerWrapper.Info(
                $"Got {nameof(DatasetQueryFile)} for id " +
                $"\"{datasetQueryFileId}\": {datasetQueryFile}.");

            Dictionary <string, AggregateQuery> aggregateQueries =
                getCensusRequest.AggregateQueries;

            Census census = null;

            if (aggregateQueries != null)
            {
                census = await this.GetCensusAggregates(
                    getCensusRequest,
                    datasetQueryFile,
                    cancellationToken)
                         .ConfigureAwait(false);
            }
            else
            {
                census = new Census();
            }

            census.Name = $"Census results from " +
                          $"{datasetQueryFile.QueryConfiguration.DatabaseName}";

            this.loggerWrapper.Info(
                $"Fetched {nameof(Census)}: {census} using " +
                $"{datasetQueryFile} and supplied aggregate queries.");

            toReturn = new GetCensusResponse()
            {
                Census = census,
            };

            return(toReturn);
        }
Exemple #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "GET", "POST", Route = "censuses/{id}")]
            HttpRequest httpRequest,
            string id,
            CancellationToken cancellationToken)
        {
            IActionResult toReturn = null;

            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            // Parse and validate the id to a CensusIdentifier.
            this.censusIdentifier = ParseIdentifier(id);

            FunctionRunContext functionRunContext = new FunctionRunContext();

            if (this.censusIdentifier != null)
            {
                switch (httpRequest.Method)
                {
                case "POST":
                    toReturn = await this.ValidateAndRunAsync(
                        httpRequest,
                        functionRunContext,
                        cancellationToken)
                               .ConfigureAwait(false);

                    break;

                case "GET":
                    IHeaderDictionary headerDictionary =
                        httpRequest.Headers;

                    this.httpSpiExecutionContextManager.SetContext(
                        headerDictionary);

                    toReturn = await this.ProcessWellFormedRequestAsync(
                        null,
                        functionRunContext,
                        cancellationToken)
                               .ConfigureAwait(false);

                    break;
                }
            }
            else
            {
                toReturn =
                    this.httpErrorBodyResultProvider.GetHttpErrorBodyResult(
                        HttpStatusCode.BadRequest,
                        3);
            }

            return(toReturn);
        }
        private async Task <Census> GetCensusAggregates(
            GetCensusRequest getCensusRequest,
            DatasetQueryFile datasetQueryFile,
            CancellationToken cancellationToken)
        {
            Census toReturn = null;

            CensusIdentifier censusIdentifier =
                getCensusRequest.CensusIdentifier;

            // Get an entire list of aggregation fields that we can use.
            Dictionary <string, Type> aggregationFieldsAndTypes = null;

            if (this.aggregationFieldsCache.AggregationFieldsAndTypes == null)
            {
                this.loggerWrapper.Debug(
                    "Fetching available aggregation mappings from the " +
                    "Translator API...");

                GetEnumerationMappingsResponse getEnumerationMappingsResponse =
                    await this.translationApiAdapter.GetEnumerationMappingsAsync(
                        this.aggregationFieldsEnumerationName,
                        this.aggregationFieldsAdapterName,
                        cancellationToken)
                    .ConfigureAwait(false);

                aggregationFieldsAndTypes =
                    ConvertMappingsResponseToFieldsAndTypes(
                        getEnumerationMappingsResponse);

                this.loggerWrapper.Info(
                    $"Aggregation fields and types obtained - " +
                    $"{aggregationFieldsAndTypes.Count} in total.");

                this.aggregationFieldsCache.AggregationFieldsAndTypes =
                    aggregationFieldsAndTypes;
            }

            aggregationFieldsAndTypes =
                this.aggregationFieldsCache.AggregationFieldsAndTypes;

            IEnumerable <string> aggregationFields =
                aggregationFieldsAndTypes.Keys.ToList();

            string parameterName  = censusIdentifier.ParameterName;
            string parameterValue = censusIdentifier.ParameterValue;

            Dictionary <string, AggregateQuery> aggregateQueries =
                getCensusRequest.AggregateQueries;

            AssertFieldsAreSupported(aggregationFields, aggregateQueries);

            this.loggerWrapper.Debug(
                $"Fetching {nameof(Census)} using {datasetQueryFile} and " +
                $"supplied aggregate queries...");

            toReturn = await this.censusAdapter.GetCensusAsync(
                aggregationFields,
                datasetQueryFile,
                aggregateQueries,
                parameterName,
                parameterValue,
                this.BuildCensusResults,
                cancellationToken)
                       .ConfigureAwait(false);

            return(toReturn);
        }