]>
Commit | Line | Data |
---|---|---|
f4fe6fc4 DM |
1 | package PMG::RuleDB::Counter; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Carp; | |
6 | use DBI; | |
7 | use Digest::SHA; | |
8 | ||
9 | use PVE::SafeSyslog; | |
10 | ||
11 | use PMG::Utils; | |
12 | use PMG::RuleDB::Object; | |
13 | ||
14 | use base qw(PMG::RuleDB::Object); | |
15 | ||
16 | sub otype { | |
17 | return 4999; | |
18 | } | |
19 | ||
20 | sub oclass { | |
21 | return 'action'; | |
22 | } | |
23 | ||
24 | sub otype_text { | |
25 | return 'Counter'; | |
26 | } | |
27 | ||
28 | sub oicon { | |
29 | return 'counter.gif'; | |
30 | } | |
31 | ||
32 | sub oconfigsite { | |
33 | return 'item_counter.epl'; | |
34 | } | |
35 | ||
36 | sub oinfo { | |
37 | return 'Counter to gather statisical data.'; | |
38 | } | |
39 | ||
40 | sub 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 | ||
54 | sub 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 | ||
69 | sub 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 | ||
99 | sub 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 | ||
137 | sub count { | |
138 | my ($self, $count) = @_; | |
139 | ||
140 | if (defined ($count)) { | |
141 | $self->{count} = $count; | |
142 | } | |
143 | ||
144 | $self->{count}; | |
145 | } | |
146 | ||
147 | ||
148 | sub short_desc { | |
149 | my $self = shift; | |
150 | ||
151 | return "Increase Counter"; | |
152 | } | |
153 | ||
154 | 1; | |
155 | ||
156 | __END__ | |
157 | ||
158 | =head1 PMG::RuleDB::Counter | |
159 | ||
160 | Counter Object | |
161 | ||
162 | =head2 Attribues | |
163 | ||
164 | =head3 count | |
165 | ||
166 | Unique Name of the Counter | |
167 | ||
168 | =head2 Examples | |
169 | ||
170 | $obj = PMG::RuleDB::Counter->new (0); | |
171 |