Skip to content

Commit f9e82c2

Browse files
committed
Feat: Get Database and Collection Usage
1 parent a1430c3 commit f9e82c2

File tree

7 files changed

+102
-3
lines changed

7 files changed

+102
-3
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ getDatabase: $(SRCS) $(EXAMPLES_DIR)/database/getDatabase.cpp
5454
$(CXX) $(CXXFLAGS) -o tests/database/getDatabase $(SRCS) $(EXAMPLES_DIR)/database/getDatabase.cpp $(LDFLAGS)
5555
listDatabase: $(SRCS) $(EXAMPLES_DIR)/database/listDatabase.cpp
5656
$(CXX) $(CXXFLAGS) -o tests/database/listDatabase $(SRCS) $(EXAMPLES_DIR)/database/listDatabase.cpp $(LDFLAGS)
57-
57+
getDatabaseUsage: $(SRCS) $(EXAMPLES_DIR)/database/getDatabaseUsage.cpp
58+
$(CXX) $(CXXFLAGS) -o tests/database/getDatabaseUsage $(SRCS) $(EXAMPLES_DIR)/database/getDatabaseUsage.cpp $(LDFLAGS)
59+
getCollectionUsage: $(SRCS) $(EXAMPLES_DIR)/database/getCollectionUsage.cpp
60+
$(CXX) $(CXXFLAGS) -o tests/database/getCollectionUsage $(SRCS) $(EXAMPLES_DIR)/database/getCollectionUsage.cpp $(LDFLAGS)
5861

5962
# Database - Collection
6063
listCollection: $(SRCS) $(EXAMPLES_DIR)/database/collection/listCollection.cpp
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
8+
Appwrite appwrite(projectId);
9+
Databases& databases = appwrite.getDatabases();
10+
11+
databases.setup(apiKey, projectId);
12+
13+
try {
14+
std::string databaseId;
15+
std::string databasesResponse = databases.list();
16+
databaseId = "database123";
17+
18+
std::string collectionId;
19+
collectionId = "test1234";
20+
21+
std::string usageRange = "24h";
22+
std::string response = databases.getDatabaseUsage(databaseId, usageRange);
23+
24+
std::cout << "Collection usage fetched successfully! \nResponse: " << response << std::endl;
25+
26+
} catch (const AppwriteException& ex) {
27+
std::cerr << "Exception: " << ex.what() << std::endl;
28+
}
29+
30+
return 0;
31+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
8+
Appwrite appwrite(projectId);
9+
Databases& databases = appwrite.getDatabases();
10+
11+
databases.setup(apiKey, projectId);
12+
13+
try {
14+
std::string databaseId;
15+
std::string databasesResponse = databases.list();
16+
databaseId = "database123";
17+
18+
std::string usageRange = "24h";
19+
std::string response = databases.getDatabaseUsage(databaseId, usageRange);
20+
21+
std::cout << "Database usage fetched successfully! \nResponse: " << response << std::endl;
22+
23+
} catch (const AppwriteException& ex) {
24+
std::cerr << "Exception: " << ex.what() << std::endl;
25+
}
26+
27+
return 0;
28+
}

include/classes/Databases.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class Databases {
1818
std::string get(const std::string& databaseId);
1919
std::string create(const std::string& databaseId, const std::string& name, bool enabled);
2020
std::string update(const std::string& databaseId, const std::string& name, bool enabled);
21-
21+
std::string getDatabaseUsage(const std::string& databaseId, const std::string& range);
22+
std::string getCollectionUsage(const std::string& databaseId, const std::string& collectionId, const std::string& range);
23+
24+
2225
// collections
2326
std::string listCollection();
2427
std::string createCollection(const std::string& databaseId, const std::string& collectionId, const std::string& name, bool enabled);

src/services/Databases.cpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -793,4 +793,38 @@ std::string Databases::getIndexes(const std::string& databaseId, const std::stri
793793
throw AppwriteException("Error fetching index. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
794794
}
795795

796-
}
796+
}
797+
798+
std::string Databases::getDatabaseUsage(const std::string& databaseId, const std::string& range) {
799+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/usage?range=" + range;
800+
std::cout << "Request URL: " << url << std::endl;
801+
802+
std::vector<std::string> headers = Config::getHeaders(projectId);
803+
headers.push_back("X-Appwrite-Key: " + apiKey);
804+
805+
std::string response;
806+
int statusCode = Utils::getRequest(url, headers, response);
807+
808+
if (statusCode == HttpStatus::OK) {
809+
return response;
810+
} else {
811+
throw AppwriteException("Error fetching database usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
812+
}
813+
}
814+
815+
std::string Databases::getCollectionUsage(const std::string& databaseId, const std::string& collectionId, const std::string& range) {
816+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/usage?range=" + range;
817+
std::cout << "Request URL: " << url << std::endl;
818+
819+
std::vector<std::string> headers = Config::getHeaders(projectId);
820+
headers.push_back("X-Appwrite-Key: " + apiKey);
821+
822+
std::string response;
823+
int statusCode = Utils::getRequest(url, headers, response);
824+
825+
if (statusCode == HttpStatus::OK) {
826+
return response;
827+
} else {
828+
throw AppwriteException("Error fetching collection usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
829+
}
830+
}

tests/database/getCollectionUsage

1.33 MB
Binary file not shown.

tests/database/getDatabaseUsage

1.33 MB
Binary file not shown.

0 commit comments

Comments
 (0)