Skip to content

Commit d6c2ae5

Browse files
committed
doc for data api done
1 parent 6929d81 commit d6c2ae5

File tree

7 files changed

+65
-69
lines changed

7 files changed

+65
-69
lines changed

codegreen_core/data/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
from .main import *
2+
from .offline import *
3+
# from . import main
24

3-
__all__ = ["energy","info"]
5+
__all__ = ["info","energy","sync_offline_data",'get_offline_data']

codegreen_core/data/offline.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,27 @@ def _get_offline_cache_data(country,start,end):
243243
return False,None
244244

245245

246-
def get_offline_data(country,start,end,sync_first=False):
246+
def get_offline_data(country,start,end,sync_first=False)->dict:
247247
"""
248-
This method returns locally stored energy data.
249-
Data is stored in 2 sources : one. Redis cache and second : csv files.
250-
Redis cache contains data only for the last 72 hours from when it was last synced
251-
Offline data files can contain data for longer durations.
252-
Both these options can be configured in the config file
253-
returns {available:True/False, data:dataframe}
254-
Note that this method assumes that syncing of the sources is being handled separately
248+
This method returns locally stored energy data.
249+
250+
Data is stored in two sources:
251+
252+
1. **Redis cache**: Contains data for a limited number of hours from the last sync.
253+
2. **CSV files**: Contain data for longer durations.
254+
255+
Both storage options can be configured in the configuration file.
256+
257+
**Note**: Unless you specify the ``sync_first`` flag, the method assumes that syncing of the data sources is handled separately. If ``sync_first`` is set to ``True`` and data files are not initialized in advance, the method may take longer to complete
258+
259+
:return: A dictionary with the following keys:
260+
- **available** (*bool*): Indicates if the data is available.
261+
- **data** (*pandas.DataFrame*): The energy data, if available. Otherwise, an empty DataFrame.
262+
263+
:rtype: dict
264+
255265
"""
266+
256267
output = {"available":False,"data":None, "partial":False,"source":""}
257268
offline = Config.get("enable_offline_energy_generation")
258269
cache = Config.get("enable_energy_caching")
@@ -264,7 +275,7 @@ def get_offline_data(country,start,end,sync_first=False):
264275
if cache :
265276
# first look in the cache
266277
if(sync_first):
267-
print("will first sync the cache to get the latest data")
278+
#print("will first sync the cache to get the latest data")
268279
_sync_offline_cache(country)
269280
partial,data = _get_offline_cache_data(country,start,end)
270281
if data is not None and partial is False:
@@ -278,37 +289,39 @@ def get_offline_data(country,start,end,sync_first=False):
278289
if offline:
279290
# first look if data files are available, if yes, return data
280291
if(sync_first):
281-
print("will first sync the offline files to get the latest data")
292+
#print("will first sync the offline files to get the latest data")
282293
_sync_offline_file(country)
283294
partial,data = _get_offline_file_data(country,start,end)
284295
output["partial"] = partial
285296
output["data"] = data
286297
output["available"] = True
287298
output["source"] = "offline_file"
288-
print("just got the data from offline file")
299+
#print("just got the data from offline file")
289300

290301
return output
291302

292303

293304
def sync_offline_data(file=False,cache=False):
294305
"""
295-
This method syncs offline data for offline sources enabled in the cache.
296-
Data is synced for all available countries
297-
You need to run this before getting offline data. you can even setup a CRON job to call this method on regular intervals
306+
This method syncs offline data for offline sources enabled in the configuration file. The data is synced for all available countries.
307+
308+
You need to run this method before retrieving offline data. It is also possible to set up a CRON job to call this method at regular intervals to keep data synchronized.
309+
310+
The sync operation can take some time, depending on the data size and the selected sync options (file, cache, or both).
311+
312+
:param bool file: If ``True``, sync data in offline files. Defaults to ``False``.
313+
:param bool cache: If ``True``, sync data in the cache. Defaults to ``False``.
298314
"""
299315
c_keys = meta.get_country_metadata()
300316
if Config.get("enable_offline_energy_generation") == True and file == True:
301317
for key in c_keys:
302318
try:
303319
_sync_offline_file(key)
304320
except Exception as e:
305-
# print(e)
306321
log_stuff("Error in syncing offline file for "+key+". Message"+ str(e))
307322
if Config.get("enable_energy_caching") == True and cache == True :
308323
for key in c_keys:
309324
try:
310325
_sync_offline_cache(key)
311326
except Exception as e:
312-
# print(e)
313327
log_stuff("Error in syncing offline file for "+key+". Message: "+ str(e))
314-

