You are not logged in.

#1 2012-01-07 22:38:17

yasar11732
Member
Registered: 2010-11-29
Posts: 127

Graph your pacman activity!

Today I wanted to create a simple script that would graph my pacman activity, and I thought I might as well share it with community. Below you can find a python script that serves that purpose. You will need python2-matplotlib to use it. And here what my version of the graph looks like:

tumblr_lxg9gfAvfc1r4gygb.png

"""
    
    Copyright (C) 2011  Yaşar Arabacı

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import re
import datetime
import time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

from collections import Counter

months    = mdates.MonthLocator()  # every month
days      = mdates.DayLocator() # Every day
monthsFmt = mdates.DateFormatter('%m-%Y')

logfile = "/var/log/pacman.log"
interesting_line = re.compile(
    r"\[(?P<datetime>\d{4}-\d{2}-\d{2} \d{2}:\d{2})\] "
    "(?P<action>installed|upgraded|removed)")

installs = Counter()
upgrades = Counter()
removes  = Counter()
with open(logfile) as logfile:
    for line in logfile:
        match = interesting_line.search(line)
        if match:
            struct = time.strptime(match.group("datetime"),"%Y-%m-%d %H:%S")
            date = datetime.date(struct.tm_year, struct.tm_mon, struct.tm_mday)
            if match.group("action") == "installed":
                installs[date] += 1
            elif match.group("action") == "upgraded":
                upgrades[date] += 1
            elif match.group("action") == "removed":
                removes[date] += 1


fig = plt.figure()
ax = fig.add_subplot(111)
insdatenumpairs = zip(installs.keys(), installs.values())
insdatenumpairs = sorted(insdatenumpairs, key = lambda pair: pair[0])

upgdatenumpairs = zip(upgrades.keys(), upgrades.values())
upgdatenumpairs = sorted(upgdatenumpairs, key = lambda pair: pair[0])

remdatenumpairs = zip(removes.keys(), removes.values())
remdatenumpairs = sorted(remdatenumpairs, key = lambda pair: pair[0])

ax.plot([pair[0] for pair in insdatenumpairs],[pair[1] for pair in insdatenumpairs],"b")
ax.plot([pair[0] for pair in upgdatenumpairs],[pair[1] for pair in upgdatenumpairs],"g")
ax.plot([pair[0] for pair in remdatenumpairs],[pair[1] for pair in remdatenumpairs],"y")

ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
ax.xaxis.set_minor_locator(days)

ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')

ax.grid(True)
fig.autofmt_xdate()
plt.legend(("installs","upgrades","removes"))
plt.xlabel("Date")
plt.ylabel("# of packages")
plt.show()

Last edited by yasar11732 (2012-01-08 11:58:35)


Yo Dawg, I heard you likes patches, so I have created a patch for your patch, so you can patch your patches before you patch.

Offline

#2 2012-01-07 22:54:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,702
Website

Re: Graph your pacman activity!

Neat!  Why not make a format package in the AUR?  That would simplify the need for deps and make it easier to get to users.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#3 2012-01-07 23:02:08

yasar11732
Member
Registered: 2010-11-29
Posts: 127

Re: Graph your pacman activity!

Good idea. I never posted a package to AUR though, and never did a PKGBUILD from scratch. But I will look into it.


Yo Dawg, I heard you likes patches, so I have created a patch for your patch, so you can patch your patches before you patch.

Offline

#4 2012-01-07 23:10:56

yasar11732
Member
Registered: 2010-11-29
Posts: 127

Re: Graph your pacman activity!

Here is a little bit different version, only showing total number of packages over time.
tumblr_lxgatn43C71r4gygb.png

"""
    
    Copyright (C) 2011  Yaşar Arabacı

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import re
import datetime
import time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

from collections import Counter

months    = mdates.MonthLocator()  # every month
days      = mdates.DayLocator() # Every day
monthsFmt = mdates.DateFormatter('%m-%Y')

logfile = "/var/log/pacman.log"
interesting_line = re.compile(
    r"\[(?P<datetime>\d{4}-\d{2}-\d{2} \d{2}:\d{2})\] "
    "(?P<action>installed|removed)")

total = 0
packs = Counter()
with open(logfile) as logfile:
    for line in logfile:
        match = interesting_line.search(line)
        if match:
            struct = time.strptime(match.group("datetime"),"%Y-%m-%d %H:%S")
            date = datetime.date(struct.tm_year, struct.tm_mon, struct.tm_mday)
            if match.group("action") == "installed":
                total += 1
            elif match.group("action") == "removed":
                total -= 1
            packs[date] = total


fig = plt.figure()
ax = fig.add_subplot(111)
datenumpairs = zip(packs.keys(), packs.values())
datenumpairs = sorted(datenumpairs, key = lambda pair: pair[0])

ax.plot([pair[0] for pair in datenumpairs],[pair[1] for pair in datenumpairs])

ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
ax.xaxis.set_minor_locator(days)

ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')

ax.grid(True)
fig.autofmt_xdate()
plt.xlabel("Date")
plt.ylabel("# of packages")
plt.show()

Last edited by yasar11732 (2012-01-08 11:59:09)


Yo Dawg, I heard you likes patches, so I have created a patch for your patch, so you can patch your patches before you patch.

Offline

#5 2012-01-08 00:23:20

yasar11732
Member
Registered: 2010-11-29
Posts: 127

Re: Graph your pacman activity!


Yo Dawg, I heard you likes patches, so I have created a patch for your patch, so you can patch your patches before you patch.

Offline

#6 2012-01-08 00:54:46

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Graph your pacman activity!

We already have pacgraph http://www.archlinux.org/packages/?name=pacgraph , not sure if packgraph won't cause confusion.

Offline

#7 2012-01-08 09:38:01

guelfi
Member
From: /home/guelfi
Registered: 2011-07-01
Posts: 111

Re: Graph your pacman activity!

Good idea, but doesn't work here:

Traceback (most recent call last):
  File "/usr/bin/packgraph", line 73, in <module>
    datemin = datetime.datetime(min(installs).year, min(installs).month -1, min(installs).day)
ValueError: month must be in 1..12

Why is there a "-1" behind min(installs)? I set up my system in this January, so "min(installs).month -1" is probably 0.

Offline

#8 2012-01-08 10:16:20

defcon
Member
From: Germany, Bruchsal
Registered: 2006-08-16
Posts: 177

Re: Graph your pacman activity!

Same here


Registed Linux User 608596

Offline

#9 2012-01-08 11:57:17

yasar11732
Member
Registered: 2010-11-29
Posts: 127

Re: Graph your pacman activity!

guelfi wrote:

Good idea, but doesn't work here:

Traceback (most recent call last):
  File "/usr/bin/packgraph", line 73, in <module>
    datemin = datetime.datetime(min(installs).year, min(installs).month -1, min(installs).day)
ValueError: month must be in 1..12

Why is there a "-1" behind min(installs)? I set up my system in this January, so "min(installs).month -1" is probably 0.

I put that to make a left margin on the graph, didn't realize that might cause that error. Fixing now.

Edit : Done
Edit2: You may want to remake the package now.

Last edited by yasar11732 (2012-01-08 12:06:17)


Yo Dawg, I heard you likes patches, so I have created a patch for your patch, so you can patch your patches before you patch.

Offline

#10 2012-01-08 12:06:50

yasar11732
Member
Registered: 2010-11-29
Posts: 127

Re: Graph your pacman activity!

karol wrote:

We already have pacgraph http://www.archlinux.org/packages/?name=pacgraph , not sure if packgraph won't cause confusion.

Show I remove it from AUR, or rename it perhaps?


Yo Dawg, I heard you likes patches, so I have created a patch for your patch, so you can patch your patches before you patch.

Offline

#11 2012-01-08 12:22:29

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Graph your pacman activity!

yasar11732 wrote:
karol wrote:

We already have pacgraph http://www.archlinux.org/packages/?name=pacgraph , not sure if packgraph won't cause confusion.

Show I remove it from AUR, or rename it perhaps?

If you want to do anything at all, renaming sounds more sensible :-)

Offline

#12 2012-01-20 19:28:50

encelo
Member
From: Oxford, UK
Registered: 2005-02-23
Posts: 96
Website

Re: Graph your pacman activity!

Have you seen PacStats? wink


Blog | Twitter | nCine 2D game engine
All problems in computer graphics can be solved with a matrix inversion. - James Blinn.

Offline

#13 2012-01-22 01:38:43

tigrmesh
IRC Op
From: Florida, US
Registered: 2007-12-11
Posts: 794

Re: Graph your pacman activity!

@yasar11732 - I like your pacman graphing program.  I had no idea what daily -Syus would look like.  Cool.  Thanks for putting it in AUR.  That made it easy for me to get.

I agree with karol that it would be good to rename it.  Packgraph is too close in name to pacgraph.

I also tried to run the second version, the one that shows the total number of packages over time, but i had this error:

$ python2 grapher.py
  File "grapher.py", line 3
SyntaxError: Non-ASCII character '\xc5' in file grapher.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Offline

Board footer

Powered by FluxBB