Хранимая процедура Cosmos DB - PullRequest
0 голосов
/ 22 апреля 2020

Я пытаюсь запустить процедуру, которая запрашивает документы и настраивает некоторые свойства в соответствии с некоторыми правилами, которые определяются параметрами, которые я передаю в запрос.

function downsample(ageInDays, downsampleFactor) {
    var collection = getContext().getCollection();
    var responseBody = {
        deleted: 0,
        message: ""
    };
    var downsampledDocuments = [];
    var count = 0;

    collection.queryDocuments(
        collection.getSelfLink(),
        
        'SELECT * FROM root r ' + 
        'WHERE (DATEDIFF(day, r.EventProcessedUtcTime, GETDATE()) > ' + ageInDays+ 'AND r.downsamplingFactor < ' + downsampleFactor + ')' +
        'OR' +
        '((DATEDIFF(day, r.EventProcessedUtcTime, GETDATE()) > ' + ageInDays + ' AND r.downsamplingFactor = null )' +
        'ORDER BY r.did, r.resourceType ASC',
        
        function (err, documents, options) {
            if (err) throw err;
            // Check the feed and if empty, set the body to 'no docs found', 
            // else perform the downsampling

            if (!documents || !documents.length) {
                var response = getContext().getResponse();
                responseBody.message = "No documents found";
                response.setBody(responseBody);
            } else {
                // We need to take into consideration that in a lot of cases, the data will already be downsampled so we
                // example: previous downsampling factor of documents: 4, if a new downsampling is performed on these docs with factor 8 then we need to
                var adjustedDownSamplingFactor;
                if (documents[0].downsamplingFactor == null) {
                    adjustedDownSamplingFactor = downsampleFactor;
                } else {
                    adjustedDownSamplingFactor = downsampleFactor / documents[0].downsamplingFactor;
                }
                var aggregatedDocument = documents[0];
                var documentValueSum = 0;
                var documentCount = 0;
                var aggregatedDocumentValue = 0;

                for(doc in documents){

                    if(!aggregatedDocument){
                        aggregatedDocument = doc; 
                    }

                    if(documentCount >= adjustedDownSamplingFactor || aggregatedDocument.did !== doc.did || aggregatedDocument.resourceType !== doc.resourceType){
                        // preparing aggregated document
                        aggregatedDocumentValue = documentValueSum / documentCount;
                        aggregatedDocument.value = aggregatedDocumentValue;
                        aggregatedDocument.downsamplingFactor = downsampleFactor;
                        
                        //Adding the downsampled data to the Array which will be uploaded to the cosmosdb
                        downsampledDocuments.push(aggregatedDocument);

                        aggregatedDocument = null;
                        documentCount = 0;
                        documentValueSum = 0;
                        
                        continue;
                    }

                    documentValueSum += doc.value;
                    documentCount++;

                    
                }
                var response = getContext().getResponse();

                tryDelete(documents);
                // Call the CRUD API to create a document.
                tryCreate(downsampledDocuments[count], callback);


                responseBody.message = "Downsampling was succesful"
                response.setBody(responseBody);
            }

    });

Таким образом, я не передаю никакие документы на запрос, поэтому я не знаю, какой ключ разделения я должен был бы предоставить хранимой процедуре. Есть ли способ избежать предоставления ключа раздела? Я вызываю эту хранимую процедуру из API, но продолжаю получать сообщение о том, что я должен предоставить ключ раздела.

1 Ответ

1 голос
/ 22 апреля 2020

Есть ли способ избежать необходимости предоставления ключа раздела?

К сожалению, нет. Вы должны указать значение ключа раздела.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...