Skip to content

Commit

Permalink
Show the network and carbon saving when using Passim
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsie committed Apr 14, 2024
1 parent c508ad0 commit 6f979fd
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 34 deletions.
6 changes: 0 additions & 6 deletions RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ appstream-util appdata-to-news ../data/org.freedesktop.Passim.metainfo.xml > NEW
Update translations:

ninja-build passim-pot
cd ..
tx push --source
tx pull --all --force --minimum-perc=5
cd build
ninja-build fix-translations
git add ../po/*.po

# MAKE SURE THIS IS CORRECT
export release_ver="0.1.6"
Expand Down
46 changes: 46 additions & 0 deletions libpassim/passim-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef struct {
gchar *version;
gchar *uri;
PassimStatus status;
guint64 download_saving;
gdouble carbon_saving;
} PassimClientPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(PassimClient, passim_client, G_TYPE_OBJECT)
Expand Down Expand Up @@ -89,10 +91,48 @@ passim_client_get_status(PassimClient *self)
return priv->status;
}

/**
* passim_client_get_download_saving:
* @self: a #PassimClient
*
* Gets the total number of bytes saved from using this project.
*
* Returns: bytes
*
* Since: 0.1.6
**/
guint64
passim_client_get_download_saving(PassimClient *self)
{
PassimClientPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(PASSIM_IS_CLIENT(self), G_MAXUINT64);
return priv->download_saving;
}

/**
* passim_client_get_carbon_saving:
* @self: a #PassimClient
*
* Gets the carbon saving from using this project.
*
* Returns: kgs of CO₂e
*
* Since: 0.1.6
**/
gdouble
passim_client_get_carbon_saving(PassimClient *self)
{
PassimClientPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(PASSIM_IS_CLIENT(self), PASSIM_STATUS_UNKNOWN);
return priv->carbon_saving;
}

static void
passim_client_load_proxy_properties(PassimClient *self)
{
PassimClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GVariant) download_saving = NULL;
g_autoptr(GVariant) carbon_saving = NULL;
g_autoptr(GVariant) status = NULL;
g_autoptr(GVariant) version = NULL;
g_autoptr(GVariant) uri = NULL;
Expand All @@ -110,6 +150,12 @@ passim_client_load_proxy_properties(PassimClient *self)
status = g_dbus_proxy_get_cached_property(priv->proxy, "Status");
if (status != NULL)
priv->status = g_variant_get_uint32(status);
download_saving = g_dbus_proxy_get_cached_property(priv->proxy, "DownloadSaving");
if (download_saving != NULL)
priv->download_saving = g_variant_get_uint64(download_saving);
carbon_saving = g_dbus_proxy_get_cached_property(priv->proxy, "CarbonSaving");
if (carbon_saving != NULL)
priv->carbon_saving = g_variant_get_double(carbon_saving);
}

