• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

georgia-tech-db / eva / d88c9094-1a92-4944-91ea-5e7e8275cb11

23 Nov 2023 10:19PM UTC coverage: 67.117% (+67.1%) from 0.0%
d88c9094-1a92-4944-91ea-5e7e8275cb11

push

circleci

web-flow
Merge branch 'georgia-tech-db:staging' into cost_batching

342 of 692 new or added lines in 47 files covered. (49.42%)

12 existing lines in 4 files now uncovered.

9189 of 13691 relevant lines covered (67.12%)

0.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/evadb/third_party/databases/hackernews/hackernews_handler.py
1
# coding=utf-8
2
# Copyright 2018-2023 EvaDB
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
NEW
15
import json
×
16

NEW
17
import pandas as pd
×
NEW
18
import requests
×
19

NEW
20
from evadb.third_party.databases.hackernews.table_column_info import HACKERNEWS_COLUMNS
×
NEW
21
from evadb.third_party.databases.types import (
×
22
    DBHandler,
23
    DBHandlerResponse,
24
    DBHandlerStatus,
25
)
26

27

NEW
28
class HackernewsSearchHandler(DBHandler):
×
NEW
29
    def connection():
×
NEW
30
        return requests.get("https://www.google.com/").status_code == 200
×
31

NEW
32
    def __init__(self, name: str, **kwargs):
×
33
        """
34
        Initialize the handler.
35
        Args:
36
            name (str): name of the DB handler instance
37
            **kwargs: arbitrary keyword arguments for establishing the connection.
38
        """
NEW
39
        super().__init__(name)
×
NEW
40
        self.query = kwargs.get("query", "")
×
NEW
41
        self.tags = kwargs.get("tags", "")
×
42

NEW
43
    @property
×
NEW
44
    def supported_table(self):
×
NEW
45
        def _hackernews_topics_generator():
×
NEW
46
            url = "http://hn.algolia.com/api/v1/search?"
×
NEW
47
            url += "query=" + self.query
×
NEW
48
            url += "&tags=" + (
×
49
                "story" if self.tags == "" else +self.tags
50
            )  # search stories by default
NEW
51
            response = requests.get(url)
×
NEW
52
            if response.status_code != 200:
×
53
                raise Exception("Could not reach website.")
NEW
54
            json_result = response.content
×
NEW
55
            dict_result = json.loads(json_result)
×
NEW
56
            for row in dict_result:
×
NEW
57
                yield {
×
58
                    property_name: row[property_name]
59
                    for property_name, _ in HACKERNEWS_COLUMNS
60
                }
61

NEW
62
        mapping = {
×
63
            "search_results": {
64
                "columns": HACKERNEWS_COLUMNS,
65
                "generator": _hackernews_topics_generator(),
66
            },
67
        }
NEW
68
        return mapping
×
69

NEW
70
    def connect(self):
×
71
        """
72
        Set up the connection required by the handler.
73
        Returns:
74
            DBHandlerStatus
75
        """
NEW
76
        return DBHandlerStatus(status=True)
×
77

NEW
78
    def disconnect(self):
×
79
        """
80
        Close any existing connections.
81
        """
NEW
82
        pass
×
83

NEW
84
    def check_connection(self) -> DBHandlerStatus:
×
85
        """
86
        Check connection to the handler.
87
        Returns:
88
            DBHandlerStatus
89
        """
NEW
90
        if self.connection():
×
NEW
91
            return DBHandlerStatus(status=True)
×
92
        else:
NEW
93
            return DBHandlerStatus(status=False, error="Not connected to the internet.")
×
94

NEW
95
    def get_tables(self) -> DBHandlerResponse:
×
96
        """
97
        Return the list of tables in the database.
98
        Returns:
99
            DBHandlerResponse
100
        """
NEW
101
        if not self.connection():
×
NEW
102
            return DBHandlerResponse(data=None, error="Not connected to the internet.")
×
103

NEW
104
        try:
×
NEW
105
            tables_df = pd.DataFrame(
×
106
                list(self.supported_table.keys()), columns=["table_name"]
107
            )
NEW
108
            return DBHandlerResponse(data=tables_df)
×
109
        except Exception as e:
110
            return DBHandlerResponse(data=None, error=str(e))
111

NEW
112
    def get_columns(self, table_name: str) -> DBHandlerResponse:
×
113
        """
114
        Returns the list of columns for the given table.
115
        Args:
116
            table_name (str): name of the table whose columns are to be retrieved.
117
        Returns:
118
            DBHandlerResponse
119
        """
NEW
120
        if not self.connection():
×
NEW
121
            return DBHandlerResponse(data=None, error="Not connected to the internet.")
×
NEW
122
        try:
×
NEW
123
            columns_df = pd.DataFrame(
×
124
                self.supported_table[table_name]["columns"], columns=["name", "dtype"]
125
            )
NEW
126
            return DBHandlerResponse(data=columns_df)
×
127
        except Exception as e:
128
            return DBHandlerResponse(data=None, error=str(e))
129

NEW
130
    def select(self, table_name: str) -> DBHandlerResponse:
×
131
        """
132
        Returns a generator that yields the data from the given table.
133
        Args:
134
            table_name (str): name of the table whose data is to be retrieved.
135
        Returns:
136
            DBHandlerResponse
137
        """
NEW
138
        if not self.connection:
×
NEW
139
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
NEW
140
        try:
×
NEW
141
            if table_name not in self.supported_table:
×
NEW
142
                return DBHandlerResponse(
×
143
                    data=None,
144
                    error="{} is not supported or does not exist.".format(table_name),
145
                )
146

NEW
147
            return DBHandlerResponse(
×
148
                data=None,
149
                data_generator=self.supported_table[table_name]["generator"],
150
            )
151
        except Exception as e:
152
            return DBHandlerResponse(data=None, error=str(e))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc