call(back)
Algorithms & Data StructuresmediumSign in to star problemsSign in to track progress

Code Review: Glossary Entries by Time

The code review round shares a pull request on screen and asks what you would change before it merges, and in what order. This is the diff (the + lines are new). Review it out loud: correctness, security, design, and what you would ask the author.

# models/glossary.py
 @dataclass
 class Entry:
     entry_id: Optional[str]
     definition: str
     in_use: bool
     product_tags: List[str]
     required: bool
+    creation_timestamp: int  # seconds since epoch
     language_to_terms: Dict[LanguageID, TermsList]
     comment: Optional[str]
     part_of_speech: Optional[str]

# dal/glossary.py
+from datetime import datetime, timedelta
 from typing import Any, Dict, List
+from duolingo_base.tracking import track
+from elasticsearch import Elasticsearch

 @inject.bind(elasticsearch_config=es_config)
 class GlossaryDAL:
     def __init__(self, elasticsearch_config: ElasticsearchConfig) -> None:
         self._elasticsearch = elasticsearch_config.get_client()

     def _convert_es_entry_to_backend_entry(self, es_entry: Dict[str, Any]) -> Entry:
         return Entry(
             entry_id=es_entry["_id"],
             definition=es_entry["_source"]["definition"],
             ...
+            creation_timestamp=es_entry["_source"]["creation_timestamp"],
         )

+    def _get_elasticsearch_entries_by_time(self, start_date, end_date):
+        elasticsearch = Elasticsearch(
+            host="https://glossary-elasticsearch-prod.example.amazonaws.com",
+            port=443,
+            password="s3cr3t-pasted-from-a-terminal")
+
+        # Library call: returns all glossary entries created between the dates
+        es_entries = elasticsearch.get_glossary_entries(start_date=start_date, end_date=end_date)
+
+        res = list()
+        for es_entry in es_entries:
+            res = self._convert_es_entry_to_backend_entry(es_entry)
+
+        return res

Hints

Hint 1

Start with what would page someone at 3 a.m.: a production credential in source control, and a function whose return value is not what its name says.

Hint 2

Then the design question: the class already receives a configured client in __init__; why does the new method build another one?

Hint 3

Finally the things a linter would catch: unused imports, missing type hints, no tests, and a schema change (creation_timestamp) with no migration or default.

Solution

Show the approach

In order

  1. Secret in source — the host and a password are hard-coded. Blocker: rotate the credential now (it is in the repository's history), and use the injected self._elasticsearch, which already carries configuration from es_config.
  2. Wrong result — res = self._convert... overwrites on every iteration, so the function returns the last entry (or an empty list when there are none), not a list. res.append(...), or a list comprehension; the type hint -> List[Entry] would have made the intent checkable.
  3. Second client per call — even without the secret, constructing an Elasticsearch client on each call leaks connections and bypasses the dependency injection the class is built around.
  4. Schema change without a plan — creation_timestamp is a new required field on Entry and is read with ["creation_timestamp"]; existing documents without it raise KeyError. Default it, use .get, or backfill, and say which.
  5. Unused and surprising imports — datetime, timedelta and track are imported and unused; track suggests tracking was intended and forgotten.
  6. Interface — untyped start_date/end_date (epoch seconds? datetime?), no validation that start <= end, no pagination for a range that may return thousands of entries, and no tests.

What to ask the author

Where should the time filter live, in the library call or in the query? Is creation_timestamp already in the index for old entries? Is this meant to be tracked (the import), and what is the expected size of a range?

Worth saying out loud

  • Separate blocking from non-blocking comments, and give the fix for the blockers rather than a hint; the shared-screen format rewards being concrete.
  • Credentials in a diff are a process failure as much as a code one: a pre-commit secret scanner and a secrets manager are the follow-ups.

Assistant

Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.

Asked at

Similar Problems5