static void
Expand Down
4 changes: 4 additions & 0 deletions libpassim/passim-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const gchar *
passim_client_get_uri(PassimClient *self);
PassimStatus
passim_client_get_status(PassimClient *self);
guint64
passim_client_get_download_saving(PassimClient *self);
gdouble
passim_client_get_carbon_saving(PassimClient *self);
gboolean
passim_client_load(PassimClient *self, GError **error);
GPtrArray *
Expand Down
2 changes: 2 additions & 0 deletions libpassim/passim.map
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ LIBPASSIM_0.1.5 {

LIBPASSIM_0.1.6 {
global:
passim_client_get_carbon_saving;
passim_client_get_download_saving;
passim_client_get_uri;
local: *;
} LIBPASSIM_0.1.5;
54 changes: 32 additions & 22 deletions po/passim.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: passim\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-19 15:36+0000\n"
"POT-Creation-Date: 2024-04-14 18:42+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -58,104 +58,114 @@ msgid "Size"
msgstr ""

#. TRANSLATORS: daemon is starting up
#: src/passim-cli.c:267
#: src/passim-cli.c:269
msgid "Loading…"
msgstr ""

#. TRANSLATORS: daemon is scared to publish files
#: src/passim-cli.c:270
#: src/passim-cli.c:272
msgid "Disabled (metered network)"
msgstr ""

#. TRANSLATORS: daemon is offering files like normal
#: src/passim-cli.c:273
#: src/passim-cli.c:275
msgid "Running"
msgstr ""

#: src/passim-cli.c:277
#: src/passim-cli.c:279
msgid "Status"
msgstr ""

#. TRANSLATORS: how many bytes we did not download from the internet
#: src/passim-cli.c:287
msgid "Network Saving"
msgstr ""

#. TRANSLATORS: how much carbon we did not *burn* by using local data
#: src/passim-cli.c:294
msgid "Carbon Saving"
msgstr ""

#. TRANSLATORS: full https://whatever of the daemon
#: src/passim-cli.c:284
#: src/passim-cli.c:302
msgid "URI"
msgstr ""

#. TRANSLATORS: user mistyped the command
#: src/passim-cli.c:323 src/passim-cli.c:353
#: src/passim-cli.c:341 src/passim-cli.c:371
msgid "Invalid arguments"
msgstr ""

#. TRANSLATORS: now sharing to the world
#: src/passim-cli.c:340
#: src/passim-cli.c:358
msgid "Published"
msgstr ""

#. TRANSLATORS: no longer sharing with the world
#: src/passim-cli.c:360
#: src/passim-cli.c:378
msgid "Unpublished"
msgstr ""

#. TRANSLATORS: --version
#: src/passim-cli.c:375
#: src/passim-cli.c:393
msgid "Show project version"
msgstr ""

#: src/passim-cli.c:382
#: src/passim-cli.c:400
msgid "Next reboot"
msgstr ""

#. TRANSLATORS: CLI action description
#: src/passim-cli.c:397
#: src/passim-cli.c:415
msgid "Show daemon status"
msgstr ""

#. TRANSLATORS: CLI option example
#: src/passim-cli.c:402
#: src/passim-cli.c:420
msgid "FILENAME [MAX-AGE] [MAX-SHARE]"
msgstr ""

#. TRANSLATORS: CLI action description
#: src/passim-cli.c:404
#: src/passim-cli.c:422
msgid "Publish an additional file"
msgstr ""

#. TRANSLATORS: CLI option example
#: src/passim-cli.c:409
#: src/passim-cli.c:427
msgid "HASH"
msgstr ""

#. TRANSLATORS: CLI action description
#: src/passim-cli.c:411
#: src/passim-cli.c:429
msgid "Unpublish an existing file"
msgstr ""

#. TRANSLATORS: CLI tool description
#: src/passim-cli.c:418
#: src/passim-cli.c:436
msgid "Interact with the local passimd process."
msgstr ""

#. TRANSLATORS: CLI tool name
#: src/passim-cli.c:420
#: src/passim-cli.c:438
msgid "Passim CLI"
msgstr ""

#. TRANSLATORS: we don't know what to do
#: src/passim-cli.c:424
#: src/passim-cli.c:442
msgid "Failed to parse arguments"
msgstr ""

#. TRANSLATORS: daemon failed to start
#: src/passim-cli.c:432
#: src/passim-cli.c:450
msgid "Failed to connect to daemon"
msgstr ""

#. TRANSLATORS: CLI tool
#: src/passim-cli.c:439
#: src/passim-cli.c:457
msgid "client version"
msgstr ""

#. TRANSLATORS: server
#: src/passim-cli.c:441
#: src/passim-cli.c:459
msgid "daemon version"
msgstr ""
18 changes: 18 additions & 0 deletions src/org.freedesktop.Passim.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@
</doc:description>
</doc:doc>
</property>
<property name='DownloadSaving' type='t' access='read'>
<doc:doc>
<doc:description>
<doc:para>
The total number of bytes saved by using this project.
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name='CarbonSaving' type='d' access='read'>
<doc:doc>
<doc:description>
<doc:para>
The carbon saving in kg CO₂e by using this project.
</doc:para>
</doc:description>
</doc:doc>
</property>
<method name='GetItems'>
<doc:doc>
<doc:description>
Expand Down
18 changes: 18 additions & 0 deletions src/passim-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ passim_cli_status(PassimCli *self, gchar **values, GError **error)
{
PassimStatus status = passim_client_get_status(self->client);
const gchar *status_value;
gdouble download_saving = passim_client_get_download_saving(self->client);
gdouble carbon_saving = passim_client_get_carbon_saving(self->client);
g_autofree gchar *status_str = NULL;
g_autoptr(GPtrArray) items = NULL;

Expand All @@ -277,6 +279,22 @@ passim_cli_status(PassimCli *self, gchar **values, GError **error)
status_str = passim_cli_align_indent(_("Status"), status_value, PASSIM_CLI_VALIGN);
g_print("%s\n", status_str);

/* this is important enough to show */
if (download_saving > 0) {
g_autofree gchar *download_value = g_format_size(download_saving);
g_autofree gchar *download_str =
/* TRANSLATORS: how many bytes we did not download from the internet */
passim_cli_align_indent(_("Network Saving"), download_value, PASSIM_CLI_VALIGN);
g_print("%s\n", download_str);
}
if (carbon_saving > 0.001) {
g_autofree gchar *carbon_value = g_strdup_printf("%.02lf kg CO₂e", carbon_saving);
g_autofree gchar *carbon_str =
/* TRANSLATORS: how much carbon we did not *burn* by using local data */
passim_cli_align_indent(_("Carbon Saving"), carbon_value, PASSIM_CLI_VALIGN);
g_print("%s\n", carbon_str);
}

/* show location of the web console */
if (passim_client_get_uri(self->client) != NULL) {
g_autofree gchar *uri_str =
Expand Down
16 changes: 16 additions & 0 deletions src/passim-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define PASSIM_CONFIG_PORT "Port"
#define PASSIM_CONFIG_PATH "Path"
#define PASSIM_CONFIG_MAX_ITEM_SIZE "MaxItemSize"
#define PASSIM_CONFIG_CARBON_COST "CarbonCost"

const gchar *
passim_status_to_string(PassimStatus status)
Expand Down Expand Up @@ -71,6 +72,21 @@ passim_config_get_max_item_size(GKeyFile *kf)
return g_key_file_get_uint64(kf, PASSIM_CONFIG_GROUP, PASSIM_CONFIG_MAX_ITEM_SIZE, NULL);
}

gdouble
passim_config_get_carbon_cost(GKeyFile *kf)
{
gdouble carbon_cost =
g_key_file_get_double(kf, PASSIM_CONFIG_GROUP, PASSIM_CONFIG_CARBON_COST, NULL);
if (carbon_cost < 0.00001) {
/* using
* https://www.carbonbrief.org/factcheck-what-is-the-carbon-footprint-of-streaming-video-on-netflix/
* we can see that 0.018 kg CO2e for 30 mins, where 3 GB/hr -- so this gives a
* kg/GB of ~0.018 kg x (3h / 2) */
carbon_cost = 0.026367;
}
return carbon_cost;
}

gchar *
passim_config_get_path(GKeyFile *kf)
{
Expand Down
2 changes: 2 additions & 0 deletions src/passim-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ guint16
passim_config_get_port(GKeyFile *kf);
gsize
passim_config_get_max_item_size(GKeyFile *kf);
gdouble
passim_config_get_carbon_cost(GKeyFile *kf);
gchar *
passim_config_get_path(GKeyFile *kf);
gboolean
Expand Down
Loading

0 comments on commit 6f979fd

Please sign in to comment.