codegreen_core/tools/loadshift_time.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ def predict_now(
7070
:type criteria: str
7171
:return: Tuple[timestamp, message, average_percent_renewable]
7272
:rtype: tuple
73+
74+
**Example usage**:
75+
76+
.. code-block:: python
77+
78+
from datetime import datetime,timedelta
79+
from codegreen_core.tools.loadshift_time import predict_now
80+
81+
country_code = "DK"
82+
est_runtime_hour = 10
83+
est_runtime_min = 0
84+
now = datetime.now()
85+
hard_finish_date = now + timedelta(days=1)
86+
criteria = "percent_renewable"
87+
per_renewable = 50
88+
89+
time = predict_now(country_code,
90+
est_runtime_hour,
91+
est_runtime_min,
92+
hard_finish_date,
93+
criteria,
94+
per_renewable)
95+
# (1728640800.0, <Message.OPTIMAL_TIME: 'OPTIMAL_TIME'>, 76.9090909090909)
96+
97+
7398
"""
7499
if criteria == "percent_renewable":
75100
try:

docs/_static/modules.png

-11.1 KB
Binary file not shown.

docs/api.rst

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,9 @@
11
codegreen_core API
22
===================
33

4-
5-
Package Organization
6-
---------------------
7-
8-
.. image:: _static/modules.png
9-
:alt: modules
10-
:width: 400px
11-
:align: center
12-
13-
14-
The package is divided into two main sub packages: `data`` and `tools`. (There is also an additional module, `utilities`, which provides helper methods that support other modules.)
15-
16-
The `data` sub package contains methods for fetching energy production data. This package relies on external data sources to retrieve this information, which is then processed to make it usable by other components of the package. For more details and a complete API , see the data module documentation.
17-
18-
The `tools` sub package provides a variety of tools, including:
19-
20-
- Carbon intensity calculator
21-
- Carbon emission calculator
22-
- Optimal time-shifting predictor
23-
- Optimal location-shifting predictor
24-
25-
26-
Example : Calculating optimal time for a computational task
27-
-------------------------------------------------------------
28-
Assuming all the above steps are done, you can now calculate the optimal starting time for a computations.
29-
30-
.. code-block:: python
31-
32-
from datetime import datetime,timedelta
33-
from codegreen_core.tools.loadshift_time import predict_now
34-
35-
country_code = "DK"
36-
est_runtime_hour = 10
37-
est_runtime_min = 0
38-
now = datetime.now()
39-
hard_finish_date = now + timedelta(days=1)
40-
criteria = "percent_renewable"
41-
per_renewable = 50
42-
43-
time = predict_now(country_code,
44-
est_runtime_hour,
45-
est_runtime_min,
46-
hard_finish_date,
47-
criteria,
48-
per_renewable)
49-
# (1728640800.0, <Message.OPTIMAL_TIME: 'OPTIMAL_TIME'>, 76.9090909090909)
50-
51-
52-
534
The core package contains 2 main module :
54-
- `data` : To fetch energy data for a country
5+
6+
- `data` : To fetch energy data for a country.
557
- `tools` : To calculate various quantities like Optimal computation time, carbon intensity etc.
568

579

docs/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@
4747
html_theme = "alabaster"
4848
html_static_path = ["_static"]
4949

50+
html_theme_options = {
51+
"github_user": "codegreen-framework",
52+
"github_repo": "codegreen-core"
53+
}
5054

5155
# import codegreen_core

docs/methodology.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Here we describe how we calcualte stuff
66

77

88
``tools`` Module
9-
=================
9+
------------------
1010

1111
This subpackage provides tools and methods for tasks like calculating the carbon intensity of energy production and calculating the emissions produced due to a computation.
1212

0 commit comments

Comments
 (0)