Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique names and default sort for technologies and category #34

Merged
merged 11 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 66 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Returns a JSON object with the following schema:
"desktop": 11
}
},
...
...
]
```

Expand All @@ -51,8 +51,8 @@ This endpoint can return a full list of categories names or a categories with al

The following parameters can be used to filter the data:

- `category` (`required`): A comma-separated string representing the category name(s).
- `onlyname` (optional): A string 'true' or 'false'.
- `category` (optional): A comma-separated string representing the category name(s).
- `onlyname` (optional): No value required. If present, only the category names will be returned.

#### Response

Expand All @@ -63,45 +63,46 @@ curl --request GET \

```json
[
{
"description": "Solutions that redirect domains to a different location or page",
"technologies": [
"Arsys Domain Parking"
],
"origins": {
"mobile": 14,
"desktop": 8
},
"category": "Domain parking"
},
{
"description": "Systems that automate building, testing, and deploying code",
"technologies": [
"Jenkins",
"TeamCity"
],
"origins": {
"mobile": 22,
"desktop": 35
{
"description": "Systems that automate building, testing, and deploying code",
"technologies": [
"Jenkins",
"TeamCity"
],
"origins": {
"mobile": 22,
"desktop": 35
},
"category": "CI"
},
"category": "CI"
}
{
"description": "Solutions that redirect domains to a different location or page",
"technologies": [
"Cloudflare",
"Arsys Domain Parking"
],
"origins": {
"mobile": 14,
"desktop": 8
},
"category": "Domain parking"
}
]
```

```bash
curl --request GET \
--url 'https://{{HOST}}/v1/categories?onlyname=true'
--url 'https://{{HOST}}/v1/categories?onlyname'
```

```json
[
"Blogs",
"LMS",
"CI",
"Cross border ecommerce",
"Cart abandonment",
"Domain parking",
"A/B Testing",
"Accessibility",
"Accounting",
"Advertising",
"Affiliate programs",
"Analytics",
...
]

Expand Down Expand Up @@ -245,31 +246,50 @@ Returns a JSON object with the following schema:

The following parameters can be used to filter the data:

- `technology` (`required`): A comma-separated string representing the technology name(s).
- `start` (optional): A string representing the start date in the format `YYYY-MM-DD`.
- `end` (optional): A string representing the end date in the format `YYYY-MM-DD`.
- `geo` (optional): A string representing the geographic location.
- `rank` (optional): An string representing the rank.
- `client` (optional): A string with the client: `mobile` or `desktop`.
- `technology` (optional): A comma-separated string representing the technology name(s) or `ALL`.
- `category` (optional): A comma-separated string representing the category name(s).
- `onlyname` (optional): No value required. If present, only the technology names will be returned.

#### Response

```bash
curl --request GET \
--url 'https://{{HOST}}/v1/technologies?start=2022-02-01&end=2022-04-01&category=Live%20chat%2C%20blog&technology=Smartsupp&client=mobile'
--url 'https://{{HOST}}/v1/technologies?category=Live%20chat%2C%20blog&technology=Smartsupp&client=mobile'
```

Returns a JSON object with the following schema:

```json
[
{
"client": "mobile",
"similar_technologies": null,
"description": "Smartsupp is a live chat tool that offers visitor recording feature.",
"origins": 25649,
"technology": "Smartsupp",
"category": "Live chat"
}
{
"client": "mobile",
"similar_technologies": null,
"description": "Smartsupp is a live chat tool that offers visitor recording feature.",
"origins": 25649,
"technology": "Smartsupp",
"category": "Live chat"
}
]
```

```bash
curl --request GET \
--url 'https://{{HOST}}/v1/technologies?onlyname'
```

Returns a JSON object with the following schema:

```json
[
"1C-Bitrix",
"2B Advice",
"33Across",
"34SP.com",
"4-Tell",
"42stores",
"51.LA",
"5centsCDN",
...
}
```
47 changes: 20 additions & 27 deletions functions/categories/libs/queries.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
import os
from google.cloud import firestore
from google.cloud.firestore_v1.base_query import FieldFilter
from .result import Result
from google.cloud.firestore_v1.base_query import FieldFilter, Or
from .result import Result
from .utils import convert_to_array

DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE'))
TABLE = 'categories'
DB = firestore.Client(
project=os.environ.get("PROJECT"), database=os.environ.get("DATABASE")
)

def list_data(params):
ref = DB.collection(TABLE)
ref = DB.collection("categories")

query = ref
query = ref.order_by("category", direction=firestore.Query.ASCENDING)

data = []
if "category" in params:
category_array = convert_to_array(params["category"])
filter_array = []
for category in category_array:
filter_array.append(FieldFilter("category", "==", category))
query = query.where(filter=Or(filters=filter_array))

if 'onlyname' in params:
documents = query.stream()
data = []

for doc in documents:
item = doc.to_dict()
if 'category' in item:
data.append(item['category'])

else:

if 'category' in params:
category_array = convert_to_array(params['category'])

for category in category_array:
results = query.where(filter=FieldFilter("category", "==", category)).stream()
for doc in results:
data.append(doc.to_dict())
if "onlyname" in params:
for doc in documents:
data.append(doc.get("category"))

else:
documents = query.stream()

for doc in documents:
data.append(doc.to_dict())
for doc in documents:
data.append(doc.to_dict())

return Result(result=data)
return Result(result=data)
85 changes: 40 additions & 45 deletions functions/technologies/libs/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,48 @@
from google.cloud import firestore
from google.cloud.firestore_v1.base_query import FieldFilter, Or

from .result import Result
from .result import Result
from .utils import convert_to_array
from .presenters import Presenters

DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE'))
DB = firestore.Client(
project=os.environ.get("PROJECT"), database=os.environ.get("DATABASE")
)

def list_data(params):
onlyname = False
ref = DB.collection('technologies')

query = ref

if 'technology' in params:
arfilters = []
params_array = convert_to_array(params['technology'])
for tech in params_array:
arfilters.append(FieldFilter('technology', '==', tech))

or_filter = Or(filters=arfilters)

query = query.where(filter=or_filter)

if 'category' in params:
params_array = convert_to_array(params['category'])
query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array))

if 'client' in params:
query = query.where(filter=FieldFilter('client', '==', params['client']))

if 'onlyname' in params:
onlyname = True

if 'sort' not in params:
query = query.order_by('technology', direction=firestore.Query.ASCENDING)
else:
if params['sort'] == 'origins':
query = query.order_by('origins', direction=firestore.Query.DESCENDING)


documents = query.stream()

data = []
for doc in documents:
item = doc.to_dict()
if onlyname:
data.append(item['technology'])
else:
data.append(Presenters.technology(doc.to_dict()))

return Result(result=data)
ref = DB.collection("technologies")

query = ref.order_by("technology", direction=firestore.Query.ASCENDING)

if "technology" in params:
arfilters = []
params_array = convert_to_array(params["technology"])
for tech in params_array:
arfilters.append(FieldFilter("technology", "==", tech))
query = query.where(filter=Or(filters=arfilters))

if "category" in params:
params_array = convert_to_array(params["category"])
query = query.where(
filter=FieldFilter("category_obj", "array_contains_any", params_array)
)

if "client" in params:
query = query.where(filter=FieldFilter("client", "==", params["client"]))

documents = query.stream()
data = []

if "onlyname" in params and "client" not in params:
appended_tech = set()
for doc in documents:
tech = doc.get("technology")
if tech not in appended_tech:
appended_tech.add(tech)
data.append(tech)

else:
for doc in documents:
data.append(Presenters.technology(doc.to_dict()))

return Result(result=data)
Loading