1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
#include <glib/gi18n.h>
30
#include "up-history.h"
31
#include "up-stats-item.h"
32
#include "up-history-item.h"
34
static void up_history_finalize (GObject *object);
36
#define UP_HISTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_HISTORY, UpHistoryPrivate))
38
#define UP_HISTORY_FILE_HEADER "PackageKit Profile"
39
#define UP_HISTORY_SAVE_INTERVAL (10*60) /* seconds */
40
#define UP_HISTORY_DEFAULT_MAX_DATA_AGE (7*24*60*60) /* seconds */
42
struct UpHistoryPrivate
46
gint64 time_full_last;
47
gint64 time_empty_last;
48
gdouble percentage_last;
51
GPtrArray *data_charge;
52
GPtrArray *data_time_full;
53
GPtrArray *data_time_empty;
61
UP_HISTORY_LAST_SIGNAL
64
G_DEFINE_TYPE (UpHistory, up_history, G_TYPE_OBJECT)
67
* up_history_set_max_data_age:
70
up_history_set_max_data_age (UpHistory *history, guint max_data_age)
72
history->priv->max_data_age = max_data_age;
76
* up_history_array_copy_cb:
79
up_history_array_copy_cb (UpHistoryItem *item, GPtrArray *dest)
81
g_ptr_array_add (dest, g_object_ref (item));
85
* up_history_array_limit_resolution:
86
* @array: The data we have for a specific graph
87
* @max_num: The max desired points
89
* We need to reduce the number of data points else the graph will take a long
90
* time to plot accuracy we don't need at the larger scales.
91
* This will not reduce the scale or range of the data.
93
* 100 + + | + | + | + +
95
* 80 + + | + | + | + +
97
* 60 + + | + | + | + +
99
* 40 + + | + | + | + E +
101
* 20 + + | + | + | + F +
103
* 0 +-----+-----+-----+-----+-----+
118
up_history_array_limit_resolution (GPtrArray *array, guint max_num)
121
UpHistoryItem *item_new;
128
UpDeviceState state = UP_DEVICE_STATE_UNKNOWN;
135
new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
136
g_debug ("length of array (before) %i", array->len);
142
if (length < max_num) {
143
/* need to copy array */
144
g_ptr_array_foreach (array, (GFunc) up_history_array_copy_cb, new);
149
item = (UpHistoryItem *) g_ptr_array_index (array, length-1);
150
last = up_history_item_get_time (item);
151
item = (UpHistoryItem *) g_ptr_array_index (array, 0);
152
first = up_history_item_get_time (item);
154
division = (first - last) / (gfloat) max_num;
155
g_debug ("Using a x division of %f (first=%i,last=%i)", division, first, last);
157
/* Reduces the number of points to a pre-set level using a time
158
* division algorithm so we don't keep diluting the previous
159
* data with a conventional 1-in-x type algorithm. */
160
for (i=length-1; i>=0; i--) {
161
item = (UpHistoryItem *) g_ptr_array_index (array, i);
162
preset = last + (division * (gfloat) step);
164
/* if state changed or we went over the preset do a new point */
166
(up_history_item_get_time (item) > preset ||
167
up_history_item_get_state (item) != state)) {
168
item_new = up_history_item_new ();
169
up_history_item_set_time (item_new, time_s / count);
170
up_history_item_set_value (item_new, value / count);
171
up_history_item_set_state (item_new, state);
172
g_ptr_array_add (new, item_new);
175
time_s = up_history_item_get_time (item);
176
value = up_history_item_get_value (item);
177
state = up_history_item_get_state (item);
181
time_s += up_history_item_get_time (item);
182
value += up_history_item_get_value (item);
186
/* only add if nonzero */
188
item_new = up_history_item_new ();
189
up_history_item_set_time (item_new, time_s / count);
190
up_history_item_set_value (item_new, value / count);
191
up_history_item_set_state (item_new, state);
192
g_ptr_array_add (new, item_new);
196
g_debug ("length of array (after) %i", new->len);
202
* up_history_copy_array_timespan:
205
up_history_copy_array_timespan (const GPtrArray *array, guint timespan)
209
GPtrArray *array_new;
216
/* no limit on data */
218
array_new = g_ptr_array_ref ((GPtrArray *) array);
223
array_new = g_ptr_array_new ();
224
g_get_current_time (&timeval);
225
g_debug ("limiting data to last %i seconds", timespan);
227
/* treat the timespan like a range, and search backwards */
229
for (i=array->len-1; i>0; i--) {
230
item = (UpHistoryItem *) g_ptr_array_index (array, i);
231
if (timeval.tv_sec - up_history_item_get_time (item) < timespan)
232
g_ptr_array_add (array_new, g_object_ref (item));
239
* up_history_get_data:
242
up_history_get_data (UpHistory *history, UpHistoryType type, guint timespan, guint resolution)
245
GPtrArray *array_resolution;
246
const GPtrArray *array_data = NULL;
248
g_return_val_if_fail (UP_IS_HISTORY (history), NULL);
250
if (history->priv->id == NULL)
253
if (type == UP_HISTORY_TYPE_CHARGE)
254
array_data = history->priv->data_charge;
255
else if (type == UP_HISTORY_TYPE_RATE)
256
array_data = history->priv->data_rate;
257
else if (type == UP_HISTORY_TYPE_TIME_FULL)
258
array_data = history->priv->data_time_full;
259
else if (type == UP_HISTORY_TYPE_TIME_EMPTY)
260
array_data = history->priv->data_time_empty;
263
if (array_data == NULL)
266
/* only return a certain time */
267
array = up_history_copy_array_timespan (array_data, timespan);
271
/* only add a certain number of points */
272
array_resolution = up_history_array_limit_resolution (array, resolution);
273
g_ptr_array_unref (array);
275
return array_resolution;
279
* up_history_get_profile_data:
282
up_history_get_profile_data (UpHistory *history, gboolean charging)
285
guint non_zero_accuracy = 0;
286
gfloat average = 0.0f;
289
UpHistoryItem *item_last = NULL;
291
UpHistoryItem *item_old = NULL;
297
gdouble total_value = 0.0f;
299
g_return_val_if_fail (UP_IS_HISTORY (history), NULL);
301
/* create 100 item list and set to zero */
302
data = g_ptr_array_new ();
303
for (i=0; i<101; i++) {
304
stats = up_stats_item_new ();
305
g_ptr_array_add (data, stats);
308
array = history->priv->data_charge;
309
for (i=0; i<array->len; i++) {
310
item = (UpHistoryItem *) g_ptr_array_index (array, i);
311
if (item_last == NULL ||
312
up_history_item_get_state (item) != up_history_item_get_state (item_last)) {
317
/* round to the nearest int */
318
bin = rint (up_history_item_get_value (item));
320
/* ensure bin is in range */
321
if (bin >= data->len)
327
if (item_old != NULL) {
328
/* not enough or too much difference */
329
value = fabs (up_history_item_get_value (item) - up_history_item_get_value (item_old));
339
time_s = up_history_item_get_time (item) - up_history_item_get_time (item_old);
340
/* use the accuracy field as a counter for now */
341
if ((charging && up_history_item_get_state (item) == UP_DEVICE_STATE_CHARGING) ||
342
(!charging && up_history_item_get_state (item) == UP_DEVICE_STATE_DISCHARGING)) {
343
stats = (UpStatsItem *) g_ptr_array_index (data, bin);
344
up_stats_item_set_value (stats, up_stats_item_get_value (stats) + time_s);
345
up_stats_item_set_accuracy (stats, up_stats_item_get_accuracy (stats) + 1);
354
/* divide the value by the number of samples to make the average */
355
for (i=0; i<101; i++) {
356
stats = (UpStatsItem *) g_ptr_array_index (data, i);
357
if (up_stats_item_get_accuracy (stats) != 0)
358
up_stats_item_set_value (stats, up_stats_item_get_value (stats) / up_stats_item_get_accuracy (stats));
361
/* find non-zero accuracy values for the average */
362
for (i=0; i<101; i++) {
363
stats = (UpStatsItem *) g_ptr_array_index (data, i);
364
if (up_stats_item_get_accuracy (stats) > 0) {
365
total_value += up_stats_item_get_value (stats);
371
if (non_zero_accuracy != 0)
372
average = total_value / non_zero_accuracy;
373
g_debug ("average is %f", average);
375
/* make the values a factor of 0, so that 1.0 is twice the
376
* average, and -1.0 is half the average */
377
for (i=0; i<101; i++) {
378
stats = (UpStatsItem *) g_ptr_array_index (data, i);
379
if (up_stats_item_get_accuracy (stats) > 0)
380
up_stats_item_set_value (stats, (up_stats_item_get_value (stats) - average) / average);
382
up_stats_item_set_value (stats, 0.0f);
385
/* accuracy is a percentage scale, where each cycle = 20% */
386
for (i=0; i<101; i++) {
387
stats = (UpStatsItem *) g_ptr_array_index (data, i);
388
up_stats_item_set_accuracy (stats, up_stats_item_get_accuracy (stats) * 20.0f);
395
* up_history_get_filename:
398
up_history_get_filename (UpHistory *history, const gchar *type)
403
filename = g_strdup_printf ("history-%s-%s.dat", type, history->priv->id);
404
path = g_build_filename (history->priv->dir, filename, NULL);
410
* up_history_set_directory:
413
up_history_set_directory (UpHistory *history, const gchar *dir)
415
g_free (history->priv->dir);
416
history->priv->dir = g_strdup (dir);
420
* up_history_array_to_file:
421
* @list: a valid #GPtrArray instance
422
* @filename: a filename
424
* Saves a copy of the list to a file
427
up_history_array_to_file (UpHistory *history, GPtrArray *list, const gchar *filename)
434
GError *error = NULL;
437
guint cull_count = 0;
439
/* get current time */
440
g_get_current_time (&time_now);
443
string = g_string_new ("");
444
for (i=0; i<list->len; i++) {
445
item = g_ptr_array_index (list, i);
447
/* only save entries for the last 24 hours */
448
time_item = up_history_item_get_time (item);
449
if (time_now.tv_sec - time_item > history->priv->max_data_age) {
453
part = up_history_item_to_string (item);
458
g_string_append_printf (string, "%s\n", part);
461
part = g_string_free (string, FALSE);
463
/* how many did we kill? */
464
g_debug ("culled %i of %i", cull_count, list->len);
466
/* we failed to convert to string */
468
g_warning ("failed to convert");
473
ret = g_file_set_contents (filename, part, -1, &error);
475
g_warning ("failed to set data: %s", error->message);
476
g_error_free (error);
479
g_debug ("saved %s", filename);
487
* up_history_array_from_file:
488
* @list: a valid #GPtrArray instance
489
* @filename: a filename
491
* Appends the list from a file
494
up_history_array_from_file (GPtrArray *list, const gchar *filename)
497
GError *error = NULL;
499
gchar **parts = NULL;
505
ret = g_file_test (filename, G_FILE_TEST_EXISTS);
507
g_debug ("failed to get data from %s as file does not exist", filename);
512
ret = g_file_get_contents (filename, &data, NULL, &error);
514
g_warning ("failed to get data: %s", error->message);
515
g_error_free (error);
519
/* split by line ending */
520
parts = g_strsplit (data, "\n", 0);
521
length = g_strv_length (parts);
523
g_debug ("no data in %s", filename);
527
/* add valid entries */
528
g_debug ("loading %i items of data from %s", length, filename);
529
for (i=0; i<length-1; i++) {
530
item = up_history_item_new ();
531
ret = up_history_item_set_from_string (item, parts[i]);
533
g_ptr_array_add (list, item);
543
* up_history_save_data:
546
up_history_save_data (UpHistory *history)
548
gboolean ret = FALSE;
549
gchar *filename_rate = NULL;
550
gchar *filename_charge = NULL;
551
gchar *filename_time_full = NULL;
552
gchar *filename_time_empty = NULL;
555
if (history->priv->id == NULL) {
556
g_warning ("no ID, cannot save");
561
filename_rate = up_history_get_filename (history, "rate");
562
filename_charge = up_history_get_filename (history, "charge");
563
filename_time_full = up_history_get_filename (history, "time-full");
564
filename_time_empty = up_history_get_filename (history, "time-empty");
567
ret = up_history_array_to_file (history, history->priv->data_rate, filename_rate);
570
ret = up_history_array_to_file (history, history->priv->data_charge, filename_charge);
573
ret = up_history_array_to_file (history, history->priv->data_time_full, filename_time_full);
576
ret = up_history_array_to_file (history, history->priv->data_time_empty, filename_time_empty);
580
g_free (filename_rate);
581
g_free (filename_charge);
582
g_free (filename_time_full);
583
g_free (filename_time_empty);
588
* up_history_schedule_save_cb:
591
up_history_schedule_save_cb (UpHistory *history)
593
up_history_save_data (history);
594
history->priv->save_id = 0;
599
* up_history_is_low_power:
602
up_history_is_low_power (UpHistory *history)
607
/* current status is always up to date */
608
if (history->priv->state != UP_DEVICE_STATE_DISCHARGING)
611
/* have we got any data? */
612
length = history->priv->data_charge->len;
616
/* get the last saved charge object */
617
item = (UpHistoryItem *) g_ptr_array_index (history->priv->data_charge, length-1);
618
if (up_history_item_get_state (item) != UP_DEVICE_STATE_DISCHARGING)
622
if (up_history_item_get_value (item) > 10)
625
/* we are low power */
630
* up_history_schedule_save:
633
up_history_schedule_save (UpHistory *history)
637
/* if low power, then don't batch up save requests */
638
ret = up_history_is_low_power (history);
640
g_debug ("saving directly to disk as low power");
641
up_history_save_data (history);
645
/* we already have one saved */
646
if (history->priv->save_id != 0) {
647
g_debug ("deferring as others queued");
651
/* nothing scheduled, do new */
652
g_debug ("saving in %i seconds", UP_HISTORY_SAVE_INTERVAL);
653
history->priv->save_id = g_timeout_add_seconds (UP_HISTORY_SAVE_INTERVAL,
654
(GSourceFunc) up_history_schedule_save_cb, history);
655
#if GLIB_CHECK_VERSION(2,25,8)
656
g_source_set_name_by_id (history->priv->save_id, "[UpHistory] save");
662
* up_history_load_data:
665
up_history_load_data (UpHistory *history)
670
/* load rate history from disk */
671
filename = up_history_get_filename (history, "rate");
672
up_history_array_from_file (history->priv->data_rate, filename);
675
/* load charge history from disk */
676
filename = up_history_get_filename (history, "charge");
677
up_history_array_from_file (history->priv->data_charge, filename);
680
/* load charge history from disk */
681
filename = up_history_get_filename (history, "time-full");
682
up_history_array_from_file (history->priv->data_time_full, filename);
685
/* load charge history from disk */
686
filename = up_history_get_filename (history, "time-empty");
687
up_history_array_from_file (history->priv->data_time_empty, filename);
690
/* save a marker so we don't use incomplete percentages */
691
item = up_history_item_new ();
692
up_history_item_set_time_to_present (item);
693
g_ptr_array_add (history->priv->data_rate, g_object_ref (item));
694
g_ptr_array_add (history->priv->data_charge, g_object_ref (item));
695
g_ptr_array_add (history->priv->data_time_full, g_object_ref (item));
696
g_ptr_array_add (history->priv->data_time_empty, g_object_ref (item));
697
g_object_unref (item);
698
up_history_schedule_save (history);
707
up_history_set_id (UpHistory *history, const gchar *id)
711
g_return_val_if_fail (UP_IS_HISTORY (history), FALSE);
713
if (history->priv->id != NULL)
718
g_debug ("using id: %s", id);
719
history->priv->id = g_strdup (id);
720
/* load all previous data */
721
ret = up_history_load_data (history);
726
* up_history_set_state:
729
up_history_set_state (UpHistory *history, UpDeviceState state)
731
g_return_val_if_fail (UP_IS_HISTORY (history), FALSE);
733
if (history->priv->id == NULL)
735
history->priv->state = state;
740
* up_history_set_charge_data:
743
up_history_set_charge_data (UpHistory *history, gdouble percentage)
747
g_return_val_if_fail (UP_IS_HISTORY (history), FALSE);
749
if (history->priv->id == NULL)
751
if (history->priv->state == UP_DEVICE_STATE_UNKNOWN)
753
if (history->priv->percentage_last == percentage)
756
/* add to array and schedule save file */
757
item = up_history_item_new ();
758
up_history_item_set_time_to_present (item);
759
up_history_item_set_value (item, percentage);
760
up_history_item_set_state (item, history->priv->state);
761
g_ptr_array_add (history->priv->data_charge, item);
762
up_history_schedule_save (history);
764
/* save last value */
765
history->priv->percentage_last = percentage;
771
* up_history_set_rate_data:
774
up_history_set_rate_data (UpHistory *history, gdouble rate)
778
g_return_val_if_fail (UP_IS_HISTORY (history), FALSE);
780
if (history->priv->id == NULL)
782
if (history->priv->state == UP_DEVICE_STATE_UNKNOWN)
784
if (history->priv->rate_last == rate)
787
/* add to array and schedule save file */
788
item = up_history_item_new ();
789
up_history_item_set_time_to_present (item);
790
up_history_item_set_value (item, rate);
791
up_history_item_set_state (item, history->priv->state);
792
g_ptr_array_add (history->priv->data_rate, item);
793
up_history_schedule_save (history);
795
/* save last value */
796
history->priv->rate_last = rate;
802
* up_history_set_time_full_data:
805
up_history_set_time_full_data (UpHistory *history, gint64 time_s)
809
g_return_val_if_fail (UP_IS_HISTORY (history), FALSE);
811
if (history->priv->id == NULL)
813
if (history->priv->state == UP_DEVICE_STATE_UNKNOWN)
817
if (history->priv->time_full_last == time_s)
820
/* add to array and schedule save file */
821
item = up_history_item_new ();
822
up_history_item_set_time_to_present (item);
823
up_history_item_set_value (item, (gdouble) time_s);
824
up_history_item_set_state (item, history->priv->state);
825
g_ptr_array_add (history->priv->data_time_full, item);
826
up_history_schedule_save (history);
828
/* save last value */
829
history->priv->time_full_last = time_s;
835
* up_history_set_time_empty_data:
838
up_history_set_time_empty_data (UpHistory *history, gint64 time_s)
842
g_return_val_if_fail (UP_IS_HISTORY (history), FALSE);
844
if (history->priv->id == NULL)
846
if (history->priv->state == UP_DEVICE_STATE_UNKNOWN)
850
if (history->priv->time_empty_last == time_s)
853
/* add to array and schedule save file */
854
item = up_history_item_new ();
855
up_history_item_set_time_to_present (item);
856
up_history_item_set_value (item, (gdouble) time_s);
857
up_history_item_set_state (item, history->priv->state);
858
g_ptr_array_add (history->priv->data_time_empty, item);
859
up_history_schedule_save (history);
861
/* save last value */
862
history->priv->time_empty_last = time_s;
868
* up_history_class_init:
869
* @klass: The UpHistoryClass
872
up_history_class_init (UpHistoryClass *klass)
874
GObjectClass *object_class = G_OBJECT_CLASS (klass);
875
object_class->finalize = up_history_finalize;
876
g_type_class_add_private (klass, sizeof (UpHistoryPrivate));
881
* @history: This class instance
884
up_history_init (UpHistory *history)
886
history->priv = UP_HISTORY_GET_PRIVATE (history);
887
history->priv->id = NULL;
888
history->priv->rate_last = 0;
889
history->priv->percentage_last = 0;
890
history->priv->state = UP_DEVICE_STATE_UNKNOWN;
891
history->priv->data_rate = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
892
history->priv->data_charge = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
893
history->priv->data_time_full = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
894
history->priv->data_time_empty = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
895
history->priv->save_id = 0;
896
history->priv->max_data_age = UP_HISTORY_DEFAULT_MAX_DATA_AGE;
897
history->priv->dir = g_build_filename (HISTORY_DIR, NULL);
901
* up_history_finalize:
902
* @object: The object to finalize
905
up_history_finalize (GObject *object)
909
g_return_if_fail (UP_IS_HISTORY (object));
911
history = UP_HISTORY (object);
914
if (history->priv->save_id > 0)
915
g_source_remove (history->priv->save_id);
916
if (history->priv->id != NULL)
917
up_history_save_data (history);
919
g_ptr_array_unref (history->priv->data_rate);
920
g_ptr_array_unref (history->priv->data_charge);
921
g_ptr_array_unref (history->priv->data_time_full);
922
g_ptr_array_unref (history->priv->data_time_empty);
924
g_free (history->priv->id);
925
g_free (history->priv->dir);
927
g_return_if_fail (history->priv != NULL);
929
G_OBJECT_CLASS (up_history_parent_class)->finalize (object);
935
* Return value: a new UpHistory object.
938
up_history_new (void)
941
history = g_object_new (UP_TYPE_HISTORY, NULL);
942
return UP_HISTORY (history);