-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'BasedHardware:main' into firmware-flashing-readme
- Loading branch information
Showing
12 changed files
with
178 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,49 @@ | ||
from datetime import datetime | ||
from typing import Dict, List | ||
|
||
from firebase_admin import firestore | ||
from google.api_core.retry import Retry | ||
from google.cloud.firestore_v1 import FieldFilter | ||
|
||
from models.memory import Memory | ||
from models.trend import Trend | ||
from ._client import db | ||
from ._client import db, document_id_from_seed | ||
|
||
|
||
def get_trends_data() -> List[Dict[str, Trend]]: | ||
def get_trends_data() -> List[Dict]: | ||
trends_ref = db.collection('trends') | ||
trends_docs = [doc for doc in trends_ref.stream(retry=Retry())] | ||
trends_list = [] | ||
for doc in trends_docs: | ||
trend = doc.to_dict() | ||
trend['id'] = doc.id | ||
trend['name'] = trend['name'] | ||
trend['created_at'] = str(trend['created_at']) | ||
data = doc.reference.collection('data').count().get()[0][0].value | ||
trend['data'] = data | ||
trends_list.append({trend['name']: trend}) | ||
print(f"{{'{trend['name']}' : {trend}}}") | ||
return trends_list | ||
|
||
|
||
def save_trends(memory: Memory, trends: List[str]): | ||
mem_data = { | ||
'date': memory.created_at, | ||
'memory_id': memory.id | ||
} | ||
print(f"trend_data: {mem_data}") | ||
trends_ref = db.collection('trends') | ||
trends_data = [] | ||
for category in trends_docs: | ||
category_data = category.to_dict() | ||
|
||
category_topics_ref = trends_ref.document(category_data['id']).collection('topics') | ||
topics_docs = [topic.to_dict() for topic in category_topics_ref.stream(retry=Retry())] | ||
topics = sorted(topics_docs, key=lambda e: len(e['memory_ids']), reverse=True) | ||
for topic in topics: | ||
topic['memories_count'] = len(topic['memory_ids']) | ||
del topic['memory_ids'] | ||
|
||
category_data['topics'] = topics | ||
trends_data.append(category_data) | ||
return trends_data | ||
|
||
|
||
def save_trends(memory: Memory, trends: List[Trend]): | ||
trends_coll_ref = db.collection('trends') | ||
|
||
for trend in trends: | ||
trend_ref = trends_ref.where(filter=FieldFilter('name', '==', trend)).get() | ||
if len(trend_ref) == 0: | ||
trends_ref.add({"created_at": datetime.now(), "name": trend}) | ||
trend_ref = trends_ref.where(filter=FieldFilter('name', '==', trend)).get() | ||
trend_ref[0].reference.collection('data').add(mem_data) | ||
category = trend.category.value | ||
topics = trend.topics | ||
category_id = document_id_from_seed(category) | ||
category_doc_ref = trends_coll_ref.document(category_id) | ||
|
||
category_doc_ref.set({"id": category_id, "category": category, "created_at": datetime.utcnow()}, merge=True) | ||
|
||
topics_coll_ref = category_doc_ref.collection('topics') | ||
|
||
for topic in topics: | ||
topic_id = document_id_from_seed(topic) | ||
topic_doc_ref = topics_coll_ref.document(topic_id) | ||
|
||
topic_doc_ref.set({"id": topic_id, "topic": topic}, merge=True) | ||
topic_doc_ref.update({'memory_ids': firestore.firestore.ArrayUnion([memory.id])}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
from datetime import datetime | ||
from enum import Enum | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class TrendEnum(str, Enum): | ||
health = 'health' | ||
finance = 'finance' | ||
science = 'science' | ||
entrepreneurship = 'entrepreneurship' | ||
technology = 'technology' | ||
sports = 'sports' | ||
|
||
|
||
class TrendData(BaseModel): | ||
memory_id: str | ||
date: datetime | ||
acquisition = "acquisition" | ||
ceo = "ceo" | ||
company = "company" | ||
event = "event" | ||
founder = "founder" | ||
industry = "industry" | ||
innovation = "innovation" | ||
investment = "investment" | ||
partnership = "partnership" | ||
product = "product" | ||
research = "research" | ||
tool = "tool" | ||
|
||
|
||
class Trend(BaseModel): | ||
id: str | ||
name: str | ||
created_at: datetime | ||
data: int | ||
category: TrendEnum = Field(description="The category identified") | ||
topics: List[str] = Field(description="The specific topic corresponding the category") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
from typing import Dict, List | ||
from typing import List | ||
|
||
import database.trends as trends_db | ||
from fastapi import APIRouter | ||
from models.trend import Trend | ||
|
||
import database.trends as trends_db | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/v1/trends", response_model=List[Dict[str, Trend]], tags=['trends']) | ||
def get_trends(offset: int = 0, limit: int = 10): | ||
trends = trends_db.get_trends_data() | ||
return trends | ||
@router.get("/v1/trends", response_model=List, tags=['trends']) | ||
def get_trends(): | ||
return trends_db.get_trends_data() |
Oops, something went wrong.