]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/Counter.pm
Accept.pm: fix short_descr
[pmg-api.git] / PMG / RuleDB / Counter.pm
CommitLineData
f4fe6fc4
DM
1package PMG::RuleDB::Counter;
2
3use strict;
4use warnings;
5use Carp;
6use DBI;
7use Digest::SHA;
8
9use PVE::SafeSyslog;
10
11use PMG::Utils;
12use PMG::RuleDB::Object;
13
14use base qw(PMG::RuleDB::Object);
15
16sub otype {
17 return 4999;
18}
19
20sub oclass {
21 return 'action';
22}
23
24sub otype_text {
25 return 'Counter';
26}
27
28sub oicon {
29 return 'counter.gif';
30}
31
32sub oconfigsite {
33 return 'item_counter.epl';
34}
35
36sub oinfo {
37 return 'Counter to gather statisical data.';
38}
39
40sub new {
41 my ($type, $count, $ogroup) = @_;
42
43 my $class = ref($type) || $type;
44
45 my $self = $class->SUPER::new(otype(), $ogroup);
46
47 $count = 0 if !defined ($count);
48
49 $self->{count} = $count;
50
51 return $self;
52}
53
54sub load_attr {
55 my ($type, $ruledb, $id, $ogroup, $value) = @_;
56
57 my $class = ref($type) || $type;
58
59 defined($value) || croak ("undefined value: ERROR");
60
61 my $obj = $class->new($value, $ogroup);
62 $obj->{id} = $id;
63
64 $obj->{digest} = Digest::SHA::sha1_hex($id, $value, $ogroup);
65
66 return $obj;
67}
68
69sub save {
70 my ($self, $ruledb) = @_;
71
72 my $adr;
73
74 defined($self->{ogroup}) || croak ("undefined ogroup: ERROR");
75 defined($self->{count}) || croak ("undefined count: ERROR");
76
77 if (defined ($self->{id})) {
78 # update
79
80 $ruledb->{dbh}->do(
81 "UPDATE Object SET Value = ? WHERE ID = ?",
82 undef, $self->{count}, $self->{id});
83
84 } else {
85 # insert
86
87 my $sth = $ruledb->{dbh}->prepare(
88 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
89 "VALUES (?, ?, ?);");
90
91 $sth->execute($self->{ogroup}, $self->otype, $self->{count});
92
93 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
94 }
95
96 return $self->{id};
97}
98
99sub execute {
100 my ($self, $queue, $ruledb, $mod_group, $targets,
101 $msginfo, $vars, $marks) = @_;
102
103 eval {
104 $ruledb->{dbh}->begin_work;
105
106 $ruledb->{dbh}->do("LOCK TABLE Object IN SHARE MODE");
107
108 my $sth = $ruledb->{dbh}->prepare(
109 "SELECT Value FROM Object where ID = ?");
110 $sth->execute($self->{id});
111
112 my $ref = $sth->fetchrow_hashref();
113
114 $sth->finish();
115
116 defined($ref->{'value'}) || croak ("undefined value: ERROR");
117
118 my $value = int($ref->{'value'});
119
120 $ruledb->{dbh}->do(
121 "UPDATE Object SET Value = ? WHERE ID = ?",
122 undef, $value + 1, $self->{id});
123
124 $ruledb->{dbh}->commit;
125
126 if ($msginfo->{testmode}) {
127 print ("counter increased\n");
128 }
129 };
130 if (my $err = $@) {
131 $ruledb->{dbh}->rollback;
132 syslog('err', $err);
133 return undef;
134 }
135}
136
137sub count {
138 my ($self, $count) = @_;
139
140 if (defined ($count)) {
141 $self->{count} = $count;
142 }
143
144 $self->{count};
145}
146
147
148sub short_desc {
149 my $self = shift;
150
151 return "Increase Counter";
152}
153
1541;
155
156__END__
157
158=head1 PMG::RuleDB::Counter
159
160Counter Object
161
162=head2 Attribues
163
164=head3 count
165
166Unique Name of the Counter
167
168=head2 Examples
169
170 $obj = PMG::RuleDB::Counter->new (0);